jdk/test/java/lang/ClassLoader/Assert.java
author chegar
Thu, 03 Mar 2011 16:48:53 +0000
changeset 8563 4b287f6663b6
parent 5506 202f599c92aa
child 23353 013edb90a29d
permissions -rw-r--r--
6721694: java/lang/Thread/StartOOMTest.java fails with timeout or with crash Summary: the test is not suitable to be run automatically Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2002, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4290640 4785473
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run shell/timeout=300 Assert.sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Test the assertion facility
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import package1.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import package2.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import package1.package3.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Assert {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Class1 testClass1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static Class2 testClass2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static Class3 testClass3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final  boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The first invocation of this test starts a loop which exhaustively tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * the object tree with all of its different settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * There are 7 test objects in a tree that has 7 different places to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * assertions untouched/on/off so this tests 3^7 or 2187 different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * configurations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * This test spawns a new VM for each run because assertions are set on or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * off at class load time. Once the class is loaded its assertion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * does not change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // Switch values: 0=don't touch, 1=off, 2 = on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int[] switches = new int[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int switchSource = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (args.length == 0) { // This is master version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // This code is for an exhaustive test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            //while(switchSource < 2187) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            //    int temp = switchSource++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            // This code is for a weaker but faster test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            for(int x=0; x<100; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                int temp = generator.nextInt(2187);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                for(int i=0; i<7; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    switches[i] = temp % 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    temp = temp / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // Spawn new VM and load classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                String command = System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    File.separator + "bin" + File.separator + "java Assert";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                StringBuffer commandString = new StringBuffer(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                for(int j=0; j<7; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    commandString.append(" "+switches[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                Process p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                p = Runtime.getRuntime().exec(commandString.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (debug) { // See output of test VMs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    BufferedReader blah = new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                          new InputStreamReader(p.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    String outString = blah.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    while (outString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        System.out.println("from slave:"+outString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        outString = blah.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                p.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                int result = p.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if (debug) { // See which switch configs failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        for(int k=6; k>=0; k--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                            System.out.print(switches[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        System.out.print("Nonzero Exit: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        for(int k=6; k>=0; k--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            System.out.print(switches[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        System.err.print("Nonzero Exit: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        for(int k=6; k>=0; k--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                            System.err.print(switches[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        throw new RuntimeException("Assertion test failure.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else { // This is a test spawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            for(int i=0; i<7; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                switches[i] = Integer.parseInt(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            SetAssertionSwitches(switches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ConstructClassTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            TestClassTree(switches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Activate/Deactivate the assertions in the tree according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * specified switches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static void SetAssertionSwitches(int[] switches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ClassLoader loader = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (switches[0] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            loader.setDefaultAssertionStatus(switches[0]==2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (switches[1] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            loader.setPackageAssertionStatus("package1", switches[1]==2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (switches[2] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            loader.setPackageAssertionStatus("package2", switches[2]==2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (switches[3] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            loader.setPackageAssertionStatus("package1.package3", switches[3]==2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (switches[4] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            loader.setClassAssertionStatus("package1.Class1", switches[4]==2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (switches[5] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            loader.setClassAssertionStatus("package2.Class2", switches[5]==2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (switches[6] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            loader.setClassAssertionStatus("package1.package3.Class3", switches[6]==2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Verify that the assertions are activated or deactivated as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * by the switches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static void TestClassTree(int[] switches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // Class1 and anonymous inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        boolean assertsOn = (switches[4]==2) ? true : (switches[4]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            (switches[1]==2) ? true : (switches[1]==1) ? false : (switches[0]==2) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            true: false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        testClass1.testAssert(assertsOn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // Class1 inner class Class11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        assertsOn = (switches[4]==2) ? true : (switches[4]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            (switches[1]==2) ? true : (switches[1]==1) ? false : (switches[0]==2) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            true: false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Class1.Class11.testAssert(assertsOn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Class2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        assertsOn = (switches[5]==2) ? true : (switches[5]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            (switches[2]==2) ? true : (switches[2]==1) ? false : (switches[0]==2) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            true: false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        testClass2.testAssert(assertsOn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // Class3 and anonymous inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        assertsOn = (switches[6]==2) ? true : (switches[6]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    (switches[3]==2) ? true : (switches[3]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    (switches[1]==2) ? true : (switches[1]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    (switches[0]==2) ? true: false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        testClass3.testAssert(assertsOn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // Class3 inner class Class31
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        assertsOn = (switches[6]==2) ? true : (switches[6]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    (switches[3]==2) ? true : (switches[3]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    (switches[1]==2) ? true : (switches[1]==1) ? false :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    (switches[0]==2) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Class3.Class31.testAssert(assertsOn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Create the class tree to be tested. Each test run must reload the classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * of the tree since assertion status is determined at class load time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static void ConstructClassTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        testClass1 = new Class1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        testClass2 = new Class2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        testClass3 = new Class3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}