1 19 20 package org.netbeans.spi.editor.highlighting.support; 21 22 import java.util.ConcurrentModificationException ; 23 import javax.swing.text.AttributeSet ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.DefaultStyledDocument ; 26 import javax.swing.text.Document ; 27 import javax.swing.text.PlainDocument ; 28 import javax.swing.text.Position ; 29 import javax.swing.text.SimpleAttributeSet ; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.lib.editor.util.GapList; 32 import org.netbeans.spi.editor.highlighting.*; 33 import org.netbeans.spi.editor.highlighting.performance.SimplePosition; 34 35 39 public class PositionsBagTest extends NbTestCase { 40 41 private static final AttributeSet EMPTY = SimpleAttributeSet.EMPTY; 42 43 private Document doc = new DefaultStyledDocument (); 44 45 46 public PositionsBagTest(String name) { 47 super(name); 48 } 49 50 public void testSimple() { 51 PositionsBag hs = new PositionsBag(doc); 52 assertEquals("Sequence should be empty", 0, hs.getMarks().size()); 53 54 hs.addHighlight(pos(10), pos(20), EMPTY); 55 GapList<Position > marks = hs.getMarks(); 56 assertEquals("Sequence should not be empty", 2, marks.size()); 57 assertEquals("Wrong highlight's start offset", 10, marks.get(0).getOffset()); 58 assertEquals("Wrong highlight's end offset", 20, marks.get(1).getOffset()); 59 60 hs.clear(); 61 assertEquals("Sequence was not cleared", 0, hs.getMarks().size()); 62 } 63 64 public void testAddLeftOverlap() { 65 PositionsBag hs = new PositionsBag(doc); 66 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 67 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 68 69 attribsA.addAttribute("set-name", "attribsA"); 70 attribsB.addAttribute("set-name", "attribsB"); 71 72 hs.addHighlight(pos(10), pos(20), attribsA); 73 hs.addHighlight(pos(5), pos(15), attribsB); 74 GapList<Position > marks = hs.getMarks(); 75 GapList<AttributeSet > atttributes = hs.getAttributes(); 76 77 assertEquals("Wrong number of highlights", 3, marks.size()); 78 assertEquals("1. highlight - wrong start offset", 5, marks.get(0).getOffset()); 79 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 80 assertEquals("1. highlight - wrong attribs", "attribsB", atttributes.get(0).getAttribute("set-name")); 81 82 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 83 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 84 assertEquals("2. highlight - wrong attribs", "attribsA", atttributes.get(1).getAttribute("set-name")); 85 assertNull(" 2. highlight - wrong end", atttributes.get(2)); 86 } 87 88 public void testAddRightOverlap() { 89 PositionsBag hs = new PositionsBag(doc); 90 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 91 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 92 93 attribsA.addAttribute("set-name", "attribsA"); 94 attribsB.addAttribute("set-name", "attribsB"); 95 96 hs.addHighlight(pos(10), pos(20), attribsA); 97 hs.addHighlight(pos(15), pos(25), attribsB); 98 GapList<Position > marks = hs.getMarks(); 99 GapList<AttributeSet > atttributes = hs.getAttributes(); 100 101 assertEquals("Wrong number of highlights", 3, marks.size()); 102 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 103 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 104 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 105 106 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 107 assertEquals("2. highlight - wrong end offset", 25, marks.get(2).getOffset()); 108 assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name")); 109 assertNull( "2. highlight - wrong end", atttributes.get(2)); 110 } 111 112 public void testAddCompleteOverlap() { 113 PositionsBag hs = new PositionsBag(doc); 114 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 115 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 116 117 attribsA.addAttribute("set-name", "attribsA"); 118 attribsB.addAttribute("set-name", "attribsB"); 119 120 hs.addHighlight(pos(10), pos(20), attribsA); 121 hs.addHighlight(pos(5), pos(25), attribsB); 122 GapList<Position > marks = hs.getMarks(); 123 GapList<AttributeSet > atttributes = hs.getAttributes(); 124 125 assertEquals("Wrong number of highlights", 2, marks.size()); 126 assertEquals("1. highlight - wrong start offset", 5, marks.get(0).getOffset()); 127 assertEquals("1. highlight - wrong end offset", 25, marks.get(1).getOffset()); 128 assertEquals("1. highlight - wrong attribs", "attribsB", atttributes.get(0).getAttribute("set-name")); 129 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 130 } 131 132 public void testAddSplit() { 133 PositionsBag hs = new PositionsBag(doc); 134 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 135 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 136 137 attribsA.addAttribute("set-name", "attribsA"); 138 attribsB.addAttribute("set-name", "attribsB"); 139 140 hs.addHighlight(pos(10), pos(25), attribsA); 141 hs.addHighlight(pos(15), pos(20), attribsB); 142 GapList<Position > marks = hs.getMarks(); 143 GapList<AttributeSet > atttributes = hs.getAttributes(); 144 145 assertEquals("Wrong number of highlights", 4, marks.size()); 146 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 147 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 148 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 149 150 assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset()); 151 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 152 assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name")); 153 154 assertEquals("3. highlight - wrong start offset", 20, marks.get(2).getOffset()); 155 assertEquals("3. highlight - wrong end offset", 25, marks.get(3).getOffset()); 156 assertEquals("3. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name")); 157 assertNull(" 3. highlight - wrong end", atttributes.get(3)); 158 } 159 160 public void testAddAligned() { 161 PositionsBag hs = new PositionsBag(doc); 162 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 163 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 164 165 attribsA.addAttribute("set-name", "attribsA"); 166 attribsB.addAttribute("set-name", "attribsB"); 167 168 hs.addHighlight(pos(10), pos(20), attribsA); 169 hs.addHighlight(pos(20), pos(30), attribsB); 170 GapList<Position > marks = hs.getMarks(); 171 GapList<AttributeSet > atttributes = hs.getAttributes(); 172 173 assertEquals("Wrong number of highlights", 3, marks.size()); 174 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 175 assertEquals("1. highlight - wrong end offset", 20, marks.get(1).getOffset()); 176 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 177 178 assertEquals("2. highlight - wrong start offset", 20, marks.get(1).getOffset()); 179 assertEquals("2. highlight - wrong end offset", 30, marks.get(2).getOffset()); 180 assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name")); 181 assertNull(" 2. highlight - wrong end", atttributes.get(2)); 182 183 hs.addHighlight(pos(0), pos(10), attribsB); 184 assertEquals("Wrong number of highlights", 4, marks.size()); 185 assertEquals("1. highlight - wrong start offset", 0, marks.get(0).getOffset()); 186 assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset()); 187 assertEquals("1. highlight - wrong attribs", "attribsB", atttributes.get(0).getAttribute("set-name")); 188 189 assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset()); 190 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 191 assertEquals("2. highlight - wrong attribs", "attribsA", atttributes.get(1).getAttribute("set-name")); 192 193 assertEquals("3. highlight - wrong start offset", 20, marks.get(2).getOffset()); 194 assertEquals("3. highlight - wrong end offset", 30, marks.get(3).getOffset()); 195 assertEquals("3. highlight - wrong attribs", "attribsB", atttributes.get(2).getAttribute("set-name")); 196 assertNull(" 3. highlight - wrong end", atttributes.get(3)); 197 } 198 199 public void testAddAligned2() { 200 PositionsBag hs = new PositionsBag(doc); 201 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 202 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 203 204 attribsA.addAttribute("set-name", "attribsA"); 205 attribsB.addAttribute("set-name", "attribsB"); 206 207 hs.addHighlight(pos(10), pos(40), attribsA); 208 hs.addHighlight(pos(10), pos(20), attribsB); 209 hs.addHighlight(pos(30), pos(40), attribsB); 210 GapList<Position > marks = hs.getMarks(); 211 GapList<AttributeSet > atttributes = hs.getAttributes(); 212 213 assertEquals("Wrong number of highlights", 4, marks.size()); 214 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 215 assertEquals("1. highlight - wrong end offset", 20, marks.get(1).getOffset()); 216 assertEquals("1. highlight - wrong attribs", "attribsB", atttributes.get(0).getAttribute("set-name")); 217 218 assertEquals("2. highlight - wrong start offset", 20, marks.get(1).getOffset()); 219 assertEquals("2. highlight - wrong end offset", 30, marks.get(2).getOffset()); 220 assertEquals("2. highlight - wrong attribs", "attribsA", atttributes.get(1).getAttribute("set-name")); 221 222 assertEquals("3. highlight - wrong start offset", 30, marks.get(2).getOffset()); 223 assertEquals("3. highlight - wrong end offset", 40, marks.get(3).getOffset()); 224 assertEquals("3. highlight - wrong attribs", "attribsB", atttributes.get(2).getAttribute("set-name")); 225 assertNull(" 3. highlight - wrong end", atttributes.get(3)); 226 } 227 228 public void testAddMiddle() { 229 for(int i = 0; i < 10; i++) { 230 addMiddle(i + 1); 231 } 232 } 233 234 private void addMiddle(int middleMarks) { 235 PositionsBag hs = new PositionsBag(doc); 236 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 237 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 238 SimpleAttributeSet attribsC = new SimpleAttributeSet (); 239 240 attribsA.addAttribute("set-name", "attribsA"); 241 attribsB.addAttribute("set-name", "attribsB"); 242 attribsC.addAttribute("set-name", "attribsC"); 243 244 for (int i = 0; i < middleMarks + 1; i++) { 245 hs.addHighlight(pos(10 * i + 10), pos(10 * i + 20), i % 2 == 0 ? attribsA : attribsB); 246 } 247 248 hs.addHighlight(pos(15), pos(middleMarks * 10 + 15), attribsC); 249 GapList<Position > marks = hs.getMarks(); 250 GapList<AttributeSet > atttributes = hs.getAttributes(); 251 252 assertEquals("Wrong number of highlights (middleMarks = " + middleMarks + ")", 253 4, marks.size()); 254 assertEquals("1. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 255 10, marks.get(0).getOffset()); 256 assertEquals("1. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 257 15, marks.get(1).getOffset()); 258 assertEquals("1. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 259 "attribsA", atttributes.get(0).getAttribute("set-name")); 260 261 assertEquals("2. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 262 15, marks.get(1).getOffset()); 263 assertEquals("2. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 264 middleMarks * 10 + 15, marks.get(2).getOffset()); 265 assertEquals("2. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 266 "attribsC", atttributes.get(1).getAttribute("set-name")); 267 268 assertEquals("3. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 269 middleMarks * 10 + 15, marks.get(2).getOffset()); 270 assertEquals("3. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 271 (middleMarks + 2) * 10, marks.get(3).getOffset()); 272 assertEquals("3. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 273 middleMarks % 2 == 0 ? "attribsA" : "attribsB", atttributes.get(2).getAttribute("set-name")); 274 assertNull(" 3. highlight - wrong end (middleMarks = " + middleMarks + ")", 275 atttributes.get(3)); 276 } 277 278 public void testRemoveLeftOverlap() { 279 PositionsBag hs = new PositionsBag(doc); 280 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 281 282 attribsA.addAttribute("set-name", "attribsA"); 283 284 hs.addHighlight(pos(10), pos(20), attribsA); 285 hs.removeHighlights(5, 15); 286 GapList<Position > marks = hs.getMarks(); 287 288 assertEquals("Wrong number of highlights", 0, marks.size()); 289 } 290 291 public void testRemoveLeftOverlapClip() { 292 PositionsBag hs = new PositionsBag(doc); 293 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 294 295 attribsA.addAttribute("set-name", "attribsA"); 296 297 hs.addHighlight(pos(10), pos(20), attribsA); 298 hs.removeHighlights(pos(5), pos(15), true); 299 GapList<Position > marks = hs.getMarks(); 300 GapList<AttributeSet > atttributes = hs.getAttributes(); 301 302 assertEquals("Wrong number of highlights", 2, marks.size()); 303 assertEquals("1. highlight - wrong start offset", 15, marks.get(0).getOffset()); 304 assertEquals("1. highlight - wrong end offset", 20, marks.get(1).getOffset()); 305 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 306 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 307 } 308 309 public void testRemoveRightOverlap() { 310 PositionsBag hs = new PositionsBag(doc); 311 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 312 313 attribsA.addAttribute("set-name", "attribsA"); 314 315 hs.addHighlight(pos(10), pos(20), attribsA); 316 hs.removeHighlights(15, 25); 317 GapList<Position > marks = hs.getMarks(); 318 319 assertEquals("Wrong number of highlights", 0, marks.size()); 320 } 321 322 public void testRemoveRightOverlapClip() { 323 PositionsBag hs = new PositionsBag(doc); 324 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 325 326 attribsA.addAttribute("set-name", "attribsA"); 327 328 hs.addHighlight(pos(10), pos(20), attribsA); 329 hs.removeHighlights(pos(15), pos(25), true); 330 GapList<Position > marks = hs.getMarks(); 331 GapList<AttributeSet > atttributes = hs.getAttributes(); 332 333 assertEquals("Wrong number of highlights", 2, marks.size()); 334 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 335 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 336 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 337 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 338 } 339 340 public void testRemoveCompleteOverlap() { 341 PositionsBag hs = new PositionsBag(doc); 342 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 343 344 attribsA.addAttribute("set-name", "attribsA"); 345 346 hs.addHighlight(pos(10), pos(20), attribsA); 347 hs.removeHighlights(5, 25); 348 GapList<Position > marks = hs.getMarks(); 349 350 assertEquals("Wrong number of highlights", 0, marks.size()); 351 } 352 353 public void testRemoveCompleteOverlapClip() { 354 PositionsBag hs = new PositionsBag(doc); 355 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 356 357 attribsA.addAttribute("set-name", "attribsA"); 358 359 hs.addHighlight(pos(10), pos(20), attribsA); 360 hs.removeHighlights(pos(5), pos(25), true); 361 GapList<Position > marks = hs.getMarks(); 362 363 assertEquals("Wrong number of highlights", 0, marks.size()); 364 } 365 366 public void testRemoveSplit() { 367 PositionsBag hs = new PositionsBag(doc); 368 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 369 370 attribsA.addAttribute("set-name", "attribsA"); 371 372 hs.addHighlight(pos(10), pos(25), attribsA); 373 hs.removeHighlights(15, 20); 374 GapList<Position > marks = hs.getMarks(); 375 376 assertEquals("Wrong number of highlights", 0, marks.size()); 377 } 378 379 public void testRemoveSplitClip() { 380 PositionsBag hs = new PositionsBag(doc); 381 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 382 383 attribsA.addAttribute("set-name", "attribsA"); 384 385 hs.addHighlight(pos(10), pos(25), attribsA); 386 hs.removeHighlights(pos(15), pos(20), true); 387 GapList<Position > marks = hs.getMarks(); 388 GapList<AttributeSet > atttributes = hs.getAttributes(); 389 390 assertEquals("Wrong number of highlights", 4, marks.size()); 391 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 392 assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset()); 393 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 394 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 395 396 assertEquals("2. highlight - wrong start offset", 20, marks.get(2).getOffset()); 397 assertEquals("2. highlight - wrong end offset", 25, marks.get(3).getOffset()); 398 assertEquals("2. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name")); 399 assertNull(" 2. highlight - wrong end", atttributes.get(3)); 400 } 401 402 public void testRemoveAlignedClip() { 403 PositionsBag hs = new PositionsBag(doc); 404 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 405 406 attribsA.addAttribute("set-name", "attribsA"); 407 408 hs.addHighlight(pos(10), pos(20), attribsA); 409 hs.removeHighlights(pos(0), pos(10), true); 410 GapList<Position > marks = hs.getMarks(); 411 GapList<AttributeSet > atttributes = hs.getAttributes(); 412 413 assertEquals("Wrong number of highlights", 2, marks.size()); 414 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 415 assertEquals("1. highlight - wrong end offset", 20, marks.get(1).getOffset()); 416 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 417 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 418 419 hs.removeHighlights(pos(20), pos(30), true); 420 assertEquals("Wrong number of highlights", 2, marks.size()); 421 assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset()); 422 assertEquals("1. highlight - wrong end offset", 20, marks.get(1).getOffset()); 423 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 424 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 425 } 426 427 public void testRemoveAligned2Clip() { 428 PositionsBag hs = new PositionsBag(doc); 429 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 430 431 attribsA.addAttribute("set-name", "attribsA"); 432 433 hs.addHighlight(pos(10), pos(40), attribsA); 434 hs.removeHighlights(pos(10), pos(20), true); 435 GapList<Position > marks = hs.getMarks(); 436 GapList<AttributeSet > atttributes = hs.getAttributes(); 437 438 assertEquals("Wrong number of highlights", 2, marks.size()); 439 assertEquals("1. highlight - wrong start offset", 20, marks.get(0).getOffset()); 440 assertEquals("1. highlight - wrong end offset", 40, marks.get(1).getOffset()); 441 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 442 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 443 444 hs.removeHighlights(pos(30), pos(40), true); 445 446 assertEquals("Wrong number of highlights", 2, marks.size()); 447 assertEquals("1. highlight - wrong start offset", 20, marks.get(0).getOffset()); 448 assertEquals("1. highlight - wrong end offset", 30, marks.get(1).getOffset()); 449 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 450 assertNull(" 1. highlight - wrong end", atttributes.get(1)); 451 } 452 453 public void testRemoveMiddle() { 454 PositionsBag hs = new PositionsBag(doc); 455 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 456 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 457 458 attribsA.addAttribute("set-name", "attribsA"); 459 attribsB.addAttribute("set-name", "attribsB"); 460 461 hs.addHighlight(pos(10), pos(20), attribsA); 462 hs.addHighlight(pos(20), pos(30), attribsB); 463 hs.removeHighlights(15, 25); 464 GapList<Position > marks = hs.getMarks(); 465 466 assertEquals("Wrong number of highlights", 0, marks.size()); 467 } 468 469 public void testRemoveMiddleClip() { 470 for(int i = 0; i < 10; i++) { 471 removeMiddleClip(i + 1); 472 } 473 } 474 475 private void removeMiddleClip(int middleMarks) { 476 PositionsBag hs = new PositionsBag(doc); 477 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 478 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 479 480 attribsA.addAttribute("set-name", "attribsA"); 481 attribsB.addAttribute("set-name", "attribsB"); 482 483 for (int i = 0; i < middleMarks + 1; i++) { 484 hs.addHighlight(pos(10 * i + 10), pos(10 * i + 20), i % 2 == 0 ? attribsA : attribsB); 485 } 486 487 hs.removeHighlights(pos(15), pos(middleMarks * 10 + 15), true); 488 GapList<Position > marks = hs.getMarks(); 489 GapList<AttributeSet > atttributes = hs.getAttributes(); 490 491 assertEquals("Wrong number of highlights (middleMarks = " + middleMarks + ")", 492 4, marks.size()); 493 assertEquals("1. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 494 10, marks.get(0).getOffset()); 495 assertEquals("1. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 496 15, marks.get(1).getOffset()); 497 assertEquals("1. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 498 "attribsA", atttributes.get(0).getAttribute("set-name")); 499 assertNull(" 1. highlight - wrong end (middleMarks = " + middleMarks + ")", 500 atttributes.get(1)); 501 502 assertEquals("2. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 503 middleMarks * 10 + 15, marks.get(2).getOffset()); 504 assertEquals("2. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 505 (middleMarks + 2) * 10, marks.get(3).getOffset()); 506 assertEquals("2. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 507 middleMarks % 2 == 0 ? "attribsA" : "attribsB", atttributes.get(2).getAttribute("set-name")); 508 assertNull(" 2. highlight - wrong end (middleMarks = " + middleMarks + ")", 509 atttributes.get(3)); 510 } 511 512 public void testAddAll() { 513 PositionsBag hsA = new PositionsBag(doc); 514 PositionsBag hsB = new PositionsBag(doc); 515 516 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 517 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 518 SimpleAttributeSet attribsC = new SimpleAttributeSet (); 519 520 attribsA.addAttribute("set-name", "attribsA"); 521 attribsB.addAttribute("set-name", "attribsB"); 522 attribsC.addAttribute("set-name", "attribsC"); 523 524 hsA.addHighlight(pos(0), pos(30), attribsA); 525 hsA.addHighlight(pos(10), pos(20), attribsB); 526 GapList<Position > marksA = hsA.getMarks(); 527 GapList<AttributeSet > atttributesA = hsA.getAttributes(); 528 529 hsB.addHighlight(pos(0), pos(40), attribsC); 530 hsB.addAllHighlights(hsA); 531 GapList<Position > marksB = hsB.getMarks(); 532 GapList<AttributeSet > atttributesB = hsB.getAttributes(); 533 534 assertEquals("Wrong number of highlights", marksA.size() + 1, marksB.size()); 535 for (int i = 0; i < marksA.size() - 1; i++) { 536 assertEquals(i + ". highlight - wrong start offset", 537 marksA.get(i).getOffset(), marksB.get(i).getOffset()); 538 assertEquals(i + ". highlight - wrong end offset", 539 marksA.get(i + 1).getOffset(), marksB.get(i + 1).getOffset()); 540 assertEquals(i + ". highlight - wrong attribs", 541 atttributesA.get(i).getAttribute("set-name"), 542 atttributesB.get(i).getAttribute("set-name")); 543 } 544 545 assertEquals("4. highlight - wrong start offset", 30, marksB.get(3).getOffset()); 546 assertEquals("4. highlight - wrong end offset", 40, marksB.get(4).getOffset()); 547 assertEquals("4. highlight - wrong attribs", "attribsC", atttributesB.get(3).getAttribute("set-name")); 548 } 549 550 public void testSet() { 551 PositionsBag hsA = new PositionsBag(doc); 552 PositionsBag hsB = new PositionsBag(doc); 553 554 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 555 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 556 SimpleAttributeSet attribsC = new SimpleAttributeSet (); 557 558 attribsA.addAttribute("set-name", "attribsA"); 559 attribsB.addAttribute("set-name", "attribsB"); 560 attribsC.addAttribute("set-name", "attribsC"); 561 562 hsA.addHighlight(pos(0), pos(30), attribsA); 563 hsA.addHighlight(pos(10), pos(20), attribsB); 564 GapList<Position > marksA = hsA.getMarks(); 565 GapList<AttributeSet > atttributesA = hsA.getAttributes(); 566 567 hsB.addHighlight(pos(0), pos(40), attribsC); 568 hsB.setHighlights(hsA); 569 GapList<Position > marksB = hsB.getMarks(); 570 GapList<AttributeSet > atttributesB = hsB.getAttributes(); 571 572 assertEquals("Wrong number of highlights", marksA.size(), marksB.size()); 573 for (int i = 0; i < marksA.size(); i++) { 574 assertEquals(i + ". highlight - wrong start offset", 575 marksA.get(i).getOffset(), marksB.get(i).getOffset()); 576 assertEquals(i + ". highlight - wrong end offset", 577 marksA.get(i).getOffset(), marksB.get(i).getOffset()); 578 579 AttributeSet attrA = atttributesA.get(i); 580 AttributeSet attrB = atttributesB.get(i); 581 582 if (attrA != null && attrB != null) { 583 assertEquals(i + ". highlight - wrong attribs", 584 attrA.getAttribute("set-name"), 585 attrB.getAttribute("set-name")); 586 } else { 587 assertTrue(i + ". highlight - wrong attribs", attrA == null && attrB == null); 588 } 589 } 590 } 591 592 public void testGetHighlights() { 593 PositionsBag hs = new PositionsBag(doc); 594 assertFalse("Sequence should be empty", hs.getHighlights( 595 Integer.MIN_VALUE, Integer.MAX_VALUE).moveNext()); 596 597 hs.addHighlight(pos(10), pos(30), EMPTY); 598 599 { 600 HighlightsSequence highlights = hs.getHighlights(20, 25); 602 assertTrue("Sequence should not be empty", highlights.moveNext()); 603 assertEquals("Wrong highlight's start offset", 20, highlights.getStartOffset()); 604 assertEquals("Wrong highlight's end offset", 25, highlights.getEndOffset()); 605 assertFalse("There should be no more highlights", highlights.moveNext()); 606 } 607 608 hs.clear(); 609 assertFalse("Sequence was not cleared", hs.getHighlights( 610 Integer.MIN_VALUE, Integer.MAX_VALUE).moveNext()); 611 } 612 613 public void testGetHighlights2() { 614 PositionsBag hb = new PositionsBag(doc); 615 hb.addHighlight(pos(10), pos(20), SimpleAttributeSet.EMPTY); 616 617 HighlightsSequence hs = hb.getHighlights(0, 5); 618 assertFalse("HighlightsSequence should be empty", hs.moveNext()); 619 620 hs = hb.getHighlights(25, 30); 621 assertFalse("HighlightsSequence should be empty", hs.moveNext()); 622 623 hs = hb.getHighlights(0, 15); 624 assertTrue("HighlightsSequence should not be empty", hs.moveNext()); 625 assertFalse("Too many highlights in the sequence", hs.moveNext()); 626 627 hs = hb.getHighlights(12, 22); 628 assertTrue("HighlightsSequence should not be empty", hs.moveNext()); 629 assertFalse("Too many highlights in the sequence", hs.moveNext()); 630 631 hs = hb.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE); 632 assertTrue("HighlightsSequence should not be empty", hs.moveNext()); 633 assertFalse("Too many highlights in the sequence", hs.moveNext()); 634 } 635 636 public void testConcurrentModification() { 637 { 638 PositionsBag hb = new PositionsBag(doc); 639 HighlightsSequence hs = hb.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE); 640 641 hb.addHighlight(pos(5), pos(10), EMPTY); 643 644 try { 645 hs.moveNext(); 646 fail("ConcurrentModificationException has not been thrown from moveNext()"); 647 } catch (ConcurrentModificationException e) { 648 } 650 } 651 652 { 653 PositionsBag hb = new PositionsBag(doc); 654 hb.addHighlight(pos(5), pos(10), EMPTY); 655 656 HighlightsSequence hs = hb.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE); 657 assertTrue("Sequence should not be empty", hs.moveNext()); 658 659 hb.addHighlight(pos(20), pos(30), EMPTY); 661 662 try { 663 hs.getStartOffset(); 664 fail("ConcurrentModificationException has not been thrown from getStartPosition()"); 665 } catch (ConcurrentModificationException e) { 666 } 668 } 669 670 { 671 PositionsBag hb = new PositionsBag(doc); 672 hb.addHighlight(pos(5), pos(10), EMPTY); 673 674 HighlightsSequence hs = hb.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE); 675 assertTrue("Sequence should not be empty", hs.moveNext()); 676 677 hb.addHighlight(pos(20), pos(30), EMPTY); 679 680 try { 681 hs.getEndOffset(); 682 fail("ConcurrentModificationException has not been thrown from getEndPosition()"); 683 } catch (ConcurrentModificationException e) { 684 } 686 } 687 688 { 689 PositionsBag hb = new PositionsBag(doc); 690 hb.addHighlight(pos(5), pos(10), EMPTY); 691 692 HighlightsSequence hs = hb.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE); 693 assertTrue("Sequence should not be empty", hs.moveNext()); 694 695 hb.addHighlight(pos(20), pos(30), EMPTY); 697 698 try { 699 hs.getAttributes(); 700 fail("ConcurrentModificationException has not been thrown from getAttributes()"); 701 } catch (ConcurrentModificationException e) { 702 } 704 } 705 } 706 707 public void testDocumentChanges() throws BadLocationException { 708 Document doc = new PlainDocument (); 709 doc.insertString(0, "01234567890123456789012345678901234567890123456789", SimpleAttributeSet.EMPTY); 710 711 PositionsBag bag = new PositionsBag(doc); 712 713 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 714 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 715 716 attribsA.addAttribute("set-name", "attribsA"); 717 attribsB.addAttribute("set-name", "attribsB"); 718 719 bag.addHighlight(doc.createPosition(0), doc.createPosition(30), attribsA); 720 bag.addHighlight(doc.createPosition(10), doc.createPosition(20), attribsB); 721 GapList<Position > marks = bag.getMarks(); 722 GapList<AttributeSet > atttributes = bag.getAttributes(); 723 724 assertEquals("Wrong number of highlights", 4, marks.size()); 725 assertEquals("1. highlight - wrong start offset", 0, marks.get(0).getOffset()); 726 assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset()); 727 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 728 729 assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset()); 730 assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset()); 731 assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name")); 732 733 assertEquals("3. highlight - wrong start offset", 20, marks.get(2).getOffset()); 734 assertEquals("3. highlight - wrong end offset", 30, marks.get(3).getOffset()); 735 assertEquals("3. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name")); 736 assertNull(" 3. highlight - wrong end", atttributes.get(3)); 737 738 doc.insertString(12, "----", SimpleAttributeSet.EMPTY); 739 740 assertEquals("Wrong number of highlights", 4, marks.size()); 741 assertEquals("1. highlight - wrong start offset", 0, marks.get(0).getOffset()); 742 assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset()); 743 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 744 745 assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset()); 746 assertEquals("2. highlight - wrong end offset", 24, marks.get(2).getOffset()); 747 assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name")); 748 749 assertEquals("3. highlight - wrong start offset", 24, marks.get(2).getOffset()); 750 assertEquals("3. highlight - wrong end offset", 34, marks.get(3).getOffset()); 751 assertEquals("3. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name")); 752 assertNull(" 3. highlight - wrong end", atttributes.get(3)); 753 754 doc.remove(1, 5); 755 756 assertEquals("Wrong number of highlights", 4, marks.size()); 757 assertEquals("1. highlight - wrong start offset", 0, marks.get(0).getOffset()); 758 assertEquals("1. highlight - wrong end offset", 5, marks.get(1).getOffset()); 759 assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name")); 760 761 assertEquals("2. highlight - wrong start offset", 5, marks.get(1).getOffset()); 762 assertEquals("2. highlight - wrong end offset", 19, marks.get(2).getOffset()); 763 assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name")); 764 765 assertEquals("3. highlight - wrong start offset", 19, marks.get(2).getOffset()); 766 assertEquals("3. highlight - wrong end offset", 29, marks.get(3).getOffset()); 767 assertEquals("3. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name")); 768 assertNull(" 3. highlight - wrong end", atttributes.get(3)); 769 } 770 771 private void dumpHighlights(HighlightsSequence seq) { 772 System.out.println("Dumping highlights from: " + seq + "{"); 773 while(seq.moveNext()) { 774 System.out.println("<" + seq.getStartOffset() + ", " + seq.getEndOffset() + ", " + seq.getAttributes() + ">"); 775 } 776 System.out.println("} --- End of Dumping highlights from: " + seq + " ---------------------"); 777 } 778 779 private Position pos(int offset) { 780 return new SimplePosition(offset); 781 } 782 } 783 | Popular Tags |