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