Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / vm / basic / AssertTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.test.vm.basic;
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.net.URLClassLoader;
25
26 import gov.nasa.jpf.test.java.net.LoadUtility;
27 import org.junit.Test;
28
29 /**
30  * JPF part of assertion test
31  */
32 public class AssertTest extends LoadUtility {
33
34   @Test public void testAssertionViolation () {
35     if (verifyAssertionErrorDetails("oops, assertion failed")){
36       int i = 1;
37       assert i == 0 : "oops, assertion failed";
38     }
39   }
40
41   @Test public void testNoAssertionViolation () {
42     if (verifyNoPropertyViolation("+vm.disable_assertions=*AssertTest")){
43       int i = 1;
44       assert i == 0 : "oops, assertion failed";
45     }
46   }
47
48   public static void invokeAssertFalse(ClassLoader loader, String cname) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException  {
49     Class<?> c = loader.loadClass(cname);
50     Method m = c.getMethod("assertFalse", new Class<?>[0]);
51     m.invoke(null, new Object[0]);
52   }
53
54   @Test
55   public void testSetClassAssertionStatus1() {
56     if (verifyAssertionErrorDetails("oops, assertion failed")) {
57       ClassLoader cl = ClassLoader.getSystemClassLoader();
58
59       // this should change the "desiredAssertionStatus()" return value to false, 
60       // but it shouldn't change the actual assertion status since the class has 
61       // been already initialized
62       cl.setClassAssertionStatus("gov.nasa.jpf.test.vm.basic.AssertTest", false);
63       assertFalse(AssertTest.class.desiredAssertionStatus());
64
65       // throw AssertionError
66       assert false : "oops, assertion failed";
67     }
68   }
69
70   @Test
71   public void testSetClassAssertionStatus2() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException {
72     movePkgOut();
73     if (verifyNoPropertyViolation()) {
74       URL[] urls = { new URL(dirUrl) };
75       URLClassLoader loader = new URLClassLoader(urls);
76
77       String c1 = pkg + ".Class1";
78       String c2 = pkg + ".Class2";
79       loader.setClassAssertionStatus(c1, false);
80
81       try {
82         invokeAssertFalse(loader, c1);
83       } catch(Exception ae) {
84         fail("setClassAssertionStatus should have avoided this!");
85       }
86
87       loader.setClassAssertionStatus(c2, false);
88       assertFalse(loader.loadClass(c2).desiredAssertionStatus());
89       try {
90         invokeAssertFalse(loader, c2);
91         fail();
92       } catch (InvocationTargetException e) {
93         // success
94       }
95     }
96     movePkgBack();
97   }
98
99   @Test
100   public void testSetPackageAssertionStatus() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException {
101     movePkgOut();
102     if (verifyNoPropertyViolation()) {
103       URL[] urls = { new URL(dirUrl) };
104       URLClassLoader loader = new URLClassLoader(urls);
105       loader.setPackageAssertionStatus(pkg, false);
106
107       String c1 = pkg + ".Class1";
108       try {
109         invokeAssertFalse(loader, c1);
110       } catch(Exception ae) {
111         fail("setPackageAssertionStatus should have avoided this!");
112       }
113
114       loader.setPackageAssertionStatus(pkg, true);
115       assertTrue(loader.loadClass(c1).desiredAssertionStatus());
116       try {
117         invokeAssertFalse(loader, c1);
118       } catch(Exception ae) {
119         fail("setPackageAssertionStatus shouldn't change actual assertion status");
120       }
121     }
122     movePkgBack();
123   }
124
125   @Test
126   public void testSetDefaultAssertionStatus() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException {
127     movePkgOut();
128     if (verifyNoPropertyViolation()) {
129       URL[] urls = { new URL(dirUrl) };
130       URLClassLoader loader = new URLClassLoader(urls);
131       loader.setDefaultAssertionStatus(false);
132
133       String c1 = pkg + ".Class1";
134       try {
135         invokeAssertFalse(loader, c1);
136       } catch(Exception ae) {
137         fail("setDefaultAssertionStatus should have avoided this!");
138       }
139
140       // shouldn't have any effect on the actual assertion status of the class since 
141       // it has been already initialized
142       loader.setDefaultAssertionStatus(true);
143       assertTrue(loader.loadClass(c1).desiredAssertionStatus());
144       try {
145         invokeAssertFalse(loader, c1);
146       } catch(Exception ae) {
147         fail("setDefaultAssertionStatus shouldn't change actual assertion status");
148       }
149     }
150     movePkgBack();
151   }
152   
153   @Test
154   public void testClearAssertionStatus() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
155     movePkgOut();
156     if (verifyNoPropertyViolation("+vm.disable_assertions=*")) {
157
158       URL[] urls = { new URL(dirUrl) };
159       URLClassLoader loader = new URLClassLoader(urls);
160       loader.setDefaultAssertionStatus(true);
161
162       String c1 = pkg + ".Class1";
163       try {
164         invokeAssertFalse(loader, c1);
165         fail();
166       } catch(InvocationTargetException ae) {
167         // success!
168       }
169       
170       // shouldn't have any effect on the actual assertion status of the class since 
171       // it has been already initialized
172       loader.clearAssertionStatus();
173       assertFalse(loader.loadClass(c1).desiredAssertionStatus());
174       try {
175         invokeAssertFalse(loader, c1);
176         fail();
177       } catch(Exception ae) {
178         // success!
179       }
180
181       String c3 = pkg + ".Class3";
182       try {
183         invokeAssertFalse(loader, c3);
184       } catch(Exception ae) {
185         fail("clearAssertionStatus() should have set the assertion status for the " +
186                         "loader to false");
187       }
188     }
189     movePkgBack();
190   }
191 }