1 17 package org.apache.geronimo.deployment; 18 19 import java.io.File ; 20 import java.net.URI ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.Collections ; 24 25 import javax.sql.DataSource ; 26 27 import junit.framework.TestCase; 28 import net.sf.cglib.core.DefaultGeneratorStrategy; 29 import net.sf.cglib.proxy.Enhancer; 30 import net.sf.cglib.proxy.MethodInterceptor; 31 import org.apache.geronimo.kernel.Jsr77Naming; 32 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 33 import org.apache.geronimo.kernel.config.SimpleConfigurationManager; 34 import org.apache.geronimo.kernel.repository.Artifact; 35 import org.apache.geronimo.kernel.repository.Environment; 36 import org.apache.geronimo.kernel.repository.ArtifactManager; 37 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 38 import org.apache.geronimo.kernel.repository.ArtifactResolver; 39 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 40 41 44 public class DeploymentContextTest extends TestCase { 45 private byte[] classBytes; 46 47 public void testAddClass() throws Exception { 48 File basedir = File.createTempFile("car", "tmp"); 49 basedir.delete(); 50 basedir.mkdirs(); 51 try { 52 basedir.deleteOnExit(); 53 Environment environment = new Environment(); 54 Artifact configId = new Artifact("foo", "artifact", "1", "car"); 55 environment.setConfigId(configId); 56 ArtifactManager artifactManager = new DefaultArtifactManager(); 57 ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.EMPTY_SET, null); 58 SimpleConfigurationManager configurationManager = new SimpleConfigurationManager(Collections.EMPTY_SET, artifactResolver, Collections.EMPTY_SET); 59 DeploymentContext context = new DeploymentContext(basedir, null, environment, null, ConfigurationModuleType.CAR, new Jsr77Naming(), configurationManager, Collections.EMPTY_SET); 60 Enhancer enhancer = new Enhancer(); 61 enhancer.setInterfaces(new Class []{DataSource .class}); 62 enhancer.setCallbackType(MethodInterceptor.class); 63 enhancer.setStrategy(new DefaultGeneratorStrategy() { 64 public byte[] transform(byte[] b) { 65 classBytes = b; 66 return b; 67 } 68 }); 69 enhancer.setClassLoader(new URLClassLoader (new URL [0], this.getClass().getClassLoader())); 70 Class type = enhancer.createClass(); 71 URI location = new URI ("cglib/"); 72 context.addClass(location, type.getName(), classBytes); 73 ClassLoader cl = context.getClassLoader(); 74 Class loadedType = cl.loadClass(type.getName()); 75 assertTrue(DataSource .class.isAssignableFrom(loadedType)); 76 assertTrue(type != loadedType); 77 } finally { 78 recursiveDelete(basedir); 79 } 80 } 81 82 private void recursiveDelete(File file) { 83 if (file.isDirectory()) { 84 File [] files = file.listFiles(); 85 for (int i = 0; i < files.length; i++) { 86 recursiveDelete(files[i]); 87 } 88 } 89 file.delete(); 90 } 91 } 92 | Popular Tags |