Skip to content

Commit

Permalink
Support QuarkusTestResourceLifecycleManager#inject in CLI test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Apr 22, 2022
1 parent 0c1ad3b commit 3bebb40
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Map;

import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import io.quarkus.test.junit.main.Launch;
import io.quarkus.test.junit.main.LaunchResult;
import io.quarkus.test.junit.main.QuarkusMainLauncher;
import io.quarkus.test.junit.main.QuarkusMainTest;

@QuarkusMainTest
@QuarkusTestResource(PicocliTest.TestResource.class)
public class PicocliTest {

private String value;

@Test
@Launch({ "test-command", "-f", "test.txt", "-f", "test2.txt", "-f", "test3.txt", "-s", "ERROR", "-h", "SOCKS=5.5.5.5",
"-p", "privateValue", "pos1", "pos2" })
Expand All @@ -23,6 +30,8 @@ public void testBasicReflection(LaunchResult result) throws UnknownHostException
.contains("-p:privateValue")
.contains("-p:privateValue")
.contains("positional:[pos1, pos2]");

assertThat(value).isNotNull();
}

@Test
Expand Down Expand Up @@ -57,6 +66,8 @@ public void testIncludeLogCommand(QuarkusMainLauncher launcher) {
@Launch({ "command-used-as-parent", "-p", "testValue", "child" })
public void testParentCommand(LaunchResult result) {
assertThat(result.getOutput()).isEqualTo("testValue");

assertThat(value).isNotNull();
}

@Test
Expand All @@ -66,18 +77,24 @@ public void testCommandWithArgGroup(LaunchResult result) {
.contains("-a:0")
.contains("-b:150")
.contains("-c:0");

assertThat(value).isNotNull();
}

@Test
@Launch({ "dynamic-proxy" })
public void testDynamicProxy(LaunchResult result) {
assertThat(result.getOutput()).isEqualTo("2007-12-03T10:15:30");

assertThat(value).isNotNull();
}

@Test
@Launch("quarkus")
public void testDynamicVersionProvider(LaunchResult launchResult) {
assertThat(launchResult.getOutput()).contains("quarkus version 1.0");

assertThat(value).isNotNull();
}

@Test
Expand All @@ -88,6 +105,8 @@ public void testUnmatched(LaunchResult launchResult) {
.contains("-b:null")
.contains("remainder:[More]")
.contains("unmatched[-x]");

assertThat(value).isNotNull();
}

@Test
Expand All @@ -110,5 +129,26 @@ public void testCompletionReflection() {
@Launch("default-value-provider")
public void testDefaultValueProvider(LaunchResult result) {
assertThat(result.getOutput()).isEqualTo("default:default-value");

assertThat(value).isNotNull();
}

public static class TestResource implements QuarkusTestResourceLifecycleManager {

@Override
public Map<String, String> start() {
return Collections.emptyMap();
}

@Override
public void inject(TestInjector testInjector) {
testInjector.injectIntoFields("dummy", f -> f.getName().equals("value"));
}

@Override
public void stop() {

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ private ArtifactLauncher.LaunchResult doProcessStart(ExtensionContext context, S
}
additionalProperties.putAll(resourceManagerProps);

testResourceManager.inject(context.getRequiredTestInstance());

ArtifactLauncher<?> launcher = null;
ServiceLoader<ArtifactLauncherProvider> loader = ServiceLoader.load(ArtifactLauncherProvider.class);
for (ArtifactLauncherProvider launcherProvider : loader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ private int doJavaStart(ExtensionContext context, Class<? extends QuarkusTestPro
hasPerTestResources = (boolean) testResourceManager.getClass().getMethod("hasPerTestResources")
.invoke(testResourceManager);

testResourceManager.getClass().getMethod("inject", Object.class)
.invoke(testResourceManager, context.getRequiredTestInstance());

var result = startupAction.runMainClassBlocking(arguments);
flushAllLoggers();
return result;
Expand Down

0 comments on commit 3bebb40

Please sign in to comment.