Skip to content

Commit

Permalink
Explicitly prevent the use of @TestProfile with @NativeImageTest
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Aug 3, 2020
1 parent 09d44b0 commit 812150a
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

import javax.inject.Inject;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
Expand All @@ -20,6 +25,7 @@
import io.quarkus.test.common.NativeImageLauncher;
import io.quarkus.test.common.PropertyTestUtil;
import io.quarkus.test.common.RestAssuredURLManager;
import io.quarkus.test.common.TestClassIndexer;
import io.quarkus.test.common.TestResourceManager;
import io.quarkus.test.common.TestScopeManager;
import io.quarkus.test.common.http.TestHTTPResourceManager;
Expand Down Expand Up @@ -49,18 +55,20 @@ public void beforeEach(ExtensionContext context) throws Exception {

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
ensureNoInjectAnnotationIsUsed(extensionContext.getRequiredTestClass());

Class<?> testClass = extensionContext.getRequiredTestClass();
ensureNoInjectAnnotationIsUsed(testClass);
ExtensionContext root = extensionContext.getRoot();
ExtensionContext.Store store = root.getStore(ExtensionContext.Namespace.GLOBAL);
ExtensionState state = store.get(ExtensionState.class.getName(), ExtensionState.class);
PropertyTestUtil.setLogFileProperty();
if (state == null) {
TestResourceManager testResourceManager = new TestResourceManager(extensionContext.getRequiredTestClass());
ensureNoTestProfile(testClass);

TestResourceManager testResourceManager = new TestResourceManager(testClass);
try {
testResourceManager.init();
Map<String, String> systemProps = testResourceManager.start();
NativeImageLauncher launcher = new NativeImageLauncher(extensionContext.getRequiredTestClass());
NativeImageLauncher launcher = new NativeImageLauncher(testClass);
launcher.addSystemProperties(systemProps);
try {
launcher.start();
Expand Down Expand Up @@ -100,6 +108,28 @@ private void ensureNoInjectAnnotationIsUsed(Class<?> testClass) {

}

/**
* We don't support {@link TestProfile} in native tests because we don't want to incur the native binary rebuild cost
* which is very high.
*
* This method looks up the annotations via Jandex in order to try and prevent the image generation if there are
* any cases of {@link NativeImageTest} being used with {@link TestProfile}
*/
private void ensureNoTestProfile(Class<?> testClass) {
Index index = TestClassIndexer.readIndex(testClass);
List<AnnotationInstance> instances = index.getAnnotations(DotName.createSimple(NativeImageTest.class.getName()));
for (AnnotationInstance instance : instances) {
if (instance.target().kind() != AnnotationTarget.Kind.CLASS) {
continue;
}
ClassInfo testClassInfo = instance.target().asClass();
if (testClassInfo.classAnnotation(DotName.createSimple(TestProfile.class.getName())) != null) {
throw new JUnitException(
"@TestProfile is not supported in NativeImageTest tests. Offending class is " + testClassInfo.name());
}
}
}

@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
TestHTTPResourceManager.inject(testInstance);
Expand Down

0 comments on commit 812150a

Please sign in to comment.