Skip to content

Commit

Permalink
Support Test Resources from @testprofile in native mode
Browse files Browse the repository at this point in the history
## Description
In quarkusio#13154, the annotation `@TestProfile` was supported also in Native tests. However, the native extension was not processing the test resources in the test profile like:

```
public class ConfluentTestProfile implements QuarkusTestProfile {

    @OverRide
    public String getConfigProfile() {
        return "confluent";
    }

    @OverRide
    public List<TestResourceEntry> testResources() {
        return Collections.singletonList(new TestResourceEntry(ConfluentKafkaResource.class));
    }
}
```

This PR makes the above to be supported.
The solution behaves the same as done in the Quarkus Test extension.
  • Loading branch information
Sgitario committed Jun 22, 2021
1 parent a5c0d77 commit 40e02c5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.quarkus.it.nat.test.profile;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.NativeImageTest;

/**
* This test ensures that the NativeTestExtension starts the test resources from the Test Profile annotation.
*/
@NativeImageTest
public class RuntimeValueChangeFromTestResourcesIT extends RuntimeValueChangeFromTestResourcesTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.it.nat.test.profile;

import static org.hamcrest.Matchers.is;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;

@QuarkusTest
@TestProfile(RuntimeValueChangeFromTestResourcesTest.CustomTestProfile.class)
public class RuntimeValueChangeFromTestResourcesTest {

private static final String EXPECTED_VALUE = "RuntimeTimeValueChangeFromTestResources";

@Test
public void failInNativeTestExtension_beforeEach() {
RestAssured.when()
.get("/native-config-profile/myConfigValue")
.then()
.body(is(EXPECTED_VALUE));
}

public static class CustomTestProfile implements QuarkusTestProfile {
@Override
public List<TestResourceEntry> testResources() {
return Collections.singletonList(new TestResourceEntry(DummyTestResource.class));
}
}

/**
* This only used to ensure that the TestResource has been handled correctly by the QuarkusTestExtension
*/
public static class DummyTestResource implements QuarkusTestResourceLifecycleManager {

@Override
public Map<String, String> start() {
return Collections.singletonMap("my.config.value", EXPECTED_VALUE);
}

@Override
public void stop() {
// do nothing
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import static io.quarkus.test.junit.IntegrationTestUtil.handleDevDb;
import static io.quarkus.test.junit.IntegrationTestUtil.startLauncher;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -113,7 +116,8 @@ private IntegrationTestExtensionState doNativeStart(ExtensionContext context, Cl
TestProfileAndProperties testProfileAndProperties = determineTestProfileAndProperties(profile, sysPropRestore);

testResourceManager = new TestResourceManager(requiredTestClass, quarkusTestProfile,
Collections.emptyList(), testProfileAndProperties.testProfile != null
getAdditionalTestResources(testProfileAndProperties.testProfile, currentJUnitTestClass.getClassLoader()),
testProfileAndProperties.testProfile != null
&& testProfileAndProperties.testProfile.disableGlobalTestResources());
testResourceManager.init();
hasPerTestResources = testResourceManager.hasPerTestResources();
Expand Down Expand Up @@ -175,6 +179,39 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
}
}

/**
* Since {@link TestResourceManager} is loaded from the ClassLoader passed in as an argument,
* we need to convert the user input {@link QuarkusTestProfile.TestResourceEntry} into instances of
* {@link TestResourceManager.TestResourceClassEntry}
* that are loaded from that ClassLoader
*/
private List<TestResourceManager.TestResourceClassEntry> getAdditionalTestResources(
QuarkusTestProfile profileInstance, ClassLoader classLoader) {
if ((profileInstance == null) || profileInstance.testResources().isEmpty()) {
return Collections.emptyList();
}

try {
Constructor<?> testResourceClassEntryConstructor = Class
.forName(TestResourceManager.TestResourceClassEntry.class.getName(), true, classLoader)
.getConstructor(Class.class, Map.class, Annotation.class, boolean.class);

List<QuarkusTestProfile.TestResourceEntry> testResources = profileInstance.testResources();
List<TestResourceManager.TestResourceClassEntry> result = new ArrayList<>(testResources.size());
for (QuarkusTestProfile.TestResourceEntry testResource : testResources) {
TestResourceManager.TestResourceClassEntry instance = (TestResourceManager.TestResourceClassEntry) testResourceClassEntryConstructor
.newInstance(
Class.forName(testResource.getClazz().getName(), true, classLoader), testResource.getArgs(),
null, testResource.isParallel());
result.add(instance);
}

return result;
} catch (Exception e) {
throw new IllegalStateException("Unable to handle profile " + profileInstance.getClass(), e);
}
}

private void throwBootFailureException() throws Exception {
if (firstException != null) {
Throwable throwable = firstException;
Expand Down

0 comments on commit 40e02c5

Please sign in to comment.