diff --git a/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGenerator.java b/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGenerator.java index 5851afc8c..ed860da3b 100644 --- a/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGenerator.java +++ b/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGenerator.java @@ -19,8 +19,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; + +import org.jboss.arquillian.config.descriptor.api.ProtocolDef; import org.jboss.arquillian.container.spi.Container; import org.jboss.arquillian.container.spi.ContainerRegistry; +import org.jboss.arquillian.container.spi.client.container.DeployableContainer; import org.jboss.arquillian.container.spi.client.deployment.Deployment; import org.jboss.arquillian.container.spi.client.deployment.DeploymentDescription; import org.jboss.arquillian.container.spi.client.deployment.DeploymentScenario; @@ -38,6 +41,7 @@ import org.jboss.arquillian.container.test.spi.client.deployment.DeploymentScenarioGenerator; import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor; import org.jboss.arquillian.container.test.spi.client.protocol.Protocol; +import org.jboss.arquillian.container.test.spi.client.protocol.ProtocolConfiguration; import org.jboss.arquillian.core.api.Instance; import org.jboss.arquillian.core.api.InstanceProducer; import org.jboss.arquillian.core.api.annotation.Inject; @@ -154,13 +158,58 @@ private void buildTestableDeployments(DeploymentScenario scenario, TestClass tes } List> auxiliaryArchives = loadAuxiliaryArchives(description); - ProtocolDefinition protocolDefinition = protoReg.getProtocol(description.getProtocol()); - if (protocolDefinition == null) { - protocolDefinition = protoReg.getProtocol( - containerRegistry.get() - .getContainer(description.getTarget()) - .getDeployableContainer() - .getDefaultProtocol()); + // First look to the target container + Container container = containerRegistry.get().getContainer(description.getTarget()); + if(container == null) { + throwNoContainerFound(description.getTarget()); + } + ProtocolDefinition protocolDefinition = null; + ProtocolConfiguration protocolConfig = null; + ProtocolDescription protocolDescription = description.getProtocol(); + if(protocolDescription == ProtocolDescription.DEFAULT) { + // Map to the actual ProtocolDescription if this is the DEFAULT placeholder + ProtocolDefinition defaultDef = protoReg.getProtocol(protocolDescription); + // There may not be any default protocol + if(defaultDef != null) { + protocolDescription = defaultDef.getProtocolDescription(); + } + } + if(container.hasProtocolConfiguration(protocolDescription)) { + ProtocolDef pdef = container.getProtocolConfiguration(protocolDescription); + try { + protocolDefinition = protoReg.getProtocol(protocolDescription); + protocolConfig = protocolDefinition.createProtocolConfiguration(pdef.getProtocolProperties()); + } catch (Exception e) { + ValidationException ve = new ValidationException("Unable to create protocol configuration for protocol " + protocolDescription.getName()); + ve.initCause(e); + throw ve; + } + } + // If the container had no protocol, try the protocol registry + if(protocolDefinition == null) { + protocolDefinition = protoReg.getProtocol(protocolDescription); + if(protocolDefinition != null) { + try { + protocolConfig = protocolDefinition.createProtocolConfiguration(); + } catch (Exception e) { + ValidationException ve = new ValidationException("Unable to create protocol configuration for protocol " + protocolDescription.getName()); + ve.initCause(e); + throw ve; + } + } + } + // Lastly see if there is a default protocol on the DeployableContainer + if(protocolDefinition == null) { + DeployableContainer deployableContainer = container.getDeployableContainer(); + ProtocolDescription defaultDef = deployableContainer.getDefaultProtocol(); + protocolDefinition = protoReg.getProtocol(defaultDef); + try { + protocolConfig = protocolDefinition.createProtocolConfiguration(); + } catch (Exception e) { + ValidationException ve = new ValidationException("Unable to create protocol configuration for protocol " + protocolDescription.getName()); + ve.initCause(e); + throw ve; + } } Protocol protocol = protocolDefinition.getProtocol(); DeploymentPackager packager = protocol.getPackager(); @@ -184,10 +233,13 @@ private void buildTestableDeployments(DeploymentScenario scenario, TestClass tes * ContianerBase implements it. Check the Archive Interface.. */ } - description.setTestableArchive( - packager.generateDeployment( - new TestDeployment(deployment.getDescription(), applicationArchive, auxiliaryArchives), - serviceLoader.get().all(ProtocolArchiveProcessor.class))); + // Load the ProtocolArchiveProcessors + Collection archiveProcessors = serviceLoader.get().all(ProtocolArchiveProcessor.class); + // Create the testable archive + TestDeployment testDeployment = new TestDeployment(deployment.getDescription(), applicationArchive, auxiliaryArchives); + testDeployment.setProtocolConfiguration(protocolConfig); + Archive testArchive = packager.generateDeployment(testDeployment, archiveProcessors); + description.setTestableArchive(testArchive); } } diff --git a/container/test-impl-base/src/test/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGeneratorTestCase.java b/container/test-impl-base/src/test/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGeneratorTestCase.java index 00c30b205..e2cd5bddc 100644 --- a/container/test-impl-base/src/test/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGeneratorTestCase.java +++ b/container/test-impl-base/src/test/java/org/jboss/arquillian/container/test/impl/client/deployment/DeploymentGeneratorTestCase.java @@ -25,6 +25,9 @@ import java.util.List; import java.util.Map; import java.util.Set; + +import org.jboss.arquillian.config.descriptor.api.ContainerDef; +import org.jboss.arquillian.config.descriptor.api.ProtocolDef; import org.jboss.arquillian.config.descriptor.impl.ContainerDefImpl; import org.jboss.arquillian.container.impl.LocalContainerRegistry; import org.jboss.arquillian.container.spi.Container; @@ -44,7 +47,9 @@ import org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveProcessor; import org.jboss.arquillian.container.test.spi.client.deployment.DeploymentPackager; import org.jboss.arquillian.container.test.spi.client.deployment.DeploymentScenarioGenerator; +import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor; import org.jboss.arquillian.container.test.spi.client.protocol.Protocol; +import org.jboss.arquillian.container.test.spi.client.protocol.ProtocolConfiguration; import org.jboss.arquillian.container.test.test.AbstractContainerTestTestBase; import org.jboss.arquillian.core.api.Injector; import org.jboss.arquillian.core.api.Instance; @@ -137,6 +142,41 @@ public void shouldUseDefaultDefinedProtocolIfFound() { verify(deployableContainer, times(0)).getDefaultProtocol(); } + @Test + public void shouldUseContainerProtocolIfFound() { + Container container = addContainer("test-contianer-with-protocol"); + ContainerDef containerDef = container.getContainerConfiguration(); + containerDef.setMode("suite"); + AltDeploymentPackager packager1 = new AltDeploymentPackager(); + addProtocolWithPackager(PROTOCOL_NAME_1, false, packager1, Collections.singletonMap("mode", "default")); + // Now add a container local protocol with a custom config + ProtocolDef protocolDef = containerDef.protocol(PROTOCOL_NAME_1) + .property("mode", "custom"); + + fire(createEvent(DeploymentWithProtocol.class)); + + verify(deployableContainer, times(0)).getDefaultProtocol(); + TheProtocolConfiguration config = (TheProtocolConfiguration) packager1.getConfig(); + Assert.assertEquals("custom", config.getMode()); + } + @Test + public void shouldUseContainerDefaultProtocolIfFound() { + Container container = addContainer("test-contianer-with-protocol"); + ContainerDef containerDef = container.getContainerConfiguration(); + containerDef.setMode("suite"); + AltDeploymentPackager packager1 = new AltDeploymentPackager(); + addProtocolWithPackager(PROTOCOL_NAME_1, true, packager1, Collections.singletonMap("mode", "default")); + // Now add a container local protocol with a custom config + ProtocolDef protocolDef = containerDef.protocol(PROTOCOL_NAME_1) + .property("mode", "custom"); + + fire(createEvent(DeploymentWithDefaults.class)); + + verify(deployableContainer, times(0)).getDefaultProtocol(); + TheProtocolConfiguration config = (TheProtocolConfiguration) packager1.getConfig(); + Assert.assertEquals("custom", config.getMode()); + } + @Test public void shouldAddAdditionalObserverClasses() { addContainer("test-contianer").getContainerConfiguration().setMode("suite"); @@ -339,14 +379,25 @@ private Container addContainer(String name) { } private ProtocolDefinition addProtocol(String name, boolean shouldBeDefault) { - Protocol protocol = mock(Protocol.class); + Protocol protocol = mock(Protocol.class); when(protocol.getPackager()).thenReturn(packager); when(protocol.getDescription()).thenReturn(new ProtocolDescription(name)); + when(protocol.getProtocolConfigurationClass()).thenReturn(TheProtocolConfiguration.class); Map config = Collections.emptyMap(); return protocolRegistry.addProtocol(new ProtocolDefinition(protocol, config, shouldBeDefault)) .getProtocol(new ProtocolDescription(name)); } + private ProtocolDefinition addProtocolWithPackager(String name, boolean shouldBeDefault, + DeploymentPackager packager, Map config) { + Protocol protocol = mock(Protocol.class); + when(protocol.getPackager()).thenReturn(packager); + when(protocol.getDescription()).thenReturn(new ProtocolDescription(name)); + when(protocol.getProtocolConfigurationClass()).thenReturn(TheProtocolConfiguration.class); + + return protocolRegistry.addProtocol(new ProtocolDefinition(protocol, config, shouldBeDefault)) + .getProtocol(new ProtocolDescription(name)); + } private Collection create(Class type, T... instances) { List list = new ArrayList(); @@ -365,6 +416,15 @@ public static JavaArchive deploy() { return ShrinkWrap.create(JavaArchive.class); } } + private static class DeploymentWithProtocol { + @SuppressWarnings("unused") + @Deployment + @OverProtocol(PROTOCOL_NAME_1) + public static JavaArchive deploy() { + return ShrinkWrap.create(JavaArchive.class); + } + } + @Observer({ObserverClass.class, SecondObserverClass.class}) private static class DeploymentWithObserver { @@ -519,4 +579,26 @@ public void process(Archive applicationArchive, TestClass testClass) { called(); } } + + public static class TheProtocolConfiguration implements ProtocolConfiguration { + private String mode; + + public String getMode() { + return mode; + } + public void setMode(String mode) { + this.mode = mode; + } + } + private static class AltDeploymentPackager implements DeploymentPackager { + private ProtocolConfiguration config = null; + @Override + public Archive generateDeployment(TestDeployment testDeployment, Collection processors) { + config = testDeployment.getProtocolConfiguration(); + return testDeployment.getApplicationArchive(); + } + public ProtocolConfiguration getConfig() { + return config; + } + } } diff --git a/container/test-spi/src/main/java/org/jboss/arquillian/container/test/spi/TestDeployment.java b/container/test-spi/src/main/java/org/jboss/arquillian/container/test/spi/TestDeployment.java index 6c440f67c..0291ef3dd 100644 --- a/container/test-spi/src/main/java/org/jboss/arquillian/container/test/spi/TestDeployment.java +++ b/container/test-spi/src/main/java/org/jboss/arquillian/container/test/spi/TestDeployment.java @@ -21,6 +21,7 @@ import org.jboss.arquillian.container.spi.client.deployment.DeploymentDescription; import org.jboss.arquillian.container.spi.client.deployment.TargetDescription; import org.jboss.arquillian.container.spi.client.protocol.ProtocolDescription; +import org.jboss.arquillian.container.test.spi.client.protocol.ProtocolConfiguration; import org.jboss.arquillian.container.test.api.Testable; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ArchivePath; @@ -38,6 +39,8 @@ */ public class TestDeployment { private DeploymentDescription deploymentDescription; + // The optional ProtocolConfiguration associated with the deployment + private ProtocolConfiguration protocolConfiguration; private Archive applicationArchive; @@ -129,4 +132,12 @@ public Archive getApplicationArchive() { public Collection> getAuxiliaryArchives() { return auxiliaryArchives; } + + public ProtocolConfiguration getProtocolConfiguration() { + return protocolConfiguration; + } + + public void setProtocolConfiguration(ProtocolConfiguration protocolConfiguration) { + this.protocolConfiguration = protocolConfiguration; + } }