Path: blob/master/test/langtools/tools/sjavac/ClasspathDependencies.java
41144 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 805471726* @summary Make sure changes of public API on classpath triggers recompilation27* @library /tools/lib28* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.main30* jdk.compiler/com.sun.tools.sjavac31* @build Wrapper toolbox.ToolBox toolbox.Assert32* @run main Wrapper ClasspathDependencies33*/343536import java.io.IOException;37import java.nio.file.FileVisitResult;38import java.nio.file.Files;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.nio.file.SimpleFileVisitor;42import java.nio.file.attribute.BasicFileAttributes;43import java.nio.file.attribute.FileTime;4445import static toolbox.Assert.check;4647public class ClasspathDependencies extends SjavacBase {4849public static void main(String... args) throws Exception {5051Path root = Paths.get(ClasspathDependencies.class.getSimpleName() + "Test");5253delete(root);5455Path src = root.resolve("src");56Path classes = root.resolve("classes");57Path srcDep = root.resolve("srcDep");58Path classesDep = root.resolve("classesDep");5960////////////////////////////////////////////////////////////////////////61headline("Create a test dependency, Dep.class, and put it in the classpath dir");62String depCode = "package dep; public class Dep { public void m1() {} }";63toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode);64int rc = compile("-d", classesDep, "--state-dir=" + classesDep, srcDep);65check(rc == 0, "Compilation failed unexpectedly");6667////////////////////////////////////////////////////////////////////////68headline("Compile and link against the Dep.class");69toolbox.writeFile(src.resolve("pkg/C.java"),70"package pkg;" +71"import dep.Dep;" +72"public class C { Dep dep; public void m() { new Dep().m1(); } }");73rc = compile("-d", classes, "--state-dir=" + classes, src, "-cp", classesDep);74check(rc == 0, "Compilation failed unexpectedly");75FileTime modTime1 = Files.getLastModifiedTime(classes.resolve("pkg/C.class"));7677////////////////////////////////////////////////////////////////////////78headline("Update dependency (without changing the public api)");79Thread.sleep(2000);80depCode = depCode.replaceAll("}$", "private void m2() {} }");81toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode);82rc = compile("-d", classesDep, "--state-dir=" + classesDep, srcDep);83check(rc == 0, "Compilation failed unexpectedly");8485////////////////////////////////////////////////////////////////////////86headline("Make sure that this does not trigger recompilation of C.java");87rc = compile("-d", classes, "--state-dir=" + classes, src, "-cp", classesDep);88check(rc == 0, "Compilation failed unexpectedly");89FileTime modTime2 = Files.getLastModifiedTime(classes.resolve("pkg/C.class"));90check(modTime1.equals(modTime2), "Recompilation erroneously triggered");9192////////////////////////////////////////////////////////////////////////93headline("Update public API of dependency");94Thread.sleep(2000);95depCode = depCode.replace("m1()", "m1(String... arg)");96toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode);97rc = compile("-d", classesDep, "--state-dir=" + classesDep, srcDep);98check(rc == 0, "Compilation failed unexpectedly");99100////////////////////////////////////////////////////////////////////////101headline("Make sure that recompilation of C.java is triggered");102rc = compile("-d", classes, "--state-dir=" + classes, src, "-cp", classesDep);103check(rc == 0, "Compilation failed unexpectedly");104FileTime modTime3 = Files.getLastModifiedTime(classes.resolve("pkg/C.class"));105check(modTime2.compareTo(modTime3) < 0, "Recompilation not triggered");106}107108static void headline(String str) {109System.out.println();110System.out.println(str);111System.out.println(str.replaceAll(".", "-"));112}113114static void delete(Path root) throws IOException {115if (!Files.exists(root))116return;117Files.walkFileTree(root, new SimpleFileVisitor<Path>() {118@Override119public FileVisitResult visitFile(Path f, BasicFileAttributes a)120throws IOException {121Files.delete(f);122return FileVisitResult.CONTINUE;123}124125@Override126public FileVisitResult postVisitDirectory(Path dir, IOException e)127throws IOException {128if (e != null)129throw e;130if (!dir.equals(root))131Files.delete(dir);132return FileVisitResult.CONTINUE;133}134});135}136}137138139