ComputeFQNsTest.java revision 3792:d516975e8110
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 *          jdk.jshell/jdk.jshell:open
32 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
33 * @build KullaTesting TestingInputStream Compiler
34 * @run testng ComputeFQNsTest
35 */
36
37import java.io.Writer;
38import java.nio.file.Files;
39import java.nio.file.Path;
40import java.nio.file.Paths;
41import java.util.Arrays;
42
43import jdk.jshell.SourceCodeAnalysis.QualifiedNames;
44import static org.testng.Assert.*;
45import org.testng.annotations.Test;
46
47@Test
48public class ComputeFQNsTest extends KullaTesting {
49
50    private final Compiler compiler = new Compiler();
51    private final Path outDir = Paths.get("ComputeFQNsTest");
52
53    public void testAddImport() throws Exception {
54        compiler.compile(outDir, "package test1; public class TestClass { }", "package test2; public class TestClass { }");
55        String jarName = "test.jar";
56        compiler.jar(outDir, jarName, "test1/TestClass.class", "test2/TestClass.class");
57        addToClasspath(compiler.getPath(outDir).resolve(jarName));
58
59        assertInferredFQNs("LinkedList", "java.util.LinkedList");
60        assertInferredFQNs("ArrayList", "java.util.ArrayList");
61        assertInferredFQNs("TestClass", "test1.TestClass", "test2.TestClass");
62        assertInferredFQNs("CharSequence", "CharSequence".length(), true, "java.lang.CharSequence");
63        assertInferredFQNs("unresolvable");
64        assertInferredFQNs("void test(ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
65        assertInferredFQNs("void test(ArrayList l) throws InvocationTargetException", "InvocationTargetException".length(), false, "java.lang.reflect.InvocationTargetException");
66        assertInferredFQNs("void test(ArrayList l) { ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
67        assertInferredFQNs("<T extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
68        assertInferredFQNs("Object l = Arrays", "Arrays".length(), false, "java.util.Arrays");
69        assertInferredFQNs("class X<T extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
70        assertInferredFQNs("class X extends ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
71        assertInferredFQNs("class X extends java.util.ArrayList<TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
72        assertInferredFQNs("class X extends java.util.ArrayList<TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
73        assertInferredFQNs("class X implements TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
74        assertInferredFQNs("class X implements TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
75        assertInferredFQNs("class X implements java.util.List<TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
76        assertInferredFQNs("class X implements java.util.List<TypeMirror, TypeElement", "TypeElement".length(), false, "javax.lang.model.element.TypeElement");
77        assertInferredFQNs("class X { ArrayList", "ArrayList".length(), false, "java.util.ArrayList");
78    }
79
80    @Test(enabled = false) //TODO 8161165
81    public void testSuspendIndexing() throws Throwable {
82        compiler.compile(outDir, "package test; public class FQNTest { }");
83        String jarName = "test.jar";
84        compiler.jar(outDir, jarName, "test/FQNTest.class");
85        Path continueMarkFile = outDir.resolve("continuemark").toAbsolutePath();
86        Files.createDirectories(continueMarkFile.getParent());
87        try (Writer w = Files.newBufferedWriter(continueMarkFile)) {}
88
89        Path runMarkFile = outDir.resolve("runmark").toAbsolutePath();
90        Files.deleteIfExists(runMarkFile);
91
92        getState().sourceCodeAnalysis();
93
94        Throwable[] evalException = new Throwable[1];
95
96        new Thread() {
97            @Override public void run() {
98                try {
99                    assertEval("{new java.io.FileOutputStream(\"" + runMarkFile.toAbsolutePath().toString().replace("\\", "\\\\") + "\").close();" +
100                               " while (java.nio.file.Files.exists(java.nio.file.Paths.get(\"" + continueMarkFile.toAbsolutePath().toString().replace("\\", "\\\\") + "\"))) Thread.sleep(100); }");
101                } catch (Throwable t) {
102                    evalException[0] = t;
103                }
104            }
105        }.start();
106
107        while (true) {
108            if (Files.exists(runMarkFile))
109                break;
110            try {
111                Thread.sleep(100);
112            } catch (Throwable t) {
113                if (evalException[0] != null) {
114                    evalException[0].addSuppressed(t);
115                } else {
116                    throw t;
117                }
118            }
119            if (evalException[0] != null) {
120                throw evalException[0];
121            }
122        }
123
124        addToClasspath(compiler.getPath(outDir).resolve(jarName));
125
126        String code = "FQNTest";
127
128        QualifiedNames candidates = getAnalysis().listQualifiedNames(code, code.length());
129
130        assertEquals(candidates.getNames(), Arrays.asList(), "Input: " + code + ", candidates=" + candidates.getNames());
131        assertEquals(candidates.isUpToDate(), false, "Input: " + code + ", up-to-date=" + candidates.isUpToDate());
132
133        Files.delete(continueMarkFile);
134
135        waitIndexingFinished();
136
137        candidates = getAnalysis().listQualifiedNames(code, code.length());
138
139        assertEquals(candidates.getNames(), Arrays.asList("test.FQNTest"), "Input: " + code + ", candidates=" + candidates.getNames());
140        assertEquals(true, candidates.isUpToDate(), "Input: " + code + ", up-to-date=" + candidates.isUpToDate());
141    }
142
143}
144