1 26 27 package net.sourceforge.groboutils.util.classes.v1.jdk0; 28 29 import net.sourceforge.groboutils.util.classes.v1.*; 30 import net.sourceforge.groboutils.junit.v1.iftc.*; 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 import java.net.URL ; 38 39 40 47 public class UrlClassLoaderUTest extends TestCase 48 { 49 private static final Class THIS_CLASS = UrlClassLoaderUTest.class; 50 51 public UrlClassLoaderUTest( String name ) 52 { 53 super( name ); 54 } 55 56 public static Test suite() 57 { 58 InterfaceTestSuite suite = IUrlClassLoaderUTestI.suite(); 59 suite.addTestSuite( THIS_CLASS ); 60 suite.addFactory( new CxFactory( "A" ) { 61 public Object createImplObject() { 62 return createLoader(); 63 } 64 } ); 65 66 return suite; 67 } 68 69 public static void main( String [] args ) 70 { 71 String [] name = { THIS_CLASS.getName() }; 72 73 76 junit.textui.TestRunner.main( name ); 77 } 78 79 protected void setUp() throws Exception 80 { 81 super.setUp(); 82 83 } 85 86 87 protected void tearDown() throws Exception 88 { 89 91 super.tearDown(); 92 } 93 94 95 protected static UrlClassLoader createLoader() 96 { 97 return new UrlClassLoader(); 98 } 99 100 101 102 103 public static class MyInner 104 { 105 } 107 public static final String MY_INNER_RES = 108 "UrlClassLoaderUTest$MyInner.class"; 109 public static final String MY_INNER_CLASS = MyInner.class.getName(); 110 public static final String MY_INNER_FULL_RES = 111 '/' + MY_INNER_CLASS.replace( '.', '/' ) + ".class"; 112 113 public static class InnerClass 114 { 115 public InnerClass() 116 { 117 } 119 } 120 121 public void testFlush1() 122 { 123 126 UrlClassLoader loader = createLoader(); 127 128 URL url = this.getClass().getResource( MY_INNER_RES ); 130 assertNotNull( 131 "No resource found for "+MY_INNER_RES, 132 url ); 133 System.out.println("Found class in URL "+url); 134 String urlStr = url.toString(); 135 if (urlStr.endsWith( MY_INNER_FULL_RES )) 136 { 137 urlStr = urlStr.substring( 0, urlStr.length() - 138 MY_INNER_FULL_RES.length() ); 139 } 140 141 Class c = loader.loadClass( MY_INNER_CLASS, urlStr ); 142 assertNotNull( "loadClass( "+MY_INNER_CLASS+" ) returned null.", c ); 143 assertEquals( "loadClass( "+MY_INNER_CLASS+ 144 " ) returned the wrong class.", 145 MyInner.class.getName(), c.getName() ); 146 147 byte b[] = loader.getBytecode( MY_INNER_CLASS ); 148 assertGetBytecodeResult( MY_INNER_CLASS, b, url ); 149 150 loader.flush(); 151 152 b = loader.getBytecode( MY_INNER_CLASS ); 154 assertEquals( "getBytecode( "+MY_INNER_CLASS+" ) did not return null.", 155 null, b ); 156 } 157 158 159 public void testGetBytecode1() 160 { 161 UrlClassLoader loader = (UrlClassLoader)createLoader(); 162 163 URL url = this.getClass().getResource( MY_INNER_RES ); 165 assertNotNull( "No resource for inner class "+MY_INNER_RES, url ); 166 System.out.println("Found class in URL "+url); 167 String urlStr = url.toString(); 168 if (urlStr.endsWith( MY_INNER_FULL_RES )) 169 { 170 urlStr = urlStr.substring( 0, urlStr.length() - 171 MY_INNER_FULL_RES.length() ); 172 } 173 174 Class c = loader.loadClass( MY_INNER_CLASS, urlStr ); 175 assertNotNull( "loadClass( "+MY_INNER_CLASS+" ) returned null.", c ); 176 assertEquals( "loadClass( "+MY_INNER_CLASS+ 177 " ) returned the wrong class.", 178 MY_INNER_CLASS, c.getName() ); 179 180 byte b[] = loader.getBytecode( MY_INNER_CLASS ); 181 assertGetBytecodeResult( MY_INNER_CLASS, b, url ); 182 } 183 184 185 protected void assertGetBytecodeResult( String className, byte[] result, 186 URL sourceURL ) 187 { 188 if (result == null) 189 { 190 try 194 { 195 InputStream is = sourceURL.openStream(); 196 is.close(); 197 fail("getBytecode( "+className+" ) returned null, "+ 198 "but no SecurityException was thrown." ); 199 } 200 catch (SecurityException e) 201 { 202 } 205 catch (IOException ioe) 206 { 207 ioe.printStackTrace(); 208 fail("Should not have encountered an IOException."); 209 } 210 } 211 else 212 { 213 assertTrue( "getBytecode( "+className+" ) returned no data.", 216 result.length > 0 ); 217 } 218 } 219 } 220 | Popular Tags |