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 java.util.Enumeration;
045: import javax.swing.text.AttributeSet;
046: import javax.swing.text.DefaultStyledDocument;
047: import javax.swing.text.Document;
048: import javax.swing.text.Position;
049: import javax.swing.text.SimpleAttributeSet;
050: import junit.framework.AssertionFailedError;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.lib.editor.util.GapList;
053: import org.netbeans.spi.editor.highlighting.*;
054: import org.netbeans.spi.editor.highlighting.performance.SimplePosition;
055:
056: /**
057: *
058: * @author vita
059: */
060: public class MergingPositionsBagTest extends NbTestCase {
061:
062: private static final AttributeSet EMPTY = SimpleAttributeSet.EMPTY;
063:
064: private Document doc = new DefaultStyledDocument();
065:
066: /** Creates a new instance of HighlightSequenceTest */
067: public MergingPositionsBagTest(String name) {
068: super (name);
069: }
070:
071: public void testSimple() {
072: PositionsBag hs = new PositionsBag(doc, true);
073: assertEquals("Sequence should be empty", 0, hs.getMarks()
074: .size());
075:
076: hs.addHighlight(pos(10), pos(20), EMPTY);
077: GapList<Position> marks = hs.getMarks();
078: GapList<AttributeSet> attributes = hs.getAttributes();
079: assertEquals("Sequence should not be empty", 2, marks.size());
080: assertEquals("Wrong highlight's start offset", 10, marks.get(0)
081: .getOffset());
082: assertEquals("Wrong highlight's end offset", 20, marks.get(1)
083: .getOffset());
084: assertNull("Wrong highlight's end", attributes.get(1));
085:
086: hs.clear();
087: assertEquals("Sequence was not cleared", 0, hs.getMarks()
088: .size());
089: }
090:
091: public void testAddLeftOverlap() {
092: PositionsBag hs = new PositionsBag(doc, true);
093: SimpleAttributeSet attribsA = new SimpleAttributeSet();
094: SimpleAttributeSet attribsB = new SimpleAttributeSet();
095:
096: attribsA.addAttribute("set-A", "attribsA");
097: attribsB.addAttribute("set-B", "attribsB");
098:
099: hs.addHighlight(pos(10), pos(20), attribsA);
100: hs.addHighlight(pos(5), pos(15), attribsB);
101: GapList<Position> marks = hs.getMarks();
102: GapList<AttributeSet> attributes = hs.getAttributes();
103:
104: assertEquals("Wrong number of highlights", 4, marks.size());
105: assertEquals("1. highlight - wrong start offset", 5, marks.get(
106: 0).getOffset());
107: assertEquals("1. highlight - wrong end offset", 10, marks
108: .get(1).getOffset());
109: assertAttribs("1. highlight - wrong attribs",
110: attributes.get(0), "set-B");
111:
112: assertEquals("2. highlight - wrong start offset", 10, marks
113: .get(1).getOffset());
114: assertEquals("2. highlight - wrong end offset", 15, marks
115: .get(2).getOffset());
116: assertAttribs("2. highlight - wrong attribs",
117: attributes.get(1), "set-A", "set-B");
118:
119: assertEquals("3. highlight - wrong start offset", 15, marks
120: .get(2).getOffset());
121: assertEquals("3. highlight - wrong end offset", 20, marks
122: .get(3).getOffset());
123: assertAttribs("3. highlight - wrong attribs",
124: attributes.get(2), "set-A");
125: assertNull("3. highlight - wrong end", attributes.get(3));
126: }
127:
128: public void testAddRightOverlap() {
129: PositionsBag hs = new PositionsBag(doc, true);
130: SimpleAttributeSet attribsA = new SimpleAttributeSet();
131: SimpleAttributeSet attribsB = new SimpleAttributeSet();
132:
133: attribsA.addAttribute("set-A", "attribsA");
134: attribsB.addAttribute("set-B", "attribsB");
135:
136: hs.addHighlight(pos(10), pos(20), attribsA);
137: hs.addHighlight(pos(15), pos(25), attribsB);
138: GapList<Position> marks = hs.getMarks();
139: GapList<AttributeSet> attributes = hs.getAttributes();
140:
141: assertEquals("Wrong number of highlights", 4, marks.size());
142: assertEquals("1. highlight - wrong start offset", 10, marks
143: .get(0).getOffset());
144: assertEquals("1. highlight - wrong end offset", 15, marks
145: .get(1).getOffset());
146: assertAttribs("1. highlight - wrong attribs",
147: attributes.get(0), "set-A");
148:
149: assertEquals("2. highlight - wrong start offset", 15, marks
150: .get(1).getOffset());
151: assertEquals("2. highlight - wrong end offset", 20, marks
152: .get(2).getOffset());
153: assertAttribs("2. highlight - wrong attribs",
154: attributes.get(1), "set-A", "set-B");
155:
156: assertEquals("3. highlight - wrong start offset", 20, marks
157: .get(2).getOffset());
158: assertEquals("3. highlight - wrong end offset", 25, marks
159: .get(3).getOffset());
160: assertAttribs("3. highlight - wrong attribs",
161: attributes.get(2), "set-B");
162: assertNull("3. highlight - wrong end", attributes.get(3));
163: }
164:
165: public void testAddLeftMatchBiggerOverlap() {
166: PositionsBag hs = new PositionsBag(doc, true);
167: SimpleAttributeSet attribsA = new SimpleAttributeSet();
168: SimpleAttributeSet attribsB = new SimpleAttributeSet();
169:
170: attribsA.addAttribute("set-A", "attribsA");
171: attribsB.addAttribute("set-B", "attribsB");
172:
173: hs.addHighlight(pos(10), pos(20), attribsA);
174: hs.addHighlight(pos(10), pos(15), attribsB);
175: GapList<Position> marks = hs.getMarks();
176: GapList<AttributeSet> attributes = hs.getAttributes();
177:
178: assertEquals("Wrong number of highlights", 3, marks.size());
179: assertEquals("1. highlight - wrong start offset", 10, marks
180: .get(0).getOffset());
181: assertEquals("1. highlight - wrong end offset", 15, marks
182: .get(1).getOffset());
183: assertAttribs("1. highlight - wrong attribs",
184: attributes.get(0), "set-A", "set-B");
185:
186: assertEquals("2. highlight - wrong start offset", 15, marks
187: .get(1).getOffset());
188: assertEquals("2. highlight - wrong end offset", 20, marks
189: .get(2).getOffset());
190: assertAttribs("2. highlight - wrong attribs",
191: attributes.get(1), "set-A");
192: assertNull("2. highlight - wrong end", attributes.get(2));
193: }
194:
195: public void testAddRightMatchBiggerOverlap() {
196: PositionsBag hs = new PositionsBag(doc, true);
197: SimpleAttributeSet attribsA = new SimpleAttributeSet();
198: SimpleAttributeSet attribsB = new SimpleAttributeSet();
199:
200: attribsA.addAttribute("set-A", "attribsA");
201: attribsB.addAttribute("set-B", "attribsB");
202:
203: hs.addHighlight(pos(10), pos(20), attribsA);
204: hs.addHighlight(pos(15), pos(20), attribsB);
205: GapList<Position> marks = hs.getMarks();
206: GapList<AttributeSet> attributes = hs.getAttributes();
207:
208: assertEquals("Wrong number of highlights", 3, marks.size());
209: assertEquals("1. highlight - wrong start offset", 10, marks
210: .get(0).getOffset());
211: assertEquals("1. highlight - wrong end offset", 15, marks
212: .get(1).getOffset());
213: assertAttribs("1. highlight - wrong attribs",
214: attributes.get(0), "set-A");
215:
216: assertEquals("2. highlight - wrong start offset", 15, marks
217: .get(1).getOffset());
218: assertEquals("2. highlight - wrong end offset", 20, marks
219: .get(2).getOffset());
220: assertAttribs("2. highlight - wrong attribs",
221: attributes.get(1), "set-A", "set-B");
222: assertNull("2. highlight - wrong end", attributes.get(2));
223: }
224:
225: public void testAddCompleteMatchOverlap() {
226: PositionsBag hs = new PositionsBag(doc, true);
227: SimpleAttributeSet attribsA = new SimpleAttributeSet();
228: SimpleAttributeSet attribsB = new SimpleAttributeSet();
229:
230: attribsA.addAttribute("set-A", "attribsA");
231: attribsB.addAttribute("set-B", "attribsB");
232:
233: hs.addHighlight(pos(10), pos(20), attribsA);
234: hs.addHighlight(pos(10), pos(20), attribsB);
235: GapList<Position> marks = hs.getMarks();
236: GapList<AttributeSet> attributes = hs.getAttributes();
237:
238: assertEquals("Wrong number of highlights", 2, marks.size());
239: assertEquals("1. highlight - wrong start offset", 10, marks
240: .get(0).getOffset());
241: assertEquals("1. highlight - wrong end offset", 20, marks
242: .get(1).getOffset());
243: assertAttribs("1. highlight - wrong attribs",
244: attributes.get(0), "set-A", "set-B");
245: assertNull("1. highlight - wrong end", attributes.get(1));
246: }
247:
248: public void testAddBiggerOverlap() {
249: PositionsBag hs = new PositionsBag(doc, true);
250: SimpleAttributeSet attribsA = new SimpleAttributeSet();
251: SimpleAttributeSet attribsB = new SimpleAttributeSet();
252:
253: attribsA.addAttribute("set-A", "attribsA");
254: attribsB.addAttribute("set-B", "attribsB");
255:
256: hs.addHighlight(pos(10), pos(20), attribsA);
257: hs.addHighlight(pos(5), pos(25), attribsB);
258: GapList<Position> marks = hs.getMarks();
259: GapList<AttributeSet> attributes = hs.getAttributes();
260:
261: assertEquals("Wrong number of highlights", 4, marks.size());
262: assertEquals("1. highlight - wrong start offset", 5, marks.get(
263: 0).getOffset());
264: assertEquals("1. highlight - wrong end offset", 10, marks
265: .get(1).getOffset());
266: assertAttribs("1. highlight - wrong attribs",
267: attributes.get(0), "set-B");
268:
269: assertEquals("2. highlight - wrong start offset", 10, marks
270: .get(1).getOffset());
271: assertEquals("2. highlight - wrong end offset", 20, marks
272: .get(2).getOffset());
273: assertAttribs("2. highlight - wrong attribs",
274: attributes.get(1), "set-A", "set-B");
275:
276: assertEquals("3. highlight - wrong start offset", 20, marks
277: .get(2).getOffset());
278: assertEquals("3. highlight - wrong end offset", 25, marks
279: .get(3).getOffset());
280: assertAttribs("3. highlight - wrong attribs",
281: attributes.get(2), "set-B");
282: assertNull("3. highlight - wrong end", attributes.get(3));
283: }
284:
285: public void testAddLeftMatchSmallerOverlap() {
286: PositionsBag hs = new PositionsBag(doc, true);
287: SimpleAttributeSet attribsA = new SimpleAttributeSet();
288: SimpleAttributeSet attribsB = new SimpleAttributeSet();
289:
290: attribsA.addAttribute("set-A", "attribsA");
291: attribsB.addAttribute("set-B", "attribsB");
292:
293: hs.addHighlight(pos(10), pos(20), attribsA);
294: hs.addHighlight(pos(10), pos(15), attribsB);
295: GapList<Position> marks = hs.getMarks();
296: GapList<AttributeSet> attributes = hs.getAttributes();
297:
298: assertEquals("Wrong number of highlights", 3, marks.size());
299: assertEquals("1. highlight - wrong start offset", 10, marks
300: .get(0).getOffset());
301: assertEquals("1. highlight - wrong end offset", 15, marks
302: .get(1).getOffset());
303: assertAttribs("1. highlight - wrong attribs",
304: attributes.get(0), "set-A", "set-B");
305:
306: assertEquals("2. highlight - wrong start offset", 15, marks
307: .get(1).getOffset());
308: assertEquals("2. highlight - wrong end offset", 20, marks
309: .get(2).getOffset());
310: assertAttribs("2. highlight - wrong attribs",
311: attributes.get(1), "set-A");
312: assertNull("2. highlight - wrong end", attributes.get(2));
313: }
314:
315: public void testAddRightMatchSmallerOverlap() {
316: PositionsBag hs = new PositionsBag(doc, true);
317: SimpleAttributeSet attribsA = new SimpleAttributeSet();
318: SimpleAttributeSet attribsB = new SimpleAttributeSet();
319:
320: attribsA.addAttribute("set-A", "attribsA");
321: attribsB.addAttribute("set-B", "attribsB");
322:
323: hs.addHighlight(pos(10), pos(20), attribsA);
324: hs.addHighlight(pos(15), pos(20), attribsB);
325: GapList<Position> marks = hs.getMarks();
326: GapList<AttributeSet> attributes = hs.getAttributes();
327:
328: assertEquals("Wrong number of highlights", 3, marks.size());
329: assertEquals("1. highlight - wrong start offset", 10, marks
330: .get(0).getOffset());
331: assertEquals("1. highlight - wrong end offset", 15, marks
332: .get(1).getOffset());
333: assertAttribs("1. highlight - wrong attribs",
334: attributes.get(0), "set-A");
335:
336: assertEquals("2. highlight - wrong start offset", 15, marks
337: .get(1).getOffset());
338: assertEquals("2. highlight - wrong end offset", 20, marks
339: .get(2).getOffset());
340: assertAttribs("2. highlight - wrong attribs",
341: attributes.get(1), "set-A", "set-B");
342: assertNull("2. highlight - wrong end", attributes.get(2));
343: }
344:
345: public void testAddSmallerOverlap() {
346: PositionsBag hs = new PositionsBag(doc, true);
347: SimpleAttributeSet attribsA = new SimpleAttributeSet();
348: SimpleAttributeSet attribsB = new SimpleAttributeSet();
349:
350: attribsA.addAttribute("set-A", "attribsA");
351: attribsB.addAttribute("set-B", "attribsB");
352:
353: hs.addHighlight(pos(5), pos(25), attribsA);
354: hs.addHighlight(pos(10), pos(20), attribsB);
355: GapList<Position> marks = hs.getMarks();
356: GapList<AttributeSet> attributes = hs.getAttributes();
357:
358: assertEquals("Wrong number of highlights", 4, marks.size());
359: assertEquals("1. highlight - wrong start offset", 5, marks.get(
360: 0).getOffset());
361: assertEquals("1. highlight - wrong end offset", 10, marks
362: .get(1).getOffset());
363: assertAttribs("1. highlight - wrong attribs",
364: attributes.get(0), "set-A");
365:
366: assertEquals("2. highlight - wrong start offset", 10, marks
367: .get(1).getOffset());
368: assertEquals("2. highlight - wrong end offset", 20, marks
369: .get(2).getOffset());
370: assertAttribs("2. highlight - wrong attribs",
371: attributes.get(1), "set-A", "set-B");
372:
373: assertEquals("3. highlight - wrong start offset", 20, marks
374: .get(2).getOffset());
375: assertEquals("3. highlight - wrong end offset", 25, marks
376: .get(3).getOffset());
377: assertAttribs("3. highlight - wrong attribs",
378: attributes.get(0), "set-A");
379: assertNull("3. highlight - wrong end", attributes.get(3));
380: }
381:
382: public void testOrdering() {
383: PositionsBag hs = new PositionsBag(doc, true);
384: SimpleAttributeSet attribsA = new SimpleAttributeSet();
385: SimpleAttributeSet attribsB = new SimpleAttributeSet();
386:
387: attribsA.addAttribute("attribute", "value-A");
388: attribsB.addAttribute("attribute", "value-B");
389:
390: hs.addHighlight(pos(5), pos(15), attribsA);
391: hs.addHighlight(pos(10), pos(20), attribsB);
392: GapList<Position> marks = hs.getMarks();
393: GapList<AttributeSet> attributes = hs.getAttributes();
394:
395: assertEquals("Wrong number of highlights", 4, marks.size());
396: assertEquals("1. highlight - wrong attribs", "value-A",
397: attributes.get(0).getAttribute("attribute"));
398: assertEquals("2. highlight - wrong attribs", "value-B",
399: attributes.get(1).getAttribute("attribute"));
400: assertEquals("3. highlight - wrong attribs", "value-B",
401: attributes.get(2).getAttribute("attribute"));
402: assertNull("3. highlight - wrong end", attributes.get(3));
403: }
404:
405: public void testAddMultipleOverlaps() {
406: Object[] src = new Object[] { 5, 10, new String[] { "set-A" },
407: 15, 20, new String[] { "set-B" }, 25, 30,
408: new String[] { "set-C" }, 0, 40,
409: new String[] { "set-D" }, };
410: Object[] trg = new Object[] { 0, 5, new String[] { "set-D" },
411: 5, 10, new String[] { "set-A", "set-D" }, 10, 15,
412: new String[] { "set-D" }, 15, 20,
413: new String[] { "set-B", "set-D" }, 20, 25,
414: new String[] { "set-D" }, 25, 30,
415: new String[] { "set-C", "set-D" }, 30, 40,
416: new String[] { "set-D" }, };
417:
418: checkMerging(src, trg, true);
419: }
420:
421: public void testAddMultipleOverlaps2() {
422: Object[] src = new Object[] { 2, 47, new String[] { "set-A" },
423: 49, 74, new String[] { "set-B" }, 74, 100,
424: new String[] { "set-C" }, 9, 48,
425: new String[] { "set-D" }, 49, 98,
426: new String[] { "set-E" }, 0, 44,
427: new String[] { "set-F" }, 46, 74,
428: new String[] { "set-G" }, 74, 100,
429: new String[] { "set-H" }, };
430: Object[] trg = new Object[] { 0, 2, new String[] { "set-F" },
431: 2, 9, new String[] { "set-A", "set-F" }, 9, 44,
432: new String[] { "set-A", "set-D", "set-F" }, 44, 46,
433: new String[] { "set-A", "set-D" }, 46, 47,
434: new String[] { "set-A", "set-D", "set-G" }, 47, 48,
435: new String[] { "set-D", "set-G" }, 48, 49,
436: new String[] { "set-G" }, 49, 74,
437: new String[] { "set-B", "set-E", "set-G" }, 74, 98,
438: new String[] { "set-C", "set-E", "set-H" }, 98, 100,
439: new String[] { "set-C", "set-H" }, };
440:
441: checkMerging(src, trg, true);
442: }
443:
444: public void testAddMultipleOverlaps3() {
445: Object[] src = new Object[] { 5, 13, new String[] { "set-A" },
446: 20, 49, new String[] { "set-B" }, 50, 53,
447: new String[] { "set-C" }, 62, 100,
448: new String[] { "set-D" }, 9, 54,
449: new String[] { "set-E" }, 57, 100,
450: new String[] { "set-F" }, 1, 41,
451: new String[] { "set-G" }, 41, 46,
452: new String[] { "set-H" }, 54, 83,
453: new String[] { "set-I" }, 88, 100,
454: new String[] { "set-J" }, };
455: Object[] trg = new Object[] { 1, 5, new String[] { "set-G" },
456: 5, 9, new String[] { "set-A", "set-G" }, 9, 13,
457: new String[] { "set-A", "set-E", "set-G" }, 13, 20,
458: new String[] { "set-E", "set-G" }, 20, 41,
459: new String[] { "set-B", "set-G", "set-E" }, 41, 46,
460: new String[] { "set-B", "set-E", "set-H" }, 46, 49,
461: new String[] { "set-B", "set-E" }, 49, 50,
462: new String[] { "set-E" }, 50, 53,
463: new String[] { "set-C", "set-E" }, 53, 54,
464: new String[] { "set-E" }, 54, 57,
465: new String[] { "set-I" }, 57, 62,
466: new String[] { "set-F", "set-I" }, 62, 83,
467: new String[] { "set-D", "set-F", "set-I" }, 83, 88,
468: new String[] { "set-D", "set-F" }, 88, 100,
469: new String[] { "set-D", "set-F", "set-J" }, };
470:
471: checkMerging(src, trg, true);
472: }
473:
474: public void testAddMultipleOverlaps4() {
475: Object[] src = new Object[] { 4, 51,
476: new String[] { "set-A-0" }, 51, 63,
477: new String[] { "set-A-1" }, 69, 75,
478: new String[] { "set-A-2" }, 83, 85,
479: new String[] { "set-A-3" }, 92, 100,
480: new String[] { "set-A-4" }, 5, 17,
481: new String[] { "set-B-0" }, 23, 37,
482: new String[] { "set-B-1" }, 39, 63,
483: new String[] { "set-B-2" }, 69, 100,
484: new String[] { "set-B-3" }, 2, 28,
485: new String[] { "set-C-0" }, 31, 64,
486: new String[] { "set-C-1" }, 66, 93,
487: new String[] { "set-C-2" }, 95, 100,
488: new String[] { "set-C-3" }, };
489: Object[] trg = new Object[] { 2, 28,
490: new String[] { "set-C-0" }, 28, 31,
491: new String[] { "set-B-1" }, 31, 64,
492: new String[] { "set-C-1" }, 66, 93,
493: new String[] { "set-C-2" }, 93, 95,
494: new String[] { "set-B-3" }, 95, 100,
495: new String[] { "set-C-3" }, };
496:
497: checkMerging(src, trg, false);
498: }
499:
500: public void checkMerging(Object[] src, Object[] trg, boolean merge) {
501: PositionsBag hs = new PositionsBag(doc, merge);
502:
503: for (int i = 0; i < src.length / 3; i++) {
504: SimpleAttributeSet as = new SimpleAttributeSet();
505: String[] keys = (String[]) src[3 * i + 2];
506:
507: for (int j = 0; j < keys.length; j++) {
508: as.addAttribute(keys[j], Boolean.TRUE);
509: }
510:
511: hs.addHighlight(pos(((Integer) src[3 * i + 0]).intValue()),
512: pos(((Integer) src[3 * i + 1]).intValue()), as);
513: }
514:
515: int lastOffset = Integer.MIN_VALUE;
516: int differentOffsets = 0;
517:
518: for (int i = 0; i < trg.length / 3; i++) {
519: if (lastOffset != ((Integer) trg[3 * i + 0]).intValue()) {
520: differentOffsets++;
521: lastOffset = ((Integer) trg[3 * i + 0]).intValue();
522: }
523: if (lastOffset != ((Integer) trg[3 * i + 1]).intValue()) {
524: differentOffsets++;
525: lastOffset = ((Integer) trg[3 * i + 1]).intValue();
526: }
527: }
528:
529: GapList<Position> marks = hs.getMarks();
530: GapList<AttributeSet> attributes = hs.getAttributes();
531:
532: try {
533: assertEquals("Wrong number of highlights",
534: differentOffsets, marks.size());
535:
536: int trgIdx = 0;
537: for (int idx = 0; idx < marks.size(); idx++) {
538: if (attributes.get(idx) == null) {
539: assertTrue("Mark at index 0 must have attributes",
540: idx > 0);
541: continue;
542: }
543:
544: assertTrue("Too few marks", idx + 1 < marks.size());
545: assertTrue("Too many marks", trgIdx < trg.length);
546:
547: // Compare one pair
548: assertEquals(trgIdx
549: + ". highlight - wrong start offset",
550: ((Integer) trg[3 * trgIdx + 0]).intValue(),
551: marks.get(idx).getOffset());
552: assertEquals(trgIdx + ". highlight - wrong end offset",
553: ((Integer) trg[3 * trgIdx + 1]).intValue(),
554: marks.get(idx + 1).getOffset());
555: assertAttribs(trgIdx + ". highlight - wrong attribs",
556: attributes.get(idx),
557: (String[]) trg[3 * trgIdx + 2]);
558:
559: trgIdx++;
560: }
561:
562: assertTrue("Wrong number of marks: marks.size() = "
563: + marks.size() + ", trg.length = " + trg.length,
564: 3 * trgIdx == trg.length);
565:
566: } catch (AssertionFailedError afe) {
567: dumpMarks(marks, attributes);
568:
569: System.out.println("Dump through getHighlights {");
570: HighlightsSequence sequence = hs.getHighlights(
571: Integer.MIN_VALUE, Integer.MAX_VALUE);
572: for (; sequence.moveNext();) {
573: System.out.println(" <" + sequence.getStartOffset()
574: + ", " + sequence.getEndOffset() + ">");
575: }
576: System.out
577: .println("} ---- End of Dump through getHighlights ------------------");
578:
579: throw afe;
580: }
581: }
582:
583: private void assertAttribs(String msg, AttributeSet as,
584: String... keys) {
585: assertEquals(msg, keys.length, as.getAttributeCount());
586: for (String key : keys) {
587: if (null == as.getAttribute(key)) {
588: fail(msg + " attribute key: " + key);
589: }
590: }
591: }
592:
593: private String dumpHighlight(Position start, Position end,
594: AttributeSet attribs) {
595: StringBuilder sb = new StringBuilder();
596:
597: sb.append("<");
598: sb.append(start == null ? " " : start.getOffset());
599: sb.append(",");
600: sb.append(end == null ? " " : end.getOffset());
601: sb.append(",");
602: dumpAttributes(sb, attribs);
603: sb.append(">");
604:
605: return sb.toString();
606: }
607:
608: private String dumpAttributes(StringBuilder sb, AttributeSet attribs) {
609: if (sb == null) {
610: sb = new StringBuilder();
611: }
612:
613: if (attribs == null) {
614: sb.append(" ");
615: } else {
616: Enumeration en = attribs.getAttributeNames();
617: while (en.hasMoreElements()) {
618: Object attrName = en.nextElement();
619: Object attrValue = attribs.getAttribute(attrName);
620:
621: sb.append("'");
622: sb.append(attrName.toString());
623: sb.append("' = '");
624: sb.append(attrValue == null ? "null" : attrValue
625: .toString());
626: sb.append("'");
627: if (en.hasMoreElements()) {
628: sb.append(", ");
629: }
630: }
631: }
632:
633: return sb.toString();
634: }
635:
636: private void dumpMarks(GapList<Position> marks,
637: GapList<AttributeSet> attributes) {
638: String signature = marks.getClass() + "@"
639: + Integer.toHexString(System.identityHashCode(marks));
640: System.out.println("Dumping marks from " + signature + " {");
641: for (int i = 0; i < marks.size(); i++) {
642: System.out.println("<" + marks.get(i).getOffset() + ", ["
643: + dumpAttributes(null, attributes.get(i)) + "]>");
644: }
645: System.out.println("} ---- End of Dumping marks from "
646: + signature + " --------");
647: }
648:
649: private Position pos(int offset) {
650: return new SimplePosition(offset);
651: }
652: }
|