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