Path: blob/master/test/jdk/java/lang/ClassLoader/Assert.java
41149 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4290640 478547326* @build package1.Class1 package2.Class2 package1.package3.Class3 Assert27* @run main/othervm Assert28* @summary Test the assertion facility29* @author Mike McCloskey30* @key randomness31*/3233import package1.*;34import package2.*;35import package1.package3.*;36import java.io.*;37import java.util.Random;3839public class Assert {4041private static Class1 testClass1;42private static Class2 testClass2;43private static Class3 testClass3;44private static final boolean debug = false;45private static Random generator = new Random();4647/**48* The first invocation of this test starts a loop which exhaustively tests49* the object tree with all of its different settings.50* There are 7 test objects in a tree that has 7 different places to set51* assertions untouched/on/off so this tests 3^7 or 2187 different52* configurations.53*54* This test spawns a new VM for each run because assertions are set on or55* off at class load time. Once the class is loaded its assertion status56* does not change.57*/58public static void main(String[] args) throws Exception {5960// Switch values: 0=don't touch, 1=off, 2 = on61int[] switches = new int[7];6263int switchSource = 0;64if (args.length == 0) { // This is the controller6566// This code is for an exhaustive test67//while(switchSource < 2187) {68// int temp = switchSource++;6970// This code is for a weaker but faster test71for(int x=0; x<100; x++) {72int temp = generator.nextInt(2187);73for(int i=0; i<7; i++) {74switches[i] = temp % 3;75temp = temp / 3;76}7778// Spawn new VM and load classes79String command = System.getProperty("java.home") +80File.separator + "bin" + File.separator + "java Assert";8182StringBuffer commandString = new StringBuffer(command);83for(int j=0; j<7; j++)84commandString.append(" "+switches[j]);8586Process p = null;87p = Runtime.getRuntime().exec(commandString.toString());8889if (debug) { // See output of test VMs90BufferedReader blah = new BufferedReader(91new InputStreamReader(p.getInputStream()));92String outString = blah.readLine();93while (outString != null) {94System.out.println("from BufferedReader:"+outString);95outString = blah.readLine();96}97}9899p.waitFor();100int result = p.exitValue();101if (debug) { // See which switch configs failed102if (result == 0) {103for(int k=6; k>=0; k--)104System.out.print(switches[k]);105System.out.println();106} else {107System.out.print("Nonzero Exit: ");108for(int k=6; k>=0; k--)109System.out.print(switches[k]);110System.out.println();111}112} else {113if (result != 0) {114System.err.print("Nonzero Exit: ");115for(int k=6; k>=0; k--)116System.err.print(switches[k]);117System.err.println();118throw new RuntimeException("Assertion test failure.");119}120}121}122} else { // This is a test spawn123for(int i=0; i<7; i++)124switches[i] = Integer.parseInt(args[i]);125126SetAssertionSwitches(switches);127ConstructClassTree();128TestClassTree(switches);129}130}131132/*133* Activate/Deactivate the assertions in the tree according to the134* specified switches.135*/136private static void SetAssertionSwitches(int[] switches) {137ClassLoader loader = ClassLoader.getSystemClassLoader();138139if (switches[0] != 0)140loader.setDefaultAssertionStatus(switches[0]==2);141if (switches[1] != 0)142loader.setPackageAssertionStatus("package1", switches[1]==2);143if (switches[2] != 0)144loader.setPackageAssertionStatus("package2", switches[2]==2);145if (switches[3] != 0)146loader.setPackageAssertionStatus("package1.package3", switches[3]==2);147if (switches[4] != 0)148loader.setClassAssertionStatus("package1.Class1", switches[4]==2);149if (switches[5] != 0)150loader.setClassAssertionStatus("package2.Class2", switches[5]==2);151if (switches[6] != 0)152loader.setClassAssertionStatus("package1.package3.Class3", switches[6]==2);153}154155/*156* Verify that the assertions are activated or deactivated as specified157* by the switches.158*/159private static void TestClassTree(int[] switches) {160161// Class1 and anonymous inner class162boolean assertsOn = (switches[4]==2) ? true : (switches[4]==1) ? false :163(switches[1]==2) ? true : (switches[1]==1) ? false : (switches[0]==2) ?164true: false;165testClass1.testAssert(assertsOn);166167// Class1 inner class Class11168assertsOn = (switches[4]==2) ? true : (switches[4]==1) ? false :169(switches[1]==2) ? true : (switches[1]==1) ? false : (switches[0]==2) ?170true: false;171Class1.Class11.testAssert(assertsOn);172173// Class2174assertsOn = (switches[5]==2) ? true : (switches[5]==1) ? false :175(switches[2]==2) ? true : (switches[2]==1) ? false : (switches[0]==2) ?176true: false;177testClass2.testAssert(assertsOn);178179// Class3 and anonymous inner class180assertsOn = (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;184testClass3.testAssert(assertsOn);185186// Class3 inner class Class31187assertsOn = (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;191Class3.Class31.testAssert(assertsOn);192193}194195/*196* Create the class tree to be tested. Each test run must reload the classes197* of the tree since assertion status is determined at class load time.198*/199private static void ConstructClassTree() {200testClass1 = new Class1();201testClass2 = new Class2();202testClass3 = new Class3();203}204205206}207208209