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.java.lang;
20 import gov.nasa.jpf.util.test.TestJPF;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import java.util.Enumeration;
27 import org.junit.Test;
30 * test of java.lang.ClassLoader API
32 public class ClassLoaderTest extends TestJPF {
35 public void testGetResource() {
36 if(verifyNoPropertyViolation()) {
37 testGetResourceImpl(new TestClassLoader());
42 public void testGetResources() throws IOException{
43 if(verifyNoPropertyViolation()) {
44 testGetResourcesImpl(new TestClassLoader());
49 public void testGetResourceAsStream() throws IOException{
50 if(verifyNoPropertyViolation()) {
51 testGetResourceAsStreamImpl(new TestClassLoader());
56 public void testLoadClass() {
57 if(verifyNoPropertyViolation()) {
58 ClassLoader classLoader = new TestClassLoader();
60 classLoader.loadClass("non_existing_class");
62 }catch(ClassNotFoundException e) {}
67 public void testLoadClass2() {
68 if(verifyNoPropertyViolation()) {
69 ClassLoader classLoader = new TestClassLoader();
71 classLoader.loadClass(ClassLoader.class.getName());
72 }catch(ClassNotFoundException e) {
79 public void testGetSystemResource() {
80 if(verifyNoPropertyViolation()) {
81 testGetResourceImpl( ClassLoader.getSystemClassLoader());
86 public void testGetSystemResources() throws IOException{
87 if(verifyNoPropertyViolation()) {
88 testGetResourcesImpl( ClassLoader.getSystemClassLoader());
93 public void testGetSystemResourceAsStream() throws IOException{
94 if(verifyNoPropertyViolation()) {
95 testGetResourceAsStreamImpl( ClassLoader.getSystemClassLoader());
100 public void testGetSystemClassLoader() {
101 if(verifyNoPropertyViolation()) {
102 ClassLoader classLoader = new TestClassLoader();
103 assertNotNull(ClassLoader.getSystemClassLoader());
104 assertNull(ClassLoader.getSystemClassLoader().getParent());
105 assertFalse(classLoader.equals(ClassLoader.getSystemClassLoader()));
110 public void testGetParent() {
111 if(verifyNoPropertyViolation()) {
112 ClassLoader classLoader = new TestClassLoader();
113 assertNotNull(classLoader.getParent());
114 assertEquals(classLoader.getParent(),ClassLoader.getSystemClassLoader());
119 public void testGetParent2() {
120 if(verifyNoPropertyViolation()) {
121 ClassLoader parentClassLoader = new TestClassLoader();
122 ClassLoader classLoader = new TestClassLoader(parentClassLoader);
123 assertEquals(parentClassLoader, classLoader.getParent());
128 public void testFoundResources() throws IOException {
129 if(verifyNoPropertyViolation()) {
130 TestClassLoader classLoader = new TestClassLoader();
131 Enumeration<URL> enm = classLoader.findResources("not_existing_resource");
133 assertFalse(enm.hasMoreElements());
137 private void testGetResourceImpl(ClassLoader classLoader) {
138 assertNull(classLoader.getResource("not_existing_resource"));
139 assertNotNull(classLoader.getResource("DiningPhil.class"));
140 assertNull(classLoader.getResource("ClassLoader.class"));
141 assertNotNull(classLoader.getResource("java/lang/ClassLoader.class"));
144 private void testGetResourcesImpl(ClassLoader classLoader) throws IOException{
145 assertFalse(classLoader.getResources("not_existing_resources").hasMoreElements());
147 Enumeration<?> e = classLoader.getResources("DiningPhil.class");
148 assertTrue(e.hasMoreElements());
149 assertNotNull(e.nextElement());
150 assertFalse(e.hasMoreElements());
152 e = classLoader.getResources("ClassLoader.class");
153 assertFalse(e.hasMoreElements());
155 // It should find at least two resources: 1. model class, 2. JDK class
156 e = classLoader.getResources("java/lang/ClassLoader.class");
157 assertTrue(e.hasMoreElements());
158 assertNotNull(e.nextElement());
159 assertTrue(e.hasMoreElements());
160 assertNotNull(e.nextElement());
163 private void testGetResourceAsStreamImpl(ClassLoader classLoader) throws IOException{
164 assertNull(classLoader.getResourceAsStream("not_existing_resources"));
165 InputStream is = classLoader.getResourceAsStream("DiningPhil.class");
167 assertTrue(is.read() > 0);
170 class TestClassLoader extends ClassLoader {
172 public TestClassLoader() {
176 public TestClassLoader(ClassLoader parent) {
181 protected Enumeration<URL> findResources(String name) throws IOException {
182 return super.findResources(name);