test/jdk/tools/launcher/modules/basic/LauncherErrors.java
changeset 55233 6c2d69a41ff9
equal deleted inserted replaced
55232:9b392b044f64 55233:6c2d69a41ff9
       
     1 /*
       
     2  * Copyright (c) 2019, 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  * @bug 8221368
       
    27  * @library /test/lib
       
    28  * @modules jdk.compiler
       
    29  * @build LauncherErrors jdk.test.lib.compiler.CompilerUtils
       
    30  * @run testng LauncherErrors
       
    31  * @summary Test launcher error message when fails to launch main class
       
    32  */
       
    33 
       
    34 
       
    35 import java.nio.file.Path;
       
    36 import java.nio.file.Paths;
       
    37 
       
    38 import jdk.test.lib.compiler.CompilerUtils;
       
    39 import jdk.test.lib.process.ProcessTools;
       
    40 import org.testng.annotations.BeforeTest;
       
    41 import org.testng.annotations.Test;
       
    42 import static org.testng.Assert.*;
       
    43 
       
    44 public class LauncherErrors {
       
    45     // test source location
       
    46     private static final String TEST_SRC = System.getProperty("test.src");
       
    47     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
       
    48 
       
    49     // module path
       
    50     private static final Path MODS_DIR = Paths.get("mods");
       
    51 
       
    52     // the module name of the test module
       
    53     private static final String TEST_MODULE = "test2";
       
    54     // the main class of the test module
       
    55     private static final String MAIN_CLASS = "jdk.test2.Main";
       
    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                                   "--add-exports", "java.base/com.sun.crypto.provider=" + TEST_MODULE);
       
    65 
       
    66         assertTrue(compiled, "test module did not compile");
       
    67     }
       
    68 
       
    69     /*
       
    70      * Run jdk.test2.Main without security manager.
       
    71      */
       
    72     @Test
       
    73     public void test() throws Exception {
       
    74         String dir = MODS_DIR.toString();
       
    75         String mid = TEST_MODULE + "/" + MAIN_CLASS;
       
    76 
       
    77         ProcessTools.executeTestJava("--module-path", dir, "--module", mid)
       
    78                     .outputTo(System.out)
       
    79                     .errorTo(System.out)
       
    80                     .shouldHaveExitValue(0);
       
    81     }
       
    82 
       
    83     /*
       
    84      * Run jdk.test2.Main with security manager such that main class will
       
    85      * fail to initialize.
       
    86      */
       
    87     @Test
       
    88     public void testErrorMessage() throws Exception {
       
    89         String dir = MODS_DIR.toString();
       
    90         String mid = TEST_MODULE + "/" + MAIN_CLASS;
       
    91 
       
    92         ProcessTools.executeTestJava("-Djava.security.manager", "--module-path", dir, "--module", mid)
       
    93                     .outputTo(System.out)
       
    94                     .errorTo(System.out)
       
    95                     .shouldContain("Error: Unable to initialize main class " + MAIN_CLASS + " in module " + TEST_MODULE)
       
    96                     .shouldContain("Caused by: java.security.AccessControlException: access denied")
       
    97                     .shouldNotHaveExitValue(0);
       
    98     }
       
    99 
       
   100 }