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