Path: blob/master/test/jdk/tools/launcher/LauncherMessageTest.java
41145 views
/*1* Copyright (c) 2017, 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*/2223/**24* @test25* @bug 816706326* @library /test/lib27* @modules jdk.compiler28* jdk.zipfs29* @build jdk.test.lib.Platform30* jdk.test.lib.util.FileUtils31* @run main LauncherMessageTest32* @summary LauncherHelper should not throw JNI error for LinkageError33*/3435import java.io.File;36import java.nio.file.Paths;37import java.util.ArrayList;38import java.util.List;39import jdk.test.lib.util.FileUtils;4041public class LauncherMessageTest {4243public static void main(String[] args) throws Exception {44String userDir = System.getProperty("user.dir", ".");45File testDir = new File(userDir, "test");46List<String> srcContent = new ArrayList<>();4748// Try to create a test directory before proceeding further49if (!testDir.mkdir()) {50throw new Exception("Test failed: unable to create"51+ " writable working directory "52+ testDir.getAbsolutePath());53}5455// Create test sub-directories for sources, classes and modules respectively56File srcA = new File(testDir.getPath(), "srcA");57srcA.mkdir();58File srcB = new File(testDir.getPath(), "srcB");59srcB.mkdir();60File classesA = new File(testDir.getPath(), "classesA");61classesA.mkdir();62File classesB = new File(testDir.getPath(), "classesB");63classesB.mkdir();64File modules = new File(testDir.getPath(), "modules");65modules.mkdir();6667// Define content and create module-info.java and corresponding source files68File modAinfo = new File(srcA.getPath(), "module-info.java");69srcContent.add("module mod.a { exports pkgA; }");70TestHelper.createFile(modAinfo, srcContent);7172File classA = new File(srcA.getPath(), "ClassA.java");73srcContent.clear();74srcContent.add("package pkgA; public class ClassA { }");75TestHelper.createFile(classA, srcContent);7677File modBinfo = new File(srcB.getPath(), "module-info.java");78srcContent.clear();79srcContent.add("module mod.b { requires mod.a; }");80TestHelper.createFile(modBinfo, srcContent);8182File classB = new File(srcB.getPath(), "ClassB.java");83srcContent.clear();84srcContent.add("package pkgB;");85srcContent.add("import pkgA.ClassA;");86srcContent.add("public class ClassB extends ClassA {");87srcContent.add("public static void main(String[] args) { } }");88TestHelper.createFile(classB, srcContent);8990// Compile all source files and create Jars91TestHelper.compile("-d", classesA.getPath(), classA.getPath(), modAinfo.getPath());92TestHelper.createJar("cf", Paths.get(modules.getPath(), "mod.a.jar").toString(),93"-C", classesA.getPath(), ".");94TestHelper.compile("-d", classesB.getPath(), "--module-path", modules.getPath(),95classB.getPath(), modBinfo.getPath());96TestHelper.createJar("cf", Paths.get(modules.getPath(), "mod.b.jar").toString(),97"-C", classesB.getPath(), ".");9899// Delete the module-info.java and Jar file corresponding to mod.a100FileUtils.deleteFileWithRetry(Paths.get(modAinfo.getPath()));101FileUtils.deleteFileWithRetry(Paths.get(modules.getPath(), "mod.a.jar"));102103// Re-create module-info.java (by removing "exports pkgA;")104// and corresponding Jar file105srcContent.clear();106srcContent.add("module mod.a { }");107TestHelper.createFile(modAinfo, srcContent);108TestHelper.compile("-d", classesA.getPath(), classA.getPath(), modAinfo.getPath());109TestHelper.createJar("cf", Paths.get(modules.getPath(), "mod.a.jar").toString(),110"-C", classesA.getPath(), ".");111112// Execute the main class113String[] commands = {TestHelper.javaCmd, "--module-path", modules.getPath(),114"-m", "mod.b/pkgB.ClassB"};115TestHelper.TestResult result = TestHelper.doExec(commands);116117// Clean the test directory and check test status118FileUtils.deleteFileTreeWithRetry(Paths.get(testDir.getPath()));119if (result.isOK()) {120throw new Exception("Test Passed Unexpectedly!");121} else {122result.testOutput.forEach(System.err::println);123if (result.contains("JNI error")) {124throw new Exception("Test Failed with JNI error!");125}126}127System.out.println("Test passes, failed with expected error message");128}129}130131132