1 22 package org.objectweb.petals.classloader; 23 24 import java.io.DataInputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 import junit.framework.TestCase; 33 34 38 public class ComponentClassLoaderTest extends TestCase { 39 40 43 private ComponentClassLoader componentClassLoader; 44 45 48 private SharedLibraryClassLoader sharedLibraryClassLoader; 49 50 public void setUp() throws IOException { 51 SharedLibrariesClassLoader sharedLibrariesClassLoader = new SharedLibrariesClassLoader(); 52 List <URL > baseUrls = new ArrayList <URL >(); 53 List <String > classpathLocations = new ArrayList <String >(); 54 classpathLocations.add("petals-core-test.jar"); 55 String baseDir = this.getClass().getResource(".").toString(); 56 baseDir = baseDir.substring(0, baseDir.indexOf("target")); 57 baseDir = baseDir.substring(baseDir.indexOf(":") + 1); 58 baseUrls.add(new URL ("file:" + baseDir.replace(File.separator, "/") 59 + "src/test-data/")); 60 baseUrls.add(new URL ("file:" + baseDir.replace(File.separator, "/") 61 + "target/test-classes/")); 62 baseUrls.add(new URL ("file:" + baseDir.replace(File.separator, "/") 63 + "target/")); 64 sharedLibraryClassLoader = new SharedLibraryClassLoader(baseUrls 65 .toArray(new URL [0]), classpathLocations); 66 sharedLibraryClassLoader.setParentFirst(false); 67 sharedLibrariesClassLoader.addSharedLibraryClassLoader("1", 68 sharedLibraryClassLoader); 69 70 baseUrls = new ArrayList <URL >(); 71 classpathLocations = new ArrayList <String >(); 72 classpathLocations.add("petals-common-test.jar"); 73 74 baseDir = baseDir.replace("core", "container"); 75 baseUrls.add(new URL ("file:" + baseDir.replace(File.separator, "/") 76 + "src/test-data/")); 77 List <String > slNames = new ArrayList <String >(); 80 slNames.add("1"); 81 componentClassLoader = new ComponentClassLoader(baseUrls 82 .toArray(new URL [0]), classpathLocations, 83 sharedLibrariesClassLoader, slNames); 84 } 85 86 89 public void testGetClasspath() { 90 String compoCP = "testGetClasspath - component classpath " 91 + componentClassLoader.getClasspath(); 92 compoCP.length(); 93 String slCP = "testGetClasspath - shared library classpath " 94 + sharedLibraryClassLoader.getClasspath(); 95 slCP.length(); 96 } 97 98 102 public void testGetResourceInSelfAndParentSelfFirst() { 103 componentClassLoader.setParentFirst(false); 104 URL url = componentClassLoader.getResource("resourceBoth.txt"); 105 if (url != null) { 106 107 } else { 108 109 fail("testGetResourceInSelfAndParentSelfFirst - resource not found "); 110 } 111 } 112 113 117 public void testGetResourceInSelfAndParentParentFirst() { 118 119 componentClassLoader.setParentFirst(true); 120 URL url = componentClassLoader.getResource("resourceBoth.txt"); 121 if (url != null) { 122 123 } else { 124 125 fail("testGetResourceInSelfAndParentParentFirst - resource not found "); 126 } 127 } 128 129 135 public void testGetResourceAsStreamInSelfAndParentSelfFirst() 136 throws IOException { 137 138 componentClassLoader.setParentFirst(false); 139 InputStream inputStream = componentClassLoader 140 .getResourceAsStream("resourceBoth.txt"); 141 if (inputStream != null) { 142 DataInputStream dis = new DataInputStream (inputStream); 143 byte[] content = new byte[dis.available()]; 144 dis.readFully(content); 145 146 } else { 147 148 fail("testGetResourceAsStreamInSelfAndParentSelfFirst - resource not found "); 149 } 150 } 151 152 159 public void testGetResourceAsStreamInSelfAndParentParentFirst() 160 throws IOException { 161 162 componentClassLoader.setParentFirst(true); 163 InputStream inputStream = componentClassLoader 164 .getResourceAsStream("resourceBoth.txt"); 165 if (inputStream != null) { 166 DataInputStream dis = new DataInputStream (inputStream); 167 byte[] content = new byte[dis.available()]; 168 dis.readFully(content); 169 170 } else { 171 172 fail("testGetResourceAsStreamInSelfAndParentParentFirst - resource not found "); 173 } 174 } 175 176 182 public void testGetResourceAsStreamInParentSelfFirst() throws IOException { 183 184 componentClassLoader.setParentFirst(false); 185 InputStream inputStream = componentClassLoader 186 .getResourceAsStream("resourceCore.txt"); 187 if (inputStream != null) { 188 DataInputStream dis = new DataInputStream (inputStream); 189 byte[] content = new byte[dis.available()]; 190 dis.readFully(content); 191 192 } else { 193 194 fail("testGetResourceAsStreamInParentSelfFirst - resource not found "); 195 } 196 } 197 198 204 public void testGetResourceAsStreamInSelfParentFirst() throws IOException { 205 206 componentClassLoader.setParentFirst(true); 207 InputStream inputStream = componentClassLoader 208 .getResourceAsStream("resourceContainer.txt"); 209 if (inputStream != null) { 210 DataInputStream dis = new DataInputStream (inputStream); 211 byte[] content = new byte[dis.available()]; 212 dis.readFully(content); 213 214 } else { 215 216 fail("testGetResourceAsStreamInSelfParentFirst - resource not found "); 217 } 218 } 219 220 223 public void testLoadClassParentFirst() { 224 225 componentClassLoader.setParentFirst(true); 226 Class clazz; 227 try { 228 clazz = componentClassLoader.loadClass( 229 "org.objectweb.petals.ServiceContext", false); 230 if (clazz != null) { 231 232 } else { 233 234 fail("testLoadClassParentFirst - class not found "); 235 } 236 } catch (ClassNotFoundException e) { 237 238 fail("testLoadClassParentFirst - class not found "); 239 } 240 } 241 242 245 public void testLoadClassSelfFirst() { 246 247 componentClassLoader.setParentFirst(false); 248 Class clazz; 249 try { 250 clazz = componentClassLoader.loadClass( 251 "org.objectweb.petals.PetalsException", false); 252 if (clazz != null) { 253 254 } else { 255 256 fail("testLoadClassSelfFirst - class not found "); 257 } 258 } catch (ClassNotFoundException e) { 259 260 fail("testLoadClassSelfFirst - class not found "); 261 } 262 } 263 264 271 public void testLoadClassInSelfAndParentParentFirst() 272 throws InstantiationException , IllegalAccessException { 273 274 componentClassLoader.setParentFirst(true); 275 Class clazz; 276 try { 277 clazz = componentClassLoader.loadClass( 278 "org.objectweb.petals.classloader.TestClassloader", false); 279 if (clazz != null) { 280 281 } else { 282 283 fail("testLoadClassInSelfAndParentParentFirst - class not found "); 284 } 285 } catch (ClassNotFoundException e) { 286 287 fail("testLoadClassInSelfAndParentParentFirst - class not found "); 288 } 289 } 290 291 298 public void testLoadClassInSelfAndParentSelfFirst() 299 throws InstantiationException , IllegalAccessException { 300 301 componentClassLoader.setParentFirst(false); 302 Class clazz; 303 try { 304 clazz = componentClassLoader.loadClass( 305 "org.objectweb.petals.classloader.TestClassloader", false); 306 if (clazz != null) { 307 308 } else { 309 310 fail("testLoadClassInSelfAndParentSelfFirst - class not found "); 311 } 312 } catch (ClassNotFoundException e) { 313 314 fail("testLoadClassInSelfAndParentSelfFirst - class not found "); 315 } 316 } 317 318 } 319 | Popular Tags |