KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > hints > AnnotationHolderTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.hints;
21
22 import java.io.OutputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import org.netbeans.editor.BaseDocument;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.modules.editor.highlights.spi.Highlight;
32 import org.netbeans.modules.editor.hints.AnnotationHolder.Attacher;
33 import org.netbeans.spi.editor.hints.ErrorDescription;
34 import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
35 import org.netbeans.spi.editor.hints.ErrorDescriptionTestSupport;
36 import org.netbeans.spi.editor.hints.Fix;
37 import org.netbeans.spi.editor.hints.Severity;
38 import org.openide.cookies.EditorCookie;
39 import org.openide.filesystems.FileLock;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileSystem;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.loaders.DataObject;
44 import org.openide.text.Annotation;
45
46 /**
47  *
48  * @author Jan Lahoda
49  */

50 public class AnnotationHolderTest extends NbTestCase {
51     
52     private FileObject file;
53     private Document JavaDoc doc;
54     private EditorCookie ec;
55     
56     /** Creates a new instance of AnnotationHolderTest */
57     public AnnotationHolderTest(String JavaDoc name) {
58         super(name);
59     }
60     
61     @Override JavaDoc
62     protected void setUp() throws Exception JavaDoc {
63         FileSystem fs = FileUtil.createMemoryFileSystem();
64         
65         file = fs.getRoot().createData("test.txt");
66         
67         writeIntoFile(file, "01234567890123456789");
68         
69         DataObject od = DataObject.find(file);
70         
71         ec = od.getCookie(EditorCookie.class);
72         doc = ec.openDocument();
73     }
74     
75     public void testComputeHighlightsOneLayer1() throws Exception JavaDoc {
76         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 3);
77         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 5, 6);
78         
79         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
80         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
81         
82         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
83         
84         assertEquals(2, highlights.size());
85         
86         Highlight h1 = highlights.get(0);
87         Highlight h2 = highlights.get(1);
88         
89         assertEquals(1, h1.getStart());
90         assertEquals(3, h1.getEnd());
91         assertEquals(5, h2.getStart());
92         assertEquals(6, h2.getEnd());
93     }
94     
95     public void testComputeHighlightsOneLayer2() throws Exception JavaDoc {
96         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 7);
97         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 5, 6);
98         
99         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
100         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
101         
102         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
103         
104         assertEquals(1, highlights.size());
105         
106         Highlight h1 = highlights.get(0);
107         
108         assertEquals(1, h1.getStart());
109         assertEquals(7, h1.getEnd());
110     }
111     
112     public void testComputeHighlightsOneLayer3() throws Exception JavaDoc {
113         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 5);
114         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 3, 7);
115         
116         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
117         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
118         
119         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
120         
121         assertEquals(1, highlights.size());
122         
123         Highlight h1 = highlights.get(0);
124         
125         assertEquals(1, h1.getStart());
126         assertEquals(7, h1.getEnd());
127     }
128     
129     public void testComputeHighlightsOneLayer4() throws Exception JavaDoc {
130         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5);
131         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 1, 7);
132         
133         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
134         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
135         
136         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
137         
138         assertEquals(1, highlights.size());
139         
140         Highlight h1 = highlights.get(0);
141         
142         assertEquals(1, h1.getStart());
143         assertEquals(7, h1.getEnd());
144     }
145     
146     public void testComputeHighlightsTwoLayers1() throws Exception JavaDoc {
147         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 3);
148         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 5, 7);
149         
150         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
151         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
152         
153         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
154         
155         assertEquals(2, highlights.size());
156         
157         Highlight h1 = highlights.get(0);
158         Highlight h2 = highlights.get(1);
159         
160         assertEquals(1, h1.getStart());
161         assertEquals(3, h1.getEnd());
162         assertEquals(5, h2.getStart());
163         assertEquals(7, h2.getEnd());
164     }
165     
166     public void testComputeHighlightsTwoLayers2() throws Exception JavaDoc {
167         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 7);
168         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 3, 5);
169         
170         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
171         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
172         
173         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
174         
175         assertEquals(1, highlights.size());
176         
177         Highlight h1 = highlights.get(0);
178         
179         assertEquals(1, h1.getStart());
180         assertEquals(7, h1.getEnd());
181         
182         assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring());
183     }
184     
185     public void testComputeHighlightsTwoLayers3() throws Exception JavaDoc {
186         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5);
187         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 4, 7);
188         
189         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
190         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
191         
192         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
193         
194         assertEquals(2, highlights.size());
195         
196         Highlight h1 = highlights.get(0);
197         Highlight h2 = highlights.get(1);
198         
199         assertEquals(3, h1.getStart());
200         assertEquals(5, h1.getEnd());
201         assertEquals(6, h2.getStart());
202         assertEquals(7, h2.getEnd());
203         
204         assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring());
205         assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h2.getColoring());
206     }
207     
208     public void testComputeHighlightsTwoLayers4() throws Exception JavaDoc {
209         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5);
210         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 4);
211         
212         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
213         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
214         
215         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
216         
217         assertEquals(2, highlights.size());
218         
219         Highlight h1 = highlights.get(0);
220         Highlight h2 = highlights.get(1);
221         
222         assertEquals(3, h1.getStart());
223         assertEquals(5, h1.getEnd());
224         assertEquals(1, h2.getStart());
225         assertEquals(2, h2.getEnd());
226         
227         assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring());
228         assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h2.getColoring());
229     }
230     
231     public void testComputeHighlightsTwoLayers5() throws Exception JavaDoc {
232         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5);
233         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 7);
234         
235         List JavaDoc<ErrorDescription> errors = Arrays.asList(ed1, ed2);
236         List JavaDoc<Highlight> highlights = new ArrayList JavaDoc<Highlight>();
237         
238         AnnotationHolder.computeHighlights(doc, 0, errors, highlights);
239         
240         assertEquals(3, highlights.size());
241         
242         Highlight h1 = highlights.get(0);
243         Highlight h2 = highlights.get(1);
244         Highlight h3 = highlights.get(2);
245         
246         assertEquals(3, h1.getStart());
247         assertEquals(5, h1.getEnd());
248         assertEquals(1, h3.getStart());
249         assertEquals(2, h3.getEnd());
250         assertEquals(6, h2.getStart());
251         assertEquals(7, h2.getEnd());
252         
253         assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring());
254         assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h2.getColoring());
255         assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h3.getColoring());
256     }
257     
258     public void testNullSpan() throws Exception JavaDoc {
259         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5);
260         ErrorDescription ed2 = ErrorDescriptionTestSupport.createErrorDescription(file, "", Severity.DISABLED, ErrorDescriptionFactory.lazyListForFixes(Collections.<Fix>emptyList()), null);
261         ErrorDescription ed3 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 7);
262         
263         ec.open();
264         
265         AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed1, ed2, ed3));
266         
267         ec.close();
268     }
269     
270     public void testComputeSeverity() throws Exception JavaDoc {
271         ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5);
272         ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.HINT, "2", file, 1, 7);
273         ErrorDescription ed3 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 7);
274         ErrorDescription ed4 = ErrorDescriptionFactory.createErrorDescription(Severity.VERIFIER, "2", file, 1, 7);
275         
276         ec.open();
277         
278         class AttacherImpl implements Attacher {
279             private ParseErrorAnnotation annotation;
280             public void attachAnnotation(int line, ParseErrorAnnotation a) throws BadLocationException JavaDoc {
281                 if (line == 0) {
282                     this.annotation = a;
283                 }
284             }
285             public void detachAnnotation(Annotation a) {}
286         }
287         
288         AttacherImpl impl = new AttacherImpl();
289         
290         AnnotationHolder.getInstance(file).attacher = impl;
291         
292         AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed1, ed2, ed3));
293         
294         assertEquals(Severity.ERROR, impl.annotation.getSeverity());
295         
296         AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed2, ed3));
297         
298         assertEquals(Severity.WARNING, impl.annotation.getSeverity());
299         
300         AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed2));
301         
302         assertEquals(Severity.HINT, impl.annotation.getSeverity());
303         
304         AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed2, ed4));
305         
306         assertEquals(Severity.VERIFIER, impl.annotation.getSeverity());
307         
308         ec.close();
309     }
310     
311     @Override JavaDoc
312     protected boolean runInEQ() {
313         return true;
314     }
315     
316     private void writeIntoFile(FileObject file, String JavaDoc what) throws Exception JavaDoc {
317         FileLock lock = file.lock();
318         OutputStream JavaDoc out = file.getOutputStream(lock);
319         
320         try {
321             out.write(what.getBytes());
322         } finally {
323             out.close();
324             lock.releaseLock();
325         }
326     }
327     
328 }
329
Popular Tags