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-2007 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: package org.netbeans.modules.java.editor.semantic;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Set;
049: import java.util.logging.Handler;
050: import java.util.logging.LogRecord;
051: import java.util.logging.Logger;
052: import javax.swing.text.BadLocationException;
053: import javax.swing.text.Document;
054: import org.netbeans.api.java.source.Task;
055: import org.netbeans.api.java.source.CompilationController;
056: import org.netbeans.api.java.source.JavaSource;
057: import org.netbeans.api.java.source.JavaSource.Phase;
058: import org.netbeans.api.java.source.SourceUtilsTestUtil;
059: import org.netbeans.api.lexer.Token;
060: import org.netbeans.api.java.source.TreePathHandle;
061: import org.netbeans.modules.java.editor.semantic.ColoringAttributes.Coloring;
062: import org.netbeans.modules.java.editor.semantic.SemanticHighlighter.ErrorDescriptionSetter;
063: import org.netbeans.modules.java.editor.semantic.TestBase.Performer;
064: import org.netbeans.spi.editor.highlighting.support.OffsetsBag;
065: import org.netbeans.spi.editor.hints.ErrorDescription;
066: import org.netbeans.spi.editor.hints.Fix;
067: import org.openide.LifecycleManager;
068: import org.openide.filesystems.FileObject;
069: import org.openide.filesystems.FileUtil;
070:
071: /**
072: *
073: * @author Jan Lahoda
074: */
075: public class DetectorTest extends TestBase {
076:
077: public DetectorTest(String testName) {
078: super (testName);
079: }
080:
081: @Override
082: protected void tearDown() throws Exception {
083: super .tearDown();
084: LifecycleManager.getDefault().saveAll();
085: }
086:
087: public void testUnusedImports() throws Exception {
088: performTest("UnusedImports");
089: }
090:
091: public void testColorings1() throws Exception {
092: performTest("Colorings1");
093: }
094:
095: public void testReadUseInstanceOf() throws Exception {
096: performTest("ReadUseInstanceOf");
097: }
098:
099: public void testReadUseTypeCast() throws Exception {
100: performTest("ReadUseTypeCast");
101: }
102:
103: public void testReadUseArrayIndex() throws Exception {
104: performTest("ReadUseArrayIndex");
105: }
106:
107: public void testReadUseUnaryOperator() throws Exception {
108: performTest("ReadUseUnaryOperator");
109: }
110:
111: public void testReadUseReturn() throws Exception {
112: performTest("ReadUseReturn");
113: }
114:
115: public void testCompoundPackage() throws Exception {
116: performTest("CompoundPackage");
117: }
118:
119: public void testSemanticInnerClasses() throws Exception {
120: performTest("SemanticInnerClasses");
121: }
122:
123: public void testForEach() throws Exception {
124: performTest("ForEach");
125: }
126:
127: public void testWriteUseArgument() throws Exception {
128: performTest("WriteUseArgument");
129: }
130:
131: public void testReturnType() throws Exception {
132: performTest("ReturnType");
133: }
134:
135: public void testFieldByThis1() throws Exception {
136: performTest("FieldByThis1");
137: }
138:
139: public void testFieldByThis2() throws Exception {
140: performTest("FieldByThis2");
141: }
142:
143: public void testWriteUseCatch() throws Exception {
144: performTest("WriteUseCatch");
145: }
146:
147: public void testReadWriteUseArgumentOfAbstractMethod()
148: throws Exception {
149: performTest("ReadWriteUseArgumentOfAbstractMethod");
150: }
151:
152: public void testReadUseExprIsIdent1() throws Exception {
153: performTest("ReadUseExprIsIdent1");
154: }
155:
156: public void testReadUseExprIsIdent2() throws Exception {
157: performTest("ReadUseExprIsIdent2");
158: }
159:
160: public void testReadUseExprIsIdent3() throws Exception {
161: performTest("ReadUseExprIsIdent3");
162: }
163:
164: public void testReadUseExprIsIdent4() throws Exception {
165: performTest("ReadUseExprIsIdent4");
166: }
167:
168: public void testClassUseNewInstance() throws Exception {
169: performTest("ClassUseNewInstance");
170: }
171:
172: public void testExecUseMethodCall() throws Exception {
173: performTest("ExecUseMethodCall");
174: }
175:
176: public void testReadUseArrayInit() throws Exception {
177: performTest("ReadUseArrayInit");
178: }
179:
180: public void testReadUseNewArrayIndex() throws Exception {
181: performTest("ReadUseNewArrayIndex");
182: }
183:
184: public void testUsages2() throws Exception {
185: performTest("Usages2");
186: }
187:
188: public void testCommentedGenerics() throws Exception {
189: performTest("CommentedGenerics");
190: }
191:
192: public void testRetentionPolicy() throws Exception {
193: performTest("RetentionPolicyTest");
194: }
195:
196: public void testSimpleGeneric() throws Exception {
197: performTest("SimpleGeneric");
198: }
199:
200: public void testReadUseMathSet() throws Exception {
201: performTest("ReadUseMathSet");
202: }
203:
204: public void testReadUseMathSet2() throws Exception {
205: performTest("ReadUseMathSet2");
206: }
207:
208: public void testReadUseTernaryOperator() throws Exception {
209: performTest("ReadUseTernaryOperator");
210: }
211:
212: public void testUseInGenerics() throws Exception {
213: performTest("UseInGenerics");
214: }
215:
216: public void testFieldIsWritten1() throws Exception {
217: performTest("FieldIsWritten1");
218: }
219:
220: public void testFieldIsWritten2() throws Exception {
221: performTest("FieldIsWritten2");
222: }
223:
224: public void testConstructorsAreMethods() throws Exception {
225: performTest("ConstructorsAreMethods");
226: }
227:
228: public void testConstructorsAreMethods2() throws Exception {
229: performTest("ConstructorsAreMethods2");
230: }
231:
232: public void testDoubleBrackets() throws Exception {
233: performTest("DoubleBrackets");
234: }
235:
236: public void testConstructorsAreMethods3() throws Exception {
237: performTest("ConstructorsAreMethods3");
238: }
239:
240: public void testMethodWithArrayAtTheEnd() throws Exception {
241: performTest("MethodWithArrayAtTheEnd");
242: }
243:
244: public void testReadUseAssert() throws Exception {
245: performTest("ReadUseAssert");
246: }
247:
248: public void testSuperIsKeyword() throws Exception {
249: performTest("SuperIsKeyword");
250: }
251:
252: public void testNewArrayIsClassUse() throws Exception {
253: performTest("NewArrayIsClassUse");
254: }
255:
256: public void testNotKeywords() throws Exception {
257: performTest("NotKeywords");
258: }
259:
260: public void testArrayThroughInitializer() throws Exception {
261: performTest("ArrayThroughInitializer");
262: }
263:
264: public void testReadUseAssert2() throws Exception {
265: performTest("ReadUseAssert2");
266: }
267:
268: public void testConstructorUsedBySuper1() throws Exception {
269: performTest("ConstructorUsedBySuper1");
270: }
271:
272: public void testConstructorUsedBySuper2() throws Exception {
273: performTest("ConstructorUsedBySuper2");
274: }
275:
276: public void testConstructorUsedByThis() throws Exception {
277: performTest("ConstructorUsedByThis");
278: }
279:
280: public void testUnresolvableImportsAreNotUnused() throws Exception {
281: performTest("UnresolvableImportsAreNotUnused");
282: }
283:
284: public void testEnums() throws Exception {
285: performTest("Enums");
286: }
287:
288: public void testReadUseThrow() throws Exception {
289: performTest("ReadUseThrow");
290: }
291:
292: public void testGenericBoundIsClassUse() throws Exception {
293: performTest("GenericBoundIsClassUse");
294: }
295:
296: public void testBLE91246() throws Exception {
297: final boolean wasThrown[] = new boolean[1];
298: Logger.getLogger(Utilities.class.getName()).addHandler(
299: new Handler() {
300: public void publish(LogRecord lr) {
301: if (lr.getThrown() != null
302: && lr.getThrown().getClass() == BadLocationException.class) {
303: wasThrown[0] = true;
304: }
305: }
306:
307: public void close() {
308: }
309:
310: public void flush() {
311: }
312: });
313: performTest("BLE91246");
314:
315: assertFalse("BLE was not thrown", wasThrown[0]);
316: }
317:
318: public void testArrayAccess() throws Exception {
319: performTest("ArrayAccess");
320: }
321:
322: public void test88119() throws Exception {
323: performTest("package-info");
324: }
325:
326: public void test111113() throws Exception {
327: performTest("UnusedImport111113");
328: }
329:
330: public void test89356() throws Exception {
331: performTest("SerialVersionUID89356");
332: }
333:
334: public void testFullMemberSelect109886() throws Exception {
335: performTest("FullMemberSelect109886");
336: }
337:
338: public void testMultiFields116520a() throws Exception {
339: performTest("MultiFields");
340: }
341:
342: public void testMultiFields116520b() throws Exception {
343: performTest("MultiFields");
344: }
345:
346: public void testUnusedParameters() throws Exception {
347: performTest("UnusedParameters");
348: }
349:
350: public void testUsedInFor() throws Exception {
351: performTest("UsedInFor");
352: }
353:
354: public void testCastIsClassUse() throws Exception {
355: performTest("CastIsClassUse");
356: }
357:
358: public void testWildcardBoundIsClassUse() throws Exception {
359: performTest("WildcardBoundIsClassUse");
360: }
361:
362: public void testStaticImport128662() throws Exception {
363: performTest("StaticImport128662");
364: }
365:
366: private void performTest(String fileName) throws Exception {
367: performTest(fileName, new Performer() {
368: public void compute(CompilationController parameter,
369: Document doc, ErrorDescriptionSetter setter) {
370: SemanticHighlighter.ERROR_DESCRIPTION_SETTER = setter;
371:
372: new SemanticHighlighter(parameter.getFileObject())
373: .process(parameter, doc);
374: }
375: });
376: }
377:
378: public void testSimpleRemoveImport() throws Exception {
379: performRemoveUnusedImportTest("SimpleRemoveImport");
380: }
381:
382: public void testRemoveImportNotLine1() throws Exception {
383: performRemoveUnusedImportTest("RemoveImportNotLine1");
384: }
385:
386: // public void testRemoveImportNotLine2() throws Exception {
387: // performRemoveUnusedImportTest("RemoveImportNotLine2");
388: // }
389:
390: public void testRemoveImportDocStart() throws Exception {
391: performRemoveUnusedImportTest("RemoveImportDocStart");
392: }
393:
394: public void testRemoveImportTrim() throws Exception {
395: performRemoveUnusedImportTest("RemoveImportTrim");
396: }
397:
398: public void testRemoveImportDocStartTrim() throws Exception {
399: performRemoveUnusedImportTest("RemoveImportDocStartTrim");
400: }
401:
402: public void testRemoveAllImports() throws Exception {
403: performRemoveUnusedImportTest("RemoveAllImports", 2, 2, 0, 1);
404: }
405:
406: private FileObject testSourceFO;
407:
408: protected void performRemoveUnusedImportTest(String fileName)
409: throws Exception {
410: performRemoveUnusedImportTest(fileName, 1, 1, 0, 0);
411: }
412:
413: protected void performRemoveUnusedImportTest(String fileName,
414: int errorCount, int fixesCount, int errorToFix,
415: int fixToPerform) throws Exception {
416: SourceUtilsTestUtil
417: .prepareTest(
418: new String[] { "org/netbeans/modules/java/editor/resources/layer.xml" },
419: new Object[] { new MIMEResolverImpl() });
420:
421: FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this );
422: FileObject cache = scratch.createFolder("cache");
423:
424: File wd = getWorkDir();
425: File testSource = new File(wd, "test/" + fileName + ".java");
426:
427: testSource.getParentFile().mkdirs();
428:
429: File dataFolder = new File(getDataDir(),
430: "org/netbeans/modules/java/editor/semantic/data/");
431:
432: for (File f : dataFolder.listFiles()) {
433: copyToWorkDir(f, new File(wd, "test/" + f.getName()));
434: }
435:
436: testSourceFO = FileUtil.toFileObject(testSource);
437:
438: assertNotNull(testSourceFO);
439:
440: File testBuildTo = new File(wd, "test-build");
441:
442: testBuildTo.mkdirs();
443:
444: SourceUtilsTestUtil.prepareTest(FileUtil
445: .toFileObject(dataFolder), FileUtil
446: .toFileObject(testBuildTo), cache);
447: SourceUtilsTestUtil.compileRecursively(FileUtil
448: .toFileObject(dataFolder));
449:
450: final Document doc = getDocument(testSourceFO);
451:
452: JavaSource source = JavaSource.forFileObject(testSourceFO);
453:
454: assertNotNull(source);
455:
456: final List<ErrorDescription> errors = new ArrayList<ErrorDescription>();
457:
458: SemanticHighlighter.ErrorDescriptionSetter oldSetter = SemanticHighlighter.ERROR_DESCRIPTION_SETTER;
459:
460: try {
461: SemanticHighlighter.ERROR_DESCRIPTION_SETTER = new SemanticHighlighter.ErrorDescriptionSetter() {
462: public void setErrors(Document doc,
463: List<ErrorDescription> errs,
464: List<TreePathHandle> allUnusedImports) {
465: errors.addAll(errs);
466: }
467:
468: public void setHighlights(Document doc,
469: OffsetsBag highlights) {
470: }
471:
472: public void setColorings(Document doc,
473: Map<Token, Coloring> colorings,
474: Set<Token> addedTokens, Set<Token> removedTokens) {
475: }
476: };
477:
478: source.runUserActionTask(new Task<CompilationController>() {
479: public void run(CompilationController parameter) {
480: try {
481: parameter.toPhase(Phase.UP_TO_DATE);
482: new SemanticHighlighter(parameter
483: .getFileObject()).process(parameter,
484: doc);
485: } catch (IOException e) {
486: e.printStackTrace();
487: }
488: }
489: }, true);
490:
491: assertEquals(errors.toString(), errorCount, errors.size());
492:
493: List<Fix> fixes = errors.get(errorToFix).getFixes()
494: .getFixes();
495:
496: assertEquals(fixesCount, fixes.size());
497:
498: fixes.get(fixToPerform).implement();
499:
500: ref(doc.getText(0, doc.getLength()));
501:
502: compareReferenceFiles();
503: } finally {
504: SemanticHighlighter.ERROR_DESCRIPTION_SETTER = oldSetter;
505: }
506: }
507: }
|