CompileWithInvisibleSources.java revision 2958:27da0c3ac83a
1/*
2 * Copyright (c) 2014, 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 * @summary Verify that we can make sources invisible to linking (sourcepath)
29 * @bug 8054689
30 * @author Fredrik O
31 * @author sogoel (rewrite)
32 * @library /tools/lib
33 * @modules jdk.compiler/com.sun.tools.javac.api
34 *          jdk.compiler/com.sun.tools.javac.file
35 *          jdk.compiler/com.sun.tools.javac.main
36 *          jdk.compiler/com.sun.tools.sjavac
37 * @build Wrapper ToolBox
38 * @run main Wrapper CompileWithInvisibleSources
39 */
40
41import java.util.*;
42import java.nio.file.*;
43
44public class CompileWithInvisibleSources extends SJavacTester {
45    public static void main(String... args) throws Exception {
46        CompileWithInvisibleSources cis = new CompileWithInvisibleSources();
47        cis.test();
48    }
49
50    // Compile gensrc and link against gensrc2 and gensrc3
51    // gensrc2 contains broken code in beta.B, thus exclude that package
52    // gensrc3 contains a proper beta.B
53    void test() throws Exception {
54        clean(TEST_ROOT);
55        Files.createDirectories(BIN);
56        clean(GENSRC, GENSRC2, GENSRC3, BIN);
57
58        Map<String,Long> previous_bin_state = collectState(BIN);
59
60        ToolBox tb = new ToolBox();
61        tb.writeFile(GENSRC.resolve("alfa/omega/A.java"),
62                     "package alfa.omega; import beta.B; import gamma.C; public class A { B b; C c; }");
63        tb.writeFile(GENSRC2.resolve("beta/B.java"),
64                     "package beta; public class B { broken");
65        tb.writeFile(GENSRC2.resolve("gamma/C.java"),
66                     "package gamma; public class C { }");
67        tb.writeFile(GENSRC3.resolve("beta/B.java"),
68                     "package beta; public class B { }");
69
70        compile(GENSRC.toString(),
71                "-x", "beta",
72                "-sourcepath", GENSRC2.toString(),
73                "-sourcepath", GENSRC3.toString(),
74                "-d", BIN.toString(),
75                "-h", HEADERS.toString(),
76                "-j", "1",
77                SERVER_ARG);
78
79        System.out.println("The first compile went well!");
80        Map<String,Long> new_bin_state = collectState(BIN);
81        verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,
82                                     BIN + "/alfa/omega/A.class",
83                                     BIN + "/javac_state");
84
85        System.out.println("----- Compile with exluded beta went well!");
86        clean(BIN);
87        compileExpectFailure(GENSRC.toString(),
88                             "-sourcepath", GENSRC2.toString(),
89                             "-sourcepath", GENSRC3.toString(),
90                             "-d", BIN.toString(),
91                             "-h", HEADERS.toString(),
92                             "-j", "1",
93                             SERVER_ARG);
94
95        System.out.println("----- Compile without exluded beta failed, as expected! Good!");
96        clean(GENSRC, BIN);
97    }
98}
99