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 // Android-changed: Adapt structure and expectations to Android.
35 // Android does not use AssertionStatuses, so this test changes expectations to reflect that.
36 // Furthermore, the test structure is simplified to avoid relying on args[] and use the
37 // org.testng.annotations.Test package instead of a main() method.
38 package test.java.lang.ClassLoader;
39 
40 import test.java.lang.ClassLoader.package1.*;
41 import test.java.lang.ClassLoader.package2.*;
42 import test.java.lang.ClassLoader.package1.package3.*;
43 import java.util.Random;
44 
45 import org.testng.annotations.Test;
46 
47 public class AssertTest {
48 
49     private static Class1 testClass1;
50     private static Class2 testClass2;
51     private static Class3 testClass3;
52     private static Random generator = new Random();
53 
54     /**
55      * AssertionStatuses don't actually do anything on Android, this test proves as much.
56      */
57     @Test
testAssert()58     public void testAssert() {
59         // Switch values: 0=don't touch, 1=off, 2 = on
60         int[] switches = new int[7];
61         for(int x=0; x<10; x++) {
62             int temp = generator.nextInt(2187);
63             for (int i = 0; i < 7; i++) {
64                 switches[i] = temp % 3;
65                 temp = temp / 3;
66             }
67             SetAssertionSwitches(switches);
68             ConstructClassTree();
69             TestClassTree();
70         }
71 
72 
73         // Android-added: Add testing of clearAssertionStatus().
74         for(int x=0; x<7; x++) {
75             switches[x]=2;
76         }
77         ClassLoader loader = SetAssertionSwitches(switches);
78         loader.clearAssertionStatus(); // Clearing also does nothing
79         ConstructClassTree();
80         TestClassTree();
81     }
82 
83     /*
84      * Activate/Deactivate the assertions in the tree according to the
85      * specified switches.
86      */
SetAssertionSwitches(int[] switches)87     private static ClassLoader SetAssertionSwitches(int[] switches) {
88         ClassLoader loader = ClassLoader.getSystemClassLoader();
89 
90         if (switches[0] != 0)
91             loader.setDefaultAssertionStatus(switches[0]==2);
92         if (switches[1] != 0)
93             loader.setPackageAssertionStatus("package1", switches[1]==2);
94         if (switches[2] != 0)
95             loader.setPackageAssertionStatus("package2", switches[2]==2);
96         if (switches[3] != 0)
97             loader.setPackageAssertionStatus("package1.package3", switches[3]==2);
98         if (switches[4] != 0)
99             loader.setClassAssertionStatus("package1.Class1", switches[4]==2);
100         if (switches[5] != 0)
101             loader.setClassAssertionStatus("package2.Class2", switches[5]==2);
102         if (switches[6] != 0)
103             loader.setClassAssertionStatus("package1.package3.Class3", switches[6]==2);
104         return loader;
105     }
106 
107     /*
108      * Verify that the assertions are activated or deactivated as specified
109      * by the switches.
110      */
TestClassTree()111     private static void TestClassTree() {
112         testClass1.testAssert(false);
113         Class1.Class11.testAssert(false);
114         testClass2.testAssert(false);
115         testClass3.testAssert(false);
116         Class3.Class31.testAssert(false);
117 
118     }
119 
120     /*
121      * Create the class tree to be tested. Each test run must reload the classes
122      * of the tree since assertion status is determined at class load time.
123      */
ConstructClassTree()124     private static void ConstructClassTree() {
125         testClass1 = new Class1();
126         testClass2 = new Class2();
127         testClass3 = new Class3();
128     }
129 
130 }