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: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.apache.harmony.rmi.server;
020:
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.net.URLClassLoader;
024: import java.rmi.server.RMIClassLoader;
025: import java.security.Permission;
026: import junit.framework.TestCase;
027:
028: public class RMIClassLoaderTest extends TestCase {
029: /**
030: * Test for java.rmi.server.RMIclassLoader.loadClass() method testing that
031: * RMI runtime does not change the order of the incoming codebase string
032: * (regression test for HARMONY-1944).
033: */
034: public void testLoadClassCodebaseOrder() throws Exception {
035: SecurityManager previous = System.getSecurityManager();
036: System.setSecurityManager(new SecurityManager() {
037: @Override
038: public void checkPermission(Permission perm) {
039: /*
040: * Override checkPermission to allow everything. Specifically,
041: * we want to allow the SecurityManager to be set to null at the
042: * end of the test and we want to allow the 'testClass.jar' file
043: * to be allowed to load.
044: */
045: return;
046: }
047: });
048: try {
049: URL testJarURL = getClass().getResource("testClass.jar");
050: String[] paths = new String[] { testJarURL.getPath(),
051: /*
052: * to be sure this path will be the first after sorting
053: */
054: "/_fake.jar" };
055: Class<?> c = RMIClassLoader.loadClass("file://" + paths[0]
056: + " file://" + paths[1], "TestClass", null);
057: ClassLoader cl = c.getClassLoader();
058: if (cl instanceof URLClassLoader) {
059: URL[] urls = ((URLClassLoader) cl).getURLs();
060: if (urls.length != 2) {
061: fail("Unexpected number of URLs: " + urls.length);
062: }
063: String failStr = "";
064: for (int i = 0; i < urls.length; ++i) {
065: if (!urls[i].getPath().equals(paths[i])) {
066: failStr += "\nURL[" + i + "].getPath() = "
067: + urls[i].getPath() + ", expected: "
068: + paths[i];
069: }
070: }
071: if (!failStr.equals("")) {
072: fail(failStr);
073: }
074: } else {
075: fail("Class is loaded by non-URLClassLoader");
076: }
077: } finally {
078: // reset the security manager back to null state
079: System.setSecurityManager(previous);
080: }
081: }
082:
083: /**
084: * Test for java.rmi.server.RMIClassLoader.loadProxyClass(String, String[], ClassLoader)
085: * testing invalid url as a codebase.
086: */
087: public void testLoadProxyClassInvalidCodebase() throws Exception {
088: //Regression for HARMONY-1133
089: SecurityManager previous = System.getSecurityManager();
090: System.setSecurityManager(null);
091:
092: try {
093: RMIClassLoader.loadProxyClass("zzz", new String[] {}, null);
094: fail("MalformedURLException expected");
095: } catch (MalformedURLException e) {
096: //expected
097: } finally {
098: System.setSecurityManager(previous);
099: }
100: }
101:
102: /**
103: * Test for java.rmi.server.RMIClassLoader.loadClass(String, String)
104: * testing invalid url as a codebase.
105: */
106: public void testLoadClassInvalidCodebase() throws Exception {
107: //Regression for HARMONY-1133
108: SecurityManager previous = System.getSecurityManager();
109: System.setSecurityManager(null);
110:
111: try {
112: RMIClassLoader.loadClass("zzz", "a123");
113: fail("MalformedURLException expected");
114: } catch (MalformedURLException e) {
115: //expected
116: } finally {
117: System.setSecurityManager(previous);
118: }
119: }
120:
121: /**
122: * Test for java.rmi.server.RMIClassLoader.getClassLoader(String)
123: * testing invalid url as a codebase.
124: */
125: public void testGetClassLoaderInvalidCodebase() throws Exception {
126: //Regression for HARMONY-1134
127: SecurityManager previous = System.getSecurityManager();
128: System.setSecurityManager(null);
129:
130: try {
131: RMIClassLoader.getClassLoader("zzz");
132: fail("MalformedURLException expected");
133: } catch (MalformedURLException e) {
134: //expected
135: } finally {
136: System.setSecurityManager(previous);
137: }
138: }
139: }
|