KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > j2ee > deployment > EARConfigBuilderTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 package org.apache.geronimo.j2ee.deployment;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30 import javax.management.MalformedObjectNameException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.xml.namespace.QName JavaDoc;
33
34 import junit.extensions.TestSetup;
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38 import org.apache.geronimo.common.DeploymentException;
39 import org.apache.geronimo.deployment.DeploymentContext;
40 import org.apache.geronimo.deployment.util.DeploymentUtil;
41 import org.apache.geronimo.gbean.GBeanData;
42 import org.apache.geronimo.gbean.GBeanInfo;
43 import org.apache.geronimo.gbean.GBeanInfoBuilder;
44 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
45 import org.apache.geronimo.kernel.Kernel;
46 import org.apache.geronimo.kernel.KernelFactory;
47 import org.apache.geronimo.kernel.config.ConfigurationManagerImpl;
48 import org.apache.geronimo.kernel.config.Configuration;
49 import org.apache.geronimo.kernel.config.ConfigurationData;
50 import org.apache.geronimo.kernel.config.ConfigurationStore;
51 import org.apache.geronimo.kernel.config.InvalidConfigException;
52 import org.apache.geronimo.kernel.config.NoSuchConfigException;
53 import org.apache.geronimo.kernel.jmx.JMXUtil;
54
55 /**
56  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
57  */

58 public class EARConfigBuilderTest extends TestCase {
59     private static final File JavaDoc basedir = new File JavaDoc(System.getProperty("basedir", System.getProperty("user.dir")));
60
61     private static JarFile JavaDoc earFile;
62     private static MockEJBConfigBuilder ejbConfigBuilder = new MockEJBConfigBuilder();
63     private static MockWARConfigBuilder webConfigBuilder = new MockWARConfigBuilder();
64     private static MockConnectorConfigBuilder connectorConfigBuilder = new MockConnectorConfigBuilder();
65     private static ResourceReferenceBuilder resourceReferenceBuilder = connectorConfigBuilder;
66     private static ModuleBuilder appClientConfigBuilder = null;
67     private static ServiceReferenceBuilder serviceReferenceBuilder = new ServiceReferenceBuilder() {
68
69         //it could return a Service or a Reference, we don't care
70
public Object JavaDoc createService(Class JavaDoc serviceInterface, URI JavaDoc wsdlURI, URI JavaDoc jaxrpcMappingURI, QName JavaDoc serviceQName, Map JavaDoc portComponentRefMap, List JavaDoc handlerInfos, Map JavaDoc portLocationMap, Map JavaDoc credentialsNameMap, DeploymentContext deploymentContext, Module module, ClassLoader JavaDoc classLoader) {
71             return null;
72         }
73     };
74
75     private static final String JavaDoc j2eeServerName = "someDomain";
76     private static final ObjectName JavaDoc transactionManagerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=TransactionManager");
77     private static final ObjectName JavaDoc connectionTrackerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=ConnectionTracker");
78     private static final ObjectName JavaDoc transactionalTimerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=ThreadPooledTimer,name=TransactionalThreaPooledTimer");
79     private static final ObjectName JavaDoc nonTransactionalTimerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=ThreadPooledTimer,name=NonTransactionalThreaPooledTimer");
80     private URI JavaDoc defaultParentId;
81     private static final Map JavaDoc portMap = null;
82
83     protected void setUp() throws Exception JavaDoc {
84         defaultParentId = new URI JavaDoc("org/apache/geronimo/Server");
85     }
86
87     public static Test suite() throws Exception JavaDoc {
88         TestSuite inner = new TestSuite(EARConfigBuilderTest.class);
89         TestSetup setup14 = new TestSetup(inner) {
90             protected void setUp() throws Exception JavaDoc {
91                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-ear14/test-ear.ear"));
92                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar", null, null, null);
93                 webConfigBuilder.contextRoot = "test";
94                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war", null, null, null, portMap);
95                 webConfigBuilder.webModule.setContextRoot("test");
96                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
97             }
98
99             protected void tearDown() {
100                 DeploymentUtil.close(earFile);
101                 close(ejbConfigBuilder.ejbModule);
102                 close(webConfigBuilder.webModule);
103                 close(connectorConfigBuilder.connectorModule);
104             }
105         };
106         TestSetup setupNaked14 = new TestSetup(inner) {
107             protected void setUp() throws Exception JavaDoc {
108                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-ear14/test-naked-ear.ear"));
109                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar", null, null, null);
110                 webConfigBuilder.contextRoot = "test";
111                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war", null, null, null, portMap);
112                 webConfigBuilder.webModule.setContextRoot("test");
113                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
114             }
115
116             protected void tearDown() {
117                 DeploymentUtil.close(earFile);
118                 close(ejbConfigBuilder.ejbModule);
119                 close(webConfigBuilder.webModule);
120                 close(connectorConfigBuilder.connectorModule);
121             }
122         };
123         TestSetup setup13 = new TestSetup(inner) {
124             protected void setUp() throws Exception JavaDoc {
125                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-ear13/test-ear.ear"));
126                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar", null, null, null);
127                 webConfigBuilder.contextRoot = "test";
128                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war", null, null, null, portMap);
129                 webConfigBuilder.webModule.setContextRoot("test");
130                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
131             }
132
133             protected void tearDown() {
134                 DeploymentUtil.close(earFile);
135                 close(ejbConfigBuilder.ejbModule);
136                 close(webConfigBuilder.webModule);
137                 close(connectorConfigBuilder.connectorModule);
138             }
139         };
140         TestSetup setupNaked13 = new TestSetup(inner) {
141             protected void setUp() throws Exception JavaDoc {
142                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-ear13/test-naked-ear.ear"));
143                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar", null, null, null);
144                 webConfigBuilder.contextRoot = "test";
145                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war", null, null, null, portMap);
146                 webConfigBuilder.webModule.setContextRoot("test");
147                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
148             }
149
150             protected void tearDown() {
151                 DeploymentUtil.close(earFile);
152                 close(ejbConfigBuilder.ejbModule);
153                 close(webConfigBuilder.webModule);
154                 close(connectorConfigBuilder.connectorModule);
155             }
156         };
157         TestSetup setupUnpacked = new TestSetup(inner) {
158             protected void setUp() throws Exception JavaDoc {
159                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-unpacked-ear/full/"));
160                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar/", null, null, null);
161                 webConfigBuilder.contextRoot = "test";
162                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war/", null, null, null, portMap);
163                 webConfigBuilder.webModule.setContextRoot("test");
164                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
165             }
166
167             protected void tearDown() {
168                 DeploymentUtil.close(earFile);
169                 close(ejbConfigBuilder.ejbModule);
170                 close(webConfigBuilder.webModule);
171                 close(connectorConfigBuilder.connectorModule);
172             }
173         };
174         TestSetup setupUnpackedNaked = new TestSetup(inner) {
175             protected void setUp() throws Exception JavaDoc {
176                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-unpacked-ear/naked/"));
177                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar/", null, null, null);
178                 webConfigBuilder.contextRoot = "test";
179                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war", null, null, null, portMap);
180                 webConfigBuilder.webModule.setContextRoot("test");
181                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
182             }
183
184             protected void tearDown() {
185                 DeploymentUtil.close(earFile);
186                 close(ejbConfigBuilder.ejbModule);
187                 close(webConfigBuilder.webModule);
188                 close(connectorConfigBuilder.connectorModule);
189             }
190         };
191         TestSetup setupUnpackedAltDD = new TestSetup(inner) {
192             protected void setUp() throws Exception JavaDoc {
193                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-unpacked-ear/alt-dd/"));
194                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar/", null, null, null);
195                 webConfigBuilder.contextRoot = "test";
196                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war/", null, null, null, portMap);
197                 webConfigBuilder.webModule.setContextRoot("test");
198                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
199             }
200
201             protected void tearDown() {
202                 DeploymentUtil.close(earFile);
203                 close(ejbConfigBuilder.ejbModule);
204                 close(webConfigBuilder.webModule);
205                 close(connectorConfigBuilder.connectorModule);
206             }
207         };
208         TestSetup setupPackedAltDD = new TestSetup(inner) {
209             protected void setUp() throws Exception JavaDoc {
210                 earFile = DeploymentUtil.createJarFile(new File JavaDoc(basedir, "target/test-unpacked-ear/alt-dd.ear"));
211                 ejbConfigBuilder.ejbModule = new EJBModule(false, null, null, null, "test-ejb-jar.jar/", null, null, null);
212                 webConfigBuilder.contextRoot = "test";
213                 webConfigBuilder.webModule = new WebModule(false, null, null, null, "test-war.war/", null, null, null, portMap);
214                 webConfigBuilder.webModule.setContextRoot("test");
215                 connectorConfigBuilder.connectorModule = new ConnectorModule(false, null, null, null, "test-rar.rar", null, null, null);
216             }
217
218             protected void tearDown() {
219                 DeploymentUtil.close(earFile);
220                 close(ejbConfigBuilder.ejbModule);
221                 close(webConfigBuilder.webModule);
222                 close(connectorConfigBuilder.connectorModule);
223             }
224         };
225
226         TestSuite suite = new TestSuite();
227         suite.addTest(setup14);
228         suite.addTest(setupNaked14);
229         suite.addTest(setup13);
230         suite.addTest(setupNaked13);
231         suite.addTest(setupUnpacked);
232         suite.addTest(setupUnpackedNaked);
233         suite.addTest(setupUnpackedAltDD);
234         suite.addTest(setupPackedAltDD);
235         return suite;
236     }
237
238     public void testBuildConfiguration() throws Exception JavaDoc {
239         Kernel kernel = KernelFactory.newInstance().createKernel("foo");
240         kernel.boot();
241
242         GBeanData store = new GBeanData(JMXUtil.getObjectName("foo:j2eeType=ConfigurationStore,name=mock"), MockConfigStore.GBEAN_INFO);
243         kernel.loadGBean(store, this.getClass().getClassLoader());
244         kernel.startGBean(store.getName());
245
246         EARConfigBuilder configBuilder = new EARConfigBuilder(defaultParentId, transactionManagerObjectName, connectionTrackerObjectName, transactionalTimerObjectName, nonTransactionalTimerObjectName, null, null, ejbConfigBuilder, ejbConfigBuilder, webConfigBuilder, connectorConfigBuilder, resourceReferenceBuilder, appClientConfigBuilder, serviceReferenceBuilder, kernel);
247
248         ObjectName JavaDoc configurationManagerName = new ObjectName JavaDoc(":j2eeType=ConfigurationManager,name=Basic");
249         GBeanData configurationManagerData = new GBeanData(configurationManagerName, ConfigurationManagerImpl.GBEAN_INFO);
250         configurationManagerData.setReferencePatterns("Stores", Collections.singleton(store.getName()));
251         kernel.loadGBean(configurationManagerData, getClass().getClassLoader());
252         kernel.startGBean(configurationManagerName);
253
254         File JavaDoc tempDir = null;
255         try {
256             tempDir = DeploymentUtil.createTempDir();
257             Object JavaDoc plan = configBuilder.getDeploymentPlan(null, earFile);
258             configBuilder.buildConfiguration(plan, earFile, tempDir);
259         } finally {
260             DeploymentUtil.recursiveDelete(tempDir);
261             kernel.shutdown();
262         }
263     }
264
265     public void testNoEJBDeployer() throws Exception JavaDoc {
266         EARConfigBuilder configBuilder = new EARConfigBuilder(defaultParentId, transactionManagerObjectName, connectionTrackerObjectName, transactionalTimerObjectName, nonTransactionalTimerObjectName, null, null, null, null, webConfigBuilder, connectorConfigBuilder, resourceReferenceBuilder, appClientConfigBuilder, serviceReferenceBuilder, null);
267
268         File JavaDoc tempDir = null;
269         try {
270             tempDir = DeploymentUtil.createTempDir();
271             Object JavaDoc plan = configBuilder.getDeploymentPlan(null, earFile);
272             configBuilder.buildConfiguration(plan, earFile, tempDir);
273             fail("Should have thrown a DeploymentException");
274         } catch (DeploymentException e) {
275             // expected
276
} finally {
277             DeploymentUtil.recursiveDelete(tempDir);
278         }
279     }
280
281     public void testNoWARDeployer() throws Exception JavaDoc {
282         EARConfigBuilder configBuilder = new EARConfigBuilder(defaultParentId, transactionManagerObjectName, connectionTrackerObjectName, transactionalTimerObjectName, nonTransactionalTimerObjectName, null, null, ejbConfigBuilder, null, null, connectorConfigBuilder, resourceReferenceBuilder, appClientConfigBuilder, serviceReferenceBuilder, null);
283
284         File JavaDoc tempDir = null;
285         try {
286             tempDir = DeploymentUtil.createTempDir();
287             Object JavaDoc plan = configBuilder.getDeploymentPlan(null, earFile);
288             configBuilder.buildConfiguration(plan, earFile, tempDir);
289             fail("Should have thrown a DeploymentException");
290         } catch (DeploymentException e) {
291             // expected
292
} finally {
293             DeploymentUtil.recursiveDelete(tempDir);
294         }
295     }
296
297     public void testNoConnectorDeployer() throws Exception JavaDoc {
298         EARConfigBuilder configBuilder = new EARConfigBuilder(defaultParentId, transactionManagerObjectName, connectionTrackerObjectName, transactionalTimerObjectName, nonTransactionalTimerObjectName, null, null, ejbConfigBuilder, null, webConfigBuilder, null, resourceReferenceBuilder, appClientConfigBuilder, serviceReferenceBuilder, null);
299
300         File JavaDoc tempDir = null;
301         try {
302             tempDir = DeploymentUtil.createTempDir();
303             Object JavaDoc plan = configBuilder.getDeploymentPlan(null, earFile);
304             configBuilder.buildConfiguration(plan, earFile, tempDir);
305             fail("Should have thrown a DeploymentException");
306         } catch (DeploymentException e) {
307             // expected
308
} finally {
309             DeploymentUtil.recursiveDelete(tempDir);
310         }
311     }
312
313     private static void close(Module module) {
314         if (module != null) {
315             module.close();
316         }
317     }
318
319     public static class MockConfigStore implements ConfigurationStore {
320         private final Kernel kernel;
321
322         public MockConfigStore(Kernel kernel) {
323             this.kernel = kernel;
324         }
325
326         public URI JavaDoc install(URL JavaDoc source) throws IOException JavaDoc, InvalidConfigException {
327             return null;
328         }
329
330         public void install(ConfigurationData configurationData, File JavaDoc source) throws IOException JavaDoc, InvalidConfigException {
331         }
332
333         public void uninstall(URI JavaDoc configID) throws NoSuchConfigException, IOException JavaDoc {
334         }
335
336         public ObjectName JavaDoc loadConfiguration(URI JavaDoc configId) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
337             ObjectName JavaDoc configurationObjectName = null;
338             try {
339                 configurationObjectName = Configuration.getConfigurationObjectName(configId);
340             } catch (MalformedObjectNameException JavaDoc e) {
341                 throw new InvalidConfigException(e);
342             }
343             GBeanData configData = new GBeanData(configurationObjectName, Configuration.GBEAN_INFO);
344             configData.setAttribute("id", configId);
345             configData.setAttribute("domain", "test");
346             configData.setAttribute("server", "bar");
347             configData.setAttribute("gBeanState", NO_OBJECTS_OS);
348
349             try {
350                 kernel.loadGBean(configData, Configuration.class.getClassLoader());
351             } catch (Exception JavaDoc e) {
352                 throw new InvalidConfigException("Unable to register configuration", e);
353             }
354
355             return configurationObjectName;
356         }
357
358         public boolean containsConfiguration(URI JavaDoc configID) {
359             return true;
360         }
361
362         public void updateConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, Exception JavaDoc {
363
364         }
365
366         public String JavaDoc getObjectName() {
367             return null;
368         }
369
370         public List JavaDoc listConfiguations() {
371             return null;
372         }
373
374         public File JavaDoc createNewConfigurationDir() {
375             return null;
376         }
377
378         public final static GBeanInfo GBEAN_INFO;
379
380         private static final byte[] NO_OBJECTS_OS;
381
382         static {
383             GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder(MockConfigStore.class, NameFactory.CONFIGURATION_STORE);
384             infoBuilder.addInterface(ConfigurationStore.class);
385             infoBuilder.addAttribute("kernel", Kernel.class, false);
386             infoBuilder.setConstructor(new String JavaDoc[] {"kernel"});
387             GBEAN_INFO = infoBuilder.getBeanInfo();
388
389             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
390             try {
391                 ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
392                 oos.flush();
393                 NO_OBJECTS_OS = baos.toByteArray();
394             } catch (IOException JavaDoc e) {
395                 throw new RuntimeException JavaDoc(e);
396             }
397         }
398     }
399 }
400
Popular Tags