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