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