Skip to content

Commit

Permalink
Fixes #553 Create a JUnit 5 test runner
Browse files Browse the repository at this point in the history
There are now three different runners:

- junit5: our preferred runner for end users
- junit4: legacy runner for JUnit 4
- junit5-internal: unit test runner for testing Shamrock itself
  • Loading branch information
stuartwdouglas committed Jan 21, 2019
1 parent 727fbe1 commit 42e68d5
Show file tree
Hide file tree
Showing 140 changed files with 1,797 additions and 1,340 deletions.
8 changes: 7 additions & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,17 @@

<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit</artifactId>
<artifactId>shamrock-junit4</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit5</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
50 changes: 46 additions & 4 deletions build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@
<h2.version>1.4.197</h2.version>
<postgresql-jdbc.version>42.2.5</postgresql-jdbc.version>
<mariadb-jdbc.version>2.3.0</mariadb-jdbc.version>
<junit.version>4.12</junit.version>
<shrinkwrap.version>1.2.6</shrinkwrap.version>
<rest-assured.version>3.3.0</rest-assured.version>
<junit4.version>4.12</junit4.version>
<junit.jupiter.version>5.3.2</junit.jupiter.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -272,7 +273,30 @@
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit</artifactId>
<artifactId>shamrock-test-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-test-h2</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit4</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit5</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit5-internal</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand Down Expand Up @@ -680,10 +704,28 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<version>${junit4.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.jboss.builder.BuildChain;
import org.jboss.builder.BuildChainBuilder;
Expand All @@ -44,12 +45,14 @@ public class ShamrockAugmentor {
private final ClassLoader classLoader;
private final Path root;
private final Set<Class<? extends BuildItem>> finalResults;
private final List<Consumer<BuildChainBuilder>> buildChainCustomizers;

ShamrockAugmentor(Builder builder) {
this.output = builder.output;
this.classLoader = builder.classLoader;
this.root = builder.root;
this.finalResults = new HashSet<>(builder.finalResults);
this.buildChainCustomizers = new ArrayList<>(builder.buildChainCustomizers);
}

public BuildResult run() throws Exception {
Expand Down Expand Up @@ -79,6 +82,9 @@ public void execute(BuildContext context) {
.produces(ShutdownContextBuildItem.class)
.produces(ClassOutputBuildItem.class)
.build();
for (Consumer<BuildChainBuilder> i : buildChainCustomizers) {
i.accept(chainBuilder);
}
for (Class<? extends BuildItem> i : finalResults) {
chainBuilder.addFinal(i);
}
Expand Down Expand Up @@ -115,7 +121,12 @@ public static final class Builder {
ClassLoader classLoader;
Path root;
Set<Class<? extends BuildItem>> finalResults = new HashSet<>();
private final List<Consumer<BuildChainBuilder>> buildChainCustomizers = new ArrayList<>();

public Builder addBuildChainCustomizer(Consumer<BuildChainBuilder> customizer) {
this.buildChainCustomizers.add(customizer);
return this;
}

public List<Path> getAdditionalApplicationArchives() {
return additionalApplicationArchives;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Red Hat, Inc.
* Copyright 2019 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,21 +14,18 @@
* limitations under the License.
*/

package org.jboss.shamrock.example.undertow;
package org.jboss.shamrock.deployment.builditem;

import java.io.IOException;
import org.jboss.builder.item.SimpleBuildItem;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class ApplicationClassNameBuildItem extends SimpleBuildItem {
private final String className;

public ApplicationClassNameBuildItem(String className) {
this.className = className;
}

@WebServlet(name = "InjectionServlet", urlPatterns = "/test")
public class TestServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().write("hello world");
public String getClassName() {
return className;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import org.jboss.builder.Version;
Expand All @@ -32,8 +33,10 @@
import org.jboss.protean.gizmo.MethodDescriptor;
import org.jboss.protean.gizmo.ResultHandle;
import org.jboss.protean.gizmo.TryBlock;
import org.jboss.shamrock.annotations.BuildProducer;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.deployment.ClassOutput;
import org.jboss.shamrock.deployment.builditem.ApplicationClassNameBuildItem;
import org.jboss.shamrock.deployment.builditem.ClassOutputBuildItem;
import org.jboss.shamrock.deployment.builditem.FeatureBuildItem;
import org.jboss.shamrock.deployment.builditem.HttpServerBuiltItem;
Expand All @@ -51,17 +54,23 @@ class MainClassBuildStep {
private static final String APP_CLASS = "org.jboss.shamrock.runner.ApplicationImpl";
private static final String MAIN_CLASS = "org.jboss.shamrock.runner.GeneratedMain";
private static final String STARTUP_CONTEXT = "STARTUP_CONTEXT";

private static final AtomicInteger COUNT = new AtomicInteger();

@BuildStep
MainClassBuildItem build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
List<MainBytecodeRecorderBuildItem> mainMethod,
List<SystemPropertyBuildItem> properties,
Optional<HttpServerBuiltItem> httpServer,
List<FeatureBuildItem> features,
BuildProducer<ApplicationClassNameBuildItem> appClassNameProducer,
ClassOutputBuildItem classOutput) {

String appClassName = APP_CLASS + COUNT.incrementAndGet();
appClassNameProducer.produce(new ApplicationClassNameBuildItem(appClassName));

// Application class
ClassCreator file = new ClassCreator(ClassOutput.gizmoAdaptor(classOutput.getClassOutput(), true), APP_CLASS, null, Application.class.getName());
ClassCreator file = new ClassCreator(ClassOutput.gizmoAdaptor(classOutput.getClassOutput(), true), appClassName, null, Application.class.getName());

// Application class: static init

Expand Down Expand Up @@ -149,7 +158,7 @@ MainClassBuildItem build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
mv = file.getMethodCreator("main", void.class, String[].class);
mv.setModifiers(Modifier.PUBLIC | Modifier.STATIC);

final ResultHandle appClassInstance = mv.newInstance(ofConstructor(APP_CLASS));
final ResultHandle appClassInstance = mv.newInstance(ofConstructor(appClassName));
// run the app
mv.invokeVirtualMethod(ofMethod(Application.class, "run", void.class, String[].class), appClassInstance, mv.getMethodParam(0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import org.jboss.builder.BuildChainBuilder;
import org.jboss.builder.BuildResult;
import org.jboss.shamrock.deployment.ShamrockAugmentor;
import org.jboss.shamrock.deployment.builditem.ApplicationClassNameBuildItem;
import org.jboss.shamrock.deployment.builditem.BytecodeTransformerBuildItem;
import org.jboss.shamrock.deployment.builditem.MainClassBuildItem;
import org.jboss.shamrock.runtime.Application;
import org.objectweb.asm.ClassVisitor;

Expand All @@ -44,11 +45,19 @@ public class RuntimeRunner implements Runnable, Closeable {
private final RuntimeClassLoader loader;
private Closeable closeTask;
private final List<Path> additionalArchives;
private final List<Consumer<BuildChainBuilder>> chainCustomizers = new ArrayList<>();


public RuntimeRunner(ClassLoader classLoader, Path target, Path frameworkClassesPath, Path transformerCache, List<Path> additionalArchives) {
this(classLoader, target, frameworkClassesPath, transformerCache, additionalArchives, Collections.emptyList());
}

public RuntimeRunner(ClassLoader classLoader, Path target, Path frameworkClassesPath, Path transformerCache, List<Path> additionalArchives, List<Consumer<BuildChainBuilder>> chainCustomizers) {
this.target = target;
this.additionalArchives = additionalArchives;
this.loader = new RuntimeClassLoader(classLoader, target, frameworkClassesPath, transformerCache);
this.chainCustomizers.addAll(chainCustomizers);
RuntimeClassLoader rcl = new RuntimeClassLoader(classLoader, target, frameworkClassesPath, transformerCache);
this.loader = rcl;
}

@Override
Expand All @@ -69,8 +78,11 @@ public void run() {
for (Path i : additionalArchives) {
builder.addAdditionalApplicationArchive(i);
}
for (Consumer<BuildChainBuilder> i : chainCustomizers) {
builder.addBuildChainCustomizer(i);
}
builder.addFinal(BytecodeTransformerBuildItem.class)
.addFinal(MainClassBuildItem.class);
.addFinal(ApplicationClassNameBuildItem.class);

BuildResult result = builder.build().run();
List<BytecodeTransformerBuildItem> bytecodeTransformerBuildItems = result.consumeMulti(BytecodeTransformerBuildItem.class);
Expand All @@ -81,33 +93,11 @@ public void run() {
}

loader.setTransformers(functions);
if (!functions.isEmpty()) {
//transformation can be slow, and classes that are transformed are generally always loaded on startup
//to speed this along we eagerly load the classes in parallel
//TODO: do we need this? apparently there have been big perf fixes
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (Map.Entry<String, List<BiFunction<String, ClassVisitor, ClassVisitor>>> entry : functions.entrySet()) {
executorService.submit(new Runnable() {
@Override
public void run() {
try {
loader.loadClass(entry.getKey(), true);
} catch (ClassNotFoundException e) {
//ignore
//this will show up at runtime anyway
}
}
});
}
executorService.shutdown();

}
}


final Application application;
// todo - I guess this class name should come from a build item?
Class<? extends Application> appClass = loader.findClass("org.jboss.shamrock.runner.ApplicationImpl").asSubclass(Application.class);
Class<? extends Application> appClass = loader.loadClass(result.consume(ApplicationClassNameBuildItem.class).getClassName()).asSubclass(Application.class);
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(loader);
Expand All @@ -124,6 +114,8 @@ public void close() {
}
};

} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/getting-started-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ Edit the `pom.xml` file to add the 2 following dependencies:
----
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit</artifactId>
<artifactId>shamrock-junit4</artifactId>
<version>${shamrock.version}</version>
<scope>test</scope>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion extensions/arc/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit</artifactId>
<artifactId>shamrock-junit5-internal</artifactId>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ public boolean test(DotName dotName) {
}
});
builder.setIndex(index);

builder.setAdditionalBeanDefiningAnnotations(additionalBeanDefiningAnnotations.stream()
.map((s) -> new BeanDefiningAnnotation(s.getName(), s.getDefaultScope()))
.collect(Collectors.toList()));
builder.setSharedAnnotationLiterals(false);
builder.setAdditionalBeanNames(new HashSet<>(additionalBeans));
builder.addResourceAnnotations(resourceAnnotations.stream()
.map(ResourceAnnotationBuildItem::getName)
.collect(Collectors.toList()));
Expand Down

This file was deleted.

Loading

0 comments on commit 42e68d5

Please sign in to comment.