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