AddReadsTest.java revision 13901:b2a69d66dc65
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 * @library /lib/testlibrary
27 * @modules jdk.compiler
28 * @build AddReadsTest CompilerUtils jdk.testlibrary.*
29 * @run testng AddReadsTest
30 * @summary Basic tests for java -XaddReads
31 */
32
33import java.nio.file.Path;
34import java.nio.file.Paths;
35
36import jdk.testlibrary.OutputAnalyzer;
37import static jdk.testlibrary.ProcessTools.*;
38
39import org.testng.annotations.BeforeTest;
40import org.testng.annotations.DataProvider;
41import org.testng.annotations.Test;
42import static org.testng.Assert.*;
43
44/**
45 * The tests consists of two modules: m1 and junit.
46 * Code in module m1 calls into code in module junit but the module-info.java
47 * does not have a 'requires'. Instead a read edge is added via the command
48 * line option -XaddReads.
49 */
50
51@Test
52public class AddReadsTest {
53
54    private static final String TEST_SRC = System.getProperty("test.src");
55
56    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
57    private static final Path CLASSES_DIR = Paths.get("classes");
58    private static final Path MODS_DIR = Paths.get("mods");
59
60    private static final String MAIN = "m1/p.Main";
61
62
63    @BeforeTest
64    public void setup() throws Exception {
65
66        // javac -d classes src/junit/**
67        assertTrue(CompilerUtils
68            .compile(SRC_DIR.resolve("junit"), CLASSES_DIR));
69
70        // javac -d mods/m1 -cp classes/ src/m1/**
71        assertTrue(CompilerUtils
72            .compile(SRC_DIR.resolve("m1"),
73                    MODS_DIR.resolve("m1"),
74                    "-cp", CLASSES_DIR.toString(),
75                    "-XaddReads:m1=ALL-UNNAMED"));
76
77        // jar cf mods/junit.jar -C classes .
78        JarUtils.createJarFile(MODS_DIR.resolve("junit.jar"), CLASSES_DIR);
79
80    }
81
82    private OutputAnalyzer run(String... options) throws Exception {
83        return executeTestJava(options)
84            .outputTo(System.out)
85            .errorTo(System.out);
86    }
87
88
89    /**
90     * Run with junit as a module on the module path.
91     */
92    public void testJUnitOnModulePath() throws Exception {
93
94        // java -mp mods -addmods junit -XaddReads:m1=junit -m ..
95        int exitValue
96            = run("-mp", MODS_DIR.toString(),
97                  "-addmods", "junit",
98                  "-XaddReads:m1=junit",
99                  "-m", MAIN)
100                .getExitValue();
101
102        assertTrue(exitValue == 0);
103    }
104
105
106    /**
107     * Exercise -XaddReads:m1=ALL-UNNAMED by running with junit on the
108     * class path.
109     */
110    public void testJUnitOnClassPath() throws Exception {
111
112        // java -mp mods -cp mods/junit.jar -XaddReads:m1=ALL-UNNAMED -m ..
113        String cp = MODS_DIR.resolve("junit.jar").toString();
114        int exitValue
115            = run("-mp", MODS_DIR.toString(),
116                  "-cp", cp,
117                  "-XaddReads:m1=ALL-UNNAMED",
118                  "-m", MAIN)
119                .getExitValue();
120
121        assertTrue(exitValue == 0);
122    }
123
124
125    /**
126     * Run with junit as a module on the module path but without -XaddReads.
127     */
128    public void testJUnitOnModulePathMissingAddReads() throws Exception {
129        // java -mp mods -addmods junit -m ..
130        int exitValue
131            = run("-mp", MODS_DIR.toString(),
132                  "-addmods", "junit",
133                  "-m", MAIN)
134                .shouldContain("IllegalAccessError")
135                .getExitValue();
136
137        assertTrue(exitValue != 0);
138    }
139
140
141    /**
142     * Run with junit on the class path but without -XaddReads.
143     */
144    public void testJUnitOnClassPathMissingAddReads() throws Exception {
145        // java -mp mods -cp mods/junit.jar -m ..
146        String cp = MODS_DIR.resolve("junit.jar").toString();
147        int exitValue
148            = run("-mp", MODS_DIR.toString(),
149                  "-cp", cp,
150                  "-m", MAIN)
151                .shouldContain("IllegalAccessError")
152                .getExitValue();
153
154        assertTrue(exitValue != 0);
155    }
156
157
158    /**
159     * Exercise -XaddReads with a more than one source module.
160     */
161    public void testJUnitWithMultiValueOption() throws Exception {
162
163        int exitValue
164            = run("-mp", MODS_DIR.toString(),
165                  "-addmods", "java.xml,junit",
166                  "-XaddReads:m1=java.xml,junit",
167                  "-m", MAIN)
168                .getExitValue();
169
170        assertTrue(exitValue == 0);
171    }
172
173
174    /**
175     * Exercise -XaddReads where the target module is specified more than once
176     */
177    public void testWithTargetSpecifiedManyTimes() throws Exception {
178
179        int exitValue
180            = run("-mp", MODS_DIR.toString(),
181                "-addmods", "java.xml,junit",
182                "-XaddReads:m1=java.xml",
183                "-XaddReads:m1=junit",
184                "-m", MAIN)
185                .shouldContain("specified more than once")
186                .getExitValue();
187
188        assertTrue(exitValue != 0);
189    }
190
191
192    /**
193     * Exercise -XaddReads with bad values
194     */
195    @Test(dataProvider = "badvalues")
196    public void testWithBadValue(String value, String ignore) throws Exception {
197
198        //  -XaddExports:$VALUE -version
199        assertTrue(run("-XaddReads:" + value, "-version").getExitValue() != 0);
200    }
201
202    @DataProvider(name = "badvalues")
203    public Object[][] badValues() {
204        return new Object[][]{
205
206            { "java.base",                  null }, // missing source
207            { "java.monkey=java.base",      null }, // unknown module
208            { "java.base=sun.monkey",       null }, // unknown source
209
210        };
211    }
212}
213