Last active
September 20, 2016 09:12
-
-
Save aslakknutsen/3975179 to your computer and use it in GitHub Desktop.
Brute Force Arquillian Suite
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 java.util.concurrent.Callable; | |
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 org.jboss.arquillian.test.spi.event.suite.BeforeClass; | |
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 restoreDeploymentScenario(@Observes EventContext<BeforeClass> event) { | |
// Setup the Suite level scenario as if it came from the TestClass | |
event.proceed(); | |
classDeploymentScenario.set(suiteDeploymentScenario); | |
} | |
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
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
Hello Aslak. Does this have to use a war? I want to run tests in Karaf, so I need to deploy bundles.