001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.lang;
019:
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.InputStream;
023: import java.security.CodeSource;
024: import java.security.Permission;
025: import java.security.PermissionCollection;
026: import java.security.Policy;
027: import java.security.ProtectionDomain;
028: import java.security.SecurityPermission;
029: import junit.framework.TestCase;
030:
031: public class ClassLoaderTest extends TestCase {
032:
033: public static volatile int flag;
034:
035: /**
036: * Tests that Classloader.defineClass() assigns appropriate
037: * default domains to the defined classes.
038: */
039: public void test_defineClass_defaultDomain() throws Exception {
040: // Regression for HARMONY-765
041: DynamicPolicy plc = new DynamicPolicy();
042: Policy back = Policy.getPolicy();
043: try {
044: Policy.setPolicy(plc);
045:
046: Class<?> a = new Ldr().define();
047:
048: Permission p = new SecurityPermission("abc");
049: assertFalse("impossible! misconfiguration?", a
050: .getProtectionDomain().implies(p));
051:
052: plc.pc = p.newPermissionCollection();
053: plc.pc.add(p);
054: assertTrue("default domain is not dynamic", a
055: .getProtectionDomain().implies(p));
056: } finally {
057: Policy.setPolicy(back);
058: }
059: }
060:
061: static class SyncTestClassLoader extends ClassLoader {
062: Object lock;
063: volatile int numFindClassCalled;
064:
065: SyncTestClassLoader(Object o) {
066: this .lock = o;
067: numFindClassCalled = 0;
068: }
069:
070: /*
071: * Byte array of bytecode equivalent to the following source code:
072: * public class TestClass {
073: * }
074: */
075: private byte[] classData = new byte[] { -54, -2, -70, -66, 0,
076: 0, 0, 49, 0, 13, 10, 0, 3, 0, 10, 7, 0, 11, 7, 0, 12,
077: 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41,
078: 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110,
079: 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101,
080: 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108,
081: 101, 1, 0, 14, 84, 101, 115, 116, 67, 108, 97, 115,
082: 115, 46, 106, 97, 118, 97, 12, 0, 4, 0, 5, 1, 0, 9, 84,
083: 101, 115, 116, 67, 108, 97, 115, 115, 1, 0, 16, 106,
084: 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106,
085: 101, 99, 116, 0, 33, 0, 2, 0, 3, 0, 0, 0, 0, 0, 1, 0,
086: 1, 0, 4, 0, 5, 0, 1, 0, 6, 0, 0, 0, 29, 0, 1, 0, 1, 0,
087: 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0, 7, 0, 0, 0,
088: 6, 0, 1, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 2, 0, 9 };
089:
090: protected Class findClass(String name)
091: throws ClassNotFoundException {
092: try {
093: while (flag != 2) {
094: synchronized (lock) {
095: lock.wait();
096: }
097: }
098: } catch (InterruptedException ie) {
099: }
100:
101: if (name.equals("TestClass")) {
102: numFindClassCalled++;
103: return defineClass(null, classData, 0, classData.length);
104: } else {
105: throw new ClassNotFoundException("Class " + name
106: + " not found.");
107: }
108: }
109: }
110:
111: static class SyncLoadTestThread extends Thread {
112: volatile boolean started;
113: ClassLoader cl;
114: Class cls;
115:
116: SyncLoadTestThread(ClassLoader cl) {
117: this .cl = cl;
118: }
119:
120: public void run() {
121: try {
122: started = true;
123: cls = Class.forName("TestClass", false, cl);
124: } catch (Exception ex) {
125: ex.printStackTrace();
126: }
127: }
128: }
129:
130: /**
131: * Regression test for HARMONY-1939:
132: * 2 threads simultaneously run Class.forName() method for the same classname
133: * and the same classloader. It is expected that both threads succeed but
134: * class must be defined just once.
135: */
136: public void test_loadClass_concurrentLoad() throws Exception {
137: Object lock = new Object();
138: SyncTestClassLoader cl = new SyncTestClassLoader(lock);
139: SyncLoadTestThread tt1 = new SyncLoadTestThread(cl);
140: SyncLoadTestThread tt2 = new SyncLoadTestThread(cl);
141: flag = 1;
142: tt1.start();
143: tt2.start();
144:
145: while (!tt1.started && !tt2.started) {
146: Thread.sleep(100);
147: }
148:
149: flag = 2;
150: synchronized (lock) {
151: lock.notifyAll();
152: }
153: tt1.join();
154: tt2.join();
155:
156: assertSame("Bad or redefined class", tt1.cls, tt2.cls);
157: assertEquals("Both threads tried to define class", 1,
158: cl.numFindClassCalled);
159: }
160:
161: /**
162: * @tests java.lang.ClassLoader#getResource(java.lang.String)
163: */
164: public void test_getResourceLjava_lang_String() {
165: // Test for method java.net.URL
166: // java.lang.ClassLoader.getResource(java.lang.String)
167: java.net.URL u = ClassLoader.getSystemClassLoader()
168: .getResource("hyts_Foo.c");
169: assertNotNull("Unable to find resource", u);
170: java.io.InputStream is = null;
171: try {
172: is = u.openStream();
173: assertNotNull("Resource returned is invalid", is);
174: is.close();
175: } catch (java.io.IOException e) {
176: fail("IOException getting stream for resource : "
177: + e.getMessage());
178: }
179: }
180:
181: /**
182: * @tests java.lang.ClassLoader#getResourceAsStream(java.lang.String)
183: */
184: public void test_getResourceAsStreamLjava_lang_String() {
185: // Test for method java.io.InputStream
186: // java.lang.ClassLoader.getResourceAsStream(java.lang.String)
187: // Need better test...
188:
189: java.io.InputStream is = null;
190: assertNotNull("Failed to find resource: hyts_Foo.c",
191: (is = ClassLoader.getSystemClassLoader()
192: .getResourceAsStream("hyts_Foo.c")));
193: try {
194: is.close();
195: } catch (java.io.IOException e) {
196: fail("Exception during getResourceAsStream: "
197: + e.toString());
198: }
199: }
200:
201: /**
202: * @tests java.lang.ClassLoader#getSystemClassLoader()
203: */
204: public void test_getSystemClassLoader() {
205: // Test for method java.lang.ClassLoader
206: // java.lang.ClassLoader.getSystemClassLoader()
207: ClassLoader cl = ClassLoader.getSystemClassLoader();
208: java.io.InputStream is = cl.getResourceAsStream("hyts_Foo.c");
209: assertNotNull("Failed to find resource from system classpath",
210: is);
211: try {
212: is.close();
213: } catch (java.io.IOException e) {
214: }
215:
216: }
217:
218: /**
219: * @tests java.lang.ClassLoader#getSystemResource(java.lang.String)
220: */
221: public void test_getSystemResourceLjava_lang_String() {
222: // Test for method java.net.URL
223: // java.lang.ClassLoader.getSystemResource(java.lang.String)
224: // Need better test...
225: assertNotNull("Failed to find resource: hyts_Foo.c",
226: ClassLoader.getSystemResource("hyts_Foo.c"));
227: }
228:
229: //Regression Test for JIRA-2047
230: public void test_getResourceAsStream_withSharpChar()
231: throws Exception {
232: InputStream in = this .getClass().getClassLoader()
233: .getResourceAsStream(ClassTest.FILENAME);
234: assertNotNull(in);
235: in.close();
236: }
237: }
238:
239: class DynamicPolicy extends Policy {
240:
241: public PermissionCollection pc;
242:
243: @Override
244: public PermissionCollection getPermissions(ProtectionDomain pd) {
245: return pc;
246: }
247:
248: @Override
249: public PermissionCollection getPermissions(CodeSource codesource) {
250: return pc;
251: }
252:
253: @Override
254: public void refresh() {
255: }
256: }
257:
258: class A {
259: }
260:
261: class Ldr extends ClassLoader {
262: @SuppressWarnings("deprecation")
263: public Class<?> define() throws Exception {
264: Package p = getClass().getPackage();
265: String path = p == null ? "" : p.getName().replace('.',
266: File.separatorChar)
267: + File.separator;
268: InputStream is = getResourceAsStream(path + "A.class");
269: byte[] buf = new byte[512];
270: int len = is.read(buf);
271: return defineClass(buf, 0, len);
272: }
273: }
|