Path: blob/master/test/jdk/java/lang/Class/forName/modules/TestDriver.java
41159 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.UncheckedIOException;25import java.nio.file.Files;26import java.nio.file.Path;27import java.nio.file.Paths;28import java.util.Arrays;29import java.util.stream.Stream;3031import jdk.test.lib.util.FileUtils;32import jdk.test.lib.compiler.CompilerUtils;33import static jdk.test.lib.process.ProcessTools.*;3435import org.testng.annotations.BeforeClass;36import org.testng.annotations.Test;37import static org.testng.Assert.assertTrue;3839/**40* @test41* @bug 808733542* @summary Tests for Class.forName(Module,String)43* @library /test/lib44* @modules jdk.compiler45* @build jdk.test.lib.Platform46* jdk.test.lib.util.FileUtils47* jdk.test.lib.compiler.CompilerUtils48* jdk.test.lib.process.ProcessTools49* TestDriver TestMain TestLayer50* @run testng TestDriver51*/5253public class TestDriver {5455private static final String TEST_SRC =56Paths.get(System.getProperty("test.src")).toString();57private static final String TEST_CLASSES =58Paths.get(System.getProperty("test.classes")).toString();5960private static final Path MOD_SRC_DIR = Paths.get(TEST_SRC, "src");61private static final Path MOD_DEST_DIR = Paths.get("mods");6263private static final String[] modules = new String[] {"m1", "m2", "m3"};6465/**66* Compiles all modules used by the test.67*/68@BeforeClass69public void setup() throws Exception {70assertTrue(CompilerUtils.compile(71MOD_SRC_DIR, MOD_DEST_DIR,72"--module-source-path",73MOD_SRC_DIR.toString()));7475copyDirectories(MOD_DEST_DIR.resolve("m1"), Paths.get("mods1"));76copyDirectories(MOD_DEST_DIR.resolve("m2"), Paths.get("mods2"));77}7879@Test80public void test() throws Exception {81String[] options = new String[] {82"-cp", TEST_CLASSES,83"--module-path", MOD_DEST_DIR.toString(),84"--add-modules", String.join(",", modules),85"-m", "m2/p2.test.Main"86};87runTest(options);88}8990@Test91public void testUnnamedModule() throws Exception {92String[] options = new String[] {93"-cp", TEST_CLASSES,94"--module-path", MOD_DEST_DIR.toString(),95"--add-modules", String.join(",", modules),96"TestMain"97};98runTest(options);99}100101@Test102public void testLayer() throws Exception {103String[] options = new String[] {104"-cp", TEST_CLASSES,105"TestLayer"106};107108runTest(options);109}110111@Test112public void testDeniedClassLoaderAccess() throws Exception {113String[] options = new String[] {114"--module-path", MOD_DEST_DIR.toString(),115"--add-modules", String.join(",", modules),116"-Djava.security.manager=allow",117"-m", "m3/p3.NoGetClassLoaderAccess"118};119assertTrue(executeTestJava(options)120.outputTo(System.out)121.errorTo(System.err)122.getExitValue() == 0);123}124125@Test126public void testDeniedAccess() throws Exception {127Path policyFile = Paths.get(TEST_SRC, "policy.denied");128129String[] options = new String[] {130"-Djava.security.manager",131"-Djava.security.policy=" + policyFile.toString(),132"--module-path", MOD_DEST_DIR.toString(),133"--add-modules", String.join(",", modules),134"-m", "m3/p3.NoAccess"135};136assertTrue(executeTestJava(options)137.outputTo(System.out)138.errorTo(System.err)139.getExitValue() == 0);140}141142private String[] runWithSecurityManager(String[] options) {143Path policyFile = Paths.get(TEST_SRC, "policy");144Stream<String> opts = Stream.concat(Stream.of("-Djava.security.manager",145"-Djava.security.policy=" + policyFile.toString()),146Arrays.stream(options));147return opts.toArray(String[]::new);148}149150private void runTest(String[] options) throws Exception {151assertTrue(executeTestJava(options)152.outputTo(System.out)153.errorTo(System.err)154.getExitValue() == 0);155156assertTrue(executeTestJava(runWithSecurityManager(options))157.outputTo(System.out)158.errorTo(System.err)159.getExitValue() == 0);160}161162private void copyDirectories(Path source, Path dest) throws IOException {163if (Files.exists(dest))164FileUtils.deleteFileTreeWithRetry(dest);165Files.walk(source, Integer.MAX_VALUE)166.filter(Files::isRegularFile)167.forEach(p -> {168try {169Path to = dest.resolve(source.relativize(p));170Files.createDirectories(to.getParent());171Files.copy(p, to);172} catch (IOException e) {173throw new UncheckedIOException(e);174}175});176}177}178179180