KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > rmi > RMIClassLoaderSpiImplTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.system.rmi;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Unit tests for {@link RMIClassLoaderSpiImpl} class.
35  *
36  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class RMIClassLoaderSpiImplTest extends TestCase {
39     private static final Log log = LogFactory.getLog(RMIClassLoaderSpiImplTest.class);
40     
41     private String JavaDoc baseURL;
42     private String JavaDoc normalizedBaseURL;
43
44     protected void setUp() throws Exception JavaDoc {
45         File JavaDoc dir = new File JavaDoc(System.getProperty("user.home"));
46
47         baseURL = dir.toURL().toString();
48         if (baseURL.endsWith("/")) {
49             baseURL = baseURL.substring(0, baseURL.length() - 1);
50         }
51
52         normalizedBaseURL = dir.toURI().toURL().toString();
53         if (normalizedBaseURL.endsWith("/")) {
54             normalizedBaseURL = normalizedBaseURL.substring(0, normalizedBaseURL.length() - 1);
55         }
56
57         log.debug("Using base URL: " + baseURL);
58         log.debug("Using normalized base URL: " + normalizedBaseURL);
59     }
60
61     public void testNormalizeURL() throws MalformedURLException JavaDoc {
62         URL JavaDoc url = new URL JavaDoc(baseURL + "/Apache Group/Geronimo");
63         URL JavaDoc normal = RMIClassLoaderSpiImpl.normalizeURL(url);
64         assertEquals(normalizedBaseURL + "/Apache%20Group/Geronimo", normal.toString());
65     }
66
67     public void testNormalizeCodebase() throws MalformedURLException JavaDoc {
68         String JavaDoc codebase = baseURL + "/Apache Group/Geronimo " + baseURL + "/Apache Group/Apache2";
69
70         String JavaDoc normal = RMIClassLoaderSpiImpl.normalizeCodebase(codebase);
71         assertEquals(normalizedBaseURL + "/Apache%20Group/Geronimo " +
72                      normalizedBaseURL + "/Apache%20Group/Apache2", normal);
73     }
74     
75     public void testGetClassAnnotationWithClassLoaderServerAware() throws Exception JavaDoc {
76         String JavaDoc url1 = "http://localhost:8090/Tester1";
77         String JavaDoc url2 = "http://localhost:8090/Tester2";
78         MockClassLoader cl = new MockClassLoader(getClass().getClassLoader(), new URL JavaDoc[] {new URL JavaDoc(url1), new URL JavaDoc(url2)});
79
80         Class JavaDoc clazz = cl.loadClass(RMIClassLoaderSpiImplTest.class.getName());
81         
82         RMIClassLoaderSpiImpl impl = new RMIClassLoaderSpiImpl();
83         String JavaDoc annotations = impl.getClassAnnotation(clazz);
84         assertEquals(url1 + " " + url2, annotations);
85     }
86     
87     private static class MockClassLoader extends ClassLoader JavaDoc implements RMIClassLoaderSpiImpl.ClassLoaderServerAware {
88         private final ClassLoader JavaDoc delegate;
89         private final URL JavaDoc[] urls;
90
91         private MockClassLoader(ClassLoader JavaDoc delegate, URL JavaDoc[] urls) {
92             this.delegate = delegate;
93             this.urls = urls;
94         }
95         
96         public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
97             if (name.startsWith("java")) {
98                 return delegate.loadClass(name);
99             }
100             String JavaDoc resourceName = name.replace('.', '/') + ".class";
101             InputStream JavaDoc in = delegate.getResourceAsStream(resourceName);
102             try {
103                 byte[] buffer = new byte[1024];
104                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
105                 int read = 0;
106                 try {
107                     while (0 < (read = in.read(buffer))) {
108                         out.write(buffer, 0, read);
109                     }
110                 } catch (IOException JavaDoc e) {
111                     fail();
112                     return null;
113                 }
114                 return defineClass(name, out.toByteArray(), 0, out.size());
115             } finally {
116                 if (in != null)
117                 {
118                     try {
119                         in.close();
120                     } catch (IOException JavaDoc ignored) {
121                         // ignored
122
}
123                 }
124             }
125         }
126         
127         public URL JavaDoc[] getClassLoaderServerURLs() {
128             return urls;
129         }
130     }
131 }
132
Popular Tags