1 19 20 package org.netbeans.modules.editor.lib2.highlighting; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.List ; 26 import java.util.Random ; 27 import javax.swing.JEditorPane ; 28 import javax.swing.text.AttributeSet ; 29 import javax.swing.text.DefaultEditorKit ; 30 import javax.swing.text.PlainDocument ; 31 import javax.swing.text.Position ; 32 import javax.swing.text.SimpleAttributeSet ; 33 import org.netbeans.api.editor.mimelookup.MimeLookup; 34 import org.netbeans.api.editor.mimelookup.MimePath; 35 import org.netbeans.junit.NbTestCase; 36 import org.netbeans.spi.editor.highlighting.support.OffsetsBag; 37 import org.netbeans.spi.editor.highlighting.HighlightsChangeEvent; 38 import org.netbeans.spi.editor.highlighting.HighlightsChangeListener; 39 import org.netbeans.spi.editor.highlighting.HighlightsContainer; 40 import org.netbeans.spi.editor.highlighting.HighlightsLayer; 41 import org.netbeans.spi.editor.highlighting.HighlightsLayerFactory; 42 import org.netbeans.spi.editor.highlighting.HighlightsSequence; 43 import org.netbeans.spi.editor.highlighting.ZOrder; 44 import org.openide.util.Lookup; 45 46 50 public class HighlightingManagerTest extends NbTestCase { 51 52 53 public HighlightingManagerTest(String name) { 54 super(name); 55 } 56 57 public void testSimple() { 58 HighlightingManager hm = HighlightingManager.getInstance(); 59 assertNotNull("Can't get instance of HighlightingManager", hm); 60 61 JEditorPane pane = new JEditorPane (); 62 HighlightsContainer hc = hm.getHighlights(pane, HighlightsLayerFilter.IDENTITY); 63 assertNotNull("Can't get fixed HighlightsContainer", hc); 64 assertFalse("There should be no fixed highlights", hc.getHighlights(0, Integer.MAX_VALUE).moveNext()); 65 } 66 67 public void testSimpleLayer() { 68 OffsetsBag bag = new OffsetsBag(new PlainDocument ()); 69 70 MemoryMimeDataProvider.reset(null); 71 MemoryMimeDataProvider.addInstances( 72 "text/plain", new SingletonLayerFactory("layer", ZOrder.DEFAULT_RACK, true, bag)); 73 74 JEditorPane pane = new JEditorPane (); 75 pane.setContentType("text/plain"); 76 assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType()); 77 78 HighlightingManager hm = HighlightingManager.getInstance(); 79 HighlightsContainer hc = hm.getHighlights(pane, HighlightsLayerFilter.IDENTITY); 80 assertNotNull("Can't get fixed HighlightsContainer", hc); 81 assertFalse("There should be no fixed highlights", hc.getHighlights(0, Integer.MAX_VALUE).moveNext()); 82 83 SimpleAttributeSet attributes = new SimpleAttributeSet (); 84 attributes.addAttribute("attrib-A", "value"); 85 86 bag.addHighlight(10, 20, attributes); 87 88 HighlightsSequence highlights = hc.getHighlights(0, Integer.MAX_VALUE); 89 assertTrue("Highlight has not been added", highlights.moveNext()); 90 assertEquals("Wrong start offset", 10, highlights.getStartOffset()); 91 assertEquals("Wrong end offset", 20, highlights.getEndOffset()); 92 assertEquals("Can't find attribute", "value", highlights.getAttributes().getAttribute("attrib-A")); 93 } 94 95 97 public void testMultipleLayers() { 98 OffsetsBag bagA = new OffsetsBag(new PlainDocument ()); 99 OffsetsBag bagB = new OffsetsBag(new PlainDocument ()); 100 OffsetsBag bagC = new OffsetsBag(new PlainDocument ()); 101 OffsetsBag bagD = new OffsetsBag(new PlainDocument ()); 102 103 MemoryMimeDataProvider.reset(null); 104 MemoryMimeDataProvider.addInstances("text/plain", 105 new SingletonLayerFactory("layerB", ZOrder.above("layerA"), false, bagB), 106 new SingletonLayerFactory("layerD", ZOrder.above("layerC"), true, bagD), 107 new SingletonLayerFactory("layerA", ZOrder.DEFAULT_RACK, true, bagA), 108 new SingletonLayerFactory("layerC", ZOrder.above("layerB"), true, bagC) 109 ); 110 111 JEditorPane pane = new JEditorPane (); 112 pane.setContentType("text/plain"); 113 assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType()); 114 115 HighlightingManager hm = HighlightingManager.getInstance(); 116 HighlightsContainer variableHC = hm.getHighlights(pane, VARIABLE_SIZE_LAYERS); 117 assertNotNull("Can't get variable HighlightsContainer", variableHC); 118 assertFalse("There should be no variable highlights", variableHC.getHighlights(0, Integer.MAX_VALUE).moveNext()); 119 120 HighlightsContainer fixedHC = hm.getHighlights(pane, FIXED_SIZE_LAYERS); 121 assertNotNull("Can't get fixed HighlightsContainer", fixedHC); 122 assertFalse("There should be no fixed highlights", fixedHC.getHighlights(0, Integer.MAX_VALUE).moveNext()); 123 124 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 125 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 126 SimpleAttributeSet attribsC = new SimpleAttributeSet (); 127 SimpleAttributeSet attribsD = new SimpleAttributeSet (); 128 attribsA.addAttribute("set-A", "value"); 129 attribsA.addAttribute("commonAttribute", "set-A-value"); 130 attribsB.addAttribute("set-B", "value"); 131 attribsB.addAttribute("commonAttribute", "set-B-value"); 132 attribsC.addAttribute("set-C", "value"); 133 attribsC.addAttribute("commonAttribute", "set-C-value"); 134 attribsD.addAttribute("set-D", "value"); 135 attribsD.addAttribute("commonAttribute", "set-D-value"); 136 137 bagA.addHighlight(10, 20, attribsA); 138 bagB.addHighlight(15, 25, attribsB); 139 bagC.addHighlight(10, 20, attribsC); 140 bagD.addHighlight(15, 25, attribsD); 141 142 143 HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE); 145 assertTrue("Wrong number of highlights", fixed.moveNext()); 147 assertEquals("Wrong start offset", 10, fixed.getStartOffset()); 148 assertEquals("Wrong end offset", 15, fixed.getEndOffset()); 149 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "commonAttribute"); 150 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-D"); 151 assertEquals("Wrong commonAttribute value", "set-C-value", fixed.getAttributes().getAttribute("commonAttribute")); 152 assertTrue("Wrong number of highlights", fixed.moveNext()); 154 assertEquals("Wrong start offset", 15, fixed.getStartOffset()); 155 assertEquals("Wrong end offset", 20, fixed.getEndOffset()); 156 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "set-D", "commonAttribute"); 157 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B"); 158 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 159 assertTrue("Wrong number of highlights", fixed.moveNext()); 161 assertEquals("Wrong start offset", 20, fixed.getStartOffset()); 162 assertEquals("Wrong end offset", 25, fixed.getEndOffset()); 163 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-D", "commonAttribute"); 164 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-C"); 165 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 166 167 168 HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE); 170 assertTrue("Wrong number of highlights", variable.moveNext()); 172 assertEquals("Wrong start offset", 10, variable.getStartOffset()); 173 assertEquals("Wrong end offset", 15, variable.getEndOffset()); 174 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "commonAttribute"); 175 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D", "set-B"); 176 assertEquals("Wrong commonAttribute value", "set-A-value", variable.getAttributes().getAttribute("commonAttribute")); 177 assertTrue("Wrong number of highlights", variable.moveNext()); 179 assertEquals("Wrong start offset", 15, variable.getStartOffset()); 180 assertEquals("Wrong end offset", 20, variable.getEndOffset()); 181 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "set-B", "commonAttribute"); 182 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D"); 183 assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute")); 184 assertTrue("Wrong number of highlights", variable.moveNext()); 186 assertEquals("Wrong start offset", 20, variable.getStartOffset()); 187 assertEquals("Wrong end offset", 25, variable.getEndOffset()); 188 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-B", "commonAttribute"); 189 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D", "set-A"); 190 assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute")); 191 } 192 193 195 public void testChangesInLayerFireEvents() { 196 OffsetsBag bagA = new OffsetsBag(new PlainDocument ()); 197 OffsetsBag bagB = new OffsetsBag(new PlainDocument ()); 198 OffsetsBag bagC = new OffsetsBag(new PlainDocument ()); 199 OffsetsBag bagD = new OffsetsBag(new PlainDocument ()); 200 201 MemoryMimeDataProvider.reset(null); 202 MemoryMimeDataProvider.addInstances("text/plain", 203 new SingletonLayerFactory("layerB", ZOrder.above("layerA"), false, bagB), 204 new SingletonLayerFactory("layerD", ZOrder.above("layerC"), true, bagD), 205 new SingletonLayerFactory("layerA", ZOrder.DEFAULT_RACK, true, bagA), 206 new SingletonLayerFactory("layerC", ZOrder.above("layerB"), true, bagC) 207 ); 208 209 JEditorPane pane = new JEditorPane (); 210 pane.setContentType("text/plain"); 211 assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType()); 212 213 HighlightingManager hm = HighlightingManager.getInstance(); 214 215 Listener variableL = new Listener(); 217 HighlightsContainer variableHC = hm.getHighlights(pane, VARIABLE_SIZE_LAYERS); 218 assertNotNull("Can't get variable HighlightsContainer", variableHC); 219 assertFalse("There should be no variable highlights", variableHC.getHighlights(0, Integer.MAX_VALUE).moveNext()); 220 221 variableHC.addHighlightsChangeListener(variableL); 222 bagA.addHighlight(10, 20, SimpleAttributeSet.EMPTY); 223 assertEquals("Wrong number of events", 1, variableL.eventsCnt); 224 assertEquals("Wrong change start offset", 10, variableL.lastStartOffset); 225 assertEquals("Wrong change end offset", 20, variableL.lastEndOffset); 226 227 variableL.reset(); 228 bagB.addHighlight(5, 15, SimpleAttributeSet.EMPTY); 229 assertEquals("Wrong number of events", 1, variableL.eventsCnt); 230 assertEquals("Wrong change start offset", 5, variableL.lastStartOffset); 231 assertEquals("Wrong change end offset", 15, variableL.lastEndOffset); 232 233 Listener fixedL = new Listener(); 235 HighlightsContainer fixedHC = hm.getHighlights(pane, FIXED_SIZE_LAYERS); 236 assertNotNull("Can't get fixed HighlightsContainer", fixedHC); 237 assertFalse("There should be no fixed highlights", fixedHC.getHighlights(0, Integer.MAX_VALUE).moveNext()); 238 239 fixedHC.addHighlightsChangeListener(fixedL); 240 bagC.addHighlight(20, 50, SimpleAttributeSet.EMPTY); 241 assertEquals("Wrong number of events", 1, fixedL.eventsCnt); 242 assertEquals("Wrong change start offset", 20, fixedL.lastStartOffset); 243 assertEquals("Wrong change end offset", 50, fixedL.lastEndOffset); 244 245 fixedL.reset(); 246 bagD.addHighlight(0, 30, SimpleAttributeSet.EMPTY); 247 assertEquals("Wrong number of events", 1, fixedL.eventsCnt); 248 assertEquals("Wrong change start offset", 0, fixedL.lastStartOffset); 249 assertEquals("Wrong change end offset", 30, fixedL.lastEndOffset); 250 } 251 252 254 public void testAddingRemovingLayers() { 255 final String mimeType = "text/plain"; 256 257 OffsetsBag bagA = new OffsetsBag(new PlainDocument ()); 258 OffsetsBag bagB = new OffsetsBag(new PlainDocument ()); 259 OffsetsBag bagC = new OffsetsBag(new PlainDocument ()); 260 OffsetsBag bagD = new OffsetsBag(new PlainDocument ()); 261 262 SimpleAttributeSet attribsA = new SimpleAttributeSet (); 263 SimpleAttributeSet attribsB = new SimpleAttributeSet (); 264 SimpleAttributeSet attribsC = new SimpleAttributeSet (); 265 SimpleAttributeSet attribsD = new SimpleAttributeSet (); 266 attribsA.addAttribute("set-A", "value"); 267 attribsA.addAttribute("commonAttribute", "set-A-value"); 268 attribsB.addAttribute("set-B", "value"); 269 attribsB.addAttribute("commonAttribute", "set-B-value"); 270 attribsC.addAttribute("set-C", "value"); 271 attribsC.addAttribute("commonAttribute", "set-C-value"); 272 attribsD.addAttribute("set-D", "value"); 273 attribsD.addAttribute("commonAttribute", "set-D-value"); 274 275 bagA.addHighlight(10, 20, attribsA); 276 bagB.addHighlight(15, 25, attribsB); 277 bagC.addHighlight(50, 60, attribsC); 278 bagD.addHighlight(55, 65, attribsD); 279 280 SingletonLayerFactory layerA = new SingletonLayerFactory("layerA", ZOrder.DEFAULT_RACK, true, bagA); 281 SingletonLayerFactory layerB = new SingletonLayerFactory("layerB", ZOrder.DEFAULT_RACK.aboveLayers("layerA"), false, bagB); 282 SingletonLayerFactory layerC = new SingletonLayerFactory("layerC", ZOrder.DEFAULT_RACK.aboveLayers("layerA", "layerB"), true, bagC); 283 SingletonLayerFactory layerD = new SingletonLayerFactory("layerD", ZOrder.DEFAULT_RACK.aboveLayers("layerA", "layerB", "layerC"), true, bagD); 284 285 MemoryMimeDataProvider.reset(null); 286 MemoryMimeDataProvider.addInstances(mimeType, layerA, layerD); 287 288 JEditorPane pane = new JEditorPane (); 289 pane.setEditorKit(new SimpleKit(mimeType)); 290 assertEquals("The pane has got wrong mime type", mimeType, pane.getContentType()); 291 292 final HighlightingManager hm = HighlightingManager.getInstance(); 293 final HighlightsContainer variableHC = hm.getHighlights(pane, VARIABLE_SIZE_LAYERS); 294 final HighlightsContainer fixedHC = hm.getHighlights(pane, FIXED_SIZE_LAYERS); 295 296 assertNotNull("Can't get variable HighlightsContainer", variableHC); 297 assertNotNull("Can't get fixed HighlightsContainer", fixedHC); 298 299 { 300 HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE); 301 assertFalse("There should be no variable highlights", variable.moveNext()); 302 } 303 304 { 305 HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE); 306 assertTrue("Wrong number of highlights", fixed.moveNext()); 308 assertEquals("Wrong start offset", 10, fixed.getStartOffset()); 309 assertEquals("Wrong end offset", 20, fixed.getEndOffset()); 310 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-A", "commonAttribute"); 311 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-B", "set-C", "set-D"); 312 assertEquals("Wrong commonAttribute value", "set-A-value", fixed.getAttributes().getAttribute("commonAttribute")); 313 assertTrue("Wrong number of highlights", fixed.moveNext()); 315 assertEquals("Wrong start offset", 55, fixed.getStartOffset()); 316 assertEquals("Wrong end offset", 65, fixed.getEndOffset()); 317 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-D", "commonAttribute"); 318 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-C"); 319 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 320 } 321 322 MemoryMimeDataProvider.addInstances(mimeType, layerB); 324 325 { 326 HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE); 327 assertTrue("Wrong number of highlights", variable.moveNext()); 329 assertEquals("Wrong start offset", 10, variable.getStartOffset()); 330 assertEquals("Wrong end offset", 15, variable.getEndOffset()); 331 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "commonAttribute"); 332 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-B", "set-C", "set-D"); 333 assertEquals("Wrong commonAttribute value", "set-A-value", variable.getAttributes().getAttribute("commonAttribute")); 334 assertTrue("Wrong number of highlights", variable.moveNext()); 336 assertEquals("Wrong start offset", 15, variable.getStartOffset()); 337 assertEquals("Wrong end offset", 20, variable.getEndOffset()); 338 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "set-B", "commonAttribute"); 339 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D"); 340 assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute")); 341 assertTrue("Wrong number of highlights", variable.moveNext()); 343 assertEquals("Wrong start offset", 20, variable.getStartOffset()); 344 assertEquals("Wrong end offset", 25, variable.getEndOffset()); 345 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-B", "commonAttribute"); 346 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-A", "set-C", "set-D"); 347 assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute")); 348 } 349 350 { 351 HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE); 352 assertTrue("Wrong number of highlights", fixed.moveNext()); 354 assertEquals("Wrong start offset", 55, fixed.getStartOffset()); 355 assertEquals("Wrong end offset", 65, fixed.getEndOffset()); 356 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-D", "commonAttribute"); 357 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-C"); 358 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 359 } 360 361 MemoryMimeDataProvider.addInstances(mimeType, layerC); 363 364 { 365 HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE); 366 assertTrue("Wrong number of highlights", variable.moveNext()); 368 assertEquals("Wrong start offset", 10, variable.getStartOffset()); 369 assertEquals("Wrong end offset", 15, variable.getEndOffset()); 370 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "commonAttribute"); 371 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-B", "set-C", "set-D"); 372 assertEquals("Wrong commonAttribute value", "set-A-value", variable.getAttributes().getAttribute("commonAttribute")); 373 assertTrue("Wrong number of highlights", variable.moveNext()); 375 assertEquals("Wrong start offset", 15, variable.getStartOffset()); 376 assertEquals("Wrong end offset", 20, variable.getEndOffset()); 377 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "set-B", "commonAttribute"); 378 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D"); 379 assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute")); 380 assertTrue("Wrong number of highlights", variable.moveNext()); 382 assertEquals("Wrong start offset", 20, variable.getStartOffset()); 383 assertEquals("Wrong end offset", 25, variable.getEndOffset()); 384 assertAttribContains("Can't find attribute", variable.getAttributes(), "set-B", "commonAttribute"); 385 assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-A", "set-C", "set-D"); 386 assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute")); 387 } 388 389 { 390 HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE); 391 assertTrue("Wrong number of highlights", fixed.moveNext()); 393 assertEquals("Wrong start offset", 50, fixed.getStartOffset()); 394 assertEquals("Wrong end offset", 55, fixed.getEndOffset()); 395 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "commonAttribute"); 396 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-D"); 397 assertEquals("Wrong commonAttribute value", "set-C-value", fixed.getAttributes().getAttribute("commonAttribute")); 398 assertTrue("Wrong number of highlights", fixed.moveNext()); 400 assertEquals("Wrong start offset", 55, fixed.getStartOffset()); 401 assertEquals("Wrong end offset", 60, fixed.getEndOffset()); 402 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "set-D", "commonAttribute"); 403 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B"); 404 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 405 assertTrue("Wrong number of highlights", fixed.moveNext()); 407 assertEquals("Wrong start offset", 60, fixed.getStartOffset()); 408 assertEquals("Wrong end offset", 65, fixed.getEndOffset()); 409 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-D", "commonAttribute"); 410 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-C"); 411 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 412 } 413 414 MemoryMimeDataProvider.removeInstances(mimeType, layerB); 416 417 { 418 HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE); 419 assertFalse("There should be no variable highlights", variable.moveNext()); 420 } 421 422 { 423 HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE); 424 assertTrue("Wrong number of highlights", fixed.moveNext()); 426 assertEquals("Wrong start offset", 10, fixed.getStartOffset()); 427 assertEquals("Wrong end offset", 20, fixed.getEndOffset()); 428 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-A", "commonAttribute"); 429 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-B", "set-C", "set-D"); 430 assertEquals("Wrong commonAttribute value", "set-A-value", fixed.getAttributes().getAttribute("commonAttribute")); 431 assertTrue("Wrong number of highlights", fixed.moveNext()); 433 assertEquals("Wrong start offset", 50, fixed.getStartOffset()); 434 assertEquals("Wrong end offset", 55, fixed.getEndOffset()); 435 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "commonAttribute"); 436 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-D"); 437 assertEquals("Wrong commonAttribute value", "set-C-value", fixed.getAttributes().getAttribute("commonAttribute")); 438 assertTrue("Wrong number of highlights", fixed.moveNext()); 440 assertEquals("Wrong start offset", 55, fixed.getStartOffset()); 441 assertEquals("Wrong end offset", 60, fixed.getEndOffset()); 442 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "set-D", "commonAttribute"); 443 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B"); 444 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 445 assertTrue("Wrong number of highlights", fixed.moveNext()); 447 assertEquals("Wrong start offset", 60, fixed.getStartOffset()); 448 assertEquals("Wrong end offset", 65, fixed.getEndOffset()); 449 assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-D", "commonAttribute"); 450 assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-C"); 451 assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute")); 452 } 453 454 MemoryMimeDataProvider.removeInstances(mimeType, layerA, layerC, layerD); 456 457 { 458 HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE); 459 assertFalse("There should be no variable highlights", variable.moveNext()); 460 } 461 462 { 463 HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE); 464 assertFalse("There should be no fixed highlights", fixed.moveNext()); 465 } 466 } 467 468 470 public void testEventsWhenAddingRemovingLayers() { 471 OffsetsBag bagA = new OffsetsBag(new PlainDocument ()); 472 OffsetsBag bagB = new OffsetsBag(new PlainDocument ()); 473 474 SingletonLayerFactory layerA = new SingletonLayerFactory("layerA", ZOrder.DEFAULT_RACK, true, bagA); 475 SingletonLayerFactory layerB = new SingletonLayerFactory("layerB", ZOrder.DEFAULT_RACK, false, bagB); 476 477 MemoryMimeDataProvider.reset(null); 478 479 JEditorPane pane = new JEditorPane (); 480 pane.setContentType("text/plain"); 481 assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType()); 482 483 final HighlightingManager hm = HighlightingManager.getInstance(); 484 final HighlightsContainer hc = hm.getHighlights(pane, HighlightsLayerFilter.IDENTITY); 485 486 assertNotNull("Can't get fixed HighlightsContainer", hc); 487 488 490 { 491 HighlightsSequence fixed = hc.getHighlights(0, Integer.MAX_VALUE); 492 assertFalse("There should be no highlights", fixed.moveNext()); 493 } 494 495 Listener listener = new Listener(); 496 hc.addHighlightsChangeListener(listener); 497 498 listener.reset(); 500 MemoryMimeDataProvider.addInstances("text/plain", layerA); 501 502 assertEquals("Wrong number of events", 1, listener.eventsCnt); 503 assertNull("Wrong change start position", listener.lastStartPosition); 504 assertNull("Wrong change end position", listener.lastEndPosition); 505 assertEquals("Wrong change start offset", 0, listener.lastStartOffset); 506 assertEquals("Wrong change end offset", Integer.MAX_VALUE, listener.lastEndOffset); 507 508 { 509 HighlightsSequence fixed = hc.getHighlights(0, Integer.MAX_VALUE); 510 assertFalse("There should be no highlights", fixed.moveNext()); 511 } 512 513 listener.reset(); 515 MemoryMimeDataProvider.addInstances("text/plain", layerB); 516 517 assertEquals("Wrong number of events", 1, listener.eventsCnt); 518 assertNull("Wrong change start position", listener.lastStartPosition); 519 assertNull("Wrong change end position", listener.lastEndPosition); 520 assertEquals("Wrong change start offset", 0, listener.lastStartOffset); 521 assertEquals("Wrong change end offset", Integer.MAX_VALUE, listener.lastEndOffset); 522 523 { 524 HighlightsSequence fixed = hc.getHighlights(0, Integer.MAX_VALUE); 525 assertFalse("There should be no highlights", fixed.moveNext()); 526 } 527 528 listener.reset(); 530 MemoryMimeDataProvider.removeInstances("text/plain", layerA); 531 532 assertEquals("Wrong number of events", 1, listener.eventsCnt); 533 assertNull("Wrong change start position", listener.lastStartPosition); 534 assertNull("Wrong change end position", listener.lastEndPosition); 535 assertEquals("Wrong change start offset", 0, listener.lastStartOffset); 536 assertEquals("Wrong change end offset", Integer.MAX_VALUE, listener.lastEndOffset); 537 538 { 539 HighlightsSequence fixed = hc.getHighlights(0, Integer.MAX_VALUE); 540 assertFalse("There should be no highlights", fixed.moveNext()); 541 } 542 543 listener.reset(); 545 MemoryMimeDataProvider.removeInstances("text/plain", layerB); 546 547 assertEquals("Wrong number of events", 1, listener.eventsCnt); 548 assertNull("Wrong change start position", listener.lastStartPosition); 549 assertNull("Wrong change end position", listener.lastEndPosition); 550 assertEquals("Wrong change start offset", 0, listener.lastStartOffset); 551 assertEquals("Wrong change end offset", Integer.MAX_VALUE, listener.lastEndOffset); 552 553 { 554 HighlightsSequence fixed = hc.getHighlights(0, Integer.MAX_VALUE); 555 assertFalse("There should be no highlights", fixed.moveNext()); 556 } 557 } 558 559 561 public void testCaching() { 562 MemoryMimeDataProvider.reset(null); 563 HighlightingManager hm = HighlightingManager.getInstance(); 564 565 JEditorPane pane1 = new JEditorPane (); 566 pane1.setContentType("text/plain"); 567 assertEquals("The pane has got wrong mime type", "text/plain", pane1.getContentType()); 568 569 JEditorPane pane2 = new JEditorPane (); 570 pane2.setContentType("text/plain"); 571 assertEquals("The pane has got wrong mime type", "text/plain", pane2.getContentType()); 572 573 { 574 HighlightsContainer hc1_A = hm.getHighlights(pane1, HighlightsLayerFilter.IDENTITY); 575 HighlightsContainer hc1_B = hm.getHighlights(pane1, HighlightsLayerFilter.IDENTITY); 576 assertSame("HighlightsContainer is not cached", hc1_A, hc1_B); 577 578 HighlightsContainer hc2 = hm.getHighlights(pane2, HighlightsLayerFilter.IDENTITY); 579 assertNotSame("HighlightsContainer should not be shared between JEPs", hc1_A, hc2); 580 } 581 582 gc(); 583 584 { 585 int hc1_A_hash = System.identityHashCode(hm.getHighlights(pane1, HighlightsLayerFilter.IDENTITY)); 586 int hc1_B_hash = System.identityHashCode(hm.getHighlights(pane1, HighlightsLayerFilter.IDENTITY)); 587 assertEquals("HighlightsContainer is not cached (different hash codes)", hc1_A_hash, hc1_B_hash); 588 } 589 } 590 591 593 public void testCachedInstancesGCed() { 594 MemoryMimeDataProvider.reset(null); 595 HighlightingManager hm = HighlightingManager.getInstance(); 596 597 gc(); 598 assertEquals("The CACHE should be empty", 0, hm.CACHE.size()); 599 600 JEditorPane pane = new JEditorPane (); 601 pane.setContentType("text/plain"); 602 assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType()); 603 604 HighlightsContainer hc = hm.getHighlights(pane, HighlightsLayerFilter.IDENTITY); 605 assertNotNull("Can't get HighlightsContainer", hc); 606 607 assertEquals("Wrong number of highlights in the CACHE", 1, hm.CACHE.size()); 608 609 WeakReference <JEditorPane > refPane = new WeakReference <JEditorPane >(pane); 610 WeakReference <HighlightsContainer> refHc = new WeakReference <HighlightsContainer>(hc); 611 612 pane = null; 614 hc = null; 615 616 assertGC("JEP has not been GCed", refPane); 617 assertGC("HC has not been GCed", refHc); 618 assertEquals("The CACHE should be empty", 0, hm.CACHE.size()); 619 } 620 621 private void assertAttribContains(String msg, AttributeSet as, String ... keys) { 622 630 assertEquals(msg, keys.length, as.getAttributeCount()); 631 for (String key : keys) { 632 if (null == as.getAttribute(key)) { 633 fail(msg + " attribute key: " + key); 634 } 635 } 636 } 637 638 private void assertAttribNotContains(String msg, AttributeSet as, String ... keys) { 639 647 for (String key : keys) { 648 if (null != as.getAttribute(key) || as.isDefined(key)) { 649 fail(msg + " attribute key: " + key); 650 } 651 } 652 } 653 654 private void gc() { 655 Random rand = new Random (System.currentTimeMillis()); 656 for(int i = 0; i < 5; i++) { 657 System.gc(); 658 try { 659 Thread.sleep(123 + rand.nextInt(1000)); 660 } catch (InterruptedException e) { 661 } 663 } 664 } 665 666 private void dumpLookupContents(String mimePath) { 667 Lookup lookup = MimeLookup.getLookup(MimePath.parse(mimePath)); 668 Lookup.Result<Object > result = lookup.lookupResult(Object .class); 669 Collection <? extends Lookup.Item<Object >> items = result.allItems(); 670 671 System.out.println("Lookup for " + mimePath + " : {"); 672 for(Lookup.Item<Object > item : items) { 673 System.out.println(" " + item.getDisplayName()); 674 } 675 System.out.println("} end of Lookup for " + mimePath + " ----"); 676 } 677 678 private static final class SingletonLayerFactory implements HighlightsLayerFactory 679 { 680 private String id; 681 private ZOrder zOrder; 682 private boolean fixed; 683 private HighlightsContainer container; 684 685 public SingletonLayerFactory(String id, ZOrder zOrder, boolean fixed, HighlightsContainer hc) { 686 this.id = id; 687 this.zOrder = zOrder; 688 this.fixed = fixed; 689 this.container = hc; 690 } 691 692 public HighlightsLayer[] createLayers(HighlightsLayerFactory.Context context) { 693 return new HighlightsLayer [] { HighlightsLayer.create(id, zOrder, fixed, container) }; 694 } 695 696 public String toString() { 697 return super.toString() + "; id = " + id; 698 } 699 700 } 702 private static final class SimplePosition implements Position { 703 private int offset; 704 705 public SimplePosition(int offset) { 706 this.offset = offset; 707 } 708 709 public int getOffset() { 710 return offset; 711 } 712 } 714 private static final class SimpleKit extends DefaultEditorKit { 715 private String mimeType; 716 717 public SimpleKit(String mimeType) { 718 this.mimeType = mimeType; 719 } 720 721 public String getContentType() { 722 return mimeType; 723 } 724 } 726 private static final class Listener implements HighlightsChangeListener { 727 728 public int eventsCnt = 0; 729 public int lastStartOffset; 730 public int lastEndOffset; 731 public Position lastStartPosition; 732 public Position lastEndPosition; 733 734 public void highlightChanged(HighlightsChangeEvent event) { 735 eventsCnt++; 736 lastStartOffset = event.getStartOffset(); 737 lastEndOffset = event.getEndOffset(); 738 } 739 740 public void reset() { 741 eventsCnt = 0; 742 lastStartOffset = -1; 743 lastEndOffset = -1; 744 } 745 } 747 private static final HighlightsLayerFilter FIXED_SIZE_LAYERS = new HighlightsLayerFilter() { 748 public List <? extends HighlightsLayer> filterLayers(List <? extends HighlightsLayer> layers) { 749 ArrayList <HighlightsLayer> filteredLayers = new ArrayList <HighlightsLayer>(); 750 751 for(int i = layers.size() - 1; i >= 0; i--) { 752 HighlightsLayer layer = layers.get(i); 753 HighlightsLayerAccessor layerAccessor = 754 HighlightingSpiPackageAccessor.get().getHighlightsLayerAccessor(layer); 755 756 if (!layerAccessor.isFixedSize()) { 757 break; 758 } 759 760 filteredLayers.add(0, layer); 761 } 762 763 return filteredLayers; 764 } 765 }; 766 767 private static final HighlightsLayerFilter VARIABLE_SIZE_LAYERS = new HighlightsLayerFilter() { 768 public List <? extends HighlightsLayer> filterLayers(List <? extends HighlightsLayer> layers) { 769 ArrayList <HighlightsLayer> filteredLayers = new ArrayList <HighlightsLayer>(); 770 boolean fixedSize = true; 771 772 for(int i = layers.size() - 1; i >= 0; i--) { 773 HighlightsLayer layer = layers.get(i); 774 HighlightsLayerAccessor layerAccessor = 775 HighlightingSpiPackageAccessor.get().getHighlightsLayerAccessor(layer); 776 777 if (!layerAccessor.isFixedSize()) { 778 fixedSize = false; 779 } 780 781 if (!fixedSize) { 782 filteredLayers.add(0, layer); 783 } 784 } 785 786 return filteredLayers; 787 } 788 }; 789 } 790 | Popular Tags |