1 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 ; 63 import org.w3c.dom.Text ; 64 import org.w3c.dom.ranges.Range; 65 66 73 public class TestCompare extends TestCase 74 { 75 78 public TestCompare(String name) { 79 super(name); 80 } 81 82 103 private Range[] buildRanges() 104 { 105 DocumentImpl doc=new org.enhydra.apache.xerces.dom.DocumentImpl(); 106 107 Element body = doc.createElement("BODY"); 108 doc.appendChild(body); 109 Element h1 = doc.createElement("H1"); 110 body.appendChild(h1); 111 Text title = doc.createTextNode("Title"); 112 h1.appendChild(title); 113 Element p = doc.createElement("P"); 114 body.appendChild(p); 115 Text blah = doc.createTextNode("Blah xyz."); 116 p.appendChild(blah); 117 118 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 146 private final int[][] results_START_TO_START = 147 { 148 { 0, -1, -1, -1 }, { 1, 0, -1, -1 }, { 1, 1, 0, -1 }, { 1, 1, 1, 0 }, }; 153 154 160 private final int[][] results_START_TO_END = 161 { 162 { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, }; 167 168 174 private final int[][] results_END_TO_START = 175 { 176 { -1, -1, -1, -1 }, { -1, -1, -1, -1 }, { -1, -1, -1, -1 }, { -1, -1, -1, -1 }, }; 181 182 188 private final int[][] results_END_TO_END = 189 { 190 { 0, -1, -1, -1 }, { 1, 0, 1, 1 }, { 1, -1, 0, 1 }, { 1, -1, -1, 0 }, }; 195 196 201 private void doTestCompare( short how, int[][] results ) 202 { 203 Range[] ranges = buildRanges(); 205 206 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 225 public void testCompareStartToStart() 226 { 227 doTestCompare( Range.START_TO_START, results_START_TO_START ); 228 } 229 230 235 public void testCompareStartToEnd() 236 { 237 doTestCompare( Range.START_TO_END, results_START_TO_END ); 238 } 239 240 245 public void testCompareEndToStart() 246 { 247 doTestCompare( Range.END_TO_START, results_END_TO_START ); 248 } 249 250 255 public void testCompareEndToEnd() 256 { 257 doTestCompare( Range.END_TO_END, results_END_TO_END ); 258 } 259 260 263 public static junit.framework.Test suite() { 264 return new TestSuite( TestCompare.class ); 265 } 266 267 270 public static void main (String [] args) { 271 junit.textui.TestRunner.run (suite()); 272 } 273 274 } 275 276 | Popular Tags |