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