KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > DeploymentContextTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.deployment;
18
19 import java.io.File JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import javax.sql.DataSource JavaDoc;
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 /**
42  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
43  */

44 public class DeploymentContextTest extends TestCase {
45     private byte[] classBytes;
46
47     public void testAddClass() throws Exception JavaDoc {
48         File JavaDoc 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 JavaDoc[]{DataSource JavaDoc.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 JavaDoc(new URL JavaDoc[0], this.getClass().getClassLoader()));
70             Class JavaDoc type = enhancer.createClass();
71             URI JavaDoc location = new URI JavaDoc("cglib/");
72             context.addClass(location, type.getName(), classBytes);
73             ClassLoader JavaDoc cl = context.getClassLoader();
74             Class JavaDoc loadedType = cl.loadClass(type.getName());
75             assertTrue(DataSource JavaDoc.class.isAssignableFrom(loadedType));
76             assertTrue(type != loadedType);
77         } finally {
78             recursiveDelete(basedir);
79         }
80     }
81
82     private void recursiveDelete(File JavaDoc file) {
83         if (file.isDirectory()) {
84             File JavaDoc[] 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