1 /*
   2  * Copyright (c) 2017, 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 8167063
  27  * @library /test/lib
  28  * @build jdk.test.lib.util.FileUtils
  29  *        jdk.test.lib.Utils
  30  *        jdk.test.lib.Asserts
  31  *        jdk.test.lib.JDKToolFinder
  32  *        jdk.test.lib.JDKToolLauncher
  33  *        jdk.test.lib.Platform
  34  *        jdk.test.lib.process.*
  35  * @run main LauncherMessageTest
  36  * @summary LauncherHelper should not throw JNI error for LinkageError
  37  */
  38 
  39 import java.io.File;
  40 import java.nio.file.Paths;
  41 import java.util.ArrayList;
  42 import java.util.List;
  43 import jdk.test.lib.util.FileUtils;
  44 
  45 public class LauncherMessageTest {
  46 
  47     public static void main(String[] args) throws Exception {
  48         String userDir = System.getProperty("user.dir", ".");
  49         File testDir = new File(userDir, "test");
  50         List<String> srcContent = new ArrayList<>();
  51 
  52         // Try to create a test directory before proceeding further
  53         if (!testDir.mkdir()) {
  54             throw new Exception("Test failed: unable to create"
  55                     + " writable working directory "
  56                     + testDir.getAbsolutePath());
  57         }
  58 
  59         // Create test sub-directories for sources, classes and modules respectively
  60         File srcA = new File(testDir.getPath(), "srcA");
  61         srcA.mkdir();
  62         File srcB = new File(testDir.getPath(), "srcB");
  63         srcB.mkdir();
  64         File classesA = new File(testDir.getPath(), "classesA");
  65         classesA.mkdir();
  66         File classesB = new File(testDir.getPath(), "classesB");
  67         classesB.mkdir();
  68         File modules = new File(testDir.getPath(), "modules");
  69         modules.mkdir();
  70 
  71         // Define content and create module-info.java and corresponding source files
  72         File modAinfo = new File(srcA.getPath(), "module-info.java");
  73         srcContent.add("module mod.a { exports pkgA; }");
  74         TestHelper.createFile(modAinfo, srcContent);
  75 
  76         File classA = new File(srcA.getPath(), "ClassA.java");
  77         srcContent.clear();
  78         srcContent.add("package pkgA; public class ClassA { }");
  79         TestHelper.createFile(classA, srcContent);
  80 
  81         File modBinfo = new File(srcB.getPath(), "module-info.java");
  82         srcContent.clear();
  83         srcContent.add("module mod.b { requires mod.a; }");
  84         TestHelper.createFile(modBinfo, srcContent);
  85 
  86         File classB = new File(srcB.getPath(), "ClassB.java");
  87         srcContent.clear();
  88         srcContent.add("package pkgB;");
  89         srcContent.add("import pkgA.ClassA;");
  90         srcContent.add("public class ClassB extends ClassA {");
  91         srcContent.add("public static void main(String[] args) { } }");
  92         TestHelper.createFile(classB, srcContent);
  93 
  94         // Compile all source files and create Jars
  95         TestHelper.compile("-d", classesA.getPath(), classA.getPath(), modAinfo.getPath());
  96         TestHelper.createJar("cf", Paths.get(modules.getPath(), "mod.a.jar").toString(),
  97                 "-C", classesA.getPath(), ".");
  98         TestHelper.compile("-d", classesB.getPath(), "--module-path", modules.getPath(),
  99                 classB.getPath(), modBinfo.getPath());
 100         TestHelper.createJar("cf", Paths.get(modules.getPath(), "mod.b.jar").toString(),
 101                 "-C", classesB.getPath(), ".");
 102 
 103         // Delete the module-info.java and Jar file corresponding to mod.a
 104         FileUtils.deleteFileWithRetry(Paths.get(modAinfo.getPath()));
 105         FileUtils.deleteFileWithRetry(Paths.get(modules.getPath(), "mod.a.jar"));
 106 
 107         // Re-create module-info.java (by removing "exports pkgA;")
 108         // and corresponding Jar file
 109         srcContent.clear();
 110         srcContent.add("module mod.a { }");
 111         TestHelper.createFile(modAinfo, srcContent);
 112         TestHelper.compile("-d", classesA.getPath(), classA.getPath(), modAinfo.getPath());
 113         TestHelper.createJar("cf", Paths.get(modules.getPath(), "mod.a.jar").toString(),
 114                 "-C", classesA.getPath(), ".");
 115 
 116         // Execute the main class
 117         String[] commands = {TestHelper.javaCmd, "--module-path", modules.getPath(),
 118             "-m", "mod.b/pkgB.ClassB"};
 119         TestHelper.TestResult result = TestHelper.doExec(commands);
 120 
 121         // Clean the test directory and check test status
 122         FileUtils.deleteFileTreeWithRetry(Paths.get(testDir.getPath()));
 123         if (result.isOK()) {
 124             throw new Exception("Test Passed Unexpectedly!");
 125         } else {
 126             result.testOutput.forEach(System.err::println);
 127             if (result.contains("JNI error")) {
 128                 throw new Exception("Test Failed with JNI error!");
 129             }
 130         }
 131         System.out.println("Test passes, failed with expected error message");
 132     }
 133 }