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