KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConcurrentModificationException JavaDoc;
23 import javax.swing.text.AttributeSet JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.DefaultStyledDocument JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.swing.text.PlainDocument JavaDoc;
28 import javax.swing.text.SimpleAttributeSet JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.modules.editor.lib2.highlighting.OffsetGapList;
31 import org.netbeans.spi.editor.highlighting.*;
32
33 /**
34  *
35  * @author vita
36  */

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