KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > highlighting > support > MergingPositionsBagTest


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.spi.editor.highlighting.support;
21
22 import java.util.Enumeration JavaDoc;
23 import javax.swing.text.AttributeSet JavaDoc;
24 import javax.swing.text.DefaultStyledDocument JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.Position JavaDoc;
27 import javax.swing.text.SimpleAttributeSet JavaDoc;
28 import junit.framework.AssertionFailedError;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.lib.editor.util.GapList;
31 import org.netbeans.spi.editor.highlighting.*;
32 import org.netbeans.spi.editor.highlighting.performance.SimplePosition;
33
34 /**
35  *
36  * @author vita
37  */

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