Path: blob/master/test/jdk/java/lang/System/Versions.java
41149 views
/*1* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4989690 6259855 6706299 820456526* @summary Check that version-related system property invariants hold.27* @author Martin Buchholz28*/2930import java.io.*;31import java.net.URLClassLoader;32import java.net.URL;3334public class Versions {35static String getProperty(String prop) throws Exception {36String value = System.getProperty(prop);37if (value == null)38throw new Exception("No such system property: " + prop);39System.out.printf("%s=%s%n", prop, value);40return value;41}4243static ClassLoader cl;4445static void checkClassVersion(int major, int minor, boolean expectSupported)46throws Exception47{48final String className = "ClassVersionTest";49final String classFile = className + ".class";5051// We create an invalid class file, (only magic and version info),52// but the version info must be checked before the body.53final DataOutputStream dos =54new DataOutputStream(new FileOutputStream(classFile));55dos.writeLong((0xCafeBabel << 32) + (minor << 16) + major);56dos.close();5758boolean supported = true;59try {60Class.forName(className, false, cl);61} catch (UnsupportedClassVersionError e) {62supported = false;63} catch (Throwable t) {64// We expect an Exception indicating invalid class file65}66new File(classFile).delete();67if (supported != expectSupported)68throw new Exception("Forgot to update java.class.version?");69}7071public static void main(String [] args) throws Exception {72String classVersion = getProperty("java.class.version");73String javaVersion = getProperty("java.version");74String runtimeVersion = getProperty("java.runtime.version");75String specVersion = getProperty("java.specification.version");76String vmSpecVersion = getProperty("java.vm.specification.version");77String featureVersion = Integer.toString(Runtime.version().feature());7879if (! (javaVersion.startsWith(specVersion) &&80runtimeVersion.startsWith(specVersion) &&81specVersion.equals(featureVersion) &&82vmSpecVersion.equals(featureVersion))) {83throw new Exception("Invalid version-related system properties");84}8586//----------------------------------------------------------------87// Check that java.class.version is correct.88// Injecting a larger major or minor version number into a89// .class file should result in UnsupportedClassVersionError.90//----------------------------------------------------------------91String[] versions = classVersion.split("\\.");92int majorVersion = Integer.parseInt(versions[0]);93int minorVersion = Integer.parseInt(versions[1]);94System.out.printf("majorVersion=%s%n",majorVersion);95System.out.printf("minorVersion=%s%n",minorVersion);9697// Look in ".", and *not* in CLASSPATH98cl = new URLClassLoader(new URL[]{new File("./").toURL()}, null);99100checkClassVersion(majorVersion , minorVersion , true );101checkClassVersion(majorVersion + 1, minorVersion , false);102checkClassVersion(majorVersion , minorVersion + 1, false);103}104}105106107