Path: blob/master/test/langtools/jdk/jshell/ComputeFQNsTest.java
40931 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 813102726* @summary Test Get FQNs27* @library /tools/lib28* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.main30* jdk.jshell/jdk.jshell:open31* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask32* @build KullaTesting TestingInputStream Compiler33* @run testng ComputeFQNsTest34*/3536import java.io.Writer;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.Arrays;4142import jdk.jshell.SourceCodeAnalysis.QualifiedNames;43import static org.testng.Assert.*;44import org.testng.annotations.Test;4546@Test47public class ComputeFQNsTest extends KullaTesting {4849private final Compiler compiler = new Compiler();50private final Path outDir = Paths.get("ComputeFQNsTest");5152public void testAddImport() throws Exception {53compiler.compile(outDir, "package test1; public class FQNTestClass { }", "package test2; public class FQNTestClass { }");54String jarName = "test.jar";55compiler.jar(outDir, jarName, "test1/FQNTestClass.class", "test2/FQNTestClass.class");56addToClasspath(compiler.getPath(outDir).resolve(jarName));5758assertInferredFQNs("LinkedList", "java.util.LinkedList");59assertInferredFQNs("ArrayList", "java.util.ArrayList");60assertInferredFQNs("FQNTestClass", "test1.FQNTestClass", "test2.FQNTestClass");61assertInferredFQNs("CharSequence", "CharSequence".length(), true, "java.lang.CharSequence");62assertInferredFQNs("unresolvable");63assertInferredFQNs("void test(ArrayList", "ArrayList".length(), false, "java.util.ArrayList");64assertInferredFQNs("void test(ArrayList l) throws InvocationTargetException", "InvocationTargetException".length(), false, "java.lang.reflect.InvocationTargetException");65assertInferredFQNs("void test(ArrayList l) { ArrayList", "ArrayList".length(), false, "java.util.ArrayList");66assertInferredFQNs("<T extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");67assertInferredFQNs("Object l = Arrays", "Arrays".length(), false, "java.util.Arrays");68assertInferredFQNs("class X<T extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");69assertInferredFQNs("class X extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");70assertInferredFQNs("class X extends java.util.ArrayList<TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");71assertInferredFQNs("class X extends java.util.ArrayList<TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");72assertInferredFQNs("class X implements TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");73assertInferredFQNs("class X implements TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");74assertInferredFQNs("class X implements java.util.List<TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");75assertInferredFQNs("class X implements java.util.List<TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");76assertInferredFQNs("class X { ArrayList", "ArrayList".length(), false, "java.util.ArrayList");77}7879@Test(enabled = false) //TODO 816116580public void testSuspendIndexing() throws Throwable {81compiler.compile(outDir, "package test; public class FQNTest { }");82String jarName = "test.jar";83compiler.jar(outDir, jarName, "test/FQNTest.class");84Path continueMarkFile = outDir.resolve("continuemark").toAbsolutePath();85Files.createDirectories(continueMarkFile.getParent());86try (Writer w = Files.newBufferedWriter(continueMarkFile)) {}8788Path runMarkFile = outDir.resolve("runmark").toAbsolutePath();89Files.deleteIfExists(runMarkFile);9091getState().sourceCodeAnalysis();9293Throwable[] evalException = new Throwable[1];9495new Thread() {96@Override public void run() {97try {98assertEval("{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) {101evalException[0] = t;102}103}104}.start();105106while (true) {107if (Files.exists(runMarkFile))108break;109try {110Thread.sleep(100);111} catch (Throwable t) {112if (evalException[0] != null) {113evalException[0].addSuppressed(t);114} else {115throw t;116}117}118if (evalException[0] != null) {119throw evalException[0];120}121}122123addToClasspath(compiler.getPath(outDir).resolve(jarName));124125String code = "FQNTest";126127QualifiedNames candidates = getAnalysis().listQualifiedNames(code, code.length());128129assertEquals(candidates.getNames(), Arrays.asList(), "Input: " + code + ", candidates=" + candidates.getNames());130assertEquals(candidates.isUpToDate(), false, "Input: " + code + ", up-to-date=" + candidates.isUpToDate());131132Files.delete(continueMarkFile);133134waitIndexingFinished();135136candidates = getAnalysis().listQualifiedNames(code, code.length());137138assertEquals(candidates.getNames(), Arrays.asList("test.FQNTest"), "Input: " + code + ", candidates=" + candidates.getNames());139assertEquals(true, candidates.isUpToDate(), "Input: " + code + ", up-to-date=" + candidates.isUpToDate());140}141142}143144145