1 /* 2 * Copyright (c) 2018, 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 8198908 27 * @summary Check that preview minor version and --enable-preview are handled 28 * correctly. 29 * @modules java.base/jdk.internal.misc 30 * @library /test/lib 31 * @run main PreviewVersion 32 */ 33 34 import java.io.File; 35 import jdk.test.lib.compiler.InMemoryJavaCompiler; 36 import jdk.test.lib.ByteCodeLoader; 37 import jdk.test.lib.process.OutputAnalyzer; 38 import jdk.test.lib.process.ProcessTools; 39 40 public class PreviewVersion { 41 42 public static void main(String args[]) throws Throwable { 43 System.out.println("Regression test for bug 8198908"); 44 45 byte klassbuf[] = InMemoryJavaCompiler.compile("PVTest", 46 "public class PVTest { " + 47 "public static void main(String argv[]) { " + 48 "System.out.println(\"Hi!\"); } }"); 49 50 // Set class's minor version to 65535. 51 klassbuf[4] = -1; 52 klassbuf[5] = -1; 53 54 // Run the test. This should fail because --enable-preview is not specified. 55 ClassFileInstaller.writeClassToDisk("PVTest", klassbuf, System.getProperty("test.classes")); 56 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 57 "-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest"); 58 OutputAnalyzer oa = new OutputAnalyzer(pb.start()); 59 oa.shouldContain("Preview features are not enabled"); 60 oa.shouldHaveExitValue(1); 61 62 // This should be successful because --enable-preview is specified. 63 pb = ProcessTools.createJavaProcessBuilder("--enable-preview", 64 "-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest"); 65 oa = new OutputAnalyzer(pb.start()); 66 oa.shouldContain("Hi!"); 67 68 // Test -Xlog:class+preview 69 pb = ProcessTools.createJavaProcessBuilder("--enable-preview", "-Xlog:class+preview", 70 "-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest"); 71 oa = new OutputAnalyzer(pb.start()); 72 oa.shouldContain("[info][class,preview] Loading preview feature type PVTest"); 73 74 // Subtract 1 from class's major version. The class should fail to load 75 // because its major_version does not match the JVM current version. 76 int prev_major_version = Runtime.version().feature() - 1; 77 klassbuf[6] = (byte)((prev_major_version >> 8) & 0xff); 78 klassbuf[7] = (byte)(prev_major_version & 0xff); 79 try { 80 ByteCodeLoader.load("PVTest", klassbuf); 81 throw new RuntimeException("UnsupportedClassVersionError exception not thrown"); 82 } catch (java.lang.UnsupportedClassVersionError e) { 83 if (!e.getMessage().contains("compiled with preview features that are unsupported")) { 84 throw new RuntimeException( 85 "Wrong UnsupportedClassVersionError exception: " + e.getMessage()); 86 } 87 } 88 89 // Set class's major version to 45. The class should load because class 90 // version 45.65535 is valid. 91 klassbuf[6] = 0; 92 klassbuf[7] = 45; 93 try { 94 ByteCodeLoader.load("PVTest", klassbuf); 95 } catch (java.lang.UnsupportedClassVersionError e) { 96 throw new RuntimeException( 97 "Unexpected UnsupportedClassVersionError exception thrown: " + e.getMessage()); 98 } 99 100 // Check that a class with a recent older major version and a non-zero 101 // minor version fails to load. 102 klassbuf[6] = 0; 103 klassbuf[7] = 53; 104 klassbuf[4] = 0; 105 klassbuf[5] = 2; 106 try { 107 ByteCodeLoader.load("PVTest", klassbuf); 108 throw new RuntimeException("UnsupportedClassVersionError exception not thrown"); 109 } catch (java.lang.UnsupportedClassVersionError e) { 110 if (!e.getMessage().contains("was compiled with an invalid non-zero minor version")) { 111 throw new RuntimeException( 112 "Wrong UnsupportedClassVersionError exception: " + e.getMessage()); 113 } 114 } 115 } 116 }