Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ClassLoader/Assert.java
38812 views
/*1* Copyright (c) 2000, 2002, 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* @run shell/timeout=300 Assert.sh27* @summary Test the assertion facility28* @author Mike McCloskey29* @key randomness30*/3132import package1.*;33import package2.*;34import package1.package3.*;35import java.io.*;36import java.util.Random;3738public class Assert {3940private static Class1 testClass1;41private static Class2 testClass2;42private static Class3 testClass3;43private static final boolean debug = false;44private static Random generator = new Random();4546/**47* The first invocation of this test starts a loop which exhaustively tests48* the object tree with all of its different settings.49* There are 7 test objects in a tree that has 7 different places to set50* assertions untouched/on/off so this tests 3^7 or 2187 different51* configurations.52*53* This test spawns a new VM for each run because assertions are set on or54* off at class load time. Once the class is loaded its assertion status55* does not change.56*/57public static void main(String[] args) throws Exception {5859// Switch values: 0=don't touch, 1=off, 2 = on60int[] switches = new int[7];6162int switchSource = 0;63if (args.length == 0) { // This is master version6465// This code is for an exhaustive test66//while(switchSource < 2187) {67// int temp = switchSource++;6869// This code is for a weaker but faster test70for(int x=0; x<100; x++) {71int temp = generator.nextInt(2187);72for(int i=0; i<7; i++) {73switches[i] = temp % 3;74temp = temp / 3;75}7677// Spawn new VM and load classes78String command = System.getProperty("java.home") +79File.separator + "bin" + File.separator + "java Assert";8081StringBuffer commandString = new StringBuffer(command);82for(int j=0; j<7; j++)83commandString.append(" "+switches[j]);8485Process p = null;86p = Runtime.getRuntime().exec(commandString.toString());8788if (debug) { // See output of test VMs89BufferedReader blah = new BufferedReader(90new InputStreamReader(p.getInputStream()));91String outString = blah.readLine();92while (outString != null) {93System.out.println("from slave:"+outString);94outString = blah.readLine();95}96}9798p.waitFor();99int result = p.exitValue();100if (debug) { // See which switch configs failed101if (result == 0) {102for(int k=6; k>=0; k--)103System.out.print(switches[k]);104System.out.println();105} else {106System.out.print("Nonzero Exit: ");107for(int k=6; k>=0; k--)108System.out.print(switches[k]);109System.out.println();110}111} else {112if (result != 0) {113System.err.print("Nonzero Exit: ");114for(int k=6; k>=0; k--)115System.err.print(switches[k]);116System.err.println();117throw new RuntimeException("Assertion test failure.");118}119}120}121} else { // This is a test spawn122for(int i=0; i<7; i++)123switches[i] = Integer.parseInt(args[i]);124125SetAssertionSwitches(switches);126ConstructClassTree();127TestClassTree(switches);128}129}130131/*132* Activate/Deactivate the assertions in the tree according to the133* specified switches.134*/135private static void SetAssertionSwitches(int[] switches) {136ClassLoader loader = ClassLoader.getSystemClassLoader();137138if (switches[0] != 0)139loader.setDefaultAssertionStatus(switches[0]==2);140if (switches[1] != 0)141loader.setPackageAssertionStatus("package1", switches[1]==2);142if (switches[2] != 0)143loader.setPackageAssertionStatus("package2", switches[2]==2);144if (switches[3] != 0)145loader.setPackageAssertionStatus("package1.package3", switches[3]==2);146if (switches[4] != 0)147loader.setClassAssertionStatus("package1.Class1", switches[4]==2);148if (switches[5] != 0)149loader.setClassAssertionStatus("package2.Class2", switches[5]==2);150if (switches[6] != 0)151loader.setClassAssertionStatus("package1.package3.Class3", switches[6]==2);152}153154/*155* Verify that the assertions are activated or deactivated as specified156* by the switches.157*/158private static void TestClassTree(int[] switches) {159160// Class1 and anonymous inner class161boolean assertsOn = (switches[4]==2) ? true : (switches[4]==1) ? false :162(switches[1]==2) ? true : (switches[1]==1) ? false : (switches[0]==2) ?163true: false;164testClass1.testAssert(assertsOn);165166// Class1 inner class Class11167assertsOn = (switches[4]==2) ? true : (switches[4]==1) ? false :168(switches[1]==2) ? true : (switches[1]==1) ? false : (switches[0]==2) ?169true: false;170Class1.Class11.testAssert(assertsOn);171172// Class2173assertsOn = (switches[5]==2) ? true : (switches[5]==1) ? false :174(switches[2]==2) ? true : (switches[2]==1) ? false : (switches[0]==2) ?175true: false;176testClass2.testAssert(assertsOn);177178// Class3 and anonymous inner class179assertsOn = (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;183testClass3.testAssert(assertsOn);184185// Class3 inner class Class31186assertsOn = (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;190Class3.Class31.testAssert(assertsOn);191192}193194/*195* Create the class tree to be tested. Each test run must reload the classes196* of the tree since assertion status is determined at class load time.197*/198private static void ConstructClassTree() {199testClass1 = new Class1();200testClass2 = new Class2();201testClass3 = new Class3();202}203204205}206207208