Mercurial > hg > Members > kono > jpf-core
comparison src/tests/gov/nasa/jpf/test/java/lang/ClassLoaderTest.java @ 0:61d41facf527
initial v8 import (history reset)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 23 Jan 2015 10:14:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61d41facf527 |
---|---|
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.java.lang; | |
19 | |
20 import gov.nasa.jpf.util.test.TestJPF; | |
21 | |
22 import java.io.IOException; | |
23 import java.io.InputStream; | |
24 import java.net.URL; | |
25 import java.util.Enumeration; | |
26 | |
27 import org.junit.Test; | |
28 | |
29 /** | |
30 * test of java.lang.ClassLoader API | |
31 */ | |
32 public class ClassLoaderTest extends TestJPF { | |
33 | |
34 @Test | |
35 public void testGetResource() { | |
36 if(verifyNoPropertyViolation()) { | |
37 testGetResourceImpl(new TestClassLoader()); | |
38 } | |
39 } | |
40 | |
41 @Test | |
42 public void testGetResources() throws IOException{ | |
43 if(verifyNoPropertyViolation()) { | |
44 testGetResourcesImpl(new TestClassLoader()); | |
45 } | |
46 } | |
47 | |
48 @Test | |
49 public void testGetResourceAsStream() throws IOException{ | |
50 if(verifyNoPropertyViolation()) { | |
51 testGetResourceAsStreamImpl(new TestClassLoader()); | |
52 } | |
53 } | |
54 | |
55 @Test | |
56 public void testLoadClass() { | |
57 if(verifyNoPropertyViolation()) { | |
58 ClassLoader classLoader = new TestClassLoader(); | |
59 try { | |
60 classLoader.loadClass("non_existing_class"); | |
61 fail(); | |
62 }catch(ClassNotFoundException e) {} | |
63 } | |
64 } | |
65 | |
66 @Test | |
67 public void testLoadClass2() { | |
68 if(verifyNoPropertyViolation()) { | |
69 ClassLoader classLoader = new TestClassLoader(); | |
70 try { | |
71 classLoader.loadClass(ClassLoader.class.getName()); | |
72 }catch(ClassNotFoundException e) { | |
73 fail(e.getMessage()); | |
74 } | |
75 } | |
76 } | |
77 | |
78 @Test | |
79 public void testGetSystemResource() { | |
80 if(verifyNoPropertyViolation()) { | |
81 testGetResourceImpl( ClassLoader.getSystemClassLoader()); | |
82 } | |
83 } | |
84 | |
85 @Test | |
86 public void testGetSystemResources() throws IOException{ | |
87 if(verifyNoPropertyViolation()) { | |
88 testGetResourcesImpl( ClassLoader.getSystemClassLoader()); | |
89 } | |
90 } | |
91 | |
92 @Test | |
93 public void testGetSystemResourceAsStream() throws IOException{ | |
94 if(verifyNoPropertyViolation()) { | |
95 testGetResourceAsStreamImpl( ClassLoader.getSystemClassLoader()); | |
96 } | |
97 } | |
98 | |
99 @Test | |
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())); | |
106 } | |
107 } | |
108 | |
109 @Test | |
110 public void testGetParent() { | |
111 if(verifyNoPropertyViolation()) { | |
112 ClassLoader classLoader = new TestClassLoader(); | |
113 assertNotNull(classLoader.getParent()); | |
114 assertEquals(classLoader.getParent(),ClassLoader.getSystemClassLoader()); | |
115 } | |
116 } | |
117 | |
118 @Test | |
119 public void testGetParent2() { | |
120 if(verifyNoPropertyViolation()) { | |
121 ClassLoader parentClassLoader = new TestClassLoader(); | |
122 ClassLoader classLoader = new TestClassLoader(parentClassLoader); | |
123 assertEquals(parentClassLoader, classLoader.getParent()); | |
124 } | |
125 } | |
126 | |
127 @Test | |
128 public void testFoundResources() throws IOException { | |
129 if(verifyNoPropertyViolation()) { | |
130 TestClassLoader classLoader = new TestClassLoader(); | |
131 Enumeration<URL> enm = classLoader.findResources("not_existing_resource"); | |
132 assertNotNull(enm); | |
133 assertFalse(enm.hasMoreElements()); | |
134 } | |
135 } | |
136 | |
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")); | |
142 } | |
143 | |
144 private void testGetResourcesImpl(ClassLoader classLoader) throws IOException{ | |
145 assertFalse(classLoader.getResources("not_existing_resources").hasMoreElements()); | |
146 | |
147 Enumeration<?> e = classLoader.getResources("DiningPhil.class"); | |
148 assertTrue(e.hasMoreElements()); | |
149 assertNotNull(e.nextElement()); | |
150 assertFalse(e.hasMoreElements()); | |
151 | |
152 e = classLoader.getResources("ClassLoader.class"); | |
153 assertFalse(e.hasMoreElements()); | |
154 | |
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()); | |
161 } | |
162 | |
163 private void testGetResourceAsStreamImpl(ClassLoader classLoader) throws IOException{ | |
164 assertNull(classLoader.getResourceAsStream("not_existing_resources")); | |
165 InputStream is = classLoader.getResourceAsStream("DiningPhil.class"); | |
166 assertNotNull(is); | |
167 assertTrue(is.read() > 0); | |
168 } | |
169 | |
170 class TestClassLoader extends ClassLoader { | |
171 | |
172 public TestClassLoader() { | |
173 super(); | |
174 } | |
175 | |
176 public TestClassLoader(ClassLoader parent) { | |
177 super(parent); | |
178 } | |
179 | |
180 @Override | |
181 protected Enumeration<URL> findResources(String name) throws IOException { | |
182 return super.findResources(name); | |
183 } | |
184 } | |
185 } |