001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.deployment.service;
017:
018: import java.io.File;
019: import java.net.URL;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Set;
023: import java.util.HashSet;
024: import java.util.jar.JarFile;
025:
026: import javax.management.ObjectName;
027:
028: import junit.framework.TestCase;
029: import org.apache.geronimo.deployment.DeploymentContext;
030: import org.apache.geronimo.deployment.FooBarBean;
031: import org.apache.geronimo.deployment.ModuleIDBuilder;
032: import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
033: import org.apache.geronimo.deployment.xbeans.ModuleDocument;
034: import org.apache.geronimo.deployment.xbeans.ModuleType;
035: import org.apache.geronimo.gbean.AbstractName;
036: import org.apache.geronimo.gbean.GBeanData;
037: import org.apache.geronimo.gbean.ReferenceCollection;
038: import org.apache.geronimo.gbean.ReferenceCollectionListener;
039: import org.apache.geronimo.kernel.Jsr77Naming;
040: import org.apache.geronimo.kernel.Naming;
041: import org.apache.geronimo.kernel.config.ConfigurationManager;
042: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
043: import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
044: import org.apache.geronimo.kernel.mock.MockRepository;
045: import org.apache.geronimo.kernel.repository.ArtifactManager;
046: import org.apache.geronimo.kernel.repository.ArtifactResolver;
047: import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
048: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
049: import org.apache.geronimo.kernel.repository.Environment;
050: import org.apache.geronimo.kernel.repository.ListableRepository;
051: import org.apache.geronimo.kernel.repository.Artifact;
052:
053: /**
054: * @version $Rev: 566893 $ $Date: 2007-08-16 17:52:52 -0700 (Thu, 16 Aug 2007) $
055: */
056: public class ServiceConfigBuilderTest extends TestCase {
057:
058: private Environment parentEnvironment = new Environment();
059:
060: public void testNonService() throws Exception {
061: URL url = getClass().getResource("/empty.jar");
062: File file = new File(url.getPath());
063: JarFile jar = new JarFile(file);
064: assertTrue(file.exists());
065: ServiceConfigBuilder builder = new ServiceConfigBuilder(
066: parentEnvironment, null, new Jsr77Naming());
067: assertNull(builder.getDeploymentPlan(null, jar,
068: new ModuleIDBuilder()));
069: jar.close();
070: }
071:
072: public void testJavaBeanXmlAttribute() throws Exception {
073: ReferenceCollection referenceCollection = new MockReferenceCollection();
074: JavaBeanXmlAttributeBuilder javaBeanXmlAttributeBuilder = new JavaBeanXmlAttributeBuilder();
075: //this is kind of cheating, we rely on the builder to iterate through existing members of the collection.
076: referenceCollection.add(javaBeanXmlAttributeBuilder);
077: Naming naming = new Jsr77Naming();
078: NamespaceDrivenBuilder gbeanBuilder = new GBeanBuilder(
079: referenceCollection, null);
080: // ConfigurationBuilder serviceBuilder = new ServiceConfigBuilder(parentEnvironment, null, Collections.singleton(gbeanBuilder), naming);
081: ClassLoader cl = Thread.currentThread().getContextClassLoader();
082: final URL plan1 = cl.getResource("services/plan1.xml");
083: ModuleDocument doc = ModuleDocument.Factory.parse(plan1);
084: ModuleType plan = doc.getModule();
085: File outFile = File.createTempFile("foo", "bar");
086: outFile.delete();
087: if (!outFile.mkdirs()) {
088: fail("could not create temp dir");
089: }
090: try {
091:
092: Environment environment = EnvironmentBuilder
093: .buildEnvironment(plan.getEnvironment());
094: Set<Artifact> repo = new HashSet<Artifact>();
095: repo.add(Artifact.create("geronimo/foo1/DEV/car"));
096: repo.add(Artifact.create("geronimo/foo2/DEV/car"));
097: repo.add(Artifact.create("geronimo/foo3/DEV/car"));
098: repo.add(Artifact.create("geronimo/foo4/DEV/car"));
099: ListableRepository mockRepository = new MockRepository(repo);
100: ArtifactManager artifactManager = new DefaultArtifactManager();
101: ArtifactResolver artifactResolver = new DefaultArtifactResolver(
102: artifactManager, Collections
103: .singleton(mockRepository), null);
104: ConfigurationManager configurationManager = new SimpleConfigurationManager(
105: Collections.EMPTY_SET, artifactResolver,
106: Collections.EMPTY_SET);
107: AbstractName moduleName = naming.createRootName(environment
108: .getConfigId(), "foo", "bar");
109: DeploymentContext context = new DeploymentContext(outFile,
110: null, environment, moduleName,
111: ConfigurationModuleType.CAR, naming,
112: configurationManager, Collections
113: .singleton(mockRepository));
114:
115: gbeanBuilder.build(plan, context, context);
116: Set gbeanNames = context.getGBeanNames();
117: assertEquals(1, gbeanNames.size());
118: AbstractName beanName = (AbstractName) gbeanNames
119: .iterator().next();
120: GBeanData data = context.getGBeanInstance(beanName);
121: FooBarBean fooBarBean = (FooBarBean) data
122: .getAttribute("fooBarBean");
123: assertNotNull(fooBarBean);
124: assertEquals("foo", fooBarBean.getFoo());
125: assertEquals(10, fooBarBean.getBar());
126: FooBarBean inner = fooBarBean.getBean();
127: assertNotNull(inner);
128: assertEquals("foo2", inner.getFoo());
129: assertEquals(100, inner.getBar());
130: assertNull(inner.getBean());
131: } finally {
132: recursiveDelete(outFile);
133: }
134: }
135:
136: private void recursiveDelete(File file) {
137:
138: File[] list = file.listFiles();
139: if (list != null) {
140: for (int i = 0; i < list.length; i++) {
141: File file1 = list[i];
142: if (file1.isDirectory()) {
143: recursiveDelete(file1);
144: }
145: }
146: }
147: file.delete();
148: }
149:
150: private static class MockReferenceCollection extends ArrayList
151: implements ReferenceCollection {
152:
153: public void addReferenceCollectionListener(
154: ReferenceCollectionListener listener) {
155:
156: }
157:
158: public void removeReferenceCollectionListener(
159: ReferenceCollectionListener listener) {
160:
161: }
162:
163: public ObjectName[] getMemberObjectNames() {
164: return new ObjectName[0];
165: }
166: }
167: }
|