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