KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > service > ServiceConfigBuilderTest


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.service;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.SortedSet JavaDoc;
26 import java.util.TreeSet JavaDoc;
27 import java.util.jar.JarFile JavaDoc;
28
29 import javax.management.ObjectName JavaDoc;
30
31 import junit.framework.TestCase;
32 import org.apache.geronimo.deployment.DeploymentContext;
33 import org.apache.geronimo.deployment.FooBarBean;
34 import org.apache.geronimo.deployment.ModuleIDBuilder;
35 import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
36 import org.apache.geronimo.deployment.xbeans.ModuleDocument;
37 import org.apache.geronimo.deployment.xbeans.ModuleType;
38 import org.apache.geronimo.gbean.AbstractName;
39 import org.apache.geronimo.gbean.GBeanData;
40 import org.apache.geronimo.gbean.ReferenceCollection;
41 import org.apache.geronimo.gbean.ReferenceCollectionListener;
42 import org.apache.geronimo.kernel.Jsr77Naming;
43 import org.apache.geronimo.kernel.Naming;
44 import org.apache.geronimo.kernel.config.ConfigurationManager;
45 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
46 import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
47 import org.apache.geronimo.kernel.repository.Artifact;
48 import org.apache.geronimo.kernel.repository.ArtifactManager;
49 import org.apache.geronimo.kernel.repository.ArtifactResolver;
50 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
51 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
52 import org.apache.geronimo.kernel.repository.Environment;
53 import org.apache.geronimo.kernel.repository.ListableRepository;
54
55 /**
56  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
57  */

58 public class ServiceConfigBuilderTest extends TestCase {
59
60     private Environment parentEnvironment = new Environment();
61
62     public void testNonService() throws Exception JavaDoc {
63         URL JavaDoc url = getClass().getResource("/empty.jar");
64         File JavaDoc file = new File JavaDoc(url.getPath());
65         JarFile JavaDoc jar = new JarFile JavaDoc(file);
66         assertTrue(file.exists());
67         ServiceConfigBuilder builder = new ServiceConfigBuilder(parentEnvironment, null, new Jsr77Naming());
68         assertNull(builder.getDeploymentPlan(null, jar, new ModuleIDBuilder()));
69         jar.close();
70     }
71
72     public void testJavaBeanXmlAttribute() throws Exception JavaDoc {
73         ReferenceCollection referenceCollection = new MockReferenceCollection();
74         JavaBeanXmlAttributeBuilder javaBeanXmlAttributeBuilder = new JavaBeanXmlAttributeBuilder();
75         //this is kind of cheating, we rely on the builder to iterate through existing members of the collection.
76
referenceCollection.add(javaBeanXmlAttributeBuilder);
77         Naming naming = new Jsr77Naming();
78         NamespaceDrivenBuilder gbeanBuilder = new GBeanBuilder(referenceCollection, null);
79 // ConfigurationBuilder serviceBuilder = new ServiceConfigBuilder(parentEnvironment, null, Collections.singleton(gbeanBuilder), naming);
80
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
81         final URL JavaDoc plan1 = cl.getResource("services/plan1.xml");
82         ModuleDocument doc = ModuleDocument.Factory.parse(plan1);
83         ModuleType plan = doc.getModule();
84         File JavaDoc outFile = File.createTempFile("foo", "bar");
85         outFile.delete();
86         if (!outFile.mkdirs()) {
87             fail("could not create temp dir");
88         }
89         try {
90
91             Environment environment = EnvironmentBuilder.buildEnvironment(plan.getEnvironment());
92             MockRepository mockRepository = new MockRepository();
93             ArtifactManager artifactManager = new DefaultArtifactManager();
94             ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.singleton(mockRepository), null);
95             ConfigurationManager configurationManager = new SimpleConfigurationManager(Collections.EMPTY_SET, artifactResolver, Collections.EMPTY_SET);
96             AbstractName moduleName = naming.createRootName(environment.getConfigId(), "foo", "bar");
97             DeploymentContext context = new DeploymentContext(outFile, null, environment, moduleName, ConfigurationModuleType.CAR, naming, configurationManager, Collections.singleton(mockRepository));
98
99             gbeanBuilder.build(plan, context, context);
100             Set JavaDoc gbeanNames = context.getGBeanNames();
101             assertEquals(1, gbeanNames.size());
102             AbstractName beanName = (AbstractName) gbeanNames.iterator().next();
103             GBeanData data = context.getGBeanInstance(beanName);
104             FooBarBean fooBarBean = (FooBarBean) data.getAttribute("fooBarBean");
105             assertNotNull(fooBarBean);
106             assertEquals("foo", fooBarBean.getFoo());
107             assertEquals(10, fooBarBean.getBar());
108             FooBarBean inner = fooBarBean.getBean();
109             assertNotNull(inner);
110             assertEquals("foo2", inner.getFoo());
111             assertEquals(100, inner.getBar());
112             assertNull(inner.getBean());
113         } finally {
114             recursiveDelete(outFile);
115         }
116     }
117
118     private void recursiveDelete(File JavaDoc file) {
119
120         File JavaDoc[] list = file.listFiles();
121         if (list != null) {
122             for (int i = 0; i < list.length; i++) {
123                 File JavaDoc file1 = list[i];
124                 if (file1.isDirectory()) {
125                     recursiveDelete(file1);
126                 }
127             }
128         }
129         file.delete();
130     }
131
132     private static class MockRepository implements ListableRepository {
133         public boolean contains(Artifact artifact) {
134             return true;
135         }
136
137         public File JavaDoc getLocation(Artifact artifact) {
138             return new File JavaDoc(".");
139         }
140
141         public LinkedHashSet JavaDoc getDependencies(Artifact artifact) {
142             return new LinkedHashSet JavaDoc();
143         }
144
145         public SortedSet JavaDoc list() {
146             return new TreeSet JavaDoc();
147         }
148
149         public SortedSet JavaDoc list(Artifact query) {
150             SortedSet JavaDoc set = new TreeSet JavaDoc();
151             if(query.getGroupId() != null && query.getArtifactId() != null && query.getVersion() != null && query.getType() == null) {
152                 set.add(new Artifact(query.getGroupId(), query.getArtifactId(), query.getVersion(), "jar"));
153             }
154             return set;
155         }
156     }
157     private static class MockReferenceCollection extends ArrayList JavaDoc implements ReferenceCollection {
158
159         public void addReferenceCollectionListener(ReferenceCollectionListener listener) {
160
161         }
162
163         public void removeReferenceCollectionListener(ReferenceCollectionListener listener) {
164
165         }
166
167         public ObjectName JavaDoc[] getMemberObjectNames() { return new ObjectName JavaDoc[0];}
168     }
169 }
170
Popular Tags