2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.test.vm.basic;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.net.MalformedURLException;
24 import java.net.URLClassLoader;
26 import gov.nasa.jpf.test.java.net.LoadUtility;
27 import org.junit.Test;
30 * JPF part of assertion test
32 public class AssertTest extends LoadUtility {
34 @Test public void testAssertionViolation () {
35 if (verifyAssertionErrorDetails("oops, assertion failed")){
37 assert i == 0 : "oops, assertion failed";
41 @Test public void testNoAssertionViolation () {
42 if (verifyNoPropertyViolation("+vm.disable_assertions=*AssertTest")){
44 assert i == 0 : "oops, assertion failed";
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]);
55 public void testSetClassAssertionStatus1() {
56 if (verifyAssertionErrorDetails("oops, assertion failed")) {
57 ClassLoader cl = ClassLoader.getSystemClassLoader();
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());
65 // throw AssertionError
66 assert false : "oops, assertion failed";
71 public void testSetClassAssertionStatus2() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException {
73 if (verifyNoPropertyViolation()) {
74 URL[] urls = { new URL(dirUrl) };
75 URLClassLoader loader = new URLClassLoader(urls);
77 String c1 = pkg + ".Class1";
78 String c2 = pkg + ".Class2";
79 loader.setClassAssertionStatus(c1, false);
82 invokeAssertFalse(loader, c1);
83 } catch(Exception ae) {
84 fail("setClassAssertionStatus should have avoided this!");
87 loader.setClassAssertionStatus(c2, false);
88 assertFalse(loader.loadClass(c2).desiredAssertionStatus());
90 invokeAssertFalse(loader, c2);
92 } catch (InvocationTargetException e) {
100 public void testSetPackageAssertionStatus() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException {
102 if (verifyNoPropertyViolation()) {
103 URL[] urls = { new URL(dirUrl) };
104 URLClassLoader loader = new URLClassLoader(urls);
105 loader.setPackageAssertionStatus(pkg, false);
107 String c1 = pkg + ".Class1";
109 invokeAssertFalse(loader, c1);
110 } catch(Exception ae) {
111 fail("setPackageAssertionStatus should have avoided this!");
114 loader.setPackageAssertionStatus(pkg, true);
115 assertTrue(loader.loadClass(c1).desiredAssertionStatus());
117 invokeAssertFalse(loader, c1);
118 } catch(Exception ae) {
119 fail("setPackageAssertionStatus shouldn't change actual assertion status");
126 public void testSetDefaultAssertionStatus() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException {
128 if (verifyNoPropertyViolation()) {
129 URL[] urls = { new URL(dirUrl) };
130 URLClassLoader loader = new URLClassLoader(urls);
131 loader.setDefaultAssertionStatus(false);
133 String c1 = pkg + ".Class1";
135 invokeAssertFalse(loader, c1);
136 } catch(Exception ae) {
137 fail("setDefaultAssertionStatus should have avoided this!");
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());
145 invokeAssertFalse(loader, c1);
146 } catch(Exception ae) {
147 fail("setDefaultAssertionStatus shouldn't change actual assertion status");
154 public void testClearAssertionStatus() throws MalformedURLException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
156 if (verifyNoPropertyViolation("+vm.disable_assertions=*")) {
158 URL[] urls = { new URL(dirUrl) };
159 URLClassLoader loader = new URLClassLoader(urls);
160 loader.setDefaultAssertionStatus(true);
162 String c1 = pkg + ".Class1";
164 invokeAssertFalse(loader, c1);
166 } catch(InvocationTargetException ae) {
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());
175 invokeAssertFalse(loader, c1);
177 } catch(Exception ae) {
181 String c3 = pkg + ".Class3";
183 invokeAssertFalse(loader, c3);
184 } catch(Exception ae) {
185 fail("clearAssertionStatus() should have set the assertion status for the " +