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