1 /*
   2  * Copyright (c) 2014, 2015, 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 /lib/testlibrary
  27  * @build LimitModsTest CompilerUtils jdk.testlibrary.*
  28  * @run testng LimitModsTest
  29  * @summary Basic tests for java -limitmods
  30  */
  31 
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 
  35 import static jdk.testlibrary.ProcessTools.*;
  36 
  37 import org.testng.annotations.BeforeTest;
  38 import org.testng.annotations.Test;
  39 import static org.testng.Assert.*;
  40 
  41 
  42 @Test
  43 public class LimitModsTest {
  44 
  45     private static final String TEST_SRC = System.getProperty("test.src");
  46 
  47     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  48     private static final Path MODS_DIR = Paths.get("mods");
  49 
  50     // the module name of the test module
  51     private static final String TEST_MODULE = "test";
  52 
  53     // the module main class
  54     private static final String MAIN_CLASS = "jdk.test.UseAWT";
  55 
  56 
  57     @BeforeTest
  58     public void compileTestModule() throws Exception {
  59 
  60         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  61         boolean compiled
  62             = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  63                                     MODS_DIR.resolve(TEST_MODULE));
  64 
  65         assertTrue(compiled, "test module did not compile");
  66     }
  67 
  68     /**
  69      * Run class path application with -limitmods
  70      */
  71     public void testUnnamedModule() throws Exception {
  72         String classpath = MODS_DIR.resolve(TEST_MODULE).toString();
  73 
  74         // java -limitmods java.base -cp mods/$TESTMODULE ...
  75         int exitValue1
  76             = executeTestJava("-limitmods", "java.base",
  77                               "-cp", classpath,
  78                               MAIN_CLASS)
  79                 .outputTo(System.out)
  80                 .errorTo(System.out)
  81                 .shouldContain("NoClassDefFoundError")
  82                 .getExitValue();
  83 
  84         assertTrue(exitValue1 != 0);
  85 
  86 
  87         // java -limitmods java.base -cp mods/$TESTMODULE ...
  88         int exitValue2
  89             = executeTestJava("-limitmods", "java.desktop",
  90                               "-cp", classpath,
  91                              MAIN_CLASS)
  92                 .outputTo(System.out)
  93                 .errorTo(System.out)
  94                 .getExitValue();
  95 
  96         assertTrue(exitValue2 == 0);
  97     }
  98 
  99     /**
 100      * Run named module with -limitmods
 101      */
 102     public void testNamedModule() throws Exception {
 103         String modulepath = MODS_DIR.toString();
 104         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 105 
 106         // java -limitmods java.base -mp mods -m $TESTMODULE/$MAINCLASS
 107         int exitValue
 108             = executeTestJava("-limitmods", "java.base",
 109                               "-mp", modulepath,
 110                               "-m", mid)
 111                 .outputTo(System.out)
 112                 .errorTo(System.out)
 113                 .getExitValue();
 114 
 115         assertTrue(exitValue == 0);
 116     }
 117 
 118 }