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