Skip to content

Commit

Permalink
Merge pull request quarkusio#1932 from stuartwdouglas/1928
Browse files Browse the repository at this point in the history
Fixes quarkusio#1928, if boot fails do not attempt to run other tests
  • Loading branch information
gsmet authored Apr 9, 2019
2 parents 081a529 + e86bd37 commit 8fa5e40
Showing 1 changed file with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstanceFactory;
import org.junit.jupiter.api.extension.TestInstanceFactoryContext;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.junit.jupiter.api.extension.TestInstantiationException;
import org.junit.platform.commons.JUnitException;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.opentest4j.TestAbortedException;

import io.quarkus.bootstrap.BootstrapClassLoaderFactory;
import io.quarkus.bootstrap.BootstrapException;
Expand All @@ -74,10 +74,11 @@
import io.quarkus.test.common.http.TestHTTPResourceManager;

public class QuarkusTestExtension
implements BeforeEachCallback, AfterEachCallback, TestInstanceFactory {
implements BeforeEachCallback, AfterEachCallback, TestInstanceFactory, BeforeAllCallback {

private URLClassLoader appCl;
private ClassLoader originalCl;
private static boolean failedBoot;

private ExtensionState doJavaStart(ExtensionContext context, TestResourceManager testResourceManager) {

Expand Down Expand Up @@ -271,27 +272,41 @@ public void beforeEach(ExtensionContext context) throws Exception {
@Override
public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext)
throws TestInstantiationException {
if (failedBoot) {
try {
return extensionContext.getRequiredTestClass().newInstance();
} catch (Exception e) {
throw new TestInstantiationException("Boot failed", e);
}
}
ExtensionContext root = extensionContext.getRoot();
ExtensionContext.Store store = root.getStore(ExtensionContext.Namespace.GLOBAL);
ExtensionState state = store.get(ExtensionState.class.getName(), ExtensionState.class);
PropertyTestUtil.setLogFileProperty();
boolean substrateTest = extensionContext.getRequiredTestClass().isAnnotationPresent(SubstrateTest.class);
if (state == null) {
TestResourceManager testResourceManager = new TestResourceManager(extensionContext.getRequiredTestClass());
testResourceManager.start();

if (substrateTest) {
NativeImageLauncher launcher = new NativeImageLauncher(extensionContext.getRequiredTestClass());
try {
launcher.start();
} catch (IOException e) {
throw new JUnitException("Quarkus native image start failed, original cause: " + e);
try {
testResourceManager.start();

if (substrateTest) {
NativeImageLauncher launcher = new NativeImageLauncher(extensionContext.getRequiredTestClass());
try {
launcher.start();
} catch (IOException e) {
throw new JUnitException("Quarkus native image start failed, original cause: " + e);
}
state = new ExtensionState(testResourceManager, launcher, true);
} else {
state = doJavaStart(extensionContext, testResourceManager);
}
state = new ExtensionState(testResourceManager, launcher, true);
} else {
state = doJavaStart(extensionContext, testResourceManager);
store.put(ExtensionState.class.getName(), state);

} catch (RuntimeException e) {
testResourceManager.stop();
failedBoot = true;
throw e;
}
store.put(ExtensionState.class.getName(), state);
} else {
if (substrateTest != state.isSubstrate()) {
throw new RuntimeException(
Expand All @@ -318,6 +333,13 @@ private static ClassLoader setCCL(ClassLoader cl) {
return original;
}

@Override
public void beforeAll(ExtensionContext context) throws Exception {
if (failedBoot) {
throw new TestAbortedException("Not running test as boot failed");
}
}

class ExtensionState implements ExtensionContext.Store.CloseableResource {

private final TestResourceManager testResourceManager;
Expand Down

0 comments on commit 8fa5e40

Please sign in to comment.