1 /* 2 * Copyright (c) 2016, 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 * @requires vm.aot 27 * @library /test/lib / 28 * @requires vm.bits == "64" & (os.arch == "amd64" | os.arch == "x86_64") 29 * @modules java.base/jdk.internal.misc 30 * @build compiler.aot.verification.ClassAndLibraryNotMatchTest 31 * @run driver compiler.aot.verification.ClassAndLibraryNotMatchTest 32 * @summary check if class and aot library are properly bound to each other 33 */ 34 35 package compiler.aot.verification; 36 37 import compiler.aot.AotCompiler; 38 import java.io.File; 39 import java.io.IOException; 40 import java.nio.file.Files; 41 import java.nio.file.Paths; 42 import java.nio.file.StandardOpenOption; 43 import java.util.Arrays; 44 import jdk.test.lib.JDKToolFinder; 45 import jdk.test.lib.Utils; 46 import jdk.test.lib.process.OutputAnalyzer; 47 import jdk.test.lib.process.ProcessTools; 48 49 public class ClassAndLibraryNotMatchTest { 50 private static final String HELLO_WORLD_CLASS_NAME = "HelloWorld"; 51 private static final String LIB_NAME = "lib" + HELLO_WORLD_CLASS_NAME + ".so"; 52 private static final String HELLO_WORLD_MSG1 = "HelloWorld1"; 53 private static final String HELLO_WORLD_MSG2 = "HelloWorld2"; 54 private static final String HELLO_WORLD_FILE = "./" + HELLO_WORLD_CLASS_NAME + ".java"; 55 private static final String HELLO_WORLD_PRE = "public class " 56 + HELLO_WORLD_CLASS_NAME + " {\n" 57 + " public static void main(String args[]) {\n" 58 + " System.out.println(\""; 59 private static final String HELLO_WORLD_POST = "\");\n" 60 + " }\n" 61 + "}\n"; 62 63 public static void main(String args[]) { 64 new ClassAndLibraryNotMatchTest().runTest(); 65 } 66 67 private void writeHelloWorld(String message) { 68 String src = HELLO_WORLD_PRE + message + HELLO_WORLD_POST; 69 try{ 70 Files.write(Paths.get(HELLO_WORLD_FILE), src.getBytes(), StandardOpenOption.CREATE); 71 } catch (IOException e) { 72 throw new Error("Can't write HelloWorld " + e, e); 73 } 74 } 75 76 private void compileHelloWorld() { 77 String javac = JDKToolFinder.getCompileJDKTool("javac"); 78 ProcessBuilder pb = new ProcessBuilder(javac, HELLO_WORLD_FILE); 79 OutputAnalyzer oa; 80 try { 81 oa = ProcessTools.executeProcess(pb); 82 } catch (Exception e) { 83 throw new Error("Can't compile class " + e, e); 84 } 85 oa.shouldHaveExitValue(0); 86 } 87 88 private void compileAotLibrary() { 89 AotCompiler.launchCompiler(LIB_NAME, HELLO_WORLD_CLASS_NAME, 90 Arrays.asList("-classpath", Utils.TEST_CLASS_PATH + File.pathSeparator + "."), null); 91 } 92 93 private void runAndCheckHelloWorld(String checkString) { 94 ProcessBuilder pb; 95 try { 96 pb = ProcessTools.createJavaProcessBuilder(true, "-cp", ".", 97 "-XX:+UseAOT", "-XX:AOTLibrary=./" + LIB_NAME, 98 HELLO_WORLD_CLASS_NAME); 99 } catch (Exception e) { 100 throw new Error("Can't create ProcessBuilder to run " 101 + HELLO_WORLD_CLASS_NAME + " " + e, e); 102 } 103 OutputAnalyzer oa; 104 try { 105 oa = ProcessTools.executeProcess(pb); 106 } catch (Exception e) { 107 throw new Error("Can't execute " + HELLO_WORLD_CLASS_NAME + " " + e, e); 108 } 109 oa.shouldHaveExitValue(0); 110 oa.shouldContain(checkString); 111 } 112 113 private void createHelloWorld(String msg) { 114 writeHelloWorld(msg); 115 compileHelloWorld(); 116 } 117 118 private void runTest() { 119 createHelloWorld(HELLO_WORLD_MSG1); 120 compileAotLibrary(); 121 runAndCheckHelloWorld(HELLO_WORLD_MSG1); 122 createHelloWorld(HELLO_WORLD_MSG2); 123 runAndCheckHelloWorld(HELLO_WORLD_MSG2); 124 } 125 }