1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8131027
  27  * @summary Test Get FQNs
  28  * @library /tools/lib
  29  * @modules jdk.compiler/com.sun.tools.javac.api
  30  *          jdk.compiler/com.sun.tools.javac.main
  31  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
  32  * @build KullaTesting TestingInputStream Compiler
  33  * @run testng ComputeFQNsTest
  34  */
  35 
  36 import java.io.Writer;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Arrays;
  41 
  42 import jdk.jshell.SourceCodeAnalysis.QualifiedNames;
  43 import static org.testng.Assert.*;
  44 import org.testng.annotations.Test;
  45 
  46 @Test
  47 public class ComputeFQNsTest extends KullaTesting {
  48 
  49     private final Compiler compiler = new Compiler();
  50     private final Path outDir = Paths.get("ComputeFQNsTest");
  51 
  52     public void testAddImport() throws Exception {
  53         compiler.compile(outDir, "package test1; public class TestClass { }", "package test2; public class TestClass { }");
  54         String jarName = "test.jar";
  55         compiler.jar(outDir, jarName, "test1/TestClass.class", "test2/TestClass.class");
  56         addToClasspath(compiler.getPath(outDir).resolve(jarName));
  57 
  58         assertInferredFQNs("LinkedList", "java.util.LinkedList");
  59         assertInferredFQNs("ArrayList", "java.util.ArrayList");
  60         assertInferredFQNs("TestClass", "test1.TestClass", "test2.TestClass");
  61         assertInferredFQNs("CharSequence", "CharSequence".length(), true, "java.lang.CharSequence");
  62         assertInferredFQNs("unresolvable");
  63         assertInferredFQNs("void test(ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
  64         assertInferredFQNs("void test(ArrayList l) throws InvocationTargetException", "InvocationTargetException".length(), false, "java.lang.reflect.InvocationTargetException");
  65         assertInferredFQNs("void test(ArrayList l) { ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
  66         assertInferredFQNs("<T extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
  67         assertInferredFQNs("Object l = Arrays", "Arrays".length(), false, "java.util.Arrays");
  68         assertInferredFQNs("class X<T extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
  69         assertInferredFQNs("class X extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
  70         assertInferredFQNs("class X extends java.util.ArrayList<TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
  71         assertInferredFQNs("class X extends java.util.ArrayList<TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
  72         assertInferredFQNs("class X implements TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
  73         assertInferredFQNs("class X implements TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
  74         assertInferredFQNs("class X implements java.util.List<TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
  75         assertInferredFQNs("class X implements java.util.List<TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
  76         assertInferredFQNs("class X { ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
  77     }
  78 
  79     @Test(enabled = false) //TODO 8161165
  80     public void testSuspendIndexing() throws Throwable {
  81         compiler.compile(outDir, "package test; public class FQNTest { }");
  82         String jarName = "test.jar";
  83         compiler.jar(outDir, jarName, "test/FQNTest.class");
  84         Path continueMarkFile = outDir.resolve("continuemark").toAbsolutePath();
  85         Files.createDirectories(continueMarkFile.getParent());
  86         try (Writer w = Files.newBufferedWriter(continueMarkFile)) {}
  87 
  88         Path runMarkFile = outDir.resolve("runmark").toAbsolutePath();
  89         Files.deleteIfExists(runMarkFile);
  90 
  91         getState().sourceCodeAnalysis();
  92 
  93         Throwable[] evalException = new Throwable[1];
  94 
  95         new Thread() {
  96             @Override public void run() {
  97                 try {
  98                     assertEval("{new java.io.FileOutputStream(\"" + runMarkFile.toAbsolutePath().toString().replace("\\", "\\\\") + "\").close();" +
  99                                " while (java.nio.file.Files.exists(java.nio.file.Paths.get(\"" + continueMarkFile.toAbsolutePath().toString().replace("\\", "\\\\") + "\"))) Thread.sleep(100); }");
 100                 } catch (Throwable t) {
 101                     evalException[0] = t;
 102                 }
 103             }
 104         }.start();
 105 
 106         while (true) {
 107             if (Files.exists(runMarkFile))
 108                 break;
 109             try {
 110                 Thread.sleep(100);
 111             } catch (Throwable t) {
 112                 if (evalException[0] != null) {
 113                     evalException[0].addSuppressed(t);
 114                 } else {
 115                     throw t;
 116                 }
 117             }
 118             if (evalException[0] != null) {
 119                 throw evalException[0];
 120             }
 121         }
 122 
 123         addToClasspath(compiler.getPath(outDir).resolve(jarName));
 124 
 125         String code = "FQNTest";
 126 
 127         QualifiedNames candidates = getAnalysis().listQualifiedNames(code, code.length());
 128 
 129         assertEquals(candidates.getNames(), Arrays.asList(), "Input: " + code + ", candidates=" + candidates.getNames());
 130         assertEquals(candidates.isUpToDate(), false, "Input: " + code + ", up-to-date=" + candidates.isUpToDate());
 131 
 132         Files.delete(continueMarkFile);
 133 
 134         waitIndexingFinished();
 135 
 136         candidates = getAnalysis().listQualifiedNames(code, code.length());
 137 
 138         assertEquals(candidates.getNames(), Arrays.asList("test.FQNTest"), "Input: " + code + ", candidates=" + candidates.getNames());
 139         assertEquals(true, candidates.isUpToDate(), "Input: " + code + ", up-to-date=" + candidates.isUpToDate());
 140     }
 141 
 142 }