Skip to content

Commit

Permalink
improve test infra
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Nov 1, 2009
1 parent 600d0af commit 84510e3
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 105 deletions.
@@ -1,5 +1,6 @@
package org.jboss.weld.mock;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -17,7 +18,7 @@ public abstract class AbstractMockDeployment implements Deployment
public AbstractMockDeployment(BeanDeploymentArchive... beanDeploymentArchives)
{
this.services = new SimpleServiceRegistry();
this.beanDeploymentArchives = Arrays.asList(beanDeploymentArchives);
this.beanDeploymentArchives = new ArrayList<BeanDeploymentArchive>(Arrays.asList(beanDeploymentArchives));
}

public List<BeanDeploymentArchive> getBeanDeploymentArchives()
Expand Down
17 changes: 6 additions & 11 deletions tests/src/main/java/org/jboss/weld/mock/MockDeployment.java
Expand Up @@ -20,23 +20,18 @@

public class MockDeployment extends AbstractMockDeployment
{
private final MockBeanDeploymentArchive archive;
public MockDeployment()

private final BeanDeploymentArchive archive;

public MockDeployment(BeanDeploymentArchive beanDeploymentArchive)
{
this.archive = new MockBeanDeploymentArchive();
this.archive = beanDeploymentArchive;
getBeanDeploymentArchives().add(archive);
}

public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass)
{
return archive;
}

public MockBeanDeploymentArchive getArchive()
{
return archive;
}

}
7 changes: 4 additions & 3 deletions tests/src/main/java/org/jboss/weld/mock/MockEELifecycle.java
Expand Up @@ -38,11 +38,12 @@ public MockEELifecycle()
getDeployment().getServices().add(SecurityServices.class, new MockSecurityServices());
getDeployment().getServices().add(ValidationServices.class, new MockValidationServices());
getDeployment().getServices().add(EjbServices.class, new MockEjBServices());
getDeployment().getArchive().getServices().add(EjbInjectionServices.class, new MockEjbInjectionServices());
getDeployment().getArchive().getServices().add(JpaInjectionServices.class, new MockJpaServices(getDeployment()));
getDeployment().getArchive().getServices().add(ResourceInjectionServices.class, new MockResourceServices());
getWar().getServices().add(EjbInjectionServices.class, new MockEjbInjectionServices());
getWar().getServices().add(JpaInjectionServices.class, new MockJpaServices(getDeployment()));
getWar().getServices().add(ResourceInjectionServices.class, new MockResourceServices());
}

@Override
public Environment getEnvironment()
{
return Environments.EE_INJECT;
Expand Down
26 changes: 22 additions & 4 deletions tests/src/main/java/org/jboss/weld/mock/MockServletLifecycle.java
Expand Up @@ -5,6 +5,7 @@
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.api.Lifecycle;
import org.jboss.weld.bootstrap.api.helpers.ForwardingLifecycle;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.context.api.BeanStore;
import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore;
Expand All @@ -16,7 +17,8 @@ public class MockServletLifecycle extends ForwardingLifecycle implements MockLif
private static final ResourceLoader MOCK_RESOURCE_LOADER = new MockResourceLoader();

private final WeldBootstrap bootstrap;
private final MockDeployment deployment;
private final Deployment deployment;
private final MockBeanDeploymentArchive war;
private final BeanStore applicationBeanStore;
private final BeanStore sessionBeanStore;
private final BeanStore requestBeanStore;
Expand All @@ -25,14 +27,25 @@ public class MockServletLifecycle extends ForwardingLifecycle implements MockLif

public MockServletLifecycle()
{
this.deployment = new MockDeployment();
this(new MockBeanDeploymentArchive());
}

private MockServletLifecycle(MockBeanDeploymentArchive war)
{
this(new MockDeployment(war), war);
}

public MockServletLifecycle(Deployment deployment, MockBeanDeploymentArchive war)
{
this.deployment = deployment;
this.war = war;
if (deployment == null)
{
throw new IllegalStateException("No WebBeanDiscovery is available");
}
this.bootstrap = new WeldBootstrap();
this.deployment.getServices().add(ResourceLoader.class, MOCK_RESOURCE_LOADER);
this.deployment.getServices().add(ServletServices.class, new MockServletServices(deployment.getArchive()));
this.deployment.getServices().add(ServletServices.class, new MockServletServices(war));
this.applicationBeanStore = new ConcurrentHashMapBeanStore();
this.sessionBeanStore = new ConcurrentHashMapBeanStore();
this.requestBeanStore = new ConcurrentHashMapBeanStore();
Expand Down Expand Up @@ -74,7 +87,7 @@ protected Lifecycle delegate()
return lifecycle;
}

protected MockDeployment getDeployment()
protected Deployment getDeployment()
{
return deployment;
}
Expand Down Expand Up @@ -146,4 +159,9 @@ protected Environment getEnvironment()
{
return Environments.SERVLET;
}

public MockBeanDeploymentArchive getWar()
{
return war;
}
}
70 changes: 18 additions & 52 deletions tests/src/main/java/org/jboss/weld/mock/TestContainer.java
Expand Up @@ -5,6 +5,7 @@
import java.util.Collection;

import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.bootstrap.spi.Deployment;

/**
* Control of the container, used for tests. Wraps up common operations.
Expand All @@ -28,31 +29,7 @@
public class TestContainer
{

public static class Status
{

private final Exception deploymentException;

public Status(Exception deploymentException)
{
this.deploymentException = deploymentException;
}

public Exception getDeploymentException()
{
return deploymentException;
}

public boolean isSuccess()
{
return deploymentException == null;
}

}

private final MockServletLifecycle lifecycle;
private final Collection<Class<?>> classes;
private final Collection<URL> beansXml;

/**
* Create a container, specifying the classes and beans.xml to deploy
Expand All @@ -62,11 +39,14 @@ public boolean isSuccess()
* @param beansXml
*/
public TestContainer(MockServletLifecycle lifecycle, Collection<Class<?>> classes, Collection<URL> beansXml)
{
this(lifecycle);
configureArchive(classes, beansXml);
}

public TestContainer(MockServletLifecycle lifecycle)
{
this.lifecycle = lifecycle;
this.classes = classes;
this.beansXml = beansXml;
configureArchive();
}

public TestContainer(MockServletLifecycle lifecycle, Class<?>[] classes, URL[] beansXml)
Expand All @@ -79,44 +59,28 @@ public TestContainer(MockServletLifecycle lifecycle, Class<?>... classes)
this(lifecycle, classes == null ? null : Arrays.asList(classes), null);
}

/**
* Start the container, returning the container state
*
* @return
*/
public Status startContainerAndReturnStatus()
{
try
{
startContainer();
}
catch (Exception e)
{
return new Status(e);
}
return new Status(null);
}

/**
* Starts the container and begins the application
*/
public void startContainer()
public TestContainer startContainer()
{
getLifecycle().initialize();
getLifecycle().beginApplication();
return this;
}

/**
* Configure's the archive with the classes and beans.xml
*/
protected void configureArchive()
protected TestContainer configureArchive(Collection<Class<?>> classes, Collection<URL> beansXml)
{
MockBeanDeploymentArchive archive = lifecycle.getDeployment().getArchive();
MockBeanDeploymentArchive archive = lifecycle.getWar();
archive.setBeanClasses(classes);
if (beansXml != null)
{
archive.setBeansXmlFiles(beansXml);
}
return this;
}

/**
Expand All @@ -131,10 +95,10 @@ public MockServletLifecycle getLifecycle()

public BeanManagerImpl getBeanManager()
{
return getLifecycle().getBootstrap().getManager(getDeployment().getArchive());
return getLifecycle().getBootstrap().getManager(getLifecycle().getWar());
}

public MockDeployment getDeployment()
public Deployment getDeployment()
{
return getLifecycle().getDeployment();
}
Expand All @@ -143,7 +107,7 @@ public MockDeployment getDeployment()
* Utility method which ensures a request is active and available for use
*
*/
public void ensureRequestActive()
public TestContainer ensureRequestActive()
{
if (!getLifecycle().isSessionActive())
{
Expand All @@ -153,13 +117,14 @@ public void ensureRequestActive()
{
getLifecycle().beginRequest();
}
return this;
}

/**
* Clean up the container, ending any active contexts
*
*/
public void stopContainer()
public TestContainer stopContainer()
{
if (getLifecycle().isRequestActive())
{
Expand All @@ -173,6 +138,7 @@ public void stopContainer()
{
getLifecycle().endApplication();
}
return this;
}

}
Expand Up @@ -7,7 +7,6 @@
import org.jboss.testharness.spi.StandaloneContainers;
import org.jboss.weld.mock.MockServletLifecycle;
import org.jboss.weld.mock.TestContainer;
import org.jboss.weld.mock.TestContainer.Status;

public abstract class AbstractStandaloneContainersImpl implements StandaloneContainers
{
Expand All @@ -20,18 +19,18 @@ public boolean deploy(Collection<Class<?>> classes, Collection<URL> beansXml)
{
this.testContainer = new TestContainer(newLifecycle(), classes, beansXml);

Status status = testContainer.startContainerAndReturnStatus();
if (!status.isSuccess())
try
{
this.deploymentException = new DeploymentException("Error deploying beans", status.getDeploymentException());
return false;
testContainer.startContainer();
}
else
catch (Exception e)
{
testContainer.getLifecycle().beginSession();
testContainer.getLifecycle().beginRequest();
return true;
this.deploymentException = new DeploymentException("Error deploying beans", e);
return false;
}
testContainer.getLifecycle().beginSession();
testContainer.getLifecycle().beginRequest();
return true;
}

protected abstract MockServletLifecycle newLifecycle();
Expand Down
Expand Up @@ -18,7 +18,7 @@ public void testInjectionOfTarget()
{
TestContainer container = new TestContainer(new MockEELifecycle(), Arrays.asList(Foo.class, Bar.class), null);
CheckableInjectionServices ijs = new CheckableInjectionServices();
container.getDeployment().getArchive().getServices().add(InjectionServices.class, ijs);
container.getLifecycle().getWar().getServices().add(InjectionServices.class, ijs);
container.startContainer();
container.ensureRequestActive();

Expand Down
Expand Up @@ -3,13 +3,12 @@
import javax.enterprise.inject.spi.Extension;

import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.bootstrap.WeldBootstrap;
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.context.beanstore.HashMapBeanStore;
import org.jboss.weld.mock.AbstractMockDeployment;
import org.jboss.weld.mock.MockBeanDeploymentArchive;
import org.jboss.weld.mock.MockServletLifecycle;
import org.jboss.weld.mock.TestContainer;
import org.jboss.weld.util.serviceProvider.PackageServiceLoaderFactory;
import org.jboss.weld.util.serviceProvider.ServiceLoaderFactory;
import org.testng.annotations.Test;
Expand All @@ -20,10 +19,8 @@ public class NonBDAExtensionTest
@Test(description="WELD-233")
public void test()
{
WeldBootstrap bootstrap = new WeldBootstrap();

// Create the BDA in which we will deploy Observer1 and Foo. This is equivalent to a war or ejb jar
final BeanDeploymentArchive bda1 = new MockBeanDeploymentArchive("1", Observer1.class, Foo.class);
final MockBeanDeploymentArchive bda1 = new MockBeanDeploymentArchive("1", Observer1.class, Foo.class);

// Create the BDA to return from loadBeanDeploymentArchive for Observer2, this is probably a library, though could be another war or ejb jar
// bda2 is accessible from bda1, but isn't added to it's accessibility graph by default. This similar to an archive which doesn't contain a beans.xml but does contain an extension
Expand All @@ -50,20 +47,19 @@ public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass)

};


// Initialize the container, we use the SE env as we aren't going to interact with the servlet lifecycle really
bootstrap.startContainer(Environments.SE, deployment, new HashMapBeanStore());
TestContainer container = new TestContainer(new MockServletLifecycle(deployment, bda1));
container.getLifecycle().initialize();

// Add custom ServiceLoader so that we can load Extension services from current package, not META-INF/services
// We do this after startContainer() so we replace the default impl
deployment.getServices().add(ServiceLoaderFactory.class, new PackageServiceLoaderFactory(NonBDAExtensionTest.class.getPackage(), Extension.class));

// Cause the container to deploy the beans etc.
bootstrap.startInitialization().deployBeans().validateBeans().endInitialization();
container.getLifecycle().beginApplication();

// Get the bean manager for bda1 and bda2
BeanManagerImpl beanManager1 = bootstrap.getManager(bda1);
BeanManagerImpl beanManager2 = bootstrap.getManager(bda2);
BeanManagerImpl beanManager1 = container.getBeanManager();
BeanManagerImpl beanManager2 = container.getLifecycle().getBootstrap().getManager(bda2);

Observer1 observer1 = beanManager1.getInstanceByType(Observer1.class);
assert observer1.isBeforeBeanDiscoveryCalled();
Expand Down

0 comments on commit 84510e3

Please sign in to comment.