Skip to content

Instantly share code, notes, and snippets.

@taichi
Last active December 17, 2015 17:19
Show Gist options
  • Save taichi/5644953 to your computer and use it in GitHub Desktop.
Save taichi/5644953 to your computer and use it in GitHub Desktop.
wildfly alpha1 + arquillian-remote + arquillian-managed on Gradle without maven resolver. production codes are java and test codes are groovy.

requirements

setup

  • download Wildfly
  • extract archive and set gradle.properties
  • run gradle wrapper

run tests using eclipse on remote wildfly

  • run gradlew eclipse
  • run gradlew resolveDeps
  • execute bin/standalone.bat at wildfly
  • run tests

run tests using gradle on managed wildfly

  • run gradlew test
package jp.example
import jp.example.ExampleApplication
import jp.example.log.Debug
import org.jboss.shrinkwrap.api.ShrinkWrap
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset
import org.jboss.shrinkwrap.api.spec.WebArchive
import com.google.common.collect.ObjectArrays
import com.google.common.io.Resources
public class Archives {
static final ClassLoaderAsset CDI = new ClassLoaderAsset(
"META-INF/beans.xml")
def static readDeps() throws IOException {
// gradlew resolveDeps task makes runtime.deps file.
return Resources.getResource("runtime.deps").readLines().collect { new File(it) } as File[]
}
public static WebArchive beans(... classes) throws IOException {
WebArchive archive = ShrinkWrap
.create(WebArchive)
.addClasses(classes as Class[])
.addPackage(Debug.class.getPackage())
.addAsResource(new ClassLoaderAsset("log4j2.xml"), "log4j2.xml")
.addAsWebInfResource(CDI, "beans.xml")
.addAsLibraries(readDeps())
return archive
}
public static WebArchive app(... classes) throws IOException {
return beans(ObjectArrays.concat(ExampleApplication, classes))
}
}
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0" />
</arquillian>
package jp.example
public class AutoCloser {
def static handle(target, Closure closure) {
try {
closure(target)
} finally {
target?.close()
}
}
}
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<!--
http://docs.jboss.org/weld/reference/2.0.0.Beta2/en-US/html_single/#interceptors
-->
<interceptors>
<class>jp.example.log.DebugInterceptor</class>
</interceptors>
</beans>
apply plugin: 'project-report'
apply plugin: 'war'
repositories {
mavenCentral()
mavenRepo url: 'http://repository.jboss.org/nexus/content/groups/public'
}
configurations.all { resolutionStrategy.cacheChangingModulesFor 14, 'days' }
[
'java',
'deps',
'test',
'ide',
'misc'
].each {
apply from: "gradle/${it}.gradle"
}
package jp.example.log;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@InterceptorBinding
@Target({ METHOD, TYPE })
@Retention(RUNTIME)
public @interface Debug {
}
package jp.example.log;
import java.lang.reflect.Method;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Debug
@Interceptor
public class DebugInterceptor {
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
Method method = context.getMethod();
Logger logger = LogManager.getLogger(method.getDeclaringClass());
try {
// TODO : do more debuggable
logger.entry(context.getParameters());
return logger.exit(context.proceed());
} catch (Exception t) {
logger.catching(t);
logger.exit();
throw t;
}
}
}
dependencies {
// compile 'net.iharder:base64:2.3.8'
compile 'com.google.guava:guava:14.+'
['api', 'core'].each { compile "org.apache.logging.log4j:log4j-${it}:2.0-beta6" }
providedCompile 'javax:javaee-web-api:7.0'
}
package jp.example;
public class Example {
int id = 0;
String name;
public Example() {
}
public Example(int id) {
this.id = id;
}
public Example(String name) {
this.name = name;
}
public Example(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package jp.example;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("")
public class ExampleApplication extends Application {
// http://docs.jboss.org/resteasy/docs/3.0-beta-5/userguide/html_single/index.html
}
package jp.example;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import jp.example.log.Debug;
@Path("/example")
public class ExampleResource {
@Inject
HogeBean hogeBean;
@Debug
@GET
@Produces("application/json")
public Response index() throws Exception {
return Response.ok(new Example(hogeBean.doHoge())).build();
}
@Debug
@GET
@Path("{name}")
@Produces("application/json")
public Response name(@PathParam("name") String name) {
return Response.ok(new Example(33, name)).build();
}
}
package jp.example
import javax.ws.rs.client.Client
import javax.ws.rs.client.ClientBuilder
import javax.ws.rs.client.WebTarget
import javax.ws.rs.client.Invocation.Builder
import jp.example.Example;
import jp.example.ExampleResource;
import jp.example.HogeBean;
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.jboss.arquillian.container.test.api.Deployment
import org.jboss.arquillian.container.test.api.RunAsClient
import org.jboss.arquillian.junit.Arquillian
import org.jboss.arquillian.test.api.ArquillianResource
import org.jboss.shrinkwrap.api.spec.WebArchive
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(Arquillian)
public class ExampleResourceTest {
static final Logger LOG = LogManager.getLogger(ExampleResourceTest)
@Deployment
public static WebArchive deploy() throws IOException {
WebArchive web = Archives.app(HogeBean, Example, ExampleResource)
LOG.debug(web.toString(true))
return web
}
Client client
@ArquillianResource
URL baseURL
@Before
public void setUp() {
client = ClientBuilder.newClient()
assert client
}
@After
public void tearDown() {
client?.close()
}
Builder request(String path) throws Exception {
WebTarget target = client.target(baseURL.toURI()).path(
"/example/$path")
return target.request()
}
@Test
@RunAsClient
public void index() throws Exception {
AutoCloser.handle(request("").get()) {
assert 200 == it.status
Example exp = it.readEntity(Example)
assert "mogemoge" == exp.name
}
}
@Test
@RunAsClient
public void name() throws Exception {
AutoCloser.handle(request("aaaa").get()) {
assert 200 == it.status
Example exp = it.readEntity(Example)
assert "aaaa" == exp.name
}
}
}
jbossHome=C:\\development\\java\\wildfly-8.0.0.Alpha1
package jp.example;
public class HogeBean {
public String doHoge() {
return "mogemoge";
}
}
package jp.example
import javax.inject.Inject
import jp.example.HogeBean;
import org.jboss.arquillian.container.test.api.Deployment
import org.jboss.arquillian.junit.Arquillian
import org.jboss.shrinkwrap.api.spec.WebArchive
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(Arquillian)
public class HogeBeanTest {
@Deployment
public static WebArchive deploy() throws IOException {
return Archives.beans(HogeBean)
}
@Inject
HogeBean bean
@Test
public void inject() {
assert bean
assert bean.doHoge()
}
}
apply plugin: 'eclipse'
eclipse {
classpath {
containers = [
'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
]
file {
whenMerged {
it.entries.findAll { it.path.startsWith 'src/main' }*.output = "$webAppDirName/WEB-INF/classes"
}
}
}
}
apply plugin: 'java'
apply plugin: 'groovy'
sourceCompatibility = targetCompatibility = 1.7
tasks.withType(AbstractCompile) each { it.options.encoding = 'UTF-8' }
org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
<?xml version="1.0" encoding="UTF-8"?>
<!--
http://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout
-->
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{2} - %msg%xEx%n"/>
</Console>
</appenders>
<loggers>
<logger name="jp.example" level="trace" />
<root level="info">
<appender-ref ref="Console" />
</root>
</loggers>
</configuration>
task wrapper(type: Wrapper) {
gradleVersion = 1.6
}
task initdirs << {
sourceSets*.allSource.srcDirs.flatten()*.mkdirs()
webAppDir.mkdirs()
}
configurations { testManaged { extendsFrom testCompile } }
dependencies {
testCompile 'junit:junit:4.+'
testCompile localGroovy()
def resteasy = '3.0-beta-4'
['client', 'jackson-provider'].each {
testCompile ("org.jboss.resteasy:resteasy-$it:$resteasy")
{ exclude group: 'org.slf4j' }
}
def arquillian = '1.0.3.Final'
testCompile "org.jboss.arquillian.junit:arquillian-junit-container:$arquillian"
testCompile "org.jboss.arquillian.protocol:arquillian-protocol-servlet:$arquillian"
testRuntime ('org.wildfly:wildfly-arquillian-container-remote:8.0.0.Alpha1') {
exclude module: 'wildfly-ee'
exclude module: 'wildfly-server'
exclude module: 'wildfly-osgi-service'
exclude module: 'wildfly-jmx'
exclude module: 'wildfly-arquillian-protocol-jmx'
exclude module: 'wildfly-arquillian-testenricher-msc'
exclude module: 'arquillian-testenricher-osgi'
exclude group: 'org.jboss.shrinkwrap.resolver'
}
testRuntime "org.jboss:jboss-common-core:2.2.17.GA"
testManaged 'org.wildfly:wildfly-arquillian-container-managed:8.0.0.Alpha1'
}
task resolveDeps << {
file('src/test/resources/runtime.deps').withWriter { w ->
def classpath = configurations.compile - configurations.providedCompile + compileTestGroovy.groovyClasspath
classpath.each { w.println it }
}
}
processTestResources.dependsOn resolveDeps
test {
systemProperty 'java.util.logging.manager', 'org.jboss.logmanager.LogManager'
systemProperty 'jboss.home', jbossHome
classpath = sourceSets.main.output + sourceSets.test.output + configurations.testManaged
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment