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