KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > semantic > DetectorTest


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 package org.netbeans.modules.java.editor.semantic;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import java.util.concurrent.CountDownLatch JavaDoc;
29 import java.util.logging.Handler JavaDoc;
30 import java.util.logging.LogRecord JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import javax.swing.text.Document JavaDoc;
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36 import org.netbeans.api.java.source.CancellableTask;
37 import org.netbeans.api.java.source.CompilationController;
38 import org.netbeans.api.java.source.JavaSource;
39 import org.netbeans.api.java.source.JavaSource.Phase;
40 import org.netbeans.api.java.source.SourceUtilsTestUtil;
41 import org.netbeans.modules.editor.highlights.HighlightComparator;
42 import org.netbeans.modules.editor.highlights.spi.Highlight;
43 import org.netbeans.modules.java.editor.semantic.TestBase.Performer;
44 import org.netbeans.spi.editor.hints.ErrorDescription;
45 import org.netbeans.spi.editor.hints.Fix;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48
49 /**
50  *
51  * @author Jan Lahoda
52  */

53 public class DetectorTest extends TestBase {
54     
55     public DetectorTest(String JavaDoc testName) {
56         super(testName);
57     }
58     
59     public void testUnusedImports() throws Exception JavaDoc {
60         performTest("UnusedImports");
61     }
62
63     public void testColorings1() throws Exception JavaDoc {
64         performTest("Colorings1");
65     }
66
67     public void testReadUseInstanceOf() throws Exception JavaDoc {
68         performTest("ReadUseInstanceOf");
69     }
70     
71     public void testReadUseTypeCast() throws Exception JavaDoc {
72         performTest("ReadUseTypeCast");
73     }
74     
75     public void testReadUseArrayIndex() throws Exception JavaDoc {
76         performTest("ReadUseArrayIndex");
77     }
78     
79     public void testReadUseUnaryOperator() throws Exception JavaDoc {
80         performTest("ReadUseUnaryOperator");
81     }
82     
83     public void testReadUseReturn() throws Exception JavaDoc {
84         performTest("ReadUseReturn");
85     }
86     
87     public void testCompoundPackage() throws Exception JavaDoc {
88     performTest("CompoundPackage");
89     }
90     
91     public void testSemanticInnerClasses() throws Exception JavaDoc {
92     performTest("SemanticInnerClasses");
93     }
94     
95     public void testForEach() throws Exception JavaDoc {
96     performTest("ForEach");
97     }
98     
99     public void testWriteUseArgument() throws Exception JavaDoc {
100     performTest("WriteUseArgument");
101     }
102     
103     public void testReturnType() throws Exception JavaDoc {
104     performTest("ReturnType");
105     }
106     
107     public void testFieldByThis1() throws Exception JavaDoc {
108     performTest("FieldByThis1");
109     }
110     
111     public void testFieldByThis2() throws Exception JavaDoc {
112         performTest("FieldByThis2");
113     }
114     
115     public void testWriteUseCatch() throws Exception JavaDoc {
116     performTest("WriteUseCatch");
117     }
118     
119     public void testReadWriteUseArgumentOfAbstractMethod() throws Exception JavaDoc {
120     performTest("ReadWriteUseArgumentOfAbstractMethod");
121     }
122     
123     public void testReadUseExprIsIdent1() throws Exception JavaDoc {
124     performTest("ReadUseExprIsIdent1");
125     }
126     
127     public void testReadUseExprIsIdent2() throws Exception JavaDoc {
128     performTest("ReadUseExprIsIdent2");
129     }
130     
131     public void testReadUseExprIsIdent3() throws Exception JavaDoc {
132     performTest("ReadUseExprIsIdent3");
133     }
134     
135     public void testReadUseExprIsIdent4() throws Exception JavaDoc {
136     performTest("ReadUseExprIsIdent4");
137     }
138     
139     public void testClassUseNewInstance() throws Exception JavaDoc {
140     performTest("ClassUseNewInstance");
141     }
142     
143     public void testExecUseMethodCall() throws Exception JavaDoc {
144     performTest("ExecUseMethodCall");
145     }
146     
147     public void testReadUseArrayInit() throws Exception JavaDoc {
148     performTest("ReadUseArrayInit");
149     }
150     
151     public void testReadUseNewArrayIndex() throws Exception JavaDoc {
152     performTest("ReadUseNewArrayIndex");
153     }
154     
155     public void testUsages2() throws Exception JavaDoc {
156         performTest("Usages2");
157     }
158
159     public void testCommentedGenerics() throws Exception JavaDoc {
160         performTest("CommentedGenerics");
161     }
162     
163     public void testRetentionPolicy() throws Exception JavaDoc {
164         performTest("RetentionPolicyTest");
165     }
166
167     public void testSimpleGeneric() throws Exception JavaDoc {
168         performTest("SimpleGeneric");
169     }
170     
171     public void testReadUseMathSet() throws Exception JavaDoc {
172         performTest("ReadUseMathSet");
173     }
174     
175     public void testReadUseMathSet2() throws Exception JavaDoc {
176         performTest("ReadUseMathSet2");
177     }
178     
179     public void testReadUseTernaryOperator() throws Exception JavaDoc {
180         performTest("ReadUseTernaryOperator");
181     }
182     
183     public void testUseInGenerics() throws Exception JavaDoc {
184         performTest("UseInGenerics");
185     }
186     
187     public void testFieldIsWritten1() throws Exception JavaDoc {
188         performTest("FieldIsWritten1");
189     }
190     
191     public void testFieldIsWritten2() throws Exception JavaDoc {
192         performTest("FieldIsWritten2");
193     }
194     
195     public void testConstructorsAreMethods() throws Exception JavaDoc {
196         performTest("ConstructorsAreMethods");
197     }
198     
199     public void testConstructorsAreMethods2() throws Exception JavaDoc {
200         performTest("ConstructorsAreMethods2");
201     }
202     
203     public void testDoubleBrackets() throws Exception JavaDoc {
204         performTest("DoubleBrackets");
205     }
206     
207     public void testConstructorsAreMethods3() throws Exception JavaDoc {
208         performTest("ConstructorsAreMethods3");
209     }
210     
211     public void testMethodWithArrayAtTheEnd() throws Exception JavaDoc {
212         performTest("MethodWithArrayAtTheEnd");
213     }
214     
215     public void testReadUseAssert() throws Exception JavaDoc {
216         performTest("ReadUseAssert");
217     }
218     
219     public void testSuperIsKeyword() throws Exception JavaDoc {
220         performTest("SuperIsKeyword");
221     }
222     
223     public void testNewArrayIsClassUse() throws Exception JavaDoc {
224         performTest("NewArrayIsClassUse");
225     }
226     
227     public void testNotKeywords() throws Exception JavaDoc {
228         performTest("NotKeywords");
229     }
230     
231     public void testArrayThroughInitializer() throws Exception JavaDoc {
232         performTest("ArrayThroughInitializer");
233     }
234     
235     public void testReadUseAssert2() throws Exception JavaDoc {
236         performTest("ReadUseAssert2");
237     }
238     
239     public void testConstructorUsedBySuper1() throws Exception JavaDoc {
240         performTest("ConstructorUsedBySuper1");
241     }
242     
243     public void testConstructorUsedBySuper2() throws Exception JavaDoc {
244         performTest("ConstructorUsedBySuper2");
245     }
246     
247     public void testConstructorUsedByThis() throws Exception JavaDoc {
248         performTest("ConstructorUsedByThis");
249     }
250     
251     public void testUnresolvableImportsAreNotUnused() throws Exception JavaDoc {
252         performTest("UnresolvableImportsAreNotUnused");
253     }
254     
255     public void testEnums() throws Exception JavaDoc {
256         performTest("Enums");
257     }
258     
259     public void testReadUseThrow() throws Exception JavaDoc {
260         performTest("ReadUseThrow");
261     }
262     
263     public void testGenericBoundIsClassUse() throws Exception JavaDoc {
264         performTest("GenericBoundIsClassUse");
265     }
266     
267     public void testBLE91246() throws Exception JavaDoc {
268         final boolean wasThrown[] = new boolean[1];
269         Logger.getLogger(Utilities.class.getName()).addHandler(new Handler JavaDoc() {
270             public void publish(LogRecord JavaDoc lr) {
271                 if (lr.getThrown() != null && lr.getThrown().getClass() == BadLocationException JavaDoc.class) {
272                     wasThrown[0] = true;
273                 }
274             }
275             public void close() {}
276             public void flush() {}
277         });
278         performTest("BLE91246");
279         
280         assertFalse("BLE was not thrown", wasThrown[0]);
281     }
282     
283     public void testArrayAccess() throws Exception JavaDoc {
284         performTest("ArrayAccess");
285     }
286     
287     private void performTest(String JavaDoc fileName) throws Exception JavaDoc {
288         performTest(fileName, new Performer() {
289             public Collection JavaDoc<Highlight> compute(CompilationController parameter, Document JavaDoc doc) {
290                 return new SemanticHighlighter(parameter.getFileObject()).process(parameter, doc);
291             }
292         });
293     }
294     
295     public void testSimpleRemoveImport() throws Exception JavaDoc {
296         performRemoveUnusedImportTest("SimpleRemoveImport");
297     }
298     
299     public void testRemoveImportNotLine1() throws Exception JavaDoc {
300         performRemoveUnusedImportTest("RemoveImportNotLine1");
301     }
302     
303 // public void testRemoveImportNotLine2() throws Exception {
304
// performRemoveUnusedImportTest("RemoveImportNotLine2");
305
// }
306

307     public void testRemoveImportDocStart() throws Exception JavaDoc {
308         performRemoveUnusedImportTest("RemoveImportDocStart");
309     }
310     
311     public void testRemoveImportTrim() throws Exception JavaDoc {
312         performRemoveUnusedImportTest("RemoveImportTrim");
313     }
314     
315     public void testRemoveImportDocStartTrim() throws Exception JavaDoc {
316         performRemoveUnusedImportTest("RemoveImportDocStartTrim");
317     }
318     
319     public void testRemoveAllImports() throws Exception JavaDoc {
320         performRemoveUnusedImportTest("RemoveAllImports", 2, 2, 0, 1);
321     }
322     
323     private FileObject testSourceFO;
324     
325     protected void performRemoveUnusedImportTest(String JavaDoc fileName) throws Exception JavaDoc {
326         performRemoveUnusedImportTest(fileName, 1, 1, 0, 0);
327     }
328     
329     protected void performRemoveUnusedImportTest(String JavaDoc fileName, int errorCount, int fixesCount, int errorToFix, int fixToPerform) throws Exception JavaDoc {
330         SourceUtilsTestUtil.prepareTest(new String JavaDoc[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object JavaDoc[0]);
331         
332     FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this);
333     FileObject cache = scratch.createFolder("cache");
334     
335         File JavaDoc wd = getWorkDir();
336         File JavaDoc testSource = new File JavaDoc(wd, "test/" + fileName + ".java");
337         
338         testSource.getParentFile().mkdirs();
339         
340         File JavaDoc dataFolder = new File JavaDoc(getDataDir(), "org/netbeans/modules/java/editor/semantic/data/");
341         
342         for (File JavaDoc f : dataFolder.listFiles()) {
343             copyToWorkDir(f, new File JavaDoc(wd, "test/" + f.getName()));
344         }
345         
346         testSourceFO = FileUtil.toFileObject(testSource);
347
348         assertNotNull(testSourceFO);
349         
350         File JavaDoc testBuildTo = new File JavaDoc(wd, "test-build");
351         
352         testBuildTo.mkdirs();
353
354         SourceUtilsTestUtil.prepareTest(FileUtil.toFileObject(dataFolder), FileUtil.toFileObject(testBuildTo), cache);
355         SourceUtilsTestUtil.compileRecursively(FileUtil.toFileObject(dataFolder));
356
357         final Document JavaDoc doc = getDocument(testSourceFO);
358         final Set JavaDoc<Highlight> highlights = new TreeSet JavaDoc<Highlight>(new HighlightComparator());
359         
360         JavaSource source = JavaSource.forFileObject(testSourceFO);
361         
362         assertNotNull(source);
363         
364         final List JavaDoc<ErrorDescription> errors = new ArrayList JavaDoc<ErrorDescription>();
365         
366         SemanticHighlighter.ErrorDescriptionSetter oldSetter = SemanticHighlighter.ERROR_DESCRIPTION_SETTER;
367         
368         try {
369             SemanticHighlighter.ERROR_DESCRIPTION_SETTER = new SemanticHighlighter.ErrorDescriptionSetter() {
370                 public void setErrors(Document JavaDoc doc, List JavaDoc<ErrorDescription> errs) {
371                     errors.addAll(errs);
372                 }
373             };
374             
375             source.runUserActionTask(new CancellableTask<CompilationController>() {
376                 public void cancel() {}
377                 public void run(CompilationController parameter) {
378                     try {
379                         parameter.toPhase(Phase.UP_TO_DATE);
380                         new SemanticHighlighter(parameter.getFileObject()).process(parameter, doc);
381                     } catch (IOException JavaDoc e) {
382                         e.printStackTrace();
383                     }
384                 }
385             }, true);
386             
387             assertEquals(errors.toString(), errorCount, errors.size());
388             
389             List JavaDoc<Fix> fixes = errors.get(errorToFix).getFixes().getFixes();
390             
391             assertEquals(fixesCount, fixes.size());
392             
393             fixes.get(fixToPerform).implement();
394             
395             ref(doc.getText(0, doc.getLength()));
396             
397             compareReferenceFiles();
398         } finally {
399             SemanticHighlighter.ERROR_DESCRIPTION_SETTER = oldSetter;
400         }
401     }
402 }
403
Popular Tags