jdk/test/java/lang/System/Versions.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 689 10d8df51c03a
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 689
diff changeset
     2
 * Copyright 2004-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
689
10d8df51c03a 6706299: System property java.class.version should be 51 for jdk7
sherman
parents: 2
diff changeset
    26
 * @bug 4989690 6259855 6706299
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that version-related system property invariants hold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Versions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static String getProperty(String prop) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        String value = System.getProperty(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            throw new Exception("No such system property: " + prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        System.out.printf("%s=%s%n", prop, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static ClassLoader cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static void checkClassVersion(int major, int minor, boolean expectSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final String className  = "ClassVersionTest";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final String classFile  = className + ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // We create an invalid class file, (only magic and version info),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // but the version info must be checked before the body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        final DataOutputStream dos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            new DataOutputStream(new FileOutputStream(classFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        dos.writeLong((0xCafeBabel << 32) + (minor << 16) + major);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        boolean supported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            Class.forName(className, false, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } catch (UnsupportedClassVersionError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            supported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // We expect an Exception indicating invalid class file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        new File(classFile).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (supported != expectSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new Exception("Forgot to update java.class.version?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static void main(String [] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String classVersion   = getProperty("java.class.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        String javaVersion    = getProperty("java.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String VMVersion      = getProperty("java.vm.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String runtimeVersion = getProperty("java.runtime.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String specVersion    = getProperty("java.specification.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (! (javaVersion.startsWith(specVersion) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
               runtimeVersion.startsWith(specVersion)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new Exception("Invalid version-related system properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // Check that java.class.version is correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // Injecting a larger major or minor version number into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // .class file should result in UnsupportedClassVersionError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String[] versions = classVersion.split("\\.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int majorVersion = Integer.parseInt(versions[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int minorVersion = Integer.parseInt(versions[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        System.out.printf("majorVersion=%s%n",majorVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        System.out.printf("minorVersion=%s%n",minorVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // Look in ".", and *not* in CLASSPATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        cl = new URLClassLoader(new URL[]{new File("./").toURL()}, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        checkClassVersion(majorVersion    , minorVersion    , true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        checkClassVersion(majorVersion + 1, minorVersion    , false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        checkClassVersion(majorVersion    , minorVersion + 1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}