1 17 18 package org.apache.geronimo.system.rmi; 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 28 import junit.framework.TestCase; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 38 public class RMIClassLoaderSpiImplTest extends TestCase { 39 private static final Log log = LogFactory.getLog(RMIClassLoaderSpiImplTest.class); 40 41 private String baseURL; 42 private String normalizedBaseURL; 43 44 protected void setUp() throws Exception { 45 File dir = new File (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 { 62 URL url = new URL (baseURL + "/Apache Group/Geronimo"); 63 URL normal = RMIClassLoaderSpiImpl.normalizeURL(url); 64 assertEquals(normalizedBaseURL + "/Apache%20Group/Geronimo", normal.toString()); 65 } 66 67 public void testNormalizeCodebase() throws MalformedURLException { 68 String codebase = baseURL + "/Apache Group/Geronimo " + baseURL + "/Apache Group/Apache2"; 69 70 String normal = RMIClassLoaderSpiImpl.normalizeCodebase(codebase); 71 assertEquals(normalizedBaseURL + "/Apache%20Group/Geronimo " + 72 normalizedBaseURL + "/Apache%20Group/Apache2", normal); 73 } 74 75 public void testGetClassAnnotationWithClassLoaderServerAware() throws Exception { 76 String url1 = "http://localhost:8090/Tester1"; 77 String url2 = "http://localhost:8090/Tester2"; 78 MockClassLoader cl = new MockClassLoader(getClass().getClassLoader(), new URL [] {new URL (url1), new URL (url2)}); 79 80 Class clazz = cl.loadClass(RMIClassLoaderSpiImplTest.class.getName()); 81 82 RMIClassLoaderSpiImpl impl = new RMIClassLoaderSpiImpl(); 83 String annotations = impl.getClassAnnotation(clazz); 84 assertEquals(url1 + " " + url2, annotations); 85 } 86 87 private static class MockClassLoader extends ClassLoader implements RMIClassLoaderSpiImpl.ClassLoaderServerAware { 88 private final ClassLoader delegate; 89 private final URL [] urls; 90 91 private MockClassLoader(ClassLoader delegate, URL [] urls) { 92 this.delegate = delegate; 93 this.urls = urls; 94 } 95 96 public Class loadClass(String name) throws ClassNotFoundException { 97 if (name.startsWith("java")) { 98 return delegate.loadClass(name); 99 } 100 String resourceName = name.replace('.', '/') + ".class"; 101 InputStream in = delegate.getResourceAsStream(resourceName); 102 try { 103 byte[] buffer = new byte[1024]; 104 ByteArrayOutputStream out = new ByteArrayOutputStream (); 105 int read = 0; 106 try { 107 while (0 < (read = in.read(buffer))) { 108 out.write(buffer, 0, read); 109 } 110 } catch (IOException 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 ignored) { 121 } 123 } 124 } 125 } 126 127 public URL [] getClassLoaderServerURLs() { 128 return urls; 129 } 130 } 131 } 132 | Popular Tags |