Forked from aslakknutsen/ArquillianSuiteExtension.java
Last active
December 16, 2015 05:49
-
-
Save blabno/5387599 to your computer and use it in GitHub Desktop.
Got it working with Drone, Graphene, JSFUnit and mock-contexts-extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/schema/arquillian" xsi:schemaLocation="http://jboss.org/schema/arquillian | |
http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> | |
<defaultProtocol type="Servlet 3.0"/> | |
<extension qualifier="suite"> | |
<property name="deploymentClass">org.jboss.arquillian.extension.suite.Deployments</property> | |
</extension> | |
</arquillian> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jboss.arquillian.extension.suite; | |
import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor; | |
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.DeploymentScenario; | |
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData; | |
import org.jboss.arquillian.container.spi.event.DeployDeployment; | |
import org.jboss.arquillian.container.spi.event.DeployManagedDeployments; | |
import org.jboss.arquillian.container.spi.event.DeploymentEvent; | |
import org.jboss.arquillian.container.spi.event.UnDeployDeployment; | |
import org.jboss.arquillian.container.spi.event.UnDeployManagedDeployments; | |
import org.jboss.arquillian.container.spi.event.container.AfterStart; | |
import org.jboss.arquillian.container.spi.event.container.BeforeStop; | |
import org.jboss.arquillian.container.test.impl.client.deployment.event.GenerateDeployment; | |
import org.jboss.arquillian.core.api.Event; | |
import org.jboss.arquillian.core.api.Instance; | |
import org.jboss.arquillian.core.api.InstanceProducer; | |
import org.jboss.arquillian.core.api.annotation.Inject; | |
import org.jboss.arquillian.core.api.annotation.Observes; | |
import org.jboss.arquillian.core.api.event.ManagerStarted; | |
import org.jboss.arquillian.core.spi.EventContext; | |
import org.jboss.arquillian.core.spi.LoadableExtension; | |
import org.jboss.arquillian.test.spi.TestClass; | |
import org.jboss.arquillian.test.spi.annotation.ClassScoped; | |
import org.jboss.arquillian.test.spi.annotation.TestScoped; | |
import org.jboss.arquillian.test.spi.context.ClassContext; | |
import org.jboss.arquillian.test.spi.event.suite.Before; | |
import java.util.concurrent.Callable; | |
public class ArquillianSuiteExtension implements LoadableExtension { | |
public void register(ExtensionBuilder builder) | |
{ | |
builder.observer(SuiteDeployer.class); | |
} | |
public static class SuiteDeployer { | |
private Class<?> deploymentClass; | |
private DeploymentScenario suiteDeploymentScenario; | |
@Inject | |
@ClassScoped | |
private InstanceProducer<DeploymentScenario> classDeploymentScenario; | |
@Inject | |
private Event<DeploymentEvent> deploymentEvent; | |
@Inject | |
private Event<GenerateDeployment> generateDeploymentEvent; | |
@Inject // Active some form of ClassContext around our deployments due to assumption bug in AS7 extension. | |
private Instance<ClassContext> classContext; | |
private ProtocolMetaData cachedProtocolMetaData; | |
@TestScoped | |
@Inject | |
private InstanceProducer<ProtocolMetaData> testScopedProtocolMetaData; | |
public void startup(@Observes(precedence = -100) ManagerStarted event, ArquillianDescriptor descriptor) | |
{ | |
deploymentClass = getDeploymentClass(descriptor); | |
executeInClassScope(new Callable<Void>() { | |
public Void call() throws Exception | |
{ | |
generateDeploymentEvent.fire(new GenerateDeployment(new TestClass(deploymentClass))); | |
suiteDeploymentScenario = classDeploymentScenario.get(); | |
return null; | |
} | |
}); | |
} | |
public void deploy(@Observes final AfterStart event, final ContainerRegistry registry) | |
{ | |
executeInClassScope(new Callable<Void>() { | |
public Void call() throws Exception | |
{ | |
for (Deployment d : suiteDeploymentScenario.deployments()) { | |
deploymentEvent.fire(new DeployDeployment(findContainer(registry, event.getDeployableContainer()), d)); | |
} | |
return null; | |
} | |
}); | |
} | |
public void undeploy(@Observes final BeforeStop event, final ContainerRegistry registry) | |
{ | |
executeInClassScope(new Callable<Void>() { | |
public Void call() throws Exception | |
{ | |
for (Deployment d : suiteDeploymentScenario.deployments()) { | |
deploymentEvent.fire(new UnDeployDeployment(findContainer(registry, event.getDeployableContainer()), d)); | |
} | |
return null; | |
} | |
}); | |
} | |
public void blockDeployManagedDeployments(@Observes EventContext<DeployManagedDeployments> ignored) | |
{ | |
// We need to block DeployManagedDeployments event | |
} | |
public void storeProtocolMetaData(@Observes ProtocolMetaData protocolMetaData) | |
{ | |
cachedProtocolMetaData = protocolMetaData; | |
} | |
public void resotreProtocolMetaData(@Observes EventContext<Before> eventContext) | |
{ | |
testScopedProtocolMetaData.set(cachedProtocolMetaData); | |
eventContext.proceed(); | |
} | |
public void blockUnDeployManagedDeployments(@Observes EventContext<UnDeployManagedDeployments> ignored) | |
{ | |
// We need to block UnDeployManagedDeployments event | |
} | |
private void executeInClassScope(Callable<Void> call) | |
{ | |
try { | |
classContext.get().activate(deploymentClass); | |
call.call(); | |
} catch (Exception e) { | |
throw new RuntimeException("Could not invoke operation", e); | |
} finally { | |
classContext.get().deactivate(); | |
} | |
} | |
private Container findContainer(ContainerRegistry registry, DeployableContainer<?> deployable) | |
{ | |
for (Container container : registry.getContainers()) { | |
if (container.getDeployableContainer() == deployable) { | |
return container; | |
} | |
} | |
return null; | |
} | |
private Class<?> getDeploymentClass(ArquillianDescriptor descriptor) | |
{ | |
if (descriptor == null) { | |
throw new IllegalArgumentException("Descriptor must be specified"); | |
} | |
String className = descriptor.extension("suite").getExtensionProperties().get("deploymentClass"); | |
if (className == null) { | |
throw new IllegalArgumentException("A extension element with property deploymentClass must be specified in arquillian.xml"); | |
} | |
try { | |
return Class.forName(className); | |
} catch (ClassNotFoundException e) { | |
throw new RuntimeException("Could not load defined deploymentClass: " + className, e); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jboss.arquillian.extension.suite; | |
import org.jboss.arquillian.container.test.api.Deployment; | |
import org.jboss.arquillian.extension.suite.tests.Test1; | |
import org.jboss.shrinkwrap.api.ShrinkWrap; | |
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | |
import org.jboss.shrinkwrap.api.spec.WebArchive; | |
public class Deployments { | |
@Deployment | |
public static WebArchive deploy() { | |
return ShrinkWrap.create(WebArchive.class) | |
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") | |
.addPackage(Test1.class.getPackage()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jboss.arquillian.extension.suite; | |
import org.jboss.arquillian.core.spi.context.NonIdBoundContext; | |
public interface ExtendedSuiteContext extends NonIdBoundContext { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jboss.arquillian.extension.suite; | |
import org.jboss.arquillian.core.spi.HashObjectStore; | |
import org.jboss.arquillian.core.spi.context.AbstractContext; | |
import org.jboss.arquillian.core.spi.context.ObjectStore; | |
import java.lang.annotation.Annotation; | |
public class ExtendedSuiteContextImpl extends AbstractContext<String> implements ExtendedSuiteContext { | |
private static final String SUITE_CONTEXT_ID = "extendedSuite"; | |
/* (non-Javadoc) | |
* @see org.jboss.arquillian.spi.Context#getScope() | |
*/ | |
@Override | |
public Class<? extends Annotation> getScope() | |
{ | |
return ExtendedSuiteScoped.class; | |
} | |
/** | |
* There can only one Suite active, so we hard code the id to "Suite". | |
*/ | |
@Override | |
public void activate() | |
{ | |
super.activate(SUITE_CONTEXT_ID); | |
} | |
/* (non-Javadoc) | |
* @see org.jboss.arquillian.core.impl.context.AbstractContext#destroy(java.lang.Object) | |
*/ | |
@Override | |
public void destroy() | |
{ | |
super.destroy(SUITE_CONTEXT_ID); | |
} | |
@Override | |
protected ObjectStore createNewObjectStore() | |
{ | |
return new HashObjectStore(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jboss.arquillian.extension.suite; | |
import org.jboss.arquillian.core.api.annotation.Scope; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.Target; | |
import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
@Scope | |
@Documented | |
@Retention(RUNTIME) | |
@Target(ElementType.FIELD) | |
public @interface ExtendedSuiteScoped { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
org.jboss.arquillian.extension.suite.ArquillianSuiteExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jboss.arquillian.extension.suite.tests; | |
import javax.enterprise.inject.spi.BeanManager; | |
import junit.framework.Assert; | |
import org.jboss.arquillian.junit.Arquillian; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
@RunWith(Arquillian.class) | |
public class Test1 { | |
@Test | |
public void shouldInject(BeanManager bm) { | |
System.out.println("Where are We?"); | |
Assert.assertNotNull(bm); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment