Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable access to the ProtocolConfiguration info from the TestDeployment. #555

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -154,13 +158,58 @@ private void buildTestableDeployments(DeploymentScenario scenario, TestClass tes
}
List<Archive<?>> 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();
Expand All @@ -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<ProtocolArchiveProcessor> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -339,14 +379,25 @@ private Container addContainer(String name) {
}

private ProtocolDefinition addProtocol(String name, boolean shouldBeDefault) {
Protocol<?> protocol = mock(Protocol.class);
Protocol<TheProtocolConfiguration> protocol = mock(Protocol.class);
when(protocol.getPackager()).thenReturn(packager);
when(protocol.getDescription()).thenReturn(new ProtocolDescription(name));
when(protocol.getProtocolConfigurationClass()).thenReturn(TheProtocolConfiguration.class);

Map<String, String> 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<String, String> config) {
Protocol<TheProtocolConfiguration> 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 <T> Collection<T> create(Class<T> type, T... instances) {
List<T> list = new ArrayList<T>();
Expand All @@ -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 {
Expand Down Expand Up @@ -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<ProtocolArchiveProcessor> processors) {
config = testDeployment.getProtocolConfiguration();
return testDeployment.getApplicationArchive();
}
public ProtocolConfiguration getConfig() {
return config;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +39,8 @@
*/
public class TestDeployment {
private DeploymentDescription deploymentDescription;
// The optional ProtocolConfiguration associated with the deployment
private ProtocolConfiguration protocolConfiguration;

private Archive<?> applicationArchive;

Expand Down Expand Up @@ -129,4 +132,12 @@ public Archive<?> getApplicationArchive() {
public Collection<Archive<?>> getAuxiliaryArchives() {
return auxiliaryArchives;
}

public ProtocolConfiguration getProtocolConfiguration() {
return protocolConfiguration;
}

public void setProtocolConfiguration(ProtocolConfiguration protocolConfiguration) {
this.protocolConfiguration = protocolConfiguration;
}
}