KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > highlighting > HighlightingManagerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2.highlighting;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Random JavaDoc;
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.text.AttributeSet JavaDoc;
29 import javax.swing.text.DefaultEditorKit JavaDoc;
30 import javax.swing.text.PlainDocument JavaDoc;
31 import javax.swing.text.Position JavaDoc;
32 import javax.swing.text.SimpleAttributeSet JavaDoc;
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 /**
47  *
48  * @author vita
49  */

50 public class HighlightingManagerTest extends NbTestCase {
51     
52     /** Creates a new instance of HighlightingManagerTest */
53     public HighlightingManagerTest(String JavaDoc 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 JavaDoc pane = new JEditorPane JavaDoc();
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 JavaDoc());
69         
70         MemoryMimeDataProvider.reset(null);
71         MemoryMimeDataProvider.addInstances(
72             "text/plain", new SingletonLayerFactory("layer", ZOrder.DEFAULT_RACK, true, bag));
73
74         JEditorPane JavaDoc pane = new JEditorPane JavaDoc();
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 JavaDoc attributes = new SimpleAttributeSet JavaDoc();
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     // test multiple layers, merging, ordering
96

97     public void testMultipleLayers() {
98         OffsetsBag bagA = new OffsetsBag(new PlainDocument JavaDoc());
99         OffsetsBag bagB = new OffsetsBag(new PlainDocument JavaDoc());
100         OffsetsBag bagC = new OffsetsBag(new PlainDocument JavaDoc());
101         OffsetsBag bagD = new OffsetsBag(new PlainDocument JavaDoc());
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 JavaDoc pane = new JEditorPane JavaDoc();
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 JavaDoc attribsA = new SimpleAttributeSet JavaDoc();
125         SimpleAttributeSet JavaDoc attribsB = new SimpleAttributeSet JavaDoc();
126         SimpleAttributeSet JavaDoc attribsC = new SimpleAttributeSet JavaDoc();
127         SimpleAttributeSet JavaDoc attribsD = new SimpleAttributeSet JavaDoc();
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         // Check fixed-size leyers sequence - should be C, D
144
HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE);
145         // Check 1. highlight
146
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         // Check 2. highlight
153
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         // Check 3. highlight
160
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         // Check variable-size leyers sequence - should be A, B
169
HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE);
170         // Check 1. highlight
171
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         // Check 2. highlight
178
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         // Check 3. highlight
185
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     // test events fired from HCs when changing highlights on a layer
194

195     public void testChangesInLayerFireEvents() {
196         OffsetsBag bagA = new OffsetsBag(new PlainDocument JavaDoc());
197         OffsetsBag bagB = new OffsetsBag(new PlainDocument JavaDoc());
198         OffsetsBag bagC = new OffsetsBag(new PlainDocument JavaDoc());
199         OffsetsBag bagD = new OffsetsBag(new PlainDocument JavaDoc());
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 JavaDoc pane = new JEditorPane JavaDoc();
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         // Test the variable-size layers - A,B
216
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         // Test the fixed-size layers
234
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     // test adding/removing a layer
253

254     public void testAddingRemovingLayers() {
255         final String JavaDoc mimeType = "text/plain";
256         
257         OffsetsBag bagA = new OffsetsBag(new PlainDocument JavaDoc());
258         OffsetsBag bagB = new OffsetsBag(new PlainDocument JavaDoc());
259         OffsetsBag bagC = new OffsetsBag(new PlainDocument JavaDoc());
260         OffsetsBag bagD = new OffsetsBag(new PlainDocument JavaDoc());
261
262         SimpleAttributeSet JavaDoc attribsA = new SimpleAttributeSet JavaDoc();
263         SimpleAttributeSet JavaDoc attribsB = new SimpleAttributeSet JavaDoc();
264         SimpleAttributeSet JavaDoc attribsC = new SimpleAttributeSet JavaDoc();
265         SimpleAttributeSet JavaDoc attribsD = new SimpleAttributeSet JavaDoc();
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 JavaDoc pane = new JEditorPane JavaDoc();
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             // Check 1. highlight
307
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             // Check 2. highlight
314
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         // Add layer B - that should put A, B in variableHC and leave D in fixedHC
323
MemoryMimeDataProvider.addInstances(mimeType, layerB);
324         
325         {
326             HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE);
327             // Check 1. highlight
328
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             // Check 2. highlight
335
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             // Check 3. highlight
342
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             // Check 1. highlight
353
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         // Add layer C - that should leave A, B in variableHC, should also leave D in fixedHC and add C in fixedHC
362
MemoryMimeDataProvider.addInstances(mimeType, layerC);
363
364         {
365             HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE);
366             // Check 1. highlight
367
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             // Check 2. highlight
374
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             // Check 3. highlight
381
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             // Check 1. highlight
392
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             // Check 2. highlight
399
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             // Check 3. highlight
406
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         // Remove layer B - that should put A in fixedHC, should also leave C, D in fixedHC
415
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             // Check 1. highlight
425
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             // Check 2. highlight
432
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             // Check 3. highlight
439
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             // Check 4. highlight
446
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         // Remove all remaining layers - that should remove all highlighs
455
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     // test events fired from HCs when adding/removing a layer
469

470     public void testEventsWhenAddingRemovingLayers() {
471         OffsetsBag bagA = new OffsetsBag(new PlainDocument JavaDoc());
472         OffsetsBag bagB = new OffsetsBag(new PlainDocument JavaDoc());
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 JavaDoc pane = new JEditorPane JavaDoc();
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         // There should be no layers and no highlights
489

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         // Add layer A - it's a fixed-size layer
499
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         // Add layer B - it's a variable-size layer
514
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         // Remove layer A - it's a fixed-size layer
529
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         // Remove layer B - it's a variable-size layer
544
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     // test getting independent HCs for different JEditorPanes with the same mime type
560

561     public void testCaching() {
562         MemoryMimeDataProvider.reset(null);
563         HighlightingManager hm = HighlightingManager.getInstance();
564
565         JEditorPane JavaDoc pane1 = new JEditorPane JavaDoc();
566         pane1.setContentType("text/plain");
567         assertEquals("The pane has got wrong mime type", "text/plain", pane1.getContentType());
568         
569         JEditorPane JavaDoc pane2 = new JEditorPane JavaDoc();
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     // test that bags and everything is GCed when the JEditorPane, which they were created for, is gone
592

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 JavaDoc pane = new JEditorPane JavaDoc();
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 JavaDoc<JEditorPane JavaDoc> refPane = new WeakReference JavaDoc<JEditorPane JavaDoc>(pane);
610         WeakReference JavaDoc<HighlightsContainer> refHc = new WeakReference JavaDoc<HighlightsContainer>(hc);
611         
612         // reset hard references
613
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 JavaDoc msg, AttributeSet JavaDoc as, String JavaDoc... keys) {
622 // System.out.print("assertAttribContains: attributes: ");
623
// for(Enumeration<?> attribKeys = as.getAttributeNames(); attribKeys.hasMoreElements(); ) {
624
// Object key = attribKeys.nextElement();
625
// Object value = as.getAttribute(key);
626
// System.out.print("'" + key + "' = '" + value + "', ");
627
// }
628
// System.out.println();
629

630         assertEquals(msg, keys.length, as.getAttributeCount());
631         for (String JavaDoc key : keys) {
632             if (null == as.getAttribute(key)) {
633                 fail(msg + " attribute key: " + key);
634             }
635         }
636     }
637
638     private void assertAttribNotContains(String JavaDoc msg, AttributeSet JavaDoc as, String JavaDoc... keys) {
639 // System.out.print("assertAttribNotContains: attributes: ");
640
// for(Enumeration<?> attribKeys = as.getAttributeNames(); attribKeys.hasMoreElements(); ) {
641
// Object key = attribKeys.nextElement();
642
// Object value = as.getAttribute(key);
643
// System.out.print("'" + key + "' = '" + value + "', ");
644
// }
645
// System.out.println();
646

647         for (String JavaDoc 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 JavaDoc rand = new Random JavaDoc(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 JavaDoc e) {
661                 // ignore
662
}
663         }
664     }
665     
666     private void dumpLookupContents(String JavaDoc mimePath) {
667         Lookup lookup = MimeLookup.getLookup(MimePath.parse(mimePath));
668         Lookup.Result<Object JavaDoc> result = lookup.lookupResult(Object JavaDoc.class);
669         Collection JavaDoc<? extends Lookup.Item<Object JavaDoc>> items = result.allItems();
670         
671         System.out.println("Lookup for " + mimePath + " : {");
672         for(Lookup.Item<Object JavaDoc> 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 JavaDoc id;
681         private ZOrder zOrder;
682         private boolean fixed;
683         private HighlightsContainer container;
684         
685         public SingletonLayerFactory(String JavaDoc 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 JavaDoc toString() {
697             return super.toString() + "; id = " + id;
698         }
699         
700     } // End of HLFactory
701

702     private static final class SimplePosition implements Position JavaDoc {
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     } // End of SimplePosition class
713

714     private static final class SimpleKit extends DefaultEditorKit JavaDoc {
715         private String JavaDoc mimeType;
716         
717         public SimpleKit(String JavaDoc mimeType) {
718             this.mimeType = mimeType;
719         }
720
721         public String JavaDoc getContentType() {
722             return mimeType;
723         }
724     } // End of SimpleKit class
725

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 JavaDoc lastStartPosition;
732         public Position JavaDoc 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     } // End of Listener class
746

747     private static final HighlightsLayerFilter FIXED_SIZE_LAYERS = new HighlightsLayerFilter() {
748         public List JavaDoc<? extends HighlightsLayer> filterLayers(List JavaDoc<? extends HighlightsLayer> layers) {
749             ArrayList JavaDoc<HighlightsLayer> filteredLayers = new ArrayList JavaDoc<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 JavaDoc<? extends HighlightsLayer> filterLayers(List JavaDoc<? extends HighlightsLayer> layers) {
769             ArrayList JavaDoc<HighlightsLayer> filteredLayers = new ArrayList JavaDoc<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