2
|
1 |
/*
|
|
2 |
* Copyright 2004-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4989690 6259855
|
|
27 |
* @summary Check that version-related system property invariants hold.
|
|
28 |
* @author Martin Buchholz
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.*;
|
|
32 |
import java.net.URLClassLoader;
|
|
33 |
import java.net.URL;
|
|
34 |
|
|
35 |
public class Versions {
|
|
36 |
static String getProperty(String prop) throws Exception {
|
|
37 |
String value = System.getProperty(prop);
|
|
38 |
if (value == null)
|
|
39 |
throw new Exception("No such system property: " + prop);
|
|
40 |
System.out.printf("%s=%s%n", prop, value);
|
|
41 |
return value;
|
|
42 |
}
|
|
43 |
|
|
44 |
static ClassLoader cl;
|
|
45 |
|
|
46 |
static void checkClassVersion(int major, int minor, boolean expectSupported)
|
|
47 |
throws Exception
|
|
48 |
{
|
|
49 |
final String className = "ClassVersionTest";
|
|
50 |
final String classFile = className + ".class";
|
|
51 |
|
|
52 |
// We create an invalid class file, (only magic and version info),
|
|
53 |
// but the version info must be checked before the body.
|
|
54 |
final DataOutputStream dos =
|
|
55 |
new DataOutputStream(new FileOutputStream(classFile));
|
|
56 |
dos.writeLong((0xCafeBabel << 32) + (minor << 16) + major);
|
|
57 |
dos.close();
|
|
58 |
|
|
59 |
boolean supported = true;
|
|
60 |
try {
|
|
61 |
Class.forName(className, false, cl);
|
|
62 |
} catch (UnsupportedClassVersionError e) {
|
|
63 |
supported = false;
|
|
64 |
} catch (Throwable t) {
|
|
65 |
// We expect an Exception indicating invalid class file
|
|
66 |
}
|
|
67 |
new File(classFile).delete();
|
|
68 |
if (supported != expectSupported)
|
|
69 |
throw new Exception("Forgot to update java.class.version?");
|
|
70 |
}
|
|
71 |
|
|
72 |
public static void main(String [] args) throws Exception {
|
|
73 |
String classVersion = getProperty("java.class.version");
|
|
74 |
String javaVersion = getProperty("java.version");
|
|
75 |
String VMVersion = getProperty("java.vm.version");
|
|
76 |
String runtimeVersion = getProperty("java.runtime.version");
|
|
77 |
String specVersion = getProperty("java.specification.version");
|
|
78 |
|
|
79 |
if (! (javaVersion.startsWith(specVersion) &&
|
|
80 |
runtimeVersion.startsWith(specVersion)))
|
|
81 |
throw new Exception("Invalid version-related system properties");
|
|
82 |
|
|
83 |
//----------------------------------------------------------------
|
|
84 |
// Check that java.class.version is correct.
|
|
85 |
// Injecting a larger major or minor version number into a
|
|
86 |
// .class file should result in UnsupportedClassVersionError.
|
|
87 |
//----------------------------------------------------------------
|
|
88 |
String[] versions = classVersion.split("\\.");
|
|
89 |
int majorVersion = Integer.parseInt(versions[0]);
|
|
90 |
int minorVersion = Integer.parseInt(versions[1]);
|
|
91 |
System.out.printf("majorVersion=%s%n",majorVersion);
|
|
92 |
System.out.printf("minorVersion=%s%n",minorVersion);
|
|
93 |
|
|
94 |
// Look in ".", and *not* in CLASSPATH
|
|
95 |
cl = new URLClassLoader(new URL[]{new File("./").toURL()}, null);
|
|
96 |
|
|
97 |
checkClassVersion(majorVersion , minorVersion , true );
|
|
98 |
checkClassVersion(majorVersion + 1, minorVersion , false);
|
|
99 |
checkClassVersion(majorVersion , minorVersion + 1, false);
|
|
100 |
}
|
|
101 |
}
|