Skip to content

Commit

Permalink
ARQ-858 Add validation rule that disallow multiple archive @deployment'…
Browse files Browse the repository at this point in the history
…s with the same archive name to target the same Container
  • Loading branch information
aslakknutsen committed Apr 10, 2012
1 parent 505b2b5 commit 2783561
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public DeploymentScenario addDeployment(DeploymentDescription deployment)
{
Validate.notNull(deployment, "Deployment must be specified");
validateNotSameNameAndTypeOfDeployment(deployment);
validateNotSameArchiveAndSameTarget(deployment);

this.deployments.add(new Deployment(deployment));
return this;
Expand Down Expand Up @@ -348,4 +349,30 @@ private void validateNotSameNameAndTypeOfDeployment(DeploymentDescription deploy
}
}
}
}

/**
* Validate that a deployment with a archive of the same name does not have the same taget
*
* @param deployment
*
*/
private void validateNotSameArchiveAndSameTarget(DeploymentDescription deployment)
{
if(!deployment.isArchiveDeployment())
{
return;
}
for (Deployment existing : archiveDeployments(deployments))
{
if(existing.getDescription().getArchive().getName().equals(deployment.getArchive().getName()))
{
if(existing.getDescription().getTarget().equals(deployment.getTarget()))
{
throw new IllegalArgumentException("Can not add multiple " +
Archive.class.getName() + " archive deployments with the same archive name " + deployment.getName() +
" that target the same target " + deployment.getTarget());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class DeploymentScenarioTestCase
* - A single Archive is default
* - A Archive and a Descriptor, Archive is default
* - Only allow equal names of deployments if they are of different Types
*
* - Only allow archive deployments with same name if they have different targets
*/

@Test
Expand Down Expand Up @@ -215,15 +215,41 @@ public void shouldAllowMultipleDeploymentWithSameNameOfDifferentTypeOrderIrrelev
Assert.assertTrue(deployment.getDescription().isArchiveDeployment());
}

@Test
public void shouldAllowMultipleArchiveDeploymentsWithSameArchiveNameWithDifferentTargets()
{
DeploymentScenario scenario = new DeploymentScenario();
scenario.addDeployment(
new DeploymentDescription(DEFAULT_NAME, ShrinkWrap.create(JavaArchive.class, "test.jar"))
.setTarget(TargetDescription.DEFAULT));
scenario.addDeployment(
new DeploymentDescription("B", ShrinkWrap.create(JavaArchive.class, "test.jar"))
.setTarget(new TargetDescription("B")));

Assert.assertEquals(2, scenario.deployments().size());
}

@Test(expected = IllegalArgumentException.class)
public void shouldNotAllowMultipleArchiveDeploymentsWithSameName()
{
DeploymentScenario scenario = new DeploymentScenario();
scenario.addDeployment(
new DeploymentDescription(DEFAULT_NAME, ShrinkWrap.create(JavaArchive.class))
new DeploymentDescription("X", ShrinkWrap.create(JavaArchive.class))
.setTarget(TargetDescription.DEFAULT));
scenario.addDeployment(
new DeploymentDescription(DEFAULT_NAME, ShrinkWrap.create(JavaArchive.class))
new DeploymentDescription("X", ShrinkWrap.create(JavaArchive.class))
.setTarget(TargetDescription.DEFAULT));
}

@Test(expected = IllegalArgumentException.class)
public void shouldNotAllowMultipleArchiveDeploymentsWithSameArchiveName()
{
DeploymentScenario scenario = new DeploymentScenario();
scenario.addDeployment(
new DeploymentDescription(DEFAULT_NAME, ShrinkWrap.create(JavaArchive.class, "test.jar"))
.setTarget(TargetDescription.DEFAULT));
scenario.addDeployment(
new DeploymentDescription("B", ShrinkWrap.create(JavaArchive.class, "test.jar"))
.setTarget(TargetDescription.DEFAULT));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class DeploymentGeneratorTestCase extends AbstractContainerTestTestBase
public static final String PROTOCOL_NAME_1 = "TEST_DEFAULT_1";
public static final String PROTOCOL_NAME_2 = "TEST_DEFAULT_2";
public static final String CONTAINER_NAME_1 = "CONTAINER_NAME_1";
public static final String CONTAINER_NAME_2 = "CONTAINER_NAME_2";


@Override
Expand Down Expand Up @@ -196,6 +197,18 @@ public void shouldAllowNonManagedDeploymentOnCustomContainer() throws Exception
verifyScenario("DeploymentNonManagedWithCustomContainerReference");
}

@Test
public void shouldAllowMultipleSameNamedArchiveDeploymentWithDifferentTargets() throws Exception
{
addContainer(CONTAINER_NAME_1).getContainerConfiguration().setMode("suite");
addContainer(CONTAINER_NAME_2).getContainerConfiguration().setMode("suite");
addProtocol(PROTOCOL_NAME_1, true);

fire(createEvent(DeploymentMultipleSameNameArchiveDifferentTarget.class));

verifyScenario("X", "Y");
}

@Test(expected = ValidationException.class)
public void shouldThrowExceptionOnMissingContainerReference() throws Exception
{
Expand All @@ -220,7 +233,6 @@ public void shouldThrowExceptionOnWrongContainerReference() throws Exception
}
catch (Exception e)
{
//e.printStackTrace();
Assert.assertTrue("Validate correct error message", e.getMessage().contains("does not match any found/configured Containers"));
throw e;
}
Expand All @@ -241,6 +253,34 @@ public void shouldThrowExceptionOnMissingProtocolReference() throws Exception
}
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionOnMultipleNoNamedDeployments() throws Exception
{
addContainer("test-contianer").getContainerConfiguration().setMode("suite");
try
{
fire(createEvent(DeploymentMultipleNoNamed.class));
}
catch (Exception e)
{
throw e;
}
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionOnMultipleSameNamedArchiveDeployments() throws Exception
{
addContainer("test-contianer").getContainerConfiguration().setMode("suite");
try
{
fire(createEvent(DeploymentMultipleSameNameArchive.class));
}
catch (Exception e)
{
throw e;
}
}

@Test(expected = ValidationException.class)
public void shouldThrowExceptionOnManagedDeploymentOnCustomContainer() throws Exception
{
Expand All @@ -263,8 +303,24 @@ private void verifyScenario(String... names)

for(int i = 0; i < names.length; i++)
{
scenario.deployments().get(i).getDescription().getName().equals(names[i]);
contains(scenario.deployments(), names[i]);
}
}

private void contains(Collection<org.jboss.arquillian.container.spi.client.deployment.Deployment> deployments, String name)
{
if(deployments == null || deployments.size() == 0)
{
Assert.fail("No deployment by name " + name + " found in scenario. Scenario is empty");
}
for(org.jboss.arquillian.container.spi.client.deployment.Deployment deployment : deployments)
{
if(name.equals(deployment.getDescription().getName()))
{
return;
}
}
Assert.fail("No deployment by name " + name + " found in scenario. " + deployments);
}

private Container addContainer(String name)
Expand Down Expand Up @@ -300,17 +356,68 @@ private static class DeploymentWithDefaults
{
@SuppressWarnings("unused")
@Deployment
public static JavaArchive deploy()
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class);
}
}

private static class DeploymentMultipleNoNamed
{
@SuppressWarnings("unused")
@Deployment
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class);
}

@SuppressWarnings("unused")
@Deployment
public static JavaArchive deploy2()
{
return ShrinkWrap.create(JavaArchive.class);
}
}

private static class DeploymentMultipleSameNameArchive
{
@SuppressWarnings("unused")
@Deployment(name = "Y")
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class, "test.jar");
}

@SuppressWarnings("unused")
@Deployment(name = "X")
public static JavaArchive deploy2()
{
return ShrinkWrap.create(JavaArchive.class, "test.jar");
}
}

private static class DeploymentMultipleSameNameArchiveDifferentTarget
{
@SuppressWarnings("unused")
@Deployment(name = "Y") @TargetsContainer(CONTAINER_NAME_1)
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class, "test.jar");
}

@SuppressWarnings("unused")
@Deployment(name = "X") @TargetsContainer(CONTAINER_NAME_2)
public static JavaArchive deploy2()
{
return ShrinkWrap.create(JavaArchive.class, "test.jar");
}
}

private static class DeploymentNonTestableWithDefaults
{
@SuppressWarnings("unused")
@Deployment(testable = false)
public static JavaArchive deploy()
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class);
}
Expand All @@ -320,7 +427,7 @@ private static class DeploymentWithContainerReference
{
@SuppressWarnings("unused")
@Deployment @TargetsContainer("DOES_NOT_EXIST")
public static JavaArchive deploy()
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class);
}
Expand All @@ -330,7 +437,7 @@ private static class DeploymentWithProtocolReference
{
@SuppressWarnings("unused")
@Deployment @OverProtocol("DOES_NOT_EXIST")
public static JavaArchive deploy()
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class);
}
Expand All @@ -341,7 +448,7 @@ private static class DeploymentManagedWithCustomContainerReference
@SuppressWarnings("unused")
@Deployment(managed = true, testable = false)
@TargetsContainer(CONTAINER_NAME_1)
public static JavaArchive deploy()
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class);
}
Expand All @@ -352,7 +459,7 @@ private static class DeploymentNonManagedWithCustomContainerReference
@SuppressWarnings("unused")
@Deployment(name = "DeploymentNonManagedWithCustomContainerReference", managed = false, testable = false)
@TargetsContainer(CONTAINER_NAME_1)
public static JavaArchive deploy()
public static JavaArchive deploy()
{
return ShrinkWrap.create(JavaArchive.class);
}
Expand Down

0 comments on commit 2783561

Please sign in to comment.