Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a JUnit 5 Internal module and post ShamrockUnitTest for our in… #571

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,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 @@ -103,9 +103,10 @@
<postgresql-jdbc.version>42.2.5</postgresql-jdbc.version>
<mariadb-jdbc.version>2.3.0</mariadb-jdbc.version>
<spring.version>5.1.4.RELEASE</spring.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 @@ -273,7 +274,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 @@ -686,10 +710,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 @@ -28,10 +28,12 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.enterprise.context.Dependent;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.DotName;
Expand All @@ -41,6 +43,7 @@
import org.jboss.jandex.MethodInfo;
import org.jboss.logging.Logger;
import org.jboss.protean.arc.ArcContainer;
import org.jboss.protean.arc.processor.AnnotationsTransformer;
import org.jboss.protean.arc.processor.BeanDefiningAnnotation;
import org.jboss.protean.arc.processor.BeanProcessor;
import org.jboss.protean.arc.processor.BeanProcessor.Builder;
Expand Down Expand Up @@ -139,6 +142,7 @@ public BeanContainerBuildItem build(ArcDeploymentTemplate arcTemplate,
indexBeanClass(beanClass.getName(), indexer, beanArchiveIndex.getIndex(), additionalIndex, beanClass.getData());
generatedClassNames.add(DotName.createSimple(beanClass.getName()));
}

CompositeIndex index = CompositeIndex.create(indexer.complete(), beanArchiveIndex.getIndex());
Builder builder = BeanProcessor.builder();
builder.setApplicationClassPredicate(new Predicate<DotName>() {
Expand All @@ -153,7 +157,21 @@ public boolean test(DotName dotName) {
return false;
}
});
builder.addAnnotationTransformer(new AnnotationsTransformer() {

@Override
public boolean appliesTo(AnnotationTarget.Kind kind) {
return AnnotationTarget.Kind.CLASS == kind;
}
@Override
public void transform(TransformationContext transformationContext) {
if (additionalBeans.contains(transformationContext.getTarget().asClass().name().toString())) {
transformationContext.transform().add(Dependent.class).done();
}
}
});
builder.setIndex(index);

builder.setAdditionalBeanDefiningAnnotations(additionalBeanDefiningAnnotations.stream()
.map((s) -> new BeanDefiningAnnotation(s.getName(), s.getDefaultScope()))
.collect(Collectors.toList()));
Expand Down
Loading