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 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 static 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 
  72 
  73     @Test
  74     public void testListAll() throws Exception {
  75         OutputAnalyzer output
  76             = executeTestJava("--list-modules")
  77                 .outputTo(System.out)
  78                 .errorTo(System.out);
  79         output.shouldContain("java.base");
  80         output.shouldContain("java.xml");
  81         assertTrue(output.getExitValue() == 0);
  82     }
  83 
  84 
  85     @Test
  86     public void testListOneModule() throws Exception {
  87         OutputAnalyzer output
  88             = executeTestJava("--list-modules=java.base")
  89                 .outputTo(System.out)
  90                 .errorTo(System.out);
  91         output.shouldContain("java.base");
  92         output.shouldContain("exports java.lang");
  93         assertTrue(output.getExitValue() == 0);
  94     }
  95 
  96 
  97     @Test
  98     public void testListTwoModules() throws Exception {
  99         OutputAnalyzer output
 100             = executeTestJava("--list-modules", "java.base,java.xml")
 101                 .outputTo(System.out)
 102                 .errorTo(System.out);
 103         output.shouldContain("java.base");
 104         output.shouldContain("exports java.lang");
 105         output.shouldContain("java.xml");
 106         output.shouldContain("exports javax.xml");
 107         assertTrue(output.getExitValue() == 0);
 108     }
 109 
 110 
 111     @Test
 112     public void testListUnknownModule() throws Exception {
 113         OutputAnalyzer output
 114             = executeTestJava("--list-modules", "java.rhubarb")
 115                 .outputTo(System.out)
 116                 .errorTo(System.out);
 117         output.shouldNotContain("java.base");
 118         output.shouldContain("java.rhubarb not observable");
 119         assertTrue(output.getExitValue() == 0);
 120     }
 121 
 122 
 123     @Test
 124     public void testListWithModulePath() throws Exception {
 125         OutputAnalyzer output
 126             = executeTestJava("--module-path", MODS_DIR.toString(), "--list-modules")
 127                 .outputTo(System.out)
 128                 .errorTo(System.out);
 129         output.shouldContain("java.base");
 130         output.shouldContain("m1");
 131         assertTrue(output.getExitValue() == 0);
 132     }
 133 
 134 
 135     @Test
 136     public void testListWithUpgradeModulePath() throws Exception {
 137         OutputAnalyzer output
 138             = executeTestJava("--upgrade-module-path", UPGRADEMODS_DIR.toString(),
 139                               "--list-modules", "java.transaction")
 140                 .outputTo(System.out)
 141                 .errorTo(System.out);
 142         output.shouldContain("exports javax.transaction.atomic");
 143         assertTrue(output.getExitValue() == 0);
 144     }
 145 
 146 
 147     @Test
 148     public void testListWithLimitMods1() throws Exception {
 149         OutputAnalyzer output
 150             = executeTestJava("--limit-modules", "java.management.rmi", "--list-modules")
 151                 .outputTo(System.out)
 152                 .errorTo(System.out);
 153         output.shouldContain("java.rmi");
 154         output.shouldContain("java.base");
 155         output.shouldNotContain("java.scripting");
 156         assertTrue(output.getExitValue() == 0);
 157     }
 158 
 159 
 160     @Test
 161     public void testListWithLimitMods2() throws Exception {
 162         OutputAnalyzer output
 163             = executeTestJava("--module-path", MODS_DIR.toString(),
 164                               "--limit-modules", "java.management",
 165                               "--list-modules")
 166                 .outputTo(System.out)
 167                 .errorTo(System.out);
 168         output.shouldContain("java.base");
 169         output.shouldNotContain("m1");
 170         assertTrue(output.getExitValue() == 0);
 171     }
 172 
 173 
 174     /**
 175      * java -version --list-modules => should print version and exit
 176      */
 177     @Test
 178     public void testListWithPrintVersion1() throws Exception {
 179         OutputAnalyzer output
 180             = executeTestJava("-version", "--list-modules")
 181                 .outputTo(System.out)
 182                 .errorTo(System.out);
 183         output.shouldNotContain("java.base");
 184         output.shouldContain("Runtime Environment");
 185         assertTrue(output.getExitValue() == 0);
 186     }
 187 
 188 
 189     /**
 190      * java --list-modules -version => should list modules and exit
 191      */
 192     @Test
 193     public void testListWithPrintVersion2() throws Exception {
 194         OutputAnalyzer output
 195             = executeTestJava("--list-modules", "-version")
 196                 .outputTo(System.out)
 197                 .errorTo(System.out);
 198         output.shouldContain("java.base");
 199         output.shouldNotContain("Runtime Environment");
 200         assertTrue(output.getExitValue() == 0);
 201     }
 202 
 203 }