1 19 20 package org.netbeans.spi.editor.highlighting.support; 21 22 import java.util.Enumeration ; 23 import javax.swing.text.AttributeSet ; 24 import javax.swing.text.DefaultStyledDocument ; 25 import javax.swing.text.Document ; 26 import javax.swing.text.Position ; 27 import javax.swing.text.SimpleAttributeSet ; 28 import junit.framework.AssertionFailedError; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.modules.editor.lib2.highlighting.OffsetGapList; 31 import org.netbeans.spi.editor.highlighting.*; 32 33 37 public class MergingOffsetsBagTest extends NbTestCase { 38 39 private static final AttributeSet EMPTY = SimpleAttributeSet.EMPTY; 40 41 private Document doc = new DefaultStyledDocument (); 42 43 44 public MergingOffsetsBagTest(String name) { 45 super(name); 46 } 47 48 public void testSimple() { 49 OffsetsBag hs = new OffsetsBag(doc, true); 50 assertEquals("Sequence should be empty", 0, hs.getMarks().size()); 51 52 hs.addHighlight(10, 20, EMPTY); 53 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 54 assertEquals("Sequence should not be empty", 2, marks.size()); 55 assertEquals("Wrong highlight's start offset", 10, marks.get(0).getOffset()); 56 assertEquals("Wrong highlight's end offset", 20, marks.get(1).getOffset()); 57 assertNull("Wrong highlight's end", marks.get(1).getAttributes()); 58 59 hs.clear(); 60 assertEquals("Sequence was not cleared", 0, hs.getMarks().size()); 61 } 62 63 public void testAddLeftOverlap() { 64 OffsetsBag hs = new OffsetsBag(doc, true); 65 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 66 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 67 68 attribsA.addAttribute("set-A", "attribsA"); 69 attribsB.addAttribute("set-B", "attribsB"); 70 71 hs.addHighlight(10, 20, attribsA); 72 hs.addHighlight(5, 15, attribsB); 73 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 74 75 assertEquals("Wrong number of highlights", 4, marks.size()); 76 assertEquals("1. highlight - wrong start offset", 5, marks.get(0).getOffset()); 77 assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset()); 78 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-B"); 79 80 assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset()); 81 assertEquals("2. highlight - wrong end offset", 15, marks.get(2).getOffset()); 82 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A", "set-B"); 83 84 assertEquals("3. highlight - wrong start offset", 15, marks.get(2).getOffset()); 85 assertEquals("3. highlight - wrong end offset", 20, marks.get(3).getOffset()); 86 assertAttribs("3. highlight - wrong attribs", marks.get(2).getAttributes(), "set-A"); 87 assertNull("3. highlight - wrong end", marks.get(3).getAttributes()); 88 } 89 90 public void testAddRightOverlap() { 91 OffsetsBag hs = new OffsetsBag(doc, true); 92 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 93 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 94 95 attribsA.addAttribute("set-A", "attribsA"); 96 attribsB.addAttribute("set-B", "attribsB"); 97 98 hs.addHighlight(10, 20, attribsA); 99 hs.addHighlight(15, 25, attribsB); 100 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 101 102 assertEquals("Wrong number of highlights", 4, marks.size()); 103 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 104 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 105 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A"); 106 107 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 108 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 109 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A", "set-B"); 110 111 assertEquals("3. highlight - wrong start offset", 20, marks.get(2).getOffset()); 112 assertEquals("3. highlight - wrong end offset", 25, marks.get(3).getOffset()); 113 assertAttribs("3. highlight - wrong attribs", marks.get(2).getAttributes(), "set-B"); 114 assertNull("3. highlight - wrong end", marks.get(3).getAttributes()); 115 } 116 117 public void testAddLeftMatchBiggerOverlap() { 118 OffsetsBag hs = new OffsetsBag(doc, true); 119 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 120 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 121 122 attribsA.addAttribute("set-A", "attribsA"); 123 attribsB.addAttribute("set-B", "attribsB"); 124 125 hs.addHighlight(10, 20, attribsA); 126 hs.addHighlight(10, 15, attribsB); 127 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 128 129 assertEquals("Wrong number of highlights", 3, marks.size()); 130 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 131 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 132 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A", "set-B"); 133 134 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 135 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 136 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A"); 137 assertNull("2. highlight - wrong end", marks.get(2).getAttributes()); 138 } 139 140 public void testAddRightMatchBiggerOverlap() { 141 OffsetsBag hs = new OffsetsBag(doc, true); 142 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 143 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 144 145 attribsA.addAttribute("set-A", "attribsA"); 146 attribsB.addAttribute("set-B", "attribsB"); 147 148 hs.addHighlight(10, 20, attribsA); 149 hs.addHighlight(15, 20, attribsB); 150 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 151 152 assertEquals("Wrong number of highlights", 3, marks.size()); 153 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 154 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 155 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A"); 156 157 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 158 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 159 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A", "set-B"); 160 assertNull("2. highlight - wrong end", marks.get(2).getAttributes()); 161 } 162 163 public void testAddCompleteMatchOverlap() { 164 OffsetsBag hs = new OffsetsBag(doc, true); 165 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 166 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 167 168 attribsA.addAttribute("set-A", "attribsA"); 169 attribsB.addAttribute("set-B", "attribsB"); 170 171 hs.addHighlight(10, 20, attribsA); 172 hs.addHighlight(10, 20, attribsB); 173 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 174 175 assertEquals("Wrong number of highlights", 2, marks.size()); 176 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 177 assertEquals("1. highlight - wrong end offset", 20, marks.get(1).getOffset()); 178 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A", "set-B"); 179 assertNull("1. highlight - wrong end", marks.get(1).getAttributes()); 180 } 181 182 public void testAddBiggerOverlap() { 183 OffsetsBag hs = new OffsetsBag(doc, true); 184 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 185 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 186 187 attribsA.addAttribute("set-A", "attribsA"); 188 attribsB.addAttribute("set-B", "attribsB"); 189 190 hs.addHighlight(10, 20, attribsA); 191 hs.addHighlight(5, 25, attribsB); 192 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 193 194 assertEquals("Wrong number of highlights", 4, marks.size()); 195 assertEquals("1. highlight - wrong start offset", 5, marks.get(0).getOffset()); 196 assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset()); 197 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-B"); 198 199 assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset()); 200 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 201 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A", "set-B"); 202 203 assertEquals("3. highlight - wrong start offset", 20, marks.get(2).getOffset()); 204 assertEquals("3. highlight - wrong end offset", 25, marks.get(3).getOffset()); 205 assertAttribs("3. highlight - wrong attribs", marks.get(2).getAttributes(), "set-B"); 206 assertNull("3. highlight - wrong end", marks.get(3).getAttributes()); 207 } 208 209 public void testAddLeftMatchSmallerOverlap() { 210 OffsetsBag hs = new OffsetsBag(doc, true); 211 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 212 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 213 214 attribsA.addAttribute("set-A", "attribsA"); 215 attribsB.addAttribute("set-B", "attribsB"); 216 217 hs.addHighlight(10, 20, attribsA); 218 hs.addHighlight(10, 15, attribsB); 219 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 220 221 assertEquals("Wrong number of highlights", 3, marks.size()); 222 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 223 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 224 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A", "set-B"); 225 226 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 227 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 228 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A"); 229 assertNull("2. highlight - wrong end", marks.get(2).getAttributes()); 230 } 231 232 public void testAddRightMatchSmallerOverlap() { 233 OffsetsBag hs = new OffsetsBag(doc, true); 234 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 235 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 236 237 attribsA.addAttribute("set-A", "attribsA"); 238 attribsB.addAttribute("set-B", "attribsB"); 239 240 hs.addHighlight(10, 20, attribsA); 241 hs.addHighlight(15, 20, attribsB); 242 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 243 244 assertEquals("Wrong number of highlights", 3, marks.size()); 245 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 246 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 247 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A"); 248 249 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 250 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 251 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A", "set-B"); 252 assertNull("2. highlight - wrong end", marks.get(2).getAttributes()); 253 } 254 255 public void testAddSmallerOverlap() { 256 OffsetsBag hs = new OffsetsBag(doc, true); 257 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 258 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 259 260 attribsA.addAttribute("set-A", "attribsA"); 261 attribsB.addAttribute("set-B", "attribsB"); 262 263 hs.addHighlight(5, 25, attribsA); 264 hs.addHighlight(10, 20, attribsB); 265 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 266 267 assertEquals("Wrong number of highlights", 4, marks.size()); 268 assertEquals("1. highlight - wrong start offset", 5, marks.get(0).getOffset()); 269 assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset()); 270 assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A"); 271 272 assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset()); 273 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 274 assertAttribs("2. highlight - wrong attribs", marks.get(1).getAttributes(), "set-A", "set-B"); 275 276 assertEquals("3. highlight - wrong start offset", 20, marks.get(2).getOffset()); 277 assertEquals("3. highlight - wrong end offset", 25, marks.get(3).getOffset()); 278 assertAttribs("3. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A"); 279 assertNull("3. highlight - wrong end", marks.get(3).getAttributes()); 280 } 281 282 public void testOrdering() { 283 OffsetsBag hs = new OffsetsBag(doc, true); 284 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 285 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 286 287 attribsA.addAttribute("attribute", "value-A"); 288 attribsB.addAttribute("attribute", "value-B"); 289 290 hs.addHighlight(5, 15, attribsA); 291 hs.addHighlight(10, 20, attribsB); 292 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 293 294 assertEquals("Wrong number of highlights", 4, marks.size()); 295 assertEquals("1. highlight - wrong attribs", "value-A", marks.get(0).getAttributes().getAttribute("attribute")); 296 assertEquals("2. highlight - wrong attribs", "value-B", marks.get(1).getAttributes().getAttribute("attribute")); 297 assertEquals("3. highlight - wrong attribs", "value-B", marks.get(2).getAttributes().getAttribute("attribute")); 298 assertNull("3. highlight - wrong end", marks.get(3).getAttributes()); 299 } 300 301 public void testAddMultipleOverlaps() { 302 Object [] src = new Object [] { 303 5, 10, new String [] { "set-A" }, 304 15, 20, new String [] { "set-B" }, 305 25, 30, new String [] { "set-C" }, 306 0, 40, new String [] { "set-D" }, 307 }; 308 Object [] trg = new Object [] { 309 0, 5, new String [] { "set-D" }, 310 5, 10, new String [] { "set-A", "set-D" }, 311 10, 15, new String [] { "set-D" }, 312 15, 20, new String [] { "set-B", "set-D" }, 313 20, 25, new String [] { "set-D" }, 314 25, 30, new String [] { "set-C", "set-D" }, 315 30, 40, new String [] { "set-D" }, 316 }; 317 318 checkMerging(src, trg); 319 } 320 321 public void testAddMultipleOverlaps2() { 322 Object [] src = new Object [] { 323 2, 47, new String [] { "set-A" }, 324 49, 74, new String [] { "set-B" }, 325 74, 100, new String [] { "set-C" }, 326 9, 48, new String [] { "set-D" }, 327 49, 98, new String [] { "set-E" }, 328 0, 44, new String [] { "set-F" }, 329 46, 74, new String [] { "set-G" }, 330 74, 100, new String [] { "set-H" }, 331 }; 332 Object [] trg = new Object [] { 333 0, 2, new String [] { "set-F" }, 334 2, 9, new String [] { "set-A", "set-F" }, 335 9, 44, new String [] { "set-A", "set-D", "set-F" }, 336 44, 46, new String [] { "set-A", "set-D" }, 337 46, 47, new String [] { "set-A", "set-D", "set-G" }, 338 47, 48, new String [] { "set-D", "set-G" }, 339 48, 49, new String [] { "set-G" }, 340 49, 74, new String [] { "set-B", "set-E", "set-G" }, 341 74, 98, new String [] { "set-C", "set-E", "set-H" }, 342 98, 100, new String [] { "set-C", "set-H" }, 343 }; 344 345 checkMerging(src, trg); 346 } 347 348 public void testAddMultipleOverlaps3() { 349 Object [] src = new Object [] { 350 5, 13, new String [] { "set-A" }, 351 20, 49, new String [] { "set-B" }, 352 50, 53, new String [] { "set-C" }, 353 62, 100, new String [] { "set-D" }, 354 9, 54, new String [] { "set-E" }, 355 57, 100, new String [] { "set-F" }, 356 1, 41, new String [] { "set-G" }, 357 41, 46, new String [] { "set-H" }, 358 54, 83, new String [] { "set-I" }, 359 88, 100, new String [] { "set-J" }, 360 }; 361 Object [] trg = new Object [] { 362 1, 5, new String [] { "set-G" }, 363 5, 9, new String [] { "set-A", "set-G" }, 364 9, 13, new String [] { "set-A", "set-E", "set-G" }, 365 13, 20, new String [] { "set-E", "set-G" }, 366 20, 41, new String [] { "set-B", "set-G", "set-E" }, 367 41, 46, new String [] { "set-B", "set-E", "set-H" }, 368 46, 49, new String [] { "set-B", "set-E" }, 369 49, 50, new String [] { "set-E" }, 370 50, 53, new String [] { "set-C", "set-E" }, 371 53, 54, new String [] { "set-E" }, 372 54, 57, new String [] { "set-I" }, 373 57, 62, new String [] { "set-F", "set-I" }, 374 62, 83, new String [] { "set-D", "set-F", "set-I" }, 375 83, 88, new String [] { "set-D", "set-F" }, 376 88, 100, new String [] { "set-D", "set-F", "set-J" }, 377 }; 378 379 checkMerging(src, trg); 380 } 381 382 public void checkMerging(Object [] src, Object [] trg) { 383 OffsetsBag hs = new OffsetsBag(doc, true); 384 385 for (int i = 0; i < src.length / 3; i++) { 386 SimpleAttributeSet as = new SimpleAttributeSet (); 387 String [] keys = (String []) src[3 * i + 2]; 388 389 for (int j = 0; j < keys.length; j++) { 390 as.addAttribute(keys[j], Boolean.TRUE); 391 } 392 393 hs.addHighlight( 394 ((Integer ) src[3 * i + 0]).intValue(), 395 ((Integer ) src[3 * i + 1]).intValue(), 396 as 397 ); 398 } 399 400 int lastOffset = Integer.MIN_VALUE; 401 int differentOffsets = 0; 402 403 for (int i = 0; i < trg.length / 3; i++) { 404 if (lastOffset != ((Integer ) trg[3 * i + 0]).intValue()) { 405 differentOffsets++; 406 lastOffset = ((Integer ) trg[3 * i + 0]).intValue(); 407 } 408 if (lastOffset != ((Integer ) trg[3 * i + 1]).intValue()) { 409 differentOffsets++; 410 lastOffset = ((Integer ) trg[3 * i + 1]).intValue(); 411 } 412 } 413 414 OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks(); 415 416 try { 417 assertEquals("Wrong number of highlights", differentOffsets, marks.size()); 418 419 int trgIdx = 0; 420 for (int idx = 0; idx < marks.size(); idx++) { 421 if (marks.get(idx).getAttributes() == null) { 422 assertTrue("Mark at index 0 must have attributes", idx > 0); 423 continue; 424 } 425 426 assertTrue("Too few marks", idx + 1 < marks.size()); 427 assertTrue("Too many marks", trgIdx < trg.length); 428 429 assertEquals(trgIdx + ". highlight - wrong start offset", 431 ((Integer ) trg[3 * trgIdx + 0]).intValue(), marks.get(idx).getOffset()); 432 assertEquals(trgIdx + ". highlight - wrong end offset", 433 ((Integer ) trg[3 * trgIdx + 1]).intValue(), marks.get(idx + 1).getOffset()); 434 assertAttribs(trgIdx + ". highlight - wrong attribs", 435 marks.get(idx).getAttributes(), (String []) trg[3 * trgIdx + 2]); 436 437 trgIdx++; 438 } 439 440 assertTrue("Wrong number of marks: marks.size() = " + marks.size() + 441 ", trg.length = " + trg.length, 3 * trgIdx == trg.length); 442 443 } catch (AssertionFailedError afe) { 444 dumpMarks(marks); 445 446 System.out.println("Dump through getHighlights {"); 447 HighlightsSequence sequence = hs.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE); 448 for ( ; sequence.moveNext(); ) { 449 System.out.println(" <" + sequence.getStartOffset() + ", " + sequence.getEndOffset() + ">"); 450 } 451 System.out.println("} ---- End of Dump through getHighlights ------------------"); 452 453 throw afe; 454 } 455 } 456 457 private void assertAttribs(String msg, AttributeSet as, String ... keys) { 458 assertEquals(msg, keys.length, as.getAttributeCount()); 459 for (String key : keys) { 460 if (null == as.getAttribute(key)) { 461 fail(msg + " attribute key: " + key); 462 } 463 } 464 } 465 466 private String dumpHighlight(Position start, Position end, AttributeSet attribs) { 467 StringBuilder sb = new StringBuilder (); 468 469 sb.append("<"); 470 sb.append(start == null ? " " : start.getOffset()); 471 sb.append(","); 472 sb.append(end == null ? " " : end.getOffset()); 473 sb.append(","); 474 dumpAttributes(sb, attribs); 475 sb.append(">"); 476 477 return sb.toString(); 478 } 479 480 private String dumpAttributes(StringBuilder sb, AttributeSet attribs) { 481 if (sb == null) { 482 sb = new StringBuilder (); 483 } 484 485 if (attribs == null) { 486 sb.append(" "); 487 } else { 488 Enumeration en = attribs.getAttributeNames(); 489 while (en.hasMoreElements()) { 490 Object attrName = en.nextElement(); 491 Object attrValue = attribs.getAttribute(attrName); 492 493 sb.append("'"); 494 sb.append(attrName.toString()); 495 sb.append("' = '"); 496 sb.append(attrValue == null ? "null" : attrValue.toString()); 497 sb.append("'"); 498 if (en.hasMoreElements()) { 499 sb.append(", "); 500 } 501 } 502 } 503 504 return sb.toString(); 505 } 506 507 private void dumpMarks(OffsetGapList<OffsetsBag.Mark> marks) { 508 String signature = marks.getClass() + "@" + Integer.toHexString(System.identityHashCode(marks)); 509 System.out.println("Dumping marks from " + signature + " {"); 510 for(OffsetsBag.Mark mark : marks) { 511 System.out.println("<" + mark.getOffset() + ", [" + dumpAttributes(null, mark.getAttributes()) + "]>"); 512 } 513 System.out.println("} ---- End of Dumping marks from " + signature + " --------"); 514 } 515 } 516 | Popular Tags |