ClasspathDependencies.java revision 2958:27da0c3ac83a
1/*
2 * Copyright (c) 2015, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * @test
28 * @bug 8054717
29 * @summary Make sure changes of public API on classpath triggers recompilation
30 * @library /tools/lib
31 * @modules jdk.compiler/com.sun.tools.javac.api
32 *          jdk.compiler/com.sun.tools.javac.file
33 *          jdk.compiler/com.sun.tools.javac.main
34 *          jdk.compiler/com.sun.tools.sjavac
35 * @build Wrapper ToolBox
36 * @run main Wrapper ClasspathDependencies
37 */
38
39import static com.sun.tools.javac.util.Assert.check;
40
41import java.io.IOException;
42import java.nio.file.FileVisitResult;
43import java.nio.file.Files;
44import java.nio.file.Path;
45import java.nio.file.Paths;
46import java.nio.file.SimpleFileVisitor;
47import java.nio.file.attribute.BasicFileAttributes;
48import java.nio.file.attribute.FileTime;
49
50public class ClasspathDependencies extends SjavacBase {
51
52    static final String server = "--server:portfile=testserver,background=false";
53
54    public static void main(String... args) throws Exception {
55
56        Path root = Paths.get(ClasspathDependencies.class.getSimpleName() + "Test");
57
58        delete(root);
59
60        Path src = root.resolve("src");
61        Path classes = root.resolve("classes");
62        Path srcDep = root.resolve("srcDep");
63        Path classesDep = root.resolve("classesDep");
64
65        ////////////////////////////////////////////////////////////////////////
66        headline("Create a test dependency, Dep.class, and put it in the classpath dir");
67        String depCode = "package dep; public class Dep { public void m1() {} }";
68        toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode);
69        int rc = compile(server, "-d", classesDep, srcDep);
70        check(rc == 0, "Compilation failed unexpectedly");
71
72        ////////////////////////////////////////////////////////////////////////
73        headline("Compile and link against the Dep.class");
74        toolbox.writeFile(src.resolve("pkg/C.java"),
75                          "package pkg;" +
76                          "import dep.Dep;" +
77                          "public class C { Dep dep; public void m() { new Dep().m1(); } }");
78        rc = compile(server, "-d", classes, src, "-cp", classesDep);
79        check(rc == 0, "Compilation failed unexpectedly");
80        FileTime modTime1 = Files.getLastModifiedTime(classes.resolve("pkg/C.class"));
81
82        ////////////////////////////////////////////////////////////////////////
83        headline("Update dependency (without changing the public api)");
84        Thread.sleep(2000);
85        depCode = depCode.replaceAll("}$", "private void m2() {} }");
86        toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode);
87        rc = compile(server, "-d", classesDep, srcDep);
88        check(rc == 0, "Compilation failed unexpectedly");
89
90        ////////////////////////////////////////////////////////////////////////
91        headline("Make sure that this does not trigger recompilation of C.java");
92        rc = compile(server, "-d", classes, src, "-cp", classesDep);
93        check(rc == 0, "Compilation failed unexpectedly");
94        FileTime modTime2 = Files.getLastModifiedTime(classes.resolve("pkg/C.class"));
95        check(modTime1.equals(modTime2), "Recompilation erroneously triggered");
96
97        ////////////////////////////////////////////////////////////////////////
98        headline("Update public API of dependency");
99        Thread.sleep(2000);
100        depCode = depCode.replace("m1()", "m1(String... arg)");
101        toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode);
102        rc = compile(server, "-d", classesDep, srcDep);
103        check(rc == 0, "Compilation failed unexpectedly");
104
105        ////////////////////////////////////////////////////////////////////////
106        headline("Make sure that recompilation of C.java is triggered");
107        rc = compile(server, "-d", classes, src, "-cp", classesDep);
108        check(rc == 0, "Compilation failed unexpectedly");
109        FileTime modTime3 = Files.getLastModifiedTime(classes.resolve("pkg/C.class"));
110        check(modTime2.compareTo(modTime3) < 0, "Recompilation not triggered");
111    }
112
113    static void headline(String str) {
114        System.out.println();
115        System.out.println(str);
116        System.out.println(str.replaceAll(".", "-"));
117    }
118
119    static void delete(Path root) throws IOException {
120        if (!Files.exists(root))
121            return;
122        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
123                 @Override
124                 public FileVisitResult visitFile(Path f, BasicFileAttributes a)
125                         throws IOException {
126                     Files.delete(f);
127                     return FileVisitResult.CONTINUE;
128                 }
129
130                 @Override
131                 public FileVisitResult postVisitDirectory(Path dir, IOException e)
132                         throws IOException {
133                     if (e != null)
134                         throw e;
135                     if (!dir.equals(root))
136                         Files.delete(dir);
137                     return FileVisitResult.CONTINUE;
138                 }
139            });
140    }
141
142}
143