1/*
2 * Copyright (c) 2014, 2017, 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.jlink/jdk.tools.jmod
28 *          jdk.compiler
29 * @build AddModsTest CompilerUtils jdk.testlibrary.*
30 * @run testng AddModsTest
31 * @summary Basic test for java --add-modules
32 */
33
34import java.io.File;
35import java.nio.file.Path;
36import java.nio.file.Paths;
37
38import static jdk.testlibrary.ProcessTools.*;
39
40import org.testng.annotations.BeforeTest;
41import org.testng.annotations.Test;
42import static org.testng.Assert.*;
43
44
45@Test
46public class AddModsTest {
47
48    private static final String TEST_SRC = System.getProperty("test.src");
49
50    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
51    private static final Path MODS1_DIR = Paths.get("mods1");
52    private static final Path MODS2_DIR = Paths.get("mods2");
53
54    // test module / main class
55    private static final String TEST_MODULE = "test";
56    private static final String TEST_MAIN_CLASS = "test.Main";
57    private static final String TEST_MID = TEST_MODULE + "/" + TEST_MAIN_CLASS;
58
59    // logger module
60    private static final String LOGGER_MODULE = "logger";
61
62
63    @BeforeTest
64    public void compile() throws Exception {
65        // javac -d mods1/test src/test/**
66        boolean compiled = CompilerUtils.compile(
67            SRC_DIR.resolve(TEST_MODULE),
68            MODS1_DIR.resolve(TEST_MODULE)
69        );
70        assertTrue(compiled, "test did not compile");
71
72        // javac -d mods1/logger src/logger/**
73        compiled= CompilerUtils.compile(
74            SRC_DIR.resolve(LOGGER_MODULE),
75            MODS2_DIR.resolve(LOGGER_MODULE)
76        );
77        assertTrue(compiled, "test did not compile");
78    }
79
80
81    /**
82     * Basic test of --add-modules ALL-DEFAULT. Module java.sql should be
83     * resolved and the types in that module should be visible.
84     */
85    public void testAddDefaultModules1() throws Exception {
86
87        // java --add-modules ALL-DEFAULT --module-path mods1 -m test ...
88        int exitValue
89            = executeTestJava("--module-path", MODS1_DIR.toString(),
90                              "--add-modules", "ALL-DEFAULT",
91                              "-m", TEST_MID,
92                              "java.sql.Connection")
93                .outputTo(System.out)
94                .errorTo(System.out)
95                .getExitValue();
96
97        assertTrue(exitValue == 0);
98    }
99
100    /**
101     * Basic test of --add-modules ALL-DEFAULT. Module java.xml.ws.annotation
102     * should not resolved and so the types in that module should not be
103     * visible.
104     */
105    public void testAddDefaultModules2() throws Exception {
106
107        // java --add-modules ALL-DEFAULT --module-path mods1 -m test ...
108        int exitValue
109            = executeTestJava("--module-path", MODS1_DIR.toString(),
110                              "--add-modules", "ALL-DEFAULT",
111                              "-m", TEST_MID,
112                              "javax.annotation.Generated")
113                .outputTo(System.out)
114                .errorTo(System.out)
115                .shouldContain("ClassNotFoundException")
116                .getExitValue();
117
118        assertTrue(exitValue != 0);
119    }
120
121    /**
122     * Basic test of --add-modules ALL-SYSTEM. All system modules should be resolved
123     * and thus all types in those modules should be visible.
124     */
125    public void testAddSystemModules() throws Exception {
126
127        // java --add-modules ALL-SYSTEM --module-path mods1 -m test ...
128        int exitValue
129            = executeTestJava("--module-path", MODS1_DIR.toString(),
130                              "--add-modules", "ALL-SYSTEM",
131                              "-m", TEST_MID,
132                              "java.sql.Connection",
133                              "javax.annotation.Generated")
134                .outputTo(System.out)
135                .errorTo(System.out)
136                .getExitValue();
137
138        assertTrue(exitValue == 0);
139    }
140
141
142    /**
143     * Run test on class path to load a type in a module on the application
144     * module path, uses {@code --add-modules logger}.
145     */
146    public void testRunWithAddMods() throws Exception {
147
148        // java --module-path mods --add-modules logger -cp classes test.Main
149        String classpath = MODS1_DIR.resolve(TEST_MODULE).toString();
150        String modulepath = MODS2_DIR.toString();
151        int exitValue
152            = executeTestJava("--module-path", modulepath,
153                              "--add-modules", LOGGER_MODULE,
154                              "-cp", classpath,
155                              TEST_MAIN_CLASS,
156                              "logger.Logger")
157                .outputTo(System.out)
158                .errorTo(System.out)
159                .getExitValue();
160
161        assertTrue(exitValue == 0);
162    }
163
164     /**
165      * Run application on class path that makes use of module on the
166      * application module path. Does not use --add-modules and so should
167      * fail at run-time.
168      */
169     public void testRunMissingAddMods() throws Exception {
170
171         // java --module-path mods -cp classes test.Main
172         String classpath = MODS1_DIR.resolve(TEST_MODULE).toString();
173         String modulepath = MODS1_DIR.toString();
174         int exitValue
175             = executeTestJava("--module-path", modulepath,
176                               "-cp", classpath,
177                               TEST_MAIN_CLASS,
178                               "logger.Logger")
179                 .outputTo(System.out)
180                 .errorTo(System.out)
181                 .shouldContain("ClassNotFoundException")
182                 .getExitValue();
183
184         assertTrue(exitValue != 0);
185     }
186
187
188    /**
189     * Run test on class path to load a type in a module on the application
190     * module path, uses {@code --add-modules ALL-MODULE-PATH}.
191     */
192    public void testAddAllModulePath() throws Exception {
193
194        // java --module-path mods --add-modules ALL-MODULE-PATH -cp classes test.Main
195        String classpath = MODS1_DIR.resolve(TEST_MODULE).toString();
196        String modulepath = MODS1_DIR.toString();
197        int exitValue
198            = executeTestJava("--module-path", modulepath,
199                              "--add-modules", "ALL-MODULE-PATH",
200                              "-cp", classpath,
201                              TEST_MAIN_CLASS)
202                .outputTo(System.out)
203                .errorTo(System.out)
204                .getExitValue();
205
206        assertTrue(exitValue == 0);
207    }
208
209
210    /**
211     * Test {@code --add-modules ALL-MODULE-PATH} without {@code --module-path}.
212     */
213    public void testAddAllModulePathWithNoModulePath() throws Exception {
214
215        // java --add-modules ALL-MODULE-PATH -version
216        int exitValue
217            = executeTestJava("--add-modules", "ALL-MODULE-PATH",
218                              "-version")
219                .outputTo(System.out)
220                .errorTo(System.out)
221                .getExitValue();
222
223        assertTrue(exitValue == 0);
224    }
225
226
227    /**
228     * Tests {@code --add-modules} be specified more than once.
229     */
230    public void testWithMultipleAddModules() throws Exception {
231
232        String modulepath = MODS1_DIR.toString() + File.pathSeparator +
233                                MODS2_DIR.toString();
234        int exitValue
235            = executeTestJava("--module-path", modulepath,
236            "--add-modules", LOGGER_MODULE,
237            "--add-modules", TEST_MODULE,
238            "-m", TEST_MID,
239            "logger.Logger")
240            .outputTo(System.out)
241            .errorTo(System.out)
242            .getExitValue();
243
244        assertTrue(exitValue == 0);
245    }
246
247
248    /**
249     * Attempt to run with a bad module name specified to --add-modules
250     */
251    public void testRunWithBadAddMods() throws Exception {
252
253        // java --module-path mods --add-modules DoesNotExist -m test ...
254        int exitValue
255            = executeTestJava("--module-path", MODS1_DIR.toString(),
256                              "--add-modules", "DoesNotExist",
257                              "-m", TEST_MID)
258                .outputTo(System.out)
259                .errorTo(System.out)
260                .shouldContain("DoesNotExist")
261                .getExitValue();
262
263        assertTrue(exitValue != 0);
264    }
265
266}
267