KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > range > TestCompare


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package dom.range;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 import org.enhydra.apache.xerces.dom.DocumentImpl;
62 import org.w3c.dom.Element JavaDoc;
63 import org.w3c.dom.Text JavaDoc;
64 import org.w3c.dom.ranges.Range;
65
66 /**
67  * This class is used to validate that the implementation of DOM Ranges
68  * is functionally correct. We include in this test suite examples
69  * from the DOM Spec.
70  *
71  * @author Lynn Monson
72  */

73 public class TestCompare extends TestCase
74 {
75     /**
76      * Creates an instance of the test
77      */

78     public TestCompare(String JavaDoc name) {
79             super(name);
80     }
81
82     /**
83      * Builds a set of ranges that correspond to the range example from
84      * section 2.1 of the DOM range spec. These ranges are based on a
85      * document that looks like:
86      *
87      * <BODY><H1>Title</H1><P>Blah xyz.</P></BODY>
88      *
89      * The ranges are as follows:
90      * Range Start-node Start-Offset End-node End-Offset
91      * 0 "Title" 2 "Blah.." 2
92      * 1 BODY 1 BODY 2
93      * 2 P 0 P 1
94      * 3 "Blah.." 0 "Blah.." 9
95      *
96      * These ranges are in sorted order based on the boundary point
97      * of the start of each range, in document order.
98      *
99      * The ending points of the ranges are not in any particular order.
100      * These ranges cover all four boundary tests as enumerated in
101      * the DOM range specification.
102      */

103     private Range[] buildRanges()
104     {
105         DocumentImpl doc=new org.enhydra.apache.xerces.dom.DocumentImpl();
106
107         Element JavaDoc body = doc.createElement("BODY");
108         doc.appendChild(body);
109         Element JavaDoc h1 = doc.createElement("H1");
110         body.appendChild(h1);
111         Text JavaDoc title = doc.createTextNode("Title");
112         h1.appendChild(title);
113         Element JavaDoc p = doc.createElement("P");
114         body.appendChild(p);
115         Text JavaDoc blah = doc.createTextNode("Blah xyz.");
116         p.appendChild(blah);
117
118         // We are creating the four ranges specified in the DOM example.
119
Range[] ranges = new Range[4];
120
121         ranges[0] = doc.createRange();
122         ranges[0].setStart( title, 2 );
123         ranges[0].setEnd( blah, 2 );
124
125         ranges[1] = doc.createRange();
126         ranges[1].setStart( body, 1 );
127         ranges[1].setEnd( body, 2 );
128
129         ranges[2] = doc.createRange();
130         ranges[2].setStart( p, 0 );
131         ranges[2].setEnd( p, 1 );
132
133         ranges[3] = doc.createRange();
134         ranges[3].setStart( blah, 0 );
135         ranges[3].setEnd( blah, 9 );
136
137         return ranges;
138     }
139
140     /**
141      * This table is the set of compareBoundaryPoints results you can
142      * expect to see when comparing the start-to-start points of ranges found in
143      * the DOM Spec section 2.1. These ranges are built by the above
144      * buildRanges() method.
145      */

146     private final int[][] results_START_TO_START =
147     {
148         { 0, -1, -1, -1 }, // range[0].compareBoundaryPoints( range[x] )
149
{ 1, 0, -1, -1 }, // range[1].compareBoundaryPoints( range[x] )
150
{ 1, 1, 0, -1 }, // range[2].compareBoundaryPoints( range[x] )
151
{ 1, 1, 1, 0 }, // range[3].compareBoundaryPoints( range[x] )
152
};
153
154     /**
155      * This table is the set of compareBoundaryPoints results you can
156      * expect to see when comparing the start-to-end points of ranges found in
157      * the DOM Spec section 2.1. These ranges are built by the above
158      * buildRanges() method.
159      */

160     private final int[][] results_START_TO_END =
161     {
162         { 1, 1, 1, 1 }, // range[0].compareBoundaryPoints( range[x] )
163
{ 1, 1, 1, 1 }, // range[1].compareBoundaryPoints( range[x] )
164
{ 1, 1, 1, 1 }, // range[2].compareBoundaryPoints( range[x] )
165
{ 1, 1, 1, 1 }, // range[3].compareBoundaryPoints( range[x] )
166
};
167
168     /**
169      * This table is the set of compareBoundaryPoints results you can
170      * expect to see when comparing the end-to-start points of ranges found in
171      * the DOM Spec section 2.1. These ranges are built by the above
172      * buildRanges() method.
173      */

174     private final int[][] results_END_TO_START =
175     {
176         { -1, -1, -1, -1 }, // range[0].compareBoundaryPoints( range[x] )
177
{ -1, -1, -1, -1 }, // range[0].compareBoundaryPoints( range[x] )
178
{ -1, -1, -1, -1 }, // range[0].compareBoundaryPoints( range[x] )
179
{ -1, -1, -1, -1 }, // range[0].compareBoundaryPoints( range[x] )
180
};
181
182     /**
183      * This table is the set of compareBoundaryPoints results you can
184      * expect to see when comparing the end-to-end points of ranges found in
185      * the DOM Spec section 2.1. These ranges are built by the above
186      * buildRanges() method.
187      */

188     private final int[][] results_END_TO_END =
189     {
190         { 0, -1, -1, -1 }, // range[0].compareBoundaryPoints( range[x] )
191
{ 1, 0, 1, 1 }, // range[1].compareBoundaryPoints( range[x] )
192
{ 1, -1, 0, 1 }, // range[2].compareBoundaryPoints( range[x] )
193
{ 1, -1, -1, 0 }, // range[3].compareBoundaryPoints( range[x] )
194
};
195
196     /**
197      * Utility method used to compare the Ranges from the
198      * buildRanges() method. The caller specifies how the ranges
199      * should be compared and what the results should be.
200      */

201     private void doTestCompare( short how, int[][] results )
202     {
203         // get the sample ranges
204
Range[] ranges = buildRanges();
205
206         // Compare every pair of ranges.
207
for( int i=0; i<ranges.length; ++i )
208         {
209             for( int j=0; j<ranges.length; ++j )
210             {
211                 int result = ranges[i].compareBoundaryPoints( how, ranges[j] );
212                 assertTrue(
213                     "Compare returned the wrong value i="+i+" j="+j + " result="+result,
214                     result == results[i][j]
215                 );
216             }
217         }
218     }
219
220     /**
221      * Using all of the sample ranges from section 2.1 of the DOM
222      * specification, compare each starting point to every other
223      * starting point.
224      */

225     public void testCompareStartToStart()
226     {
227         doTestCompare( Range.START_TO_START, results_START_TO_START );
228     }
229
230     /**
231      * Using all of the sample ranges from section 2.1 of the DOM
232      * specification, compare each starting point to every other
233      * ending point.
234      */

235     public void testCompareStartToEnd()
236     {
237         doTestCompare( Range.START_TO_END, results_START_TO_END );
238     }
239
240     /**
241      * Using all of the sample ranges from section 2.1 of the DOM
242      * specification, compare each ending point to every other
243      * starting point.
244      */

245     public void testCompareEndToStart()
246     {
247         doTestCompare( Range.END_TO_START, results_END_TO_START );
248     }
249     
250     /**
251      * Using all of the sample ranges from section 2.1 of the DOM
252      * specification, compare each ending point to every other
253      * ending point.
254      */

255     public void testCompareEndToEnd()
256     {
257         doTestCompare( Range.END_TO_END, results_END_TO_END );
258     }
259
260     /**
261      * Returns the set of all tests in this class
262      */

263     public static junit.framework.Test suite() {
264         return new TestSuite( TestCompare.class );
265     }
266
267     /**
268      * Utility for invoking the class from the command line.
269      */

270     public static void main (String JavaDoc[] args) {
271             junit.textui.TestRunner.run (suite());
272     }
273
274 }
275
276
Popular Tags