1/*
2 * Copyright (c) 2015, 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 /test/lib
27 * @modules java.se
28 * @build ListModsTest jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.*
29 * @run testng ListModsTest
30 * @summary Basic test for java --list-modules
31 */
32
33import java.nio.file.Path;
34import java.nio.file.Paths;
35
36import jdk.test.lib.compiler.CompilerUtils;
37import jdk.testlibrary.ProcessTools;
38import jdk.testlibrary.OutputAnalyzer;
39
40import org.testng.annotations.BeforeTest;
41import org.testng.annotations.Test;
42import static org.testng.Assert.*;
43
44/**
45 * Basic tests for java --list-modules
46 */
47
48public class ListModsTest {
49
50    private static final String TEST_SRC = System.getProperty("test.src");
51    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
52    private static final Path MODS_DIR = Paths.get("mods");
53    private static final Path UPGRADEMODS_DIR = Paths.get("upgrademods");
54
55    @BeforeTest
56    public void setup() throws Exception {
57        boolean compiled;
58
59        // javac -d mods/m1 --module-path mods src/m1/**
60        compiled = CompilerUtils.compile(
61                SRC_DIR.resolve("m1"),
62                MODS_DIR.resolve("m1"));
63        assertTrue(compiled);
64
65        // javac -d upgrademods/java.transaction --module-path mods src/java.transaction/**
66        compiled = CompilerUtils.compile(
67                SRC_DIR.resolve("java.transaction"),
68                UPGRADEMODS_DIR.resolve("java.transaction"));
69        assertTrue(compiled);
70    }
71
72    @Test
73    public void testListAll() throws Exception {
74        exec("--list-modules")
75                .shouldContain("java.base")
76                .shouldContain("java.xml")
77                .shouldHaveExitValue(0);
78    }
79
80    @Test
81    public void testListWithModulePath() throws Exception {
82        exec("--list-modules", "--module-path", MODS_DIR.toString())
83                .shouldContain("java.base")
84                .shouldContain("m1")
85                .shouldHaveExitValue(0);
86    }
87
88    @Test
89    public void testListWithUpgradeModulePath() throws Exception {
90        String dir = UPGRADEMODS_DIR.toString();
91        exec("--list-modules", "--upgrade-module-path", dir)
92                .shouldContain(UPGRADEMODS_DIR.toString())
93                .shouldHaveExitValue(0);
94    }
95
96    @Test
97    public void testListWithLimitMods1() throws Exception {
98        exec("--limit-modules", "java.management.rmi", "--list-modules")
99                .shouldContain("java.rmi")
100                .shouldContain("java.base")
101                .shouldNotContain("java.scripting")
102                .shouldHaveExitValue(0);
103    }
104
105    @Test
106    public void testListWithLimitMods2() throws Exception {
107        exec("--list-modules",
108                    "--module-path", MODS_DIR.toString(),
109                    "--limit-modules", "java.management")
110                .shouldContain("java.base")
111                .shouldNotContain("m1")
112                .shouldHaveExitValue(0);
113    }
114
115    /**
116     * java -version --list-modules => should print version and exit
117     */
118    @Test
119    public void testListWithPrintVersion1() throws Exception {
120        exec("-version", "--list-modules")
121                .shouldNotContain("java.base")
122                .shouldContain("Runtime Environment")
123                .shouldHaveExitValue(0);
124    }
125
126    /**
127     * java --list-modules -version => should list modules and exit
128     */
129    @Test
130    public void testListWithPrintVersion2() throws Exception {
131        exec("--list-modules", "-version")
132                .shouldContain("java.base")
133                .shouldNotContain("Runtime Environment")
134                .shouldHaveExitValue(0);
135    }
136
137    /**
138     * java args... returning the OutputAnalyzer to analyzer the output
139     */
140    private OutputAnalyzer exec(String... args) throws Exception {
141        return ProcessTools.executeTestJava(args)
142                .outputTo(System.out)
143                .errorTo(System.out);
144    }
145
146}
147