001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.editor.highlighting.support;
043:
044: import javax.swing.text.AttributeSet;
045: import javax.swing.text.BadLocationException;
046: import javax.swing.text.DefaultStyledDocument;
047: import javax.swing.text.Document;
048: import javax.swing.text.PlainDocument;
049: import javax.swing.text.SimpleAttributeSet;
050: import org.netbeans.junit.NbTestCase;
051: import org.netbeans.modules.editor.lib2.highlighting.OffsetGapList;
052: import org.netbeans.spi.editor.highlighting.*;
053:
054: /**
055: *
056: * @author vita
057: */
058: public class OffsetsBagTest extends NbTestCase {
059:
060: private static final AttributeSet EMPTY = SimpleAttributeSet.EMPTY;
061:
062: private Document doc = new DefaultStyledDocument();
063:
064: /** Creates a new instance of HighlightSequenceTest */
065: public OffsetsBagTest(String name) {
066: super (name);
067: }
068:
069: public void testSimple() {
070: OffsetsBag hs = new OffsetsBag(doc);
071: assertEquals("Sequence should be empty", 0, hs.getMarks()
072: .size());
073:
074: hs.addHighlight(10, 20, EMPTY);
075: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
076: assertEquals("Sequence should not be empty", 2, marks.size());
077: assertEquals("Wrong highlight's start offset", 10, marks.get(0)
078: .getOffset());
079: assertEquals("Wrong highlight's end offset", 20, marks.get(1)
080: .getOffset());
081:
082: hs.clear();
083: assertEquals("Sequence was not cleared", 0, hs.getMarks()
084: .size());
085: }
086:
087: public void testRemoveBeyondEnd() throws Exception {
088: Document doc = new PlainDocument();
089: doc.insertString(0, "0123456789", null);
090: OffsetsBag hs = new OffsetsBag(doc);
091: hs.addHighlight(0, 5, EMPTY);
092: hs.addHighlight(6, 8, EMPTY);
093: assertEquals("Sequence size", 4, hs.getMarks().size());
094: hs.removeHighlights(0, 11, false);
095: hs.clear();
096: assertEquals("Sequence was not cleared", 0, hs.getMarks()
097: .size());
098: }
099:
100: public void testAddLeftOverlap() {
101: OffsetsBag hs = new OffsetsBag(doc);
102: SimpleAttributeSet attribsA = new SimpleAttributeSet();
103: SimpleAttributeSet attribsB = new SimpleAttributeSet();
104:
105: attribsA.addAttribute("set-name", "attribsA");
106: attribsB.addAttribute("set-name", "attribsB");
107:
108: hs.addHighlight(10, 20, attribsA);
109: hs.addHighlight(5, 15, attribsB);
110: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
111:
112: assertEquals("Wrong number of highlights", 3, marks.size());
113: assertEquals("1. highlight - wrong start offset", 5, marks.get(
114: 0).getOffset());
115: assertEquals("1. highlight - wrong end offset", 15, marks
116: .get(1).getOffset());
117: assertEquals("1. highlight - wrong attribs", "attribsB", marks
118: .get(0).getAttributes().getAttribute("set-name"));
119:
120: assertEquals("2. highlight - wrong start offset", 15, marks
121: .get(1).getOffset());
122: assertEquals("2. highlight - wrong end offset", 20, marks
123: .get(2).getOffset());
124: assertEquals("2. highlight - wrong attribs", "attribsA", marks
125: .get(1).getAttributes().getAttribute("set-name"));
126: assertNull(" 2. highlight - wrong end", marks.get(2)
127: .getAttributes());
128: }
129:
130: public void testAddRightOverlap() {
131: OffsetsBag hs = new OffsetsBag(doc);
132: SimpleAttributeSet attribsA = new SimpleAttributeSet();
133: SimpleAttributeSet attribsB = new SimpleAttributeSet();
134:
135: attribsA.addAttribute("set-name", "attribsA");
136: attribsB.addAttribute("set-name", "attribsB");
137:
138: hs.addHighlight(10, 20, attribsA);
139: hs.addHighlight(15, 25, attribsB);
140: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
141:
142: assertEquals("Wrong number of highlights", 3, marks.size());
143: assertEquals("1. highlight - wrong start offset", 10, marks
144: .get(0).getOffset());
145: assertEquals("1. highlight - wrong end offset", 15, marks
146: .get(1).getOffset());
147: assertEquals("1. highlight - wrong attribs", "attribsA", marks
148: .get(0).getAttributes().getAttribute("set-name"));
149:
150: assertEquals("2. highlight - wrong start offset", 15, marks
151: .get(1).getOffset());
152: assertEquals("2. highlight - wrong end offset", 25, marks
153: .get(2).getOffset());
154: assertEquals("2. highlight - wrong attribs", "attribsB", marks
155: .get(1).getAttributes().getAttribute("set-name"));
156: assertNull("2. highlight - wrong end", marks.get(2)
157: .getAttributes());
158: }
159:
160: public void testAddCompleteOverlap() {
161: OffsetsBag hs = new OffsetsBag(doc);
162: SimpleAttributeSet attribsA = new SimpleAttributeSet();
163: SimpleAttributeSet attribsB = new SimpleAttributeSet();
164:
165: attribsA.addAttribute("set-name", "attribsA");
166: attribsB.addAttribute("set-name", "attribsB");
167:
168: hs.addHighlight(10, 20, attribsA);
169: hs.addHighlight(5, 25, attribsB);
170: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
171:
172: assertEquals("Wrong number of highlights", 2, marks.size());
173: assertEquals("1. highlight - wrong start offset", 5, marks.get(
174: 0).getOffset());
175: assertEquals("1. highlight - wrong end offset", 25, marks
176: .get(1).getOffset());
177: assertEquals("1. highlight - wrong attribs", "attribsB", marks
178: .get(0).getAttributes().getAttribute("set-name"));
179: assertNull(" 1. highlight - wrong end", marks.get(1)
180: .getAttributes());
181: }
182:
183: public void testAddSplit() {
184: OffsetsBag hs = new OffsetsBag(doc);
185: SimpleAttributeSet attribsA = new SimpleAttributeSet();
186: SimpleAttributeSet attribsB = new SimpleAttributeSet();
187:
188: attribsA.addAttribute("set-name", "attribsA");
189: attribsB.addAttribute("set-name", "attribsB");
190:
191: hs.addHighlight(10, 25, attribsA);
192: hs.addHighlight(15, 20, attribsB);
193: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
194:
195: assertEquals("Wrong number of highlights", 4, marks.size());
196: assertEquals("1. highlight - wrong start offset", 10, marks
197: .get(0).getOffset());
198: assertEquals("1. highlight - wrong end offset", 15, marks
199: .get(1).getOffset());
200: assertEquals("1. highlight - wrong attribs", "attribsA", marks
201: .get(0).getAttributes().getAttribute("set-name"));
202:
203: assertEquals("2. highlight - wrong start offset", 15, marks
204: .get(1).getOffset());
205: assertEquals("2. highlight - wrong end offset", 20, marks
206: .get(2).getOffset());
207: assertEquals("2. highlight - wrong attribs", "attribsB", marks
208: .get(1).getAttributes().getAttribute("set-name"));
209:
210: assertEquals("3. highlight - wrong start offset", 20, marks
211: .get(2).getOffset());
212: assertEquals("3. highlight - wrong end offset", 25, marks
213: .get(3).getOffset());
214: assertEquals("3. highlight - wrong attribs", "attribsA", marks
215: .get(2).getAttributes().getAttribute("set-name"));
216: assertNull(" 3. highlight - wrong end", marks.get(3)
217: .getAttributes());
218: }
219:
220: public void testAddAligned() {
221: OffsetsBag hs = new OffsetsBag(doc);
222: SimpleAttributeSet attribsA = new SimpleAttributeSet();
223: SimpleAttributeSet attribsB = new SimpleAttributeSet();
224:
225: attribsA.addAttribute("set-name", "attribsA");
226: attribsB.addAttribute("set-name", "attribsB");
227:
228: hs.addHighlight(10, 20, attribsA);
229: hs.addHighlight(20, 30, attribsB);
230: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
231:
232: assertEquals("Wrong number of highlights", 3, marks.size());
233: assertEquals("1. highlight - wrong start offset", 10, marks
234: .get(0).getOffset());
235: assertEquals("1. highlight - wrong end offset", 20, marks
236: .get(1).getOffset());
237: assertEquals("1. highlight - wrong attribs", "attribsA", marks
238: .get(0).getAttributes().getAttribute("set-name"));
239:
240: assertEquals("2. highlight - wrong start offset", 20, marks
241: .get(1).getOffset());
242: assertEquals("2. highlight - wrong end offset", 30, marks
243: .get(2).getOffset());
244: assertEquals("2. highlight - wrong attribs", "attribsB", marks
245: .get(1).getAttributes().getAttribute("set-name"));
246: assertNull(" 2. highlight - wrong end", marks.get(2)
247: .getAttributes());
248:
249: hs.addHighlight(0, 10, attribsB);
250: assertEquals("Wrong number of highlights", 4, marks.size());
251: assertEquals("1. highlight - wrong start offset", 0, marks.get(
252: 0).getOffset());
253: assertEquals("1. highlight - wrong end offset", 10, marks
254: .get(1).getOffset());
255: assertEquals("1. highlight - wrong attribs", "attribsB", marks
256: .get(0).getAttributes().getAttribute("set-name"));
257:
258: assertEquals("2. highlight - wrong start offset", 10, marks
259: .get(1).getOffset());
260: assertEquals("2. highlight - wrong end offset", 20, marks
261: .get(2).getOffset());
262: assertEquals("2. highlight - wrong attribs", "attribsA", marks
263: .get(1).getAttributes().getAttribute("set-name"));
264:
265: assertEquals("3. highlight - wrong start offset", 20, marks
266: .get(2).getOffset());
267: assertEquals("3. highlight - wrong end offset", 30, marks
268: .get(3).getOffset());
269: assertEquals("3. highlight - wrong attribs", "attribsB", marks
270: .get(2).getAttributes().getAttribute("set-name"));
271: assertNull(" 3. highlight - wrong end", marks.get(3)
272: .getAttributes());
273: }
274:
275: public void testAddAligned2() {
276: OffsetsBag hs = new OffsetsBag(doc);
277: SimpleAttributeSet attribsA = new SimpleAttributeSet();
278: SimpleAttributeSet attribsB = new SimpleAttributeSet();
279:
280: attribsA.addAttribute("set-name", "attribsA");
281: attribsB.addAttribute("set-name", "attribsB");
282:
283: hs.addHighlight(10, 40, attribsA);
284: hs.addHighlight(10, 20, attribsB);
285: hs.addHighlight(30, 40, attribsB);
286: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
287:
288: assertEquals("Wrong number of highlights", 4, marks.size());
289: assertEquals("1. highlight - wrong start offset", 10, marks
290: .get(0).getOffset());
291: assertEquals("1. highlight - wrong end offset", 20, marks
292: .get(1).getOffset());
293: assertEquals("1. highlight - wrong attribs", "attribsB", marks
294: .get(0).getAttributes().getAttribute("set-name"));
295:
296: assertEquals("2. highlight - wrong start offset", 20, marks
297: .get(1).getOffset());
298: assertEquals("2. highlight - wrong end offset", 30, marks
299: .get(2).getOffset());
300: assertEquals("2. highlight - wrong attribs", "attribsA", marks
301: .get(1).getAttributes().getAttribute("set-name"));
302:
303: assertEquals("3. highlight - wrong start offset", 30, marks
304: .get(2).getOffset());
305: assertEquals("3. highlight - wrong end offset", 40, marks
306: .get(3).getOffset());
307: assertEquals("3. highlight - wrong attribs", "attribsB", marks
308: .get(2).getAttributes().getAttribute("set-name"));
309: assertNull(" 3. highlight - wrong end", marks.get(3)
310: .getAttributes());
311: }
312:
313: public void testAddMiddle() {
314: for (int i = 0; i < 10; i++) {
315: addMiddle(i + 1);
316: }
317: }
318:
319: private void addMiddle(int middleMarks) {
320: OffsetsBag hs = new OffsetsBag(doc);
321: SimpleAttributeSet attribsA = new SimpleAttributeSet();
322: SimpleAttributeSet attribsB = new SimpleAttributeSet();
323: SimpleAttributeSet attribsC = new SimpleAttributeSet();
324:
325: attribsA.addAttribute("set-name", "attribsA");
326: attribsB.addAttribute("set-name", "attribsB");
327: attribsC.addAttribute("set-name", "attribsC");
328:
329: for (int i = 0; i < middleMarks + 1; i++) {
330: hs.addHighlight(10 * i + 10, 10 * i + 20,
331: i % 2 == 0 ? attribsA : attribsB);
332: }
333:
334: hs.addHighlight(15, middleMarks * 10 + 15, attribsC);
335: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
336:
337: assertEquals("Wrong number of highlights (middleMarks = "
338: + middleMarks + ")", 4, marks.size());
339: assertEquals(
340: "1. highlight - wrong start offset (middleMarks = "
341: + middleMarks + ")", 10, marks.get(0)
342: .getOffset());
343: assertEquals("1. highlight - wrong end offset (middleMarks = "
344: + middleMarks + ")", 15, marks.get(1).getOffset());
345: assertEquals("1. highlight - wrong attribs (middleMarks = "
346: + middleMarks + ")", "attribsA", marks.get(0)
347: .getAttributes().getAttribute("set-name"));
348:
349: assertEquals(
350: "2. highlight - wrong start offset (middleMarks = "
351: + middleMarks + ")", 15, marks.get(1)
352: .getOffset());
353: assertEquals("2. highlight - wrong end offset (middleMarks = "
354: + middleMarks + ")", middleMarks * 10 + 15, marks
355: .get(2).getOffset());
356: assertEquals("2. highlight - wrong attribs (middleMarks = "
357: + middleMarks + ")", "attribsC", marks.get(1)
358: .getAttributes().getAttribute("set-name"));
359:
360: assertEquals(
361: "3. highlight - wrong start offset (middleMarks = "
362: + middleMarks + ")", middleMarks * 10 + 15,
363: marks.get(2).getOffset());
364: assertEquals("3. highlight - wrong end offset (middleMarks = "
365: + middleMarks + ")", (middleMarks + 2) * 10, marks.get(
366: 3).getOffset());
367: assertEquals("3. highlight - wrong attribs (middleMarks = "
368: + middleMarks + ")", middleMarks % 2 == 0 ? "attribsA"
369: : "attribsB", marks.get(2).getAttributes()
370: .getAttribute("set-name"));
371: assertNull(" 3. highlight - wrong end (middleMarks = "
372: + middleMarks + ")", marks.get(3).getAttributes());
373: }
374:
375: public void testRemoveLeftOverlap() {
376: OffsetsBag hs = new OffsetsBag(doc);
377: SimpleAttributeSet attribsA = new SimpleAttributeSet();
378:
379: attribsA.addAttribute("set-name", "attribsA");
380:
381: hs.addHighlight(10, 20, attribsA);
382: hs.removeHighlights(5, 15, false);
383: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
384:
385: assertEquals("Wrong number of highlights", 0, marks.size());
386: }
387:
388: public void testRemoveLeftOverlapClip() {
389: OffsetsBag hs = new OffsetsBag(doc);
390: SimpleAttributeSet attribsA = new SimpleAttributeSet();
391:
392: attribsA.addAttribute("set-name", "attribsA");
393:
394: hs.addHighlight(10, 20, attribsA);
395: hs.removeHighlights(5, 15, true);
396: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
397:
398: assertEquals("Wrong number of highlights", 2, marks.size());
399: assertEquals("1. highlight - wrong start offset", 15, marks
400: .get(0).getOffset());
401: assertEquals("1. highlight - wrong end offset", 20, marks
402: .get(1).getOffset());
403: assertEquals("1. highlight - wrong attribs", "attribsA", marks
404: .get(0).getAttributes().getAttribute("set-name"));
405: assertNull(" 1. highlight - wrong end", marks.get(1)
406: .getAttributes());
407: }
408:
409: public void testRemoveRightOverlap() {
410: OffsetsBag hs = new OffsetsBag(doc);
411: SimpleAttributeSet attribsA = new SimpleAttributeSet();
412:
413: attribsA.addAttribute("set-name", "attribsA");
414:
415: hs.addHighlight(10, 20, attribsA);
416: hs.removeHighlights(15, 25, false);
417: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
418:
419: assertEquals("Wrong number of highlights", 0, marks.size());
420: }
421:
422: public void testRemoveRightOverlapClip() {
423: OffsetsBag hs = new OffsetsBag(doc);
424: SimpleAttributeSet attribsA = new SimpleAttributeSet();
425:
426: attribsA.addAttribute("set-name", "attribsA");
427:
428: hs.addHighlight(10, 20, attribsA);
429: hs.removeHighlights(15, 25, true);
430: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
431:
432: assertEquals("Wrong number of highlights", 2, marks.size());
433: assertEquals("1. highlight - wrong start offset", 10, marks
434: .get(0).getOffset());
435: assertEquals("1. highlight - wrong end offset", 15, marks
436: .get(1).getOffset());
437: assertEquals("1. highlight - wrong attribs", "attribsA", marks
438: .get(0).getAttributes().getAttribute("set-name"));
439: assertNull(" 1. highlight - wrong end", marks.get(1)
440: .getAttributes());
441: }
442:
443: public void testRemoveCompleteOverlap() {
444: OffsetsBag hs = new OffsetsBag(doc);
445: SimpleAttributeSet attribsA = new SimpleAttributeSet();
446:
447: attribsA.addAttribute("set-name", "attribsA");
448:
449: hs.addHighlight(10, 20, attribsA);
450: hs.removeHighlights(5, 25, false);
451: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
452:
453: assertEquals("Wrong number of highlights", 0, marks.size());
454: }
455:
456: public void testRemoveCompleteOverlapClip() {
457: OffsetsBag hs = new OffsetsBag(doc);
458: SimpleAttributeSet attribsA = new SimpleAttributeSet();
459:
460: attribsA.addAttribute("set-name", "attribsA");
461:
462: hs.addHighlight(10, 20, attribsA);
463: hs.removeHighlights(5, 25, true);
464: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
465:
466: assertEquals("Wrong number of highlights", 0, marks.size());
467: }
468:
469: public void testRemoveSplit() {
470: OffsetsBag hs = new OffsetsBag(doc);
471: SimpleAttributeSet attribsA = new SimpleAttributeSet();
472:
473: attribsA.addAttribute("set-name", "attribsA");
474:
475: hs.addHighlight(10, 25, attribsA);
476: hs.removeHighlights(15, 20, false);
477: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
478:
479: assertEquals("Wrong number of highlights", 0, marks.size());
480: }
481:
482: public void testRemoveSplitClip() {
483: OffsetsBag hs = new OffsetsBag(doc);
484: SimpleAttributeSet attribsA = new SimpleAttributeSet();
485:
486: attribsA.addAttribute("set-name", "attribsA");
487:
488: hs.addHighlight(10, 25, attribsA);
489: hs.removeHighlights(15, 20, true);
490: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
491:
492: assertEquals("Wrong number of highlights", 4, marks.size());
493: assertEquals("1. highlight - wrong start offset", 10, marks
494: .get(0).getOffset());
495: assertEquals("1. highlight - wrong end offset", 15, marks
496: .get(1).getOffset());
497: assertEquals("1. highlight - wrong attribs", "attribsA", marks
498: .get(0).getAttributes().getAttribute("set-name"));
499: assertNull(" 1. highlight - wrong end", marks.get(1)
500: .getAttributes());
501:
502: assertEquals("2. highlight - wrong start offset", 20, marks
503: .get(2).getOffset());
504: assertEquals("2. highlight - wrong end offset", 25, marks
505: .get(3).getOffset());
506: assertEquals("2. highlight - wrong attribs", "attribsA", marks
507: .get(2).getAttributes().getAttribute("set-name"));
508: assertNull(" 2. highlight - wrong end", marks.get(3)
509: .getAttributes());
510: }
511:
512: public void testRemoveAlignedClip() {
513: OffsetsBag hs = new OffsetsBag(doc);
514: SimpleAttributeSet attribsA = new SimpleAttributeSet();
515:
516: attribsA.addAttribute("set-name", "attribsA");
517:
518: hs.addHighlight(10, 20, attribsA);
519: hs.removeHighlights(0, 10, true);
520: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
521:
522: assertEquals("Wrong number of highlights", 2, marks.size());
523: assertEquals("1. highlight - wrong start offset", 10, marks
524: .get(0).getOffset());
525: assertEquals("1. highlight - wrong end offset", 20, marks
526: .get(1).getOffset());
527: assertEquals("1. highlight - wrong attribs", "attribsA", marks
528: .get(0).getAttributes().getAttribute("set-name"));
529: assertNull(" 1. highlight - wrong end", marks.get(1)
530: .getAttributes());
531:
532: hs.removeHighlights(20, 30, true);
533: assertEquals("Wrong number of highlights", 2, marks.size());
534: assertEquals("1. highlight - wrong start offset", 10, marks
535: .get(0).getOffset());
536: assertEquals("1. highlight - wrong end offset", 20, marks
537: .get(1).getOffset());
538: assertEquals("1. highlight - wrong attribs", "attribsA", marks
539: .get(0).getAttributes().getAttribute("set-name"));
540: assertNull(" 1. highlight - wrong end", marks.get(1)
541: .getAttributes());
542: }
543:
544: public void testRemoveAligned2Clip() {
545: OffsetsBag hs = new OffsetsBag(doc);
546: SimpleAttributeSet attribsA = new SimpleAttributeSet();
547:
548: attribsA.addAttribute("set-name", "attribsA");
549:
550: hs.addHighlight(10, 40, attribsA);
551: hs.removeHighlights(10, 20, true);
552: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
553:
554: assertEquals("Wrong number of highlights", 2, marks.size());
555: assertEquals("1. highlight - wrong start offset", 20, marks
556: .get(0).getOffset());
557: assertEquals("1. highlight - wrong end offset", 40, marks
558: .get(1).getOffset());
559: assertEquals("1. highlight - wrong attribs", "attribsA", marks
560: .get(0).getAttributes().getAttribute("set-name"));
561: assertNull(" 1. highlight - wrong end", marks.get(1)
562: .getAttributes());
563:
564: hs.removeHighlights(30, 40, true);
565:
566: assertEquals("Wrong number of highlights", 2, marks.size());
567: assertEquals("1. highlight - wrong start offset", 20, marks
568: .get(0).getOffset());
569: assertEquals("1. highlight - wrong end offset", 30, marks
570: .get(1).getOffset());
571: assertEquals("1. highlight - wrong attribs", "attribsA", marks
572: .get(0).getAttributes().getAttribute("set-name"));
573: assertNull(" 1. highlight - wrong end", marks.get(1)
574: .getAttributes());
575: }
576:
577: public void testRemoveMiddle() {
578: OffsetsBag hs = new OffsetsBag(doc);
579: SimpleAttributeSet attribsA = new SimpleAttributeSet();
580: SimpleAttributeSet attribsB = new SimpleAttributeSet();
581:
582: attribsA.addAttribute("set-name", "attribsA");
583: attribsB.addAttribute("set-name", "attribsB");
584:
585: hs.addHighlight(10, 20, attribsA);
586: hs.addHighlight(20, 30, attribsB);
587: hs.removeHighlights(15, 25, false);
588: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
589:
590: assertEquals("Wrong number of highlights", 0, marks.size());
591: }
592:
593: public void testRemoveMiddleEmptyHighlight() {
594: OffsetsBag hs = new OffsetsBag(doc);
595: SimpleAttributeSet attribsA = new SimpleAttributeSet();
596:
597: attribsA.addAttribute("set-name", "attribsA");
598:
599: hs.addHighlight(10, 20, attribsA);
600: hs.removeHighlights(15, 15, false);
601: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
602:
603: assertEquals("Wrong number of highlights", 0, marks.size());
604: }
605:
606: public void testRemoveMiddleClip() {
607: for (int i = 0; i < 10; i++) {
608: removeMiddleClip(i + 1);
609: }
610: }
611:
612: private void removeMiddleClip(int middleMarks) {
613: OffsetsBag hs = new OffsetsBag(doc);
614: SimpleAttributeSet attribsA = new SimpleAttributeSet();
615: SimpleAttributeSet attribsB = new SimpleAttributeSet();
616:
617: attribsA.addAttribute("set-name", "attribsA");
618: attribsB.addAttribute("set-name", "attribsB");
619:
620: for (int i = 0; i < middleMarks + 1; i++) {
621: hs.addHighlight(10 * i + 10, 10 * i + 20,
622: i % 2 == 0 ? attribsA : attribsB);
623: }
624:
625: hs.removeHighlights(15, middleMarks * 10 + 15, true);
626: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
627:
628: assertEquals("Wrong number of highlights (middleMarks = "
629: + middleMarks + ")", 4, marks.size());
630: assertEquals(
631: "1. highlight - wrong start offset (middleMarks = "
632: + middleMarks + ")", 10, marks.get(0)
633: .getOffset());
634: assertEquals("1. highlight - wrong end offset (middleMarks = "
635: + middleMarks + ")", 15, marks.get(1).getOffset());
636: assertEquals("1. highlight - wrong attribs (middleMarks = "
637: + middleMarks + ")", "attribsA", marks.get(0)
638: .getAttributes().getAttribute("set-name"));
639: assertNull(" 1. highlight - wrong end (middleMarks = "
640: + middleMarks + ")", marks.get(1).getAttributes());
641:
642: assertEquals(
643: "2. highlight - wrong start offset (middleMarks = "
644: + middleMarks + ")", middleMarks * 10 + 15,
645: marks.get(2).getOffset());
646: assertEquals("2. highlight - wrong end offset (middleMarks = "
647: + middleMarks + ")", (middleMarks + 2) * 10, marks.get(
648: 3).getOffset());
649: assertEquals("2. highlight - wrong attribs (middleMarks = "
650: + middleMarks + ")", middleMarks % 2 == 0 ? "attribsA"
651: : "attribsB", marks.get(2).getAttributes()
652: .getAttribute("set-name"));
653: assertNull(" 2. highlight - wrong end (middleMarks = "
654: + middleMarks + ")", marks.get(3).getAttributes());
655: }
656:
657: public void testAddAll() {
658: OffsetsBag hsA = new OffsetsBag(doc);
659: OffsetsBag hsB = new OffsetsBag(doc);
660:
661: SimpleAttributeSet attribsA = new SimpleAttributeSet();
662: SimpleAttributeSet attribsB = new SimpleAttributeSet();
663: SimpleAttributeSet attribsC = new SimpleAttributeSet();
664:
665: attribsA.addAttribute("set-name", "attribsA");
666: attribsB.addAttribute("set-name", "attribsB");
667: attribsC.addAttribute("set-name", "attribsC");
668:
669: hsA.addHighlight(0, 30, attribsA);
670: hsA.addHighlight(10, 20, attribsB);
671: OffsetGapList<OffsetsBag.Mark> marksA = hsA.getMarks();
672:
673: hsB.addHighlight(0, 40, attribsC);
674: hsB.addAllHighlights(hsA.getHighlights(Integer.MIN_VALUE,
675: Integer.MAX_VALUE));
676: OffsetGapList<OffsetsBag.Mark> marksB = hsB.getMarks();
677:
678: assertEquals("Wrong number of highlights", marksA.size() + 1,
679: marksB.size());
680: for (int i = 0; i < marksA.size() - 1; i++) {
681: assertEquals(i + ". highlight - wrong start offset", marksA
682: .get(i).getOffset(), marksB.get(i).getOffset());
683: assertEquals(i + ". highlight - wrong end offset", marksA
684: .get(i + 1).getOffset(), marksB.get(i + 1)
685: .getOffset());
686: assertEquals(i + ". highlight - wrong attribs", marksA.get(
687: i).getAttributes().getAttribute("set-name"), marksB
688: .get(i).getAttributes().getAttribute("set-name"));
689: }
690:
691: assertEquals("4. highlight - wrong start offset", 30, marksB
692: .get(3).getOffset());
693: assertEquals("4. highlight - wrong end offset", 40, marksB.get(
694: 4).getOffset());
695: assertEquals("4. highlight - wrong attribs", "attribsC", marksB
696: .get(3).getAttributes().getAttribute("set-name"));
697: }
698:
699: public void testSet() {
700: OffsetsBag hsA = new OffsetsBag(doc);
701: OffsetsBag hsB = new OffsetsBag(doc);
702:
703: SimpleAttributeSet attribsA = new SimpleAttributeSet();
704: SimpleAttributeSet attribsB = new SimpleAttributeSet();
705: SimpleAttributeSet attribsC = new SimpleAttributeSet();
706:
707: attribsA.addAttribute("set-name", "attribsA");
708: attribsB.addAttribute("set-name", "attribsB");
709: attribsC.addAttribute("set-name", "attribsC");
710:
711: hsA.addHighlight(0, 30, attribsA);
712: hsA.addHighlight(10, 20, attribsB);
713: OffsetGapList<OffsetsBag.Mark> marksA = hsA.getMarks();
714:
715: hsB.addHighlight(0, 40, attribsC);
716: hsB.setHighlights(hsA.getHighlights(Integer.MIN_VALUE,
717: Integer.MAX_VALUE));
718: OffsetGapList<OffsetsBag.Mark> marksB = hsB.getMarks();
719:
720: assertEquals("Wrong number of highlights", marksA.size(),
721: marksB.size());
722: for (int i = 0; i < marksA.size(); i++) {
723: assertEquals(i + ". highlight - wrong start offset", marksA
724: .get(i).getOffset(), marksB.get(i).getOffset());
725: assertEquals(i + ". highlight - wrong end offset", marksA
726: .get(i).getOffset(), marksB.get(i).getOffset());
727:
728: AttributeSet attrA = marksA.get(i).getAttributes();
729: AttributeSet attrB = marksB.get(i).getAttributes();
730:
731: if (attrA != null && attrB != null) {
732: assertEquals(i + ". highlight - wrong attribs", attrA
733: .getAttribute("set-name"), attrB
734: .getAttribute("set-name"));
735: } else {
736: assertTrue(i + ". highlight - wrong attribs",
737: attrA == null && attrB == null);
738: }
739: }
740: }
741:
742: public void testGetHighlights() {
743: OffsetsBag hs = new OffsetsBag(doc);
744: assertFalse("Sequence should be empty", hs.getHighlights(
745: Integer.MIN_VALUE, Integer.MAX_VALUE).moveNext());
746:
747: hs.addHighlight(10, 30, EMPTY);
748:
749: {
750: // Do not clip the highlights
751: HighlightsSequence highlights = hs.getHighlights(20, 25);
752: assertTrue("Sequence should not be empty", highlights
753: .moveNext());
754: assertEquals("Wrong highlight's start offset", 20,
755: highlights.getStartOffset());
756: assertEquals("Wrong highlight's end offset", 25, highlights
757: .getEndOffset());
758: assertFalse("There should be no more highlights",
759: highlights.moveNext());
760: }
761:
762: hs.clear();
763: assertFalse("Sequence was not cleared", hs.getHighlights(
764: Integer.MIN_VALUE, Integer.MAX_VALUE).moveNext());
765: }
766:
767: public void testGetHighlights2() {
768: OffsetsBag hb = new OffsetsBag(doc);
769: hb.addHighlight(10, 20, SimpleAttributeSet.EMPTY);
770:
771: HighlightsSequence hs = hb.getHighlights(0, 5);
772: assertFalse("HighlightsSequence should be empty", hs.moveNext());
773:
774: hs = hb.getHighlights(25, 30);
775: assertFalse("HighlightsSequence should be empty", hs.moveNext());
776:
777: hs = hb.getHighlights(0, 15);
778: assertTrue("HighlightsSequence should not be empty", hs
779: .moveNext());
780: assertFalse("Too many highlights in the sequence", hs
781: .moveNext());
782:
783: hs = hb.getHighlights(12, 22);
784: assertTrue("HighlightsSequence should not be empty", hs
785: .moveNext());
786: assertFalse("Too many highlights in the sequence", hs
787: .moveNext());
788:
789: hs = hb.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
790: assertTrue("HighlightsSequence should not be empty", hs
791: .moveNext());
792: assertFalse("Too many highlights in the sequence", hs
793: .moveNext());
794: }
795:
796: public void testConcurrentModification() {
797: OffsetsBag hb = new OffsetsBag(doc);
798:
799: // Modify the bag
800: hb.addHighlight(5, 10, EMPTY);
801: hb.addHighlight(15, 20, EMPTY);
802: hb.addHighlight(25, 30, EMPTY);
803:
804: HighlightsSequence hs = hb.getHighlights(Integer.MIN_VALUE,
805: Integer.MAX_VALUE);
806: assertTrue("There should be some highlights", hs.moveNext());
807:
808: int s = hs.getStartOffset();
809: int e = hs.getEndOffset();
810: AttributeSet a = hs.getAttributes();
811:
812: // Modification after the sequence was acquired
813: hb.addHighlight(100, 110, EMPTY);
814:
815: assertEquals("Wrong highlight start", s, hs.getStartOffset());
816: assertEquals("Wrong highlight end", e, hs.getEndOffset());
817: assertEquals("Wrong highlight attributes", a, hs
818: .getAttributes());
819: assertFalse(
820: "There should be no more highlights after co-modification",
821: hs.moveNext());
822: }
823:
824: public void testDocumentChanges() throws BadLocationException {
825: Document doc = new PlainDocument();
826: doc.insertString(0,
827: "01234567890123456789012345678901234567890123456789",
828: SimpleAttributeSet.EMPTY);
829:
830: OffsetsBag bag = new OffsetsBag(doc);
831:
832: SimpleAttributeSet attribsA = new SimpleAttributeSet();
833: SimpleAttributeSet attribsB = new SimpleAttributeSet();
834:
835: attribsA.addAttribute("set-name", "attribsA");
836: attribsB.addAttribute("set-name", "attribsB");
837:
838: bag.addHighlight(0, 30, attribsA);
839: bag.addHighlight(10, 20, attribsB);
840: OffsetGapList<OffsetsBag.Mark> marks = bag.getMarks();
841:
842: assertEquals("Wrong number of highlights", 4, marks.size());
843: assertEquals("1. highlight - wrong start offset", 0, marks.get(
844: 0).getOffset());
845: assertEquals("1. highlight - wrong end offset", 10, marks
846: .get(1).getOffset());
847: assertEquals("1. highlight - wrong attribs", "attribsA", marks
848: .get(0).getAttributes().getAttribute("set-name"));
849:
850: assertEquals("2. highlight - wrong start offset", 10, marks
851: .get(1).getOffset());
852: assertEquals("2. highlight - wrong end offset", 20, marks
853: .get(2).getOffset());
854: assertEquals("2. highlight - wrong attribs", "attribsB", marks
855: .get(1).getAttributes().getAttribute("set-name"));
856:
857: assertEquals("3. highlight - wrong start offset", 20, marks
858: .get(2).getOffset());
859: assertEquals("3. highlight - wrong end offset", 30, marks
860: .get(3).getOffset());
861: assertEquals("3. highlight - wrong attribs", "attribsA", marks
862: .get(2).getAttributes().getAttribute("set-name"));
863: assertNull(" 3. highlight - wrong end", marks.get(3)
864: .getAttributes());
865:
866: doc.insertString(12, "----", SimpleAttributeSet.EMPTY);
867:
868: assertEquals("Wrong number of highlights", 4, marks.size());
869: assertEquals("1. highlight - wrong start offset", 0, marks.get(
870: 0).getOffset());
871: assertEquals("1. highlight - wrong end offset", 10, marks
872: .get(1).getOffset());
873: assertEquals("1. highlight - wrong attribs", "attribsA", marks
874: .get(0).getAttributes().getAttribute("set-name"));
875:
876: assertEquals("2. highlight - wrong start offset", 10, marks
877: .get(1).getOffset());
878: assertEquals("2. highlight - wrong end offset", 24, marks
879: .get(2).getOffset());
880: assertEquals("2. highlight - wrong attribs", "attribsB", marks
881: .get(1).getAttributes().getAttribute("set-name"));
882:
883: assertEquals("3. highlight - wrong start offset", 24, marks
884: .get(2).getOffset());
885: assertEquals("3. highlight - wrong end offset", 34, marks
886: .get(3).getOffset());
887: assertEquals("3. highlight - wrong attribs", "attribsA", marks
888: .get(2).getAttributes().getAttribute("set-name"));
889: assertNull(" 3. highlight - wrong end", marks.get(3)
890: .getAttributes());
891:
892: doc.remove(1, 5);
893:
894: assertEquals("Wrong number of highlights", 4, marks.size());
895: assertEquals("1. highlight - wrong start offset", 0, marks.get(
896: 0).getOffset());
897: assertEquals("1. highlight - wrong end offset", 5, marks.get(1)
898: .getOffset());
899: assertEquals("1. highlight - wrong attribs", "attribsA", marks
900: .get(0).getAttributes().getAttribute("set-name"));
901:
902: assertEquals("2. highlight - wrong start offset", 5, marks.get(
903: 1).getOffset());
904: assertEquals("2. highlight - wrong end offset", 19, marks
905: .get(2).getOffset());
906: assertEquals("2. highlight - wrong attribs", "attribsB", marks
907: .get(1).getAttributes().getAttribute("set-name"));
908:
909: assertEquals("3. highlight - wrong start offset", 19, marks
910: .get(2).getOffset());
911: assertEquals("3. highlight - wrong end offset", 29, marks
912: .get(3).getOffset());
913: assertEquals("3. highlight - wrong attribs", "attribsA", marks
914: .get(2).getAttributes().getAttribute("set-name"));
915: assertNull(" 3. highlight - wrong end", marks.get(3)
916: .getAttributes());
917: }
918:
919: private void dumpHighlights(HighlightsSequence seq) {
920: System.out.println("Dumping highlights from: " + seq + "{");
921: while (seq.moveNext()) {
922: System.out.println("<" + seq.getStartOffset() + ", "
923: + seq.getEndOffset() + ", " + seq.getAttributes()
924: + ">");
925: }
926: System.out.println("} --- End of Dumping highlights from: "
927: + seq + " ---------------------");
928: }
929: }
|