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.hints;
043:
044: import java.io.OutputStream;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.List;
048: import javax.swing.text.AttributeSet;
049: import javax.swing.text.BadLocationException;
050: import javax.swing.text.DefaultEditorKit;
051: import javax.swing.text.Document;
052: import javax.swing.text.Position;
053: import org.netbeans.api.editor.mimelookup.MimePath;
054: import org.netbeans.editor.BaseDocument;
055: import org.netbeans.editor.GuardedDocument;
056: import org.netbeans.editor.Utilities;
057: import org.netbeans.junit.MockServices;
058: import org.netbeans.junit.NbTestCase;
059: import org.netbeans.modules.editor.hints.AnnotationHolder.Attacher;
060: import org.netbeans.spi.editor.highlighting.HighlightsSequence;
061: import org.netbeans.spi.editor.highlighting.support.OffsetsBag;
062: import org.netbeans.spi.editor.hints.ErrorDescription;
063: import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
064: import org.netbeans.spi.editor.hints.ErrorDescriptionTestSupport;
065: import org.netbeans.spi.editor.hints.Fix;
066: import org.netbeans.spi.editor.hints.Severity;
067: import org.netbeans.spi.editor.mimelookup.MimeDataProvider;
068: import org.openide.LifecycleManager;
069: import org.openide.cookies.EditorCookie;
070: import org.openide.filesystems.FileLock;
071: import org.openide.filesystems.FileObject;
072: import org.openide.filesystems.FileSystem;
073: import org.openide.filesystems.FileUtil;
074: import org.openide.loaders.DataObject;
075: import org.openide.text.Annotation;
076: import org.openide.util.Lookup;
077: import org.openide.util.lookup.Lookups;
078:
079: import static org.netbeans.modules.editor.hints.AnnotationHolder.*;
080: import static org.netbeans.spi.editor.hints.Severity.*;
081:
082: /**
083: *
084: * @author Jan Lahoda
085: */
086: public class AnnotationHolderTest extends NbTestCase {
087:
088: private FileObject file;
089: private Document doc;
090: private EditorCookie ec;
091:
092: /** Creates a new instance of AnnotationHolderTest */
093: public AnnotationHolderTest(String name) {
094: super (name);
095: }
096:
097: @Override
098: protected void setUp() throws Exception {
099: MockServices.setServices(MimeDataProviderImpl.class);
100: FileSystem fs = FileUtil.createMemoryFileSystem();
101:
102: file = fs.getRoot().createData("test.txt");
103:
104: writeIntoFile(file,
105: "01234567890123456789\n abcdefg \n hijklmnop");
106:
107: DataObject od = DataObject.find(file);
108:
109: ec = od.getCookie(EditorCookie.class);
110: doc = ec.openDocument();
111: }
112:
113: public void testComputeHighlightsOneLayer1() throws Exception {
114: ErrorDescription ed1 = ErrorDescriptionFactory
115: .createErrorDescription(Severity.ERROR, "1", file, 1, 3);
116: ErrorDescription ed2 = ErrorDescriptionFactory
117: .createErrorDescription(Severity.ERROR, "2", file, 5, 6);
118:
119: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
120: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
121: errors);
122:
123: assertHighlights("", bag, new int[] { 1, 3, 5, 6 },
124: new AttributeSet[] { COLORINGS.get(ERROR),
125: COLORINGS.get(ERROR) });
126: }
127:
128: public void testComputeHighlightsOneLayer2() throws Exception {
129: ErrorDescription ed1 = ErrorDescriptionFactory
130: .createErrorDescription(Severity.ERROR, "1", file, 1, 7);
131: ErrorDescription ed2 = ErrorDescriptionFactory
132: .createErrorDescription(Severity.ERROR, "2", file, 5, 6);
133:
134: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
135: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
136: errors);
137:
138: assertHighlights("", bag, new int[] { 1, 7 },
139: new AttributeSet[] { COLORINGS.get(ERROR) });
140: }
141:
142: public void testComputeHighlightsOneLayer3() throws Exception {
143: ErrorDescription ed1 = ErrorDescriptionFactory
144: .createErrorDescription(Severity.ERROR, "1", file, 1, 5);
145: ErrorDescription ed2 = ErrorDescriptionFactory
146: .createErrorDescription(Severity.ERROR, "2", file, 3, 7);
147:
148: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
149: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
150: errors);
151:
152: assertHighlights("", bag, new int[] { 1, 7 },
153: new AttributeSet[] { COLORINGS.get(ERROR) });
154: }
155:
156: public void testComputeHighlightsOneLayer4() throws Exception {
157: ErrorDescription ed1 = ErrorDescriptionFactory
158: .createErrorDescription(Severity.ERROR, "1", file, 3, 5);
159: ErrorDescription ed2 = ErrorDescriptionFactory
160: .createErrorDescription(Severity.ERROR, "2", file, 1, 7);
161:
162: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
163: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
164: errors);
165:
166: assertHighlights("", bag, new int[] { 1, 7 },
167: new AttributeSet[] { COLORINGS.get(ERROR) });
168: }
169:
170: public void testComputeHighlightsTwoLayers1() throws Exception {
171: ErrorDescription ed1 = ErrorDescriptionFactory
172: .createErrorDescription(Severity.ERROR, "1", file, 1, 3);
173: ErrorDescription ed2 = ErrorDescriptionFactory
174: .createErrorDescription(Severity.WARNING, "2", file, 5,
175: 7);
176:
177: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
178: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
179: errors);
180:
181: assertHighlights("", bag, new int[] { 1, 3, 5, 7 },
182: new AttributeSet[] { COLORINGS.get(ERROR),
183: COLORINGS.get(WARNING) });
184: }
185:
186: public void testComputeHighlightsTwoLayers2() throws Exception {
187: ErrorDescription ed1 = ErrorDescriptionFactory
188: .createErrorDescription(Severity.ERROR, "1", file, 1, 7);
189: ErrorDescription ed2 = ErrorDescriptionFactory
190: .createErrorDescription(Severity.WARNING, "2", file, 3,
191: 5);
192:
193: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
194: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
195: errors);
196:
197: assertHighlights("", bag, new int[] { 1, 7 },
198: new AttributeSet[] { COLORINGS.get(ERROR) });
199: }
200:
201: public void testComputeHighlightsTwoLayers3() throws Exception {
202: ErrorDescription ed1 = ErrorDescriptionFactory
203: .createErrorDescription(Severity.ERROR, "1", file, 3, 5);
204: ErrorDescription ed2 = ErrorDescriptionFactory
205: .createErrorDescription(Severity.WARNING, "2", file, 4,
206: 7);
207:
208: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
209: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
210: errors);
211:
212: assertHighlights("", bag, new int[] { 3, 5, /*6*/5, 7 },
213: new AttributeSet[] { COLORINGS.get(ERROR),
214: COLORINGS.get(WARNING) });
215: }
216:
217: public void testComputeHighlightsTwoLayers4() throws Exception {
218: ErrorDescription ed1 = ErrorDescriptionFactory
219: .createErrorDescription(Severity.ERROR, "1", file, 3, 5);
220: ErrorDescription ed2 = ErrorDescriptionFactory
221: .createErrorDescription(Severity.WARNING, "2", file, 1,
222: 4);
223:
224: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
225: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
226: errors);
227:
228: assertHighlights("", bag, new int[] { 1, /*2*/3, 3, 5 },
229: new AttributeSet[] { COLORINGS.get(WARNING),
230: COLORINGS.get(ERROR) });
231: }
232:
233: public void testComputeHighlightsTwoLayers5() throws Exception {
234: ErrorDescription ed1 = ErrorDescriptionFactory
235: .createErrorDescription(Severity.ERROR, "1", file, 3, 5);
236: ErrorDescription ed2 = ErrorDescriptionFactory
237: .createErrorDescription(Severity.WARNING, "2", file, 1,
238: 7);
239:
240: List<ErrorDescription> errors = Arrays.asList(ed1, ed2);
241: final OffsetsBag bag = AnnotationHolder.computeHighlights(doc,
242: errors);
243:
244: assertHighlights("", bag, new int[] { 1, /*2*/3, 3, 5, /*6*/
245: 5, 7 }, new AttributeSet[] { COLORINGS.get(WARNING),
246: COLORINGS.get(ERROR), COLORINGS.get(WARNING) });
247: }
248:
249: public void testNullSpan() throws Exception {
250: ErrorDescription ed1 = ErrorDescriptionFactory
251: .createErrorDescription(Severity.ERROR, "1", file, 3, 5);
252: ErrorDescription ed3 = ErrorDescriptionFactory
253: .createErrorDescription(Severity.WARNING, "2", file, 1,
254: 7);
255:
256: ec.open();
257:
258: AnnotationHolder.getInstance(file).setErrorDescriptions("foo",
259: Arrays.asList(ed1, ed3));
260:
261: ec.close();
262: }
263:
264: public void testMultilineHighlights() throws Exception {
265: ErrorDescription ed1 = ErrorDescriptionFactory
266: .createErrorDescription(Severity.ERROR, "1", file,
267: 47 - 30, 72 - 30);
268: OffsetsBag bag = new OffsetsBag(doc);
269:
270: List<ErrorDescription> errors = Arrays.asList(ed1);
271: BaseDocument bdoc = (BaseDocument) doc;
272:
273: bag = new OffsetsBag(doc);
274: AnnotationHolder.updateHighlightsOnLine(bag, bdoc, bdoc
275: .createPosition(Utilities.getRowStartFromLineOffset(
276: bdoc, 0)), errors);
277:
278: assertHighlights("", bag, new int[] { 47 - 30, 50 - 30 },
279: new AttributeSet[] { COLORINGS.get(ERROR) });
280:
281: bag = new OffsetsBag(doc);
282: AnnotationHolder.updateHighlightsOnLine(bag, bdoc, bdoc
283: .createPosition(Utilities.getRowStartFromLineOffset(
284: bdoc, 1)), errors);
285:
286: assertHighlights("", bag, new int[] { 53 - 30, 60 - 30 },
287: new AttributeSet[] { COLORINGS.get(ERROR) });
288:
289: bag = new OffsetsBag(doc);
290: AnnotationHolder.updateHighlightsOnLine(bag, bdoc, bdoc
291: .createPosition(Utilities.getRowStartFromLineOffset(
292: bdoc, 2)), errors);
293:
294: assertHighlights("", bag, new int[] { 65 - 30, 72 - 30 },
295: new AttributeSet[] { COLORINGS.get(ERROR) });
296: }
297:
298: public void testComputeSeverity() throws Exception {
299: ErrorDescription ed1 = ErrorDescriptionFactory
300: .createErrorDescription(Severity.ERROR, "1", file, 3, 5);
301: ErrorDescription ed2 = ErrorDescriptionFactory
302: .createErrorDescription(Severity.HINT, "2", file, 1, 7);
303: ErrorDescription ed3 = ErrorDescriptionFactory
304: .createErrorDescription(Severity.WARNING, "2", file, 1,
305: 7);
306: ErrorDescription ed4 = ErrorDescriptionFactory
307: .createErrorDescription(Severity.VERIFIER, "2", file,
308: 1, 7);
309:
310: ec.open();
311:
312: class AttacherImpl implements Attacher {
313: private ParseErrorAnnotation annotation;
314:
315: public void attachAnnotation(Position line,
316: ParseErrorAnnotation a) throws BadLocationException {
317: if (line.getOffset() == 0) {
318: this .annotation = a;
319: }
320: }
321:
322: public void detachAnnotation(Annotation a) {
323: }
324: }
325:
326: AttacherImpl impl = new AttacherImpl();
327:
328: AnnotationHolder.getInstance(file).attacher = impl;
329:
330: AnnotationHolder.getInstance(file).setErrorDescriptions("foo",
331: Arrays.asList(ed1, ed2, ed3));
332:
333: assertEquals(Severity.ERROR, impl.annotation.getSeverity());
334:
335: AnnotationHolder.getInstance(file).setErrorDescriptions("foo",
336: Arrays.asList(ed2, ed3));
337:
338: assertEquals(Severity.WARNING, impl.annotation.getSeverity());
339:
340: AnnotationHolder.getInstance(file).setErrorDescriptions("foo",
341: Arrays.asList(ed2));
342:
343: assertEquals(Severity.HINT, impl.annotation.getSeverity());
344:
345: AnnotationHolder.getInstance(file).setErrorDescriptions("foo",
346: Arrays.asList(ed2, ed4));
347:
348: assertEquals(Severity.VERIFIER, impl.annotation.getSeverity());
349:
350: ec.close();
351: }
352:
353: public void testTypeIntoLine() throws Exception {
354: performTypingTest(25, "a", new int[0], new AttributeSet[0]);
355: }
356:
357: public void testTypeOnLineStart() throws Exception {
358: performTypingTest(21, "a", new int[0], new AttributeSet[0]);
359: }
360:
361: public void testTypeOnLineStartWithNewline() throws Exception {
362: performTypingTest(21, "a\n", new int[0], new AttributeSet[0]);
363: }
364:
365: public void testTypeOnLineStartWithNewlines() throws Exception {
366: performTypingTest(21, "a\na\na\na\n", new int[0],
367: new AttributeSet[0]);
368: }
369:
370: public void testTypeNewline() throws Exception {
371: performTypingTest(22, "asdasd\nasdfasdf", new int[] { 23, 25 },
372: new int[0], new AttributeSet[0]);
373: }
374:
375: private void performTypingTest(int index, String insertWhat,
376: int[] highlightSpans, AttributeSet[] highlightValues)
377: throws Exception {
378: performTypingTest(index, insertWhat, new int[] { 21, 32 },
379: highlightSpans, highlightValues);
380: }
381:
382: private void performTypingTest(int index, String insertWhat,
383: int[] errorSpan, int[] highlightSpans,
384: AttributeSet[] highlightValues) throws Exception {
385: ErrorDescription ed1 = ErrorDescriptionFactory
386: .createErrorDescription(Severity.ERROR, "1", file,
387: errorSpan[0], errorSpan[1]);
388:
389: ec.open();
390:
391: //these tests currently ignore annotations:
392: class AttacherImpl implements Attacher {
393: public void attachAnnotation(Position line,
394: ParseErrorAnnotation a) throws BadLocationException {
395: }
396:
397: public void detachAnnotation(Annotation a) {
398: }
399: }
400:
401: AnnotationHolder.getInstance(file).attacher = new AttacherImpl();
402:
403: AnnotationHolder.getInstance(file).setErrorDescriptions("foo",
404: Arrays.asList(ed1));
405:
406: doc.insertString(index, insertWhat, null);
407:
408: assertHighlights("highlights correct", AnnotationHolder
409: .getBag(doc), highlightSpans, highlightValues);
410:
411: LifecycleManager.getDefault().saveAll();
412:
413: ec.close();
414: }
415:
416: private void assertHighlights(String message, OffsetsBag bag,
417: int[] spans, AttributeSet[] values) {
418: HighlightsSequence hs = bag.getHighlights(0, Integer.MAX_VALUE);
419: int index = 0;
420:
421: while (hs.moveNext()) {
422: assertEquals(message, spans[2 * index], hs.getStartOffset());
423: assertEquals(message, spans[2 * index + 1], hs
424: .getEndOffset());
425: assertEquals(message, values[index], hs.getAttributes());
426: index++;
427: }
428: }
429:
430: @Override
431: protected boolean runInEQ() {
432: return true;
433: }
434:
435: private void writeIntoFile(FileObject file, String what)
436: throws Exception {
437: FileLock lock = file.lock();
438: OutputStream out = file.getOutputStream(lock);
439:
440: try {
441: out.write(what.getBytes());
442: } finally {
443: out.close();
444: lock.releaseLock();
445: }
446: }
447:
448: public static final class MimeDataProviderImpl implements
449: MimeDataProvider {
450:
451: @SuppressWarnings("deprecation")
452: public Lookup getLookup(MimePath mimePath) {
453: return Lookups.singleton(new DefaultEditorKit() {
454: @Override
455: public Document createDefaultDocument() {
456: return new GuardedDocument(this.getClass());
457: }
458: });
459: }
460:
461: }
462: }
|