Skip to content

Commit

Permalink
FORGE-673 - FKLJSDF(*#$ YES! Arquillian test harness for Forge 2.0 is…
Browse files Browse the repository at this point in the history
… ALIVE
  • Loading branch information
lincolnthree committed Sep 11, 2012
1 parent 4f5daf6 commit fa51917
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ public void setup(ForgeContainerConfiguration configuration)
@Override
public void start() throws LifecycleException
{
// try
// {
// this.process = NativeSystemCall.exec("java", "-Dforge.home=" + FORGE_HOME,
// "-jar", FORGE_HOME + "/jboss-modules.jar", "-modulepath",
// FORGE_HOME + "/modules:" + OSUtils.getUserHomePath() + "/.forge/plugins:", "org.jboss.forge");
// }
// catch (Exception e)
// {
// throw new LifecycleException("Could not start Forge process.", e);
// }
try
{
this.process = NativeSystemCall.exec("java", "-Dforge.home=" + FORGE_HOME,
"-jar", FORGE_HOME + "/jboss-modules.jar", "-modulepath",
FORGE_HOME + "/modules:" + OSUtils.getUserHomePath() + "/.forge/plugins:", "org.jboss.forge");
}
catch (Exception e)
{
throw new LifecycleException("Could not start Forge process.", e);
}
}

@Override
public void stop() throws LifecycleException
{
// try
// {
// this.process.destroy();
// int status = this.process.waitFor();
// System.out.println("Forge exited with status: " + status);
// }
// catch (InterruptedException e)
// {
// throw new LifecycleException("Container was interrupted while stopping.", e);
// }
try
{
this.process.destroy();
int status = this.process.waitFor();
System.out.println("Forge exited with status: " + status);
}
catch (InterruptedException e)
{
throw new LifecycleException("Container was interrupted while stopping.", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.util.Collection;

import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
import org.jboss.arquillian.container.test.spi.TestDeployment;
import org.jboss.arquillian.container.test.spi.client.deployment.DeploymentPackager;
import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor;
import org.jboss.arquillian.core.spi.LoadableExtension;
import org.jboss.forge.arquillian.runner.ServletTestServer;
import org.jboss.forge.arquillian.runner.BeanManagerProducer;
import org.jboss.forge.arquillian.runner.CDIEnricherRemoteExtensionWorkaround;
import org.jboss.forge.arquillian.runner.ServletLoadableExtension;
import org.jboss.forge.arquillian.runner.ServletTestRunner;
import org.jboss.forge.arquillian.runner.ServletTestServer;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand All @@ -26,11 +29,12 @@ public Archive<?> generateDeployment(TestDeployment testDeployment, Collection<P
throw new IllegalStateException("Cannot deploy non JavaArchive.");

JavaArchive deployment = JavaArchive.class.cast(testDeployment.getApplicationArchive());
deployment.addClasses(ServletTestServer.class);
deployment.addClasses(ServletTestServer.class, ServletTestRunner.class, BeanManagerProducer.class,
CDIEnricherRemoteExtensionWorkaround.class);
deployment.addAsServiceProvider(LoadableExtension.class, ServletLoadableExtension.class);
deployment.addAsServiceProvider(RemoteLoadableExtension.class, CDIEnricherRemoteExtensionWorkaround.class);

WebArchive container = ShrinkWrap.create(WebArchive.class);
container.addAsLibraries(ShrinkWrap.create(JavaArchive.class).addClass(ServletTestRunner.class));
container.addAsLibraries(deployment);
container.addAsLibraries(testDeployment.getAuxiliaryArchives());
container.addAsLibraries(resolveDependencies("org.eclipse.jetty:jetty-server:8.1.5.v20120716"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jboss.forge.arquillian.runner;

import javax.enterprise.inject.spi.BeanManager;

import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.ApplicationScoped;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.test.spi.event.suite.BeforeSuite;
import org.jboss.arquillian.testenricher.cdi.container.CDIExtension;

/**
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class BeanManagerProducer
{
@Inject
@ApplicationScoped
private InstanceProducer<BeanManager> beanManagerProducer;

public void findBeanManager(@Observes BeforeSuite context)
{
BeanManager beanManager = CDIExtension.getBeanManager();
if (beanManager != null)
beanManagerProducer.set(beanManager);
else
throw new IllegalStateException("Could not locate BeanManager");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.forge.arquillian.runner;

import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class CDIEnricherRemoteExtensionWorkaround implements RemoteLoadableExtension
{
@Override
public void register(ExtensionBuilder builder)
{
if (Validate.classExists("javax.enterprise.inject.spi.BeanManager"))
{
builder.observer(BeanManagerProducer.class);
}
else
{
throw new IllegalStateException("Test not being deployed to a valid CDI environment");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.jboss.arquillian.container.test.spi.command.CommandService;
import org.jboss.arquillian.core.spi.LoadableExtension;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class ServletLoadableExtension implements LoadableExtension
{
@Override
Expand Down
55 changes: 44 additions & 11 deletions forge-example-plugin/src/main/java/org/example/SimpleService.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,66 @@
package org.example;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

import javax.enterprise.event.Observes;
import javax.inject.Singleton;

import org.jboss.forge.container.event.PostStartup;
import org.jboss.forge.container.event.PreShutdown;
import org.jboss.forge.container.event.Shutdown;
import org.jboss.forge.container.event.Startup;
import org.jboss.forge.container.services.Remote;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
@Remote
@Singleton
public class SimpleService
{
private boolean startupObserved;
private boolean postStartupObserved;
private boolean preShutdownObserved;
private boolean shutdownObserved;

public void startup(@Observes Startup event) throws IOException
{
startupObserved = true;
}

public void postStartup(@Observes PostStartup event) throws IOException
{
System.out.println("SimpleService has been started, and says hello!");
postStartupObserved = true;
}

Enumeration<URL> resources = Thread.currentThread().getContextClassLoader()
.getResources("META-INF/services/javax.enterprise.inject.spi.Extension");
while (resources.hasMoreElements())
{
System.out.println(resources.nextElement().toString());
}
public void preShutdown(@Observes PreShutdown event)
{
preShutdownObserved = true;
}

public void shutdown(@Observes Shutdown event) throws IOException
{
shutdownObserved = true;
}

public void preShutdown(@Observes PreShutdown event)
public boolean isStartupObserved()
{
System.out.println("SimpleService will shut down, and says goodbye!");
return startupObserved;
}

public boolean isPostStartupObserved()
{
return postStartupObserved;
}

public boolean isPreShutdownObserved()
{
return preShutdownObserved;
}

public boolean isShutdownObserved()
{
return shutdownObserved;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.example;

import static org.junit.Assert.*;

import javax.enterprise.inject.spi.Extension;
import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -25,9 +24,11 @@ public static JavaArchive getDeployment()
{
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar")
.addClasses(SimpleService.class, ConsumingService.class, TestExtension.class)
.addAsManifestResource("META-INF/beans.xml", ArchivePaths.create("beans.xml"))
.addAsManifestResource(new StringAsset(""), ArchivePaths.create("beans.xml"))
.addAsServiceProvider(Extension.class, TestExtension.class);

System.out.println(archive.toString(true));

return archive;
}

Expand All @@ -38,11 +39,20 @@ public static JavaArchive getDeployment()
private TestExtension extension;

@Test
public void testContainerStartup()
public void testContainerInjection()
{
Assert.assertNotNull(service);
}

@Test
public void testLifecycle() throws Exception
{
Assert.assertTrue(service.isStartupObserved());
Assert.assertTrue(service.isPostStartupObserved());
Assert.assertFalse(service.isPreShutdownObserved());
Assert.assertFalse(service.isShutdownObserved());
}

@Test
public void testCDIExtensionsFunctionNormally() throws Exception
{
Expand Down

0 comments on commit fa51917

Please sign in to comment.