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.net.URLStreamHandlerFactory ; 30 import java.util.ArrayList ; 31 import java.util.Enumeration ; 32 import java.util.List ; 33 34 import junit.framework.TestCase; 35 36 import org.easymock.classextension.EasyMock; 37 38 43 public class PetalsClassLoaderTest extends TestCase { 44 45 48 private PetalsClassLoader petalsClassLoader; 49 50 public void setUp() throws IOException { 51 List <URL > baseUrls = new ArrayList <URL >(); 52 List <String > classpathLocations = new ArrayList <String >(); 53 String baseDir = this.getClass().getResource(".").toString(); 54 baseDir = baseDir.substring(0, baseDir.indexOf("target")); 55 baseDir = baseDir.substring(baseDir.indexOf(":") + 1); 56 baseUrls.add(new URL ("file:" 57 + baseDir.replace(File.separator, "/"))); 58 baseUrls.add(new URL ("file:" 59 + baseDir.replace(File.separator, "/") + "target/test-classes/")); 60 baseUrls.add(new URL ("file:" 61 + baseDir.replace(File.separator, "/") + "target/")); 62 baseUrls.add(new URL ("file:" 63 + baseDir.replace(File.separator, "/") 64 + "src/test-data/junit-3.8.jar")); 65 petalsClassLoader = new PetalsClassLoader(baseUrls.toArray(new URL [0]), 66 classpathLocations); 67 petalsClassLoader.setParentFirst(false); 68 } 69 70 74 public void testGetClasspath() { 75 petalsClassLoader.getClasspath(); 76 77 } 78 79 83 public void testLoadClassFromDirectory() { 84 try { 85 86 Class class1 = petalsClassLoader.loadClass( 87 "org.objectweb.petals.classloader.ComponentClassLoaderTest", 88 false); 89 class1.getName(); 90 91 } catch (ClassNotFoundException e) { 92 93 fail("Class not found : org.objectweb.petals.classloader.ComponentClassLoaderTest"); 94 } 95 } 96 97 101 public void testLoadClassFromJar() { 102 try { 103 104 Class class1 = petalsClassLoader.loadClass( 105 "junit.framework.TestCase", false); 106 class1.getName(); 107 108 } catch (ClassNotFoundException e) { 109 110 fail("Class not found : junit.framework.TestCase"); 111 } 112 } 113 114 118 public void testGetResourceFromAJar() { 119 URL url = petalsClassLoader.getResource("META-INF/MANIFEST.MF"); 120 if (url != null) { 121 } else { 122 fail("testGetResource - resource not found"); 123 } 124 } 125 126 130 public void testGetResourceFromADirectory() { 131 URL url = petalsClassLoader.getResource("ResourceToLoad"); 132 if (url != null) { 133 } else { 134 fail("testGetResource - resource not found"); 135 } 136 } 137 138 142 public void testGetResourceThatDoesNotExist() { 143 URL url = petalsClassLoader.getResource("test"); 144 if (url == null) { 145 } else { 146 fail("testGetResource - resource found problem"); 147 } 148 } 149 150 155 public void testGetResourceAsStream() throws IOException { 156 InputStream inputStream = petalsClassLoader 157 .getResourceAsStream("ResourceToLoad"); 158 if (inputStream != null) { 159 DataInputStream dis = new DataInputStream (inputStream); 160 byte[] content = new byte[dis.available()]; 161 dis.readFully(content); 162 } else { 163 fail("testGetResourceAsStream - stream not found"); 164 } 165 } 166 167 172 public void testGetResourceAsStreamFromAJar() throws IOException { 173 InputStream inputStream = petalsClassLoader 174 .getResourceAsStream("META-INF/MANIFEST.MF"); 175 if (inputStream != null) { 176 DataInputStream dis = new DataInputStream (inputStream); 177 byte[] content = new byte[dis.available()]; 178 dis.readFully(content); 179 } else { 180 fail("testGetResourceAsStream - stream not found"); 181 } 182 } 183 184 189 public void testGetResources() throws IOException { 190 Enumeration <URL > enumeration = petalsClassLoader 191 .getResources("ResourceToLoad"); 192 if (enumeration.hasMoreElements()) { 193 } else { 194 fail("testGetResource - resource not found"); 195 } 196 } 197 198 public void testConstructor() throws Throwable { 199 URLStreamHandlerFactory urlStreamHandlerFactory = EasyMock 200 .createMock(URLStreamHandlerFactory .class); 201 PetalsClassLoader petalsClassLoader = new PetalsClassLoader(new URL [0], 202 new ArrayList <String >(), this.getClass().getClassLoader(), 203 urlStreamHandlerFactory); 204 assertEquals(petalsClassLoader.parent, this.getClass().getClassLoader()); 205 petalsClassLoader.close(); 206 assertEquals(petalsClassLoader.getBases().length, 0); 207 } 208 209 } 210 | Popular Tags |