1 19 package org.netbeans.modules.java.editor.semantic; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.List ; 26 import java.util.Set ; 27 import java.util.TreeSet ; 28 import java.util.concurrent.CountDownLatch ; 29 import java.util.logging.Handler ; 30 import java.util.logging.LogRecord ; 31 import java.util.logging.Logger ; 32 import javax.swing.text.BadLocationException ; 33 import javax.swing.text.Document ; 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 53 public class DetectorTest extends TestBase { 54 55 public DetectorTest(String testName) { 56 super(testName); 57 } 58 59 public void testUnusedImports() throws Exception { 60 performTest("UnusedImports"); 61 } 62 63 public void testColorings1() throws Exception { 64 performTest("Colorings1"); 65 } 66 67 public void testReadUseInstanceOf() throws Exception { 68 performTest("ReadUseInstanceOf"); 69 } 70 71 public void testReadUseTypeCast() throws Exception { 72 performTest("ReadUseTypeCast"); 73 } 74 75 public void testReadUseArrayIndex() throws Exception { 76 performTest("ReadUseArrayIndex"); 77 } 78 79 public void testReadUseUnaryOperator() throws Exception { 80 performTest("ReadUseUnaryOperator"); 81 } 82 83 public void testReadUseReturn() throws Exception { 84 performTest("ReadUseReturn"); 85 } 86 87 public void testCompoundPackage() throws Exception { 88 performTest("CompoundPackage"); 89 } 90 91 public void testSemanticInnerClasses() throws Exception { 92 performTest("SemanticInnerClasses"); 93 } 94 95 public void testForEach() throws Exception { 96 performTest("ForEach"); 97 } 98 99 public void testWriteUseArgument() throws Exception { 100 performTest("WriteUseArgument"); 101 } 102 103 public void testReturnType() throws Exception { 104 performTest("ReturnType"); 105 } 106 107 public void testFieldByThis1() throws Exception { 108 performTest("FieldByThis1"); 109 } 110 111 public void testFieldByThis2() throws Exception { 112 performTest("FieldByThis2"); 113 } 114 115 public void testWriteUseCatch() throws Exception { 116 performTest("WriteUseCatch"); 117 } 118 119 public void testReadWriteUseArgumentOfAbstractMethod() throws Exception { 120 performTest("ReadWriteUseArgumentOfAbstractMethod"); 121 } 122 123 public void testReadUseExprIsIdent1() throws Exception { 124 performTest("ReadUseExprIsIdent1"); 125 } 126 127 public void testReadUseExprIsIdent2() throws Exception { 128 performTest("ReadUseExprIsIdent2"); 129 } 130 131 public void testReadUseExprIsIdent3() throws Exception { 132 performTest("ReadUseExprIsIdent3"); 133 } 134 135 public void testReadUseExprIsIdent4() throws Exception { 136 performTest("ReadUseExprIsIdent4"); 137 } 138 139 public void testClassUseNewInstance() throws Exception { 140 performTest("ClassUseNewInstance"); 141 } 142 143 public void testExecUseMethodCall() throws Exception { 144 performTest("ExecUseMethodCall"); 145 } 146 147 public void testReadUseArrayInit() throws Exception { 148 performTest("ReadUseArrayInit"); 149 } 150 151 public void testReadUseNewArrayIndex() throws Exception { 152 performTest("ReadUseNewArrayIndex"); 153 } 154 155 public void testUsages2() throws Exception { 156 performTest("Usages2"); 157 } 158 159 public void testCommentedGenerics() throws Exception { 160 performTest("CommentedGenerics"); 161 } 162 163 public void testRetentionPolicy() throws Exception { 164 performTest("RetentionPolicyTest"); 165 } 166 167 public void testSimpleGeneric() throws Exception { 168 performTest("SimpleGeneric"); 169 } 170 171 public void testReadUseMathSet() throws Exception { 172 performTest("ReadUseMathSet"); 173 } 174 175 public void testReadUseMathSet2() throws Exception { 176 performTest("ReadUseMathSet2"); 177 } 178 179 public void testReadUseTernaryOperator() throws Exception { 180 performTest("ReadUseTernaryOperator"); 181 } 182 183 public void testUseInGenerics() throws Exception { 184 performTest("UseInGenerics"); 185 } 186 187 public void testFieldIsWritten1() throws Exception { 188 performTest("FieldIsWritten1"); 189 } 190 191 public void testFieldIsWritten2() throws Exception { 192 performTest("FieldIsWritten2"); 193 } 194 195 public void testConstructorsAreMethods() throws Exception { 196 performTest("ConstructorsAreMethods"); 197 } 198 199 public void testConstructorsAreMethods2() throws Exception { 200 performTest("ConstructorsAreMethods2"); 201 } 202 203 public void testDoubleBrackets() throws Exception { 204 performTest("DoubleBrackets"); 205 } 206 207 public void testConstructorsAreMethods3() throws Exception { 208 performTest("ConstructorsAreMethods3"); 209 } 210 211 public void testMethodWithArrayAtTheEnd() throws Exception { 212 performTest("MethodWithArrayAtTheEnd"); 213 } 214 215 public void testReadUseAssert() throws Exception { 216 performTest("ReadUseAssert"); 217 } 218 219 public void testSuperIsKeyword() throws Exception { 220 performTest("SuperIsKeyword"); 221 } 222 223 public void testNewArrayIsClassUse() throws Exception { 224 performTest("NewArrayIsClassUse"); 225 } 226 227 public void testNotKeywords() throws Exception { 228 performTest("NotKeywords"); 229 } 230 231 public void testArrayThroughInitializer() throws Exception { 232 performTest("ArrayThroughInitializer"); 233 } 234 235 public void testReadUseAssert2() throws Exception { 236 performTest("ReadUseAssert2"); 237 } 238 239 public void testConstructorUsedBySuper1() throws Exception { 240 performTest("ConstructorUsedBySuper1"); 241 } 242 243 public void testConstructorUsedBySuper2() throws Exception { 244 performTest("ConstructorUsedBySuper2"); 245 } 246 247 public void testConstructorUsedByThis() throws Exception { 248 performTest("ConstructorUsedByThis"); 249 } 250 251 public void testUnresolvableImportsAreNotUnused() throws Exception { 252 performTest("UnresolvableImportsAreNotUnused"); 253 } 254 255 public void testEnums() throws Exception { 256 performTest("Enums"); 257 } 258 259 public void testReadUseThrow() throws Exception { 260 performTest("ReadUseThrow"); 261 } 262 263 public void testGenericBoundIsClassUse() throws Exception { 264 performTest("GenericBoundIsClassUse"); 265 } 266 267 public void testBLE91246() throws Exception { 268 final boolean wasThrown[] = new boolean[1]; 269 Logger.getLogger(Utilities.class.getName()).addHandler(new Handler () { 270 public void publish(LogRecord lr) { 271 if (lr.getThrown() != null && lr.getThrown().getClass() == BadLocationException .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 { 284 performTest("ArrayAccess"); 285 } 286 287 private void performTest(String fileName) throws Exception { 288 performTest(fileName, new Performer() { 289 public Collection <Highlight> compute(CompilationController parameter, Document doc) { 290 return new SemanticHighlighter(parameter.getFileObject()).process(parameter, doc); 291 } 292 }); 293 } 294 295 public void testSimpleRemoveImport() throws Exception { 296 performRemoveUnusedImportTest("SimpleRemoveImport"); 297 } 298 299 public void testRemoveImportNotLine1() throws Exception { 300 performRemoveUnusedImportTest("RemoveImportNotLine1"); 301 } 302 303 307 public void testRemoveImportDocStart() throws Exception { 308 performRemoveUnusedImportTest("RemoveImportDocStart"); 309 } 310 311 public void testRemoveImportTrim() throws Exception { 312 performRemoveUnusedImportTest("RemoveImportTrim"); 313 } 314 315 public void testRemoveImportDocStartTrim() throws Exception { 316 performRemoveUnusedImportTest("RemoveImportDocStartTrim"); 317 } 318 319 public void testRemoveAllImports() throws Exception { 320 performRemoveUnusedImportTest("RemoveAllImports", 2, 2, 0, 1); 321 } 322 323 private FileObject testSourceFO; 324 325 protected void performRemoveUnusedImportTest(String fileName) throws Exception { 326 performRemoveUnusedImportTest(fileName, 1, 1, 0, 0); 327 } 328 329 protected void performRemoveUnusedImportTest(String fileName, int errorCount, int fixesCount, int errorToFix, int fixToPerform) throws Exception { 330 SourceUtilsTestUtil.prepareTest(new String [] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object [0]); 331 332 FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this); 333 FileObject cache = scratch.createFolder("cache"); 334 335 File wd = getWorkDir(); 336 File testSource = new File (wd, "test/" + fileName + ".java"); 337 338 testSource.getParentFile().mkdirs(); 339 340 File dataFolder = new File (getDataDir(), "org/netbeans/modules/java/editor/semantic/data/"); 341 342 for (File f : dataFolder.listFiles()) { 343 copyToWorkDir(f, new File (wd, "test/" + f.getName())); 344 } 345 346 testSourceFO = FileUtil.toFileObject(testSource); 347 348 assertNotNull(testSourceFO); 349 350 File testBuildTo = new File (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 doc = getDocument(testSourceFO); 358 final Set <Highlight> highlights = new TreeSet <Highlight>(new HighlightComparator()); 359 360 JavaSource source = JavaSource.forFileObject(testSourceFO); 361 362 assertNotNull(source); 363 364 final List <ErrorDescription> errors = new ArrayList <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 doc, List <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 e) { 382 e.printStackTrace(); 383 } 384 } 385 }, true); 386 387 assertEquals(errors.toString(), errorCount, errors.size()); 388 389 List <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 |