1 17 package org.apache.geronimo.deployment.service; 18 19 import java.io.File ; 20 import java.net.URL ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.LinkedHashSet ; 24 import java.util.Set ; 25 import java.util.SortedSet ; 26 import java.util.TreeSet ; 27 import java.util.jar.JarFile ; 28 29 import javax.management.ObjectName ; 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 58 public class ServiceConfigBuilderTest extends TestCase { 59 60 private Environment parentEnvironment = new Environment(); 61 62 public void testNonService() throws Exception { 63 URL url = getClass().getResource("/empty.jar"); 64 File file = new File (url.getPath()); 65 JarFile jar = new JarFile (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 { 73 ReferenceCollection referenceCollection = new MockReferenceCollection(); 74 JavaBeanXmlAttributeBuilder javaBeanXmlAttributeBuilder = new JavaBeanXmlAttributeBuilder(); 75 referenceCollection.add(javaBeanXmlAttributeBuilder); 77 Naming naming = new Jsr77Naming(); 78 NamespaceDrivenBuilder gbeanBuilder = new GBeanBuilder(referenceCollection, null); 79 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 81 final URL plan1 = cl.getResource("services/plan1.xml"); 82 ModuleDocument doc = ModuleDocument.Factory.parse(plan1); 83 ModuleType plan = doc.getModule(); 84 File 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 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 file) { 119 120 File [] list = file.listFiles(); 121 if (list != null) { 122 for (int i = 0; i < list.length; i++) { 123 File 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 getLocation(Artifact artifact) { 138 return new File ("."); 139 } 140 141 public LinkedHashSet getDependencies(Artifact artifact) { 142 return new LinkedHashSet (); 143 } 144 145 public SortedSet list() { 146 return new TreeSet (); 147 } 148 149 public SortedSet list(Artifact query) { 150 SortedSet set = new TreeSet (); 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 implements ReferenceCollection { 158 159 public void addReferenceCollectionListener(ReferenceCollectionListener listener) { 160 161 } 162 163 public void removeReferenceCollectionListener(ReferenceCollectionListener listener) { 164 165 } 166 167 public ObjectName [] getMemberObjectNames() { return new ObjectName [0];} 168 } 169 } 170 | Popular Tags |