1 17 18 package org.apache.geronimo.j2ee.deployment; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.ObjectOutputStream ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.jar.JarFile ; 30 import javax.management.MalformedObjectNameException ; 31 import javax.management.ObjectName ; 32 import javax.xml.namespace.QName ; 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 58 public class EARConfigBuilderTest extends TestCase { 59 private static final File basedir = new File (System.getProperty("basedir", System.getProperty("user.dir"))); 60 61 private static JarFile 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 public Object createService(Class serviceInterface, URI wsdlURI, URI jaxrpcMappingURI, QName serviceQName, Map portComponentRefMap, List handlerInfos, Map portLocationMap, Map credentialsNameMap, DeploymentContext deploymentContext, Module module, ClassLoader classLoader) { 71 return null; 72 } 73 }; 74 75 private static final String j2eeServerName = "someDomain"; 76 private static final ObjectName transactionManagerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=TransactionManager"); 77 private static final ObjectName connectionTrackerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=ConnectionTracker"); 78 private static final ObjectName transactionalTimerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=ThreadPooledTimer,name=TransactionalThreaPooledTimer"); 79 private static final ObjectName nonTransactionalTimerObjectName = JMXUtil.getObjectName(j2eeServerName + ":type=ThreadPooledTimer,name=NonTransactionalThreaPooledTimer"); 80 private URI defaultParentId; 81 private static final Map portMap = null; 82 83 protected void setUp() throws Exception { 84 defaultParentId = new URI ("org/apache/geronimo/Server"); 85 } 86 87 public static Test suite() throws Exception { 88 TestSuite inner = new TestSuite(EARConfigBuilderTest.class); 89 TestSetup setup14 = new TestSetup(inner) { 90 protected void setUp() throws Exception { 91 earFile = DeploymentUtil.createJarFile(new File (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 { 108 earFile = DeploymentUtil.createJarFile(new File (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 { 125 earFile = DeploymentUtil.createJarFile(new File (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 { 142 earFile = DeploymentUtil.createJarFile(new File (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 { 159 earFile = DeploymentUtil.createJarFile(new File (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 { 176 earFile = DeploymentUtil.createJarFile(new File (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 { 193 earFile = DeploymentUtil.createJarFile(new File (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 { 210 earFile = DeploymentUtil.createJarFile(new File (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 { 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 configurationManagerName = new ObjectName (":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 tempDir = null; 255 try { 256 tempDir = DeploymentUtil.createTempDir(); 257 Object 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 { 266 EARConfigBuilder configBuilder = new EARConfigBuilder(defaultParentId, transactionManagerObjectName, connectionTrackerObjectName, transactionalTimerObjectName, nonTransactionalTimerObjectName, null, null, null, null, webConfigBuilder, connectorConfigBuilder, resourceReferenceBuilder, appClientConfigBuilder, serviceReferenceBuilder, null); 267 268 File tempDir = null; 269 try { 270 tempDir = DeploymentUtil.createTempDir(); 271 Object plan = configBuilder.getDeploymentPlan(null, earFile); 272 configBuilder.buildConfiguration(plan, earFile, tempDir); 273 fail("Should have thrown a DeploymentException"); 274 } catch (DeploymentException e) { 275 } finally { 277 DeploymentUtil.recursiveDelete(tempDir); 278 } 279 } 280 281 public void testNoWARDeployer() throws Exception { 282 EARConfigBuilder configBuilder = new EARConfigBuilder(defaultParentId, transactionManagerObjectName, connectionTrackerObjectName, transactionalTimerObjectName, nonTransactionalTimerObjectName, null, null, ejbConfigBuilder, null, null, connectorConfigBuilder, resourceReferenceBuilder, appClientConfigBuilder, serviceReferenceBuilder, null); 283 284 File tempDir = null; 285 try { 286 tempDir = DeploymentUtil.createTempDir(); 287 Object plan = configBuilder.getDeploymentPlan(null, earFile); 288 configBuilder.buildConfiguration(plan, earFile, tempDir); 289 fail("Should have thrown a DeploymentException"); 290 } catch (DeploymentException e) { 291 } finally { 293 DeploymentUtil.recursiveDelete(tempDir); 294 } 295 } 296 297 public void testNoConnectorDeployer() throws Exception { 298 EARConfigBuilder configBuilder = new EARConfigBuilder(defaultParentId, transactionManagerObjectName, connectionTrackerObjectName, transactionalTimerObjectName, nonTransactionalTimerObjectName, null, null, ejbConfigBuilder, null, webConfigBuilder, null, resourceReferenceBuilder, appClientConfigBuilder, serviceReferenceBuilder, null); 299 300 File tempDir = null; 301 try { 302 tempDir = DeploymentUtil.createTempDir(); 303 Object plan = configBuilder.getDeploymentPlan(null, earFile); 304 configBuilder.buildConfiguration(plan, earFile, tempDir); 305 fail("Should have thrown a DeploymentException"); 306 } catch (DeploymentException e) { 307 } 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 install(URL source) throws IOException , InvalidConfigException { 327 return null; 328 } 329 330 public void install(ConfigurationData configurationData, File source) throws IOException , InvalidConfigException { 331 } 332 333 public void uninstall(URI configID) throws NoSuchConfigException, IOException { 334 } 335 336 public ObjectName loadConfiguration(URI configId) throws NoSuchConfigException, IOException , InvalidConfigException { 337 ObjectName configurationObjectName = null; 338 try { 339 configurationObjectName = Configuration.getConfigurationObjectName(configId); 340 } catch (MalformedObjectNameException 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 e) { 352 throw new InvalidConfigException("Unable to register configuration", e); 353 } 354 355 return configurationObjectName; 356 } 357 358 public boolean containsConfiguration(URI configID) { 359 return true; 360 } 361 362 public void updateConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, Exception { 363 364 } 365 366 public String getObjectName() { 367 return null; 368 } 369 370 public List listConfiguations() { 371 return null; 372 } 373 374 public File 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 [] {"kernel"}); 387 GBEAN_INFO = infoBuilder.getBeanInfo(); 388 389 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 390 try { 391 ObjectOutputStream oos = new ObjectOutputStream (baos); 392 oos.flush(); 393 NO_OBJECTS_OS = baos.toByteArray(); 394 } catch (IOException e) { 395 throw new RuntimeException (e); 396 } 397 } 398 } 399 } 400 | Popular Tags |