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.modules.editor.errorstripe;
043:
044: import java.awt.BorderLayout;
045: import java.util.Arrays;
046: import javax.swing.JFrame;
047: import javax.swing.JScrollPane;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.HashSet;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.SortedMap;
054: import javax.swing.JEditorPane;
055: import javax.swing.text.BadLocationException;
056: import javax.swing.text.Position;
057: import org.netbeans.editor.AnnotationDesc;
058: import org.netbeans.editor.AnnotationType;
059: import org.netbeans.editor.AnnotationTypes;
060: import org.netbeans.editor.BaseDocument;
061: import org.netbeans.editor.BaseKit;
062: import org.netbeans.editor.Utilities;
063: import org.netbeans.junit.NbTestCase;
064: import org.netbeans.modules.editor.errorstripe.caret.CaretMarkProviderCreator;
065: import org.netbeans.modules.editor.errorstripe.privatespi.Mark;
066: import org.netbeans.modules.editor.errorstripe.privatespi.Status;
067: import org.netbeans.spi.editor.errorstripe.UpToDateStatus;
068: import org.netbeans.modules.editor.options.AnnotationTypeProcessor;
069: import org.netbeans.modules.editor.options.BaseOptions;
070: import org.netbeans.modules.editor.plain.PlainKit;
071: import org.openide.filesystems.FileObject;
072: import org.openide.filesystems.Repository;
073:
074: /**
075: *
076: * @author Jan Lahoda
077: */
078: public class AnnotationViewDataImplTest extends NbTestCase {
079:
080: public AnnotationViewDataImplTest(String testName) {
081: super (testName);
082: }
083:
084: protected void setUp() throws Exception {
085: UnitUtilities
086: .prepareTest(
087: new String[] {
088: "/org/netbeans/modules/editor/resources/annotations-test-layer.xml",
089: "/org/netbeans/modules/editor/plain/resources/layer.xml",
090: "/org/netbeans/modules/editor/errorstripe/test-layer.xml" },
091: new Object[0]);
092: BaseKit.getKit(PlainKit.class);
093: BaseOptions.findObject(BaseOptions.class, true);
094:
095: AnnotationTypes.getTypes().registerLoader(
096: new AnnotationsLoader());
097: CaretMarkProviderCreator.switchOff = true;
098: }
099:
100: public void testGetMainMarkForBlock()
101: throws /*BadLocation*/Exception {
102: JEditorPane editor = new JEditorPane();
103:
104: editor.setEditorKit(BaseKit.getKit(PlainKit.class));
105:
106: BaseDocument bd = (BaseDocument) editor.getDocument();
107:
108: bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null);
109:
110: TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null,
111: new int[] { 2, 2 });
112: TestMark mark2 = new TestMark(Status.STATUS_OK, null, null,
113: new int[] { 2, 2 });
114: TestMark mark3 = new TestMark(Status.STATUS_WARNING, null,
115: null, new int[] { 2, 4 });
116:
117: AnnotationDesc test1 = new TestAnnotationDesc(bd, bd
118: .createPosition(7), "test-annotation-1");
119: AnnotationDesc test2 = new TestAnnotationDesc(bd, bd
120: .createPosition(8), "test-annotation-2");
121: AnnotationDesc test3 = new TestAnnotationDesc(bd, bd
122: .createPosition(8), "test-annotation-8");
123: AnnotationDesc test4 = new TestAnnotationDesc(bd, bd
124: .createPosition(9), "test-annotation-8");
125:
126: bd.getAnnotations().addAnnotation(test1);
127: bd.getAnnotations().addAnnotation(test2);
128: bd.getAnnotations().addAnnotation(test3);
129: bd.getAnnotations().addAnnotation(test4);
130:
131: List marks1 = Arrays.asList(new Mark[] { mark1, mark2, mark3 });
132: List marks2 = Arrays.asList(new Mark[] { mark1, mark3 });
133: List marks3 = Arrays.asList(new Mark[] { mark2, mark3 });
134: List marks4 = Arrays.asList(new Mark[] { mark1, mark2 });
135: List marks5 = Arrays.asList(new Mark[] { mark3 });
136:
137: TestMarkProvider provider = new TestMarkProvider(marks1,
138: UpToDateStatus.UP_TO_DATE_OK);
139: TestMarkProviderCreator creator = TestMarkProviderCreator
140: .getDefault();
141:
142: creator.setProvider(provider);
143:
144: AnnotationView aView = new AnnotationView(editor);
145: AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView
146: .getData();
147:
148: assertEquals(mark1, data.getMainMarkForBlock(2, 2));
149: assertEquals(mark1, data.getMainMarkForBlock(2, 3));
150: assertEquals(mark1, data.getMainMarkForBlock(2, 4));
151: assertEquals(mark1, data.getMainMarkForBlock(2, 6));
152: assertEquals(mark3, data.getMainMarkForBlock(3, 6));
153: assertEquals(mark3, data.getMainMarkForBlock(3, 3));
154: assertEquals(null, data.getMainMarkForBlock(6, 6));
155: assertEquals(Status.STATUS_ERROR, data
156: .getMainMarkForBlock(7, 7).getStatus());
157: assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8,
158: 8).getStatus());
159: bd.getAnnotations().activateNextAnnotation(8);
160: assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8,
161: 8).getStatus());
162: bd.getAnnotations().activateNextAnnotation(8);
163: assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8,
164: 8).getStatus());
165: assertNull(data.getMainMarkForBlock(9, 9));
166: assertEquals(Status.STATUS_ERROR, data
167: .getMainMarkForBlock(7, 9).getStatus());
168:
169: provider.setMarks(marks2);
170:
171: bd.getAnnotations().removeAnnotation(test3);
172:
173: assertEquals(mark1, data.getMainMarkForBlock(2, 2));
174: assertEquals(mark1, data.getMainMarkForBlock(2, 3));
175: assertEquals(mark1, data.getMainMarkForBlock(2, 4));
176: assertEquals(mark1, data.getMainMarkForBlock(2, 6));
177: assertEquals(mark3, data.getMainMarkForBlock(3, 6));
178: assertEquals(mark3, data.getMainMarkForBlock(3, 3));
179: assertEquals(null, data.getMainMarkForBlock(6, 6));
180:
181: assertEquals(Status.STATUS_ERROR, data
182: .getMainMarkForBlock(7, 7).getStatus());
183: assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8,
184: 8).getStatus());
185: assertNull(data.getMainMarkForBlock(9, 9));
186: assertEquals(Status.STATUS_ERROR, data
187: .getMainMarkForBlock(7, 9).getStatus());
188:
189: provider.setMarks(marks3);
190:
191: assertEquals(mark3, data.getMainMarkForBlock(2, 2));
192: assertEquals(mark3, data.getMainMarkForBlock(2, 3));
193: assertEquals(mark3, data.getMainMarkForBlock(2, 4));
194: assertEquals(mark3, data.getMainMarkForBlock(2, 6));
195: assertEquals(mark3, data.getMainMarkForBlock(3, 6));
196: assertEquals(mark3, data.getMainMarkForBlock(3, 3));
197: assertEquals(null, data.getMainMarkForBlock(6, 6));
198:
199: provider.setMarks(marks4);
200:
201: assertEquals(mark1, data.getMainMarkForBlock(2, 2));
202: assertEquals(mark1, data.getMainMarkForBlock(2, 3));
203: assertEquals(mark1, data.getMainMarkForBlock(2, 4));
204: assertEquals(mark1, data.getMainMarkForBlock(2, 6));
205: assertEquals(null, data.getMainMarkForBlock(3, 6));
206: assertEquals(null, data.getMainMarkForBlock(3, 3));
207: assertEquals(null, data.getMainMarkForBlock(6, 6));
208:
209: provider.setMarks(marks5);
210:
211: assertEquals(mark3, data.getMainMarkForBlock(2, 2));
212: assertEquals(mark3, data.getMainMarkForBlock(2, 3));
213: assertEquals(mark3, data.getMainMarkForBlock(2, 4));
214: assertEquals(mark3, data.getMainMarkForBlock(2, 6));
215: assertEquals(mark3, data.getMainMarkForBlock(3, 6));
216: assertEquals(mark3, data.getMainMarkForBlock(3, 3));
217: assertEquals(null, data.getMainMarkForBlock(6, 6));
218: }
219:
220: public void testComputeTotalStatus() throws Exception {
221: JFrame f = new JFrame();
222: JEditorPane editor = new JEditorPane();
223:
224: editor.setEditorKit(BaseKit.getKit(PlainKit.class));
225:
226: AnnotationView aView = new AnnotationView(editor);
227: AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView
228: .getData();
229:
230: f.getContentPane().setLayout(new BorderLayout());
231: f.getContentPane().add(new JScrollPane(editor),
232: BorderLayout.CENTER);
233: f.getContentPane().add(aView, BorderLayout.EAST);
234:
235: f.setSize(500, 500);
236:
237: f.setVisible(true);
238:
239: BaseDocument bd = (BaseDocument) editor.getDocument();
240:
241: bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null);
242:
243: Position start = bd.createPosition(Utilities
244: .getRowStartFromLineOffset(bd, 2));
245:
246: AnnotationDesc a1 = new AnnotationTestUtilities.TestAnnotationDesc1(
247: bd, start);
248: AnnotationDesc a2 = new AnnotationTestUtilities.TestAnnotationDesc2(
249: bd, start);
250:
251: bd.getAnnotations().addAnnotation(a1);
252: bd.getAnnotations().addAnnotation(a2);
253:
254: assertEquals(Status.STATUS_ERROR, data.computeTotalStatus());
255:
256: bd.getAnnotations().activateNextAnnotation(2);
257:
258: assertEquals(Status.STATUS_ERROR, data.computeTotalStatus());
259:
260: f.setVisible(false);
261: }
262:
263: public void testMarkUpdates() {
264: JEditorPane editor = new JEditorPane();
265:
266: editor.setEditorKit(BaseKit.getKit(PlainKit.class));
267:
268: TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null,
269: new int[] { 2, 2 });
270: TestMark mark2 = new TestMark(Status.STATUS_OK, null, null,
271: new int[] { 2, 2 });
272: TestMark mark3 = new TestMark(Status.STATUS_OK, null, null,
273: new int[] { 4, 6 });
274:
275: List marks = Arrays.asList(new Mark[] { mark1, mark2 });
276: List marksOnlyFirst = Arrays.asList(new Mark[] { mark1 });
277: List marksOnlySecond = Arrays.asList(new Mark[] { mark2 });
278: List marksFirstAndThird = Arrays.asList(new Mark[] { mark1,
279: mark3 });
280:
281: TestMarkProvider provider = new TestMarkProvider(marks,
282: UpToDateStatus.UP_TO_DATE_OK);
283: TestMarkProviderCreator creator = TestMarkProviderCreator
284: .getDefault();
285:
286: creator.setProvider(provider);
287:
288: AnnotationView aView = new AnnotationView(editor);
289: AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView
290: .getData();
291:
292: List mergedMarks;
293: SortedMap map;
294:
295: mergedMarks = data.getMergedMarks();
296:
297: assertEquals(marks, mergedMarks);
298:
299: map = data.getMarkMap();
300:
301: assertEquals(1, map.size());
302: assertEquals(marks, map.get(map.firstKey()));
303:
304: provider.setMarks(marksOnlyFirst);
305:
306: mergedMarks = data.getMergedMarks();
307:
308: assertEquals(marksOnlyFirst, mergedMarks);
309:
310: map = data.getMarkMap();
311:
312: assertEquals(1, map.size());
313: assertEquals(marksOnlyFirst, map.get(map.firstKey()));
314:
315: provider.setMarks(marksFirstAndThird);
316:
317: mergedMarks = data.getMergedMarks();
318:
319: assertEquals(marksFirstAndThird, mergedMarks);
320:
321: map = data.getMarkMap();
322:
323: assertEquals(4, map.size());
324: assertEquals(new HashSet(Arrays.asList(new Integer[] {
325: new Integer(2), new Integer(4), new Integer(5),
326: new Integer(6) })), map.keySet());
327: assertEquals(Arrays.asList(new Mark[] { mark1 }), map
328: .get(new Integer(2)));
329: assertEquals(Arrays.asList(new Mark[] { mark3 }), map
330: .get(new Integer(4)));
331: assertEquals(Arrays.asList(new Mark[] { mark3 }), map
332: .get(new Integer(5)));
333: assertEquals(Arrays.asList(new Mark[] { mark3 }), map
334: .get(new Integer(6)));
335:
336: provider.setMarks(Collections.EMPTY_LIST);
337:
338: mergedMarks = data.getMergedMarks();
339:
340: assertEquals(Collections.EMPTY_LIST, mergedMarks);
341:
342: map = data.getMarkMap();
343:
344: assertEquals(0, map.size());
345:
346: provider.setMarks(marksFirstAndThird);
347:
348: mergedMarks = data.getMergedMarks();
349:
350: assertEquals(marksFirstAndThird, mergedMarks);
351:
352: map = data.getMarkMap();
353:
354: assertEquals(4, map.size());
355: assertEquals(new HashSet(Arrays.asList(new Integer[] {
356: new Integer(2), new Integer(4), new Integer(5),
357: new Integer(6) })), map.keySet());
358: assertEquals(Arrays.asList(new Mark[] { mark1 }), map
359: .get(new Integer(2)));
360: assertEquals(Arrays.asList(new Mark[] { mark3 }), map
361: .get(new Integer(4)));
362: assertEquals(Arrays.asList(new Mark[] { mark3 }), map
363: .get(new Integer(5)));
364: assertEquals(Arrays.asList(new Mark[] { mark3 }), map
365: .get(new Integer(6)));
366: }
367:
368: public void testMarkPriorities() throws Exception {
369: JEditorPane editor = new JEditorPane();
370:
371: editor.setEditorKit(BaseKit.getKit(PlainKit.class));
372:
373: BaseDocument bd = (BaseDocument) editor.getDocument();
374:
375: bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null);
376:
377: //test marks:
378: TestMark mark1 = new TestMark(Status.STATUS_OK, null, null,
379: new int[] { 2, 2 }, 99);
380: TestMark mark2 = new TestMark(Status.STATUS_OK, null, null,
381: new int[] { 2, 2 }, 10);
382: TestMark mark3 = new TestMark(Status.STATUS_OK, null, null,
383: new int[] { 3, 4 }, 5);
384:
385: TestMark mark4 = new TestMark(Status.STATUS_ERROR, null, null,
386: new int[] { 2, 2 }, 1000);
387: TestMark mark5 = new TestMark(Status.STATUS_ERROR, null, null,
388: new int[] { 2, 2 }, 100);
389: TestMark mark6 = new TestMark(Status.STATUS_ERROR, null, null,
390: new int[] { 3, 4 }, 50);
391:
392: List marks1 = Arrays.asList(new Mark[] { mark1, mark2, mark3 });
393: List marks2 = Arrays.asList(new Mark[] { mark2, mark1, mark3 });
394: List marks3 = Arrays.asList(new Mark[] { mark1, mark2, mark3,
395: mark4, mark5, mark6 });
396:
397: TestMarkProvider provider = new TestMarkProvider(marks1,
398: UpToDateStatus.UP_TO_DATE_OK);
399: TestMarkProviderCreator creator = TestMarkProviderCreator
400: .getDefault();
401:
402: creator.setProvider(provider);
403:
404: AnnotationView aView = new AnnotationView(editor);
405: AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView
406: .getData();
407:
408: assertEquals(mark2, data.getMainMarkForBlock(2, 2));
409: assertEquals(mark3, data.getMainMarkForBlock(2, 3));
410: assertEquals(mark3, data.getMainMarkForBlock(3, 4));
411:
412: assertEquals(null, data.getMainMarkForBlock(6, 6));
413:
414: provider.setMarks(marks2);
415:
416: assertEquals(mark2, data.getMainMarkForBlock(2, 2));
417: assertEquals(mark3, data.getMainMarkForBlock(2, 3));
418: assertEquals(mark3, data.getMainMarkForBlock(3, 4));
419:
420: assertEquals(null, data.getMainMarkForBlock(6, 6));
421:
422: provider.setMarks(marks3);
423:
424: assertEquals(mark5, data.getMainMarkForBlock(2, 2));
425: assertEquals(mark6, data.getMainMarkForBlock(2, 3));
426: assertEquals(mark6, data.getMainMarkForBlock(3, 4));
427:
428: assertEquals(null, data.getMainMarkForBlock(6, 6));
429:
430: provider.setMarks(Collections.EMPTY_LIST);
431:
432: //test annotations:
433: AnnotationDesc test1 = new TestAnnotationDesc(bd, bd
434: .createPosition(2), "test-annotation-priority-1");
435: AnnotationDesc test2 = new TestAnnotationDesc(bd, bd
436: .createPosition(2), "test-annotation-priority-2");
437: AnnotationDesc test3 = new TestAnnotationDesc(bd, bd
438: .createPosition(2), "test-annotation-priority-3");
439: AnnotationDesc test4 = new TestAnnotationDesc(bd, bd
440: .createPosition(2), "test-annotation-priority-4");
441:
442: bd.getAnnotations().addAnnotation(test1);
443: bd.getAnnotations().addAnnotation(test2);
444:
445: assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(
446: 2, 2)).getAnnotationDesc());
447:
448: bd.getAnnotations().activateNextAnnotation(2);
449:
450: assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(
451: 2, 2)).getAnnotationDesc());
452:
453: bd.getAnnotations().activateNextAnnotation(2);
454:
455: bd.getAnnotations().addAnnotation(test3);
456: bd.getAnnotations().addAnnotation(test4);
457:
458: assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(
459: 2, 2)).getAnnotationDesc());
460:
461: bd.getAnnotations().activateNextAnnotation(2);
462:
463: assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(
464: 2, 2)).getAnnotationDesc());
465:
466: bd.getAnnotations().activateNextAnnotation(2);
467:
468: assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(
469: 2, 2)).getAnnotationDesc());
470:
471: //test interaction between annotations and marks:
472: List marks4 = Arrays.asList(new Mark[] { mark1 });
473:
474: provider.setMarks(marks4);
475:
476: bd.getAnnotations().removeAnnotation(test2);
477: bd.getAnnotations().removeAnnotation(test3);
478: bd.getAnnotations().removeAnnotation(test4);
479:
480: assertEquals(mark1, data.getMainMarkForBlock(2, 2));
481:
482: bd.getAnnotations().addAnnotation(test2);
483:
484: assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(
485: 2, 2)).getAnnotationDesc());
486: }
487:
488: protected boolean runInEQ() {
489: return true;
490: }
491:
492: static class TestAnnotationDesc extends AnnotationDesc {
493:
494: private BaseDocument doc;
495: private Position position;
496: private String type;
497:
498: public TestAnnotationDesc(BaseDocument bd, Position position,
499: String type) {
500: super (position.getOffset(), -1);
501: this .doc = bd;
502: this .position = position;
503: this .type = type;
504: }
505:
506: public String getShortDescription() {
507: return getAnnotationType();
508: }
509:
510: public String getAnnotationType() {
511: return type;
512: }
513:
514: public int getOffset() {
515: return position.getOffset();
516: }
517:
518: public int getLine() {
519: try {
520: return Utilities.getLineOffset(doc, getOffset());
521: } catch (BadLocationException e) {
522: IllegalStateException exc = new IllegalStateException();
523:
524: exc.initCause(e);
525:
526: throw exc;
527: }
528: }
529:
530: }
531:
532: static final class AnnotationsLoader implements
533: AnnotationTypes.Loader {
534:
535: public void loadTypes() {
536: try {
537: Map typesInstances = new HashMap();
538: FileObject typesFolder = Repository.getDefault()
539: .getDefaultFileSystem().findResource(
540: "Editors/AnnotationTypes");
541: FileObject[] types = typesFolder.getChildren();
542:
543: for (int cntr = 0; cntr < types.length; cntr++) {
544: AnnotationTypeProcessor proc = new AnnotationTypeProcessor();
545:
546: System.err.println("CCC:"
547: + types[cntr].getNameExt());
548: if (types[cntr].getName().startsWith(
549: "testAnnotation")
550: && "xml".equals(types[cntr].getExt())) {
551: proc.attachTo(types[cntr]);
552: AnnotationType type = (AnnotationType) proc
553: .instanceCreate();
554: typesInstances.put(type.getName(), type);
555: }
556: }
557:
558: AnnotationTypes.getTypes().setTypes(typesInstances);
559: } catch (Exception e) {
560: e.printStackTrace();
561: }
562: }
563:
564: public void loadSettings() {
565: }
566:
567: public void saveType(AnnotationType type) {
568: }
569:
570: public void saveSetting(String settingName, Object value) {
571: }
572:
573: }
574:
575: }
|