Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Sep 19, 2024
1 parent 443e67a commit 19686f0
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.quarkus.ts.qe.command;

import io.quarkus.logging.Log;
import io.quarkus.picocli.runtime.annotations.TopCommand;

import picocli.CommandLine;

@TopCommand
@CommandLine.Command(name = "customized-command", mixinStandardHelpOptions = true, subcommands = { HelloCommand.class,
AgeCommand.class })
public class EntryCommand implements Runnable {
Expand All @@ -14,6 +12,6 @@ public class EntryCommand implements Runnable {

@Override
public void run() {
Log.info("Running command with name: " + spec.name());
Log.info("Running EntryCommand with name: " + spec.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.ts.qe.command;

import io.quarkus.logging.Log;

import picocli.CommandLine;

@CommandLine.Command(name = "start", description = "Starts the service.")
public class OtherCommand implements Runnable {

@CommandLine.Mixin
CommonOptions commonOptions;

@CommandLine.Option(names = { "-t", "--timeout" }, description = "Timeout in seconds", defaultValue = "60")
int timeout;

@Override
public void run() {
Log.infof("Service started with timeout: %d and verbosity: %s%n", timeout, commonOptions);
}

}

class CommonOptions {
@CommandLine.Option(names = { "-v", "--verbose" }, description = "Enable verbose mode")
boolean verbose;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.ts.qe.command;

import io.quarkus.logging.Log;

import picocli.CommandLine;

@CommandLine.Command(name = "other", mixinStandardHelpOptions = true, subcommands = { OtherCommand.class })
public class OtherEntryCommand implements Runnable {
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

@Override
public void run() {
Log.info("OtherEntryCommand with name " + spec.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.ts.qe.configuration;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

import io.quarkus.arc.profile.IfBuildProfile;
import io.quarkus.picocli.runtime.annotations.TopCommand;
import io.quarkus.ts.qe.command.EntryCommand;
import io.quarkus.ts.qe.command.OtherEntryCommand;

@ApplicationScoped
public class Config {
@Produces
@TopCommand
@IfBuildProfile("dev")
public Object devCommand() {
return EntryCommand.class;
}

@Produces
@TopCommand
@IfBuildProfile("prod")
public Object prodCommand() {
return OtherEntryCommand.class;
}

}
1 change: 1 addition & 0 deletions quarkus-picocli/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.profile=dev
23 changes: 7 additions & 16 deletions quarkus-picocli/src/test/java/PicocliIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ public class PicocliIT {
static final RestService greetingApp = new RestService()
.withProperty("quarkus.args",
GREETING_COMMAND_DESC + " " + GREETING_NAME_OPTION + " " + GREETING_NAME_VALUE)
.withProperty("quarkus.profile", "dev")
.setAutoStart(false);

@QuarkusApplication
static final RestService ageApp = new RestService()
.withProperty("quarkus.args",
AGE_COMMAND_DESC + " " + AGE_OPTION + " " + AGE_VALUE)
.withProperty("quarkus.profile", "dev")
.setAutoStart(false);

@QuarkusApplication
static final RestService greetingBlankArgumentApp = new RestService()
.withProperty("quarkus.args",
"" + " " + GREETING_NAME_OPTION + " " + GREETING_NAME_VALUE)
.withProperty("quarkus.profile", "dev")
.setAutoStart(false);

@QuarkusApplication
static final RestService greetingInvalidArgumentApp = new RestService()
.withProperty("quarkus.args",
GREETING_COMMAND_DESC + " " + "-x" + " " + GREETING_NAME_VALUE)
.withProperty("quarkus.profile", "dev")
.setAutoStart(false);

@QuarkusApplication
static final RestService bothTopCommandApp = new RestService()
.setAutoStart(false);

@QuarkusApplication
static final RestService customizedCommandApp = new RestService()
.withProperty("quarkus.args", "customized-command")
.withProperty("quarkus.profile", "dev")
.setAutoStart(false);

@Test
Expand All @@ -62,11 +62,12 @@ public void verifyErrorForApplicationScopedBeanInPicocliCommand() {
}

@Test
public void verifyGreetingCommandOutputsExpectedMessage() {
public void verifyGreetingCommandOutputsExpectedMessage() throws InterruptedException {
String expectedOutput = String.format("Hello %s!", GREETING_NAME_VALUE);
try {
runAsync(greetingApp::start);
greetingApp.logs().assertContains(expectedOutput);
Thread.sleep(3500);
} finally {
greetingApp.stop();
}
Expand Down Expand Up @@ -114,14 +115,4 @@ public void verifyErrorForMultipleCommandsWithoutTopCommand() {
}
}

@Test
public void verifyCustomizedCommandLineBehavior() {
String expectedOutput = "customized";
try {
runAsync(customizedCommandApp::start);
customizedCommandApp.logs().assertContains(expectedOutput);
} finally {
customizedCommandApp.stop();
}
}
}
29 changes: 29 additions & 0 deletions quarkus-picocli/src/test/java/ProdiIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import static java.util.concurrent.CompletableFuture.runAsync;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;

@Disabled ("No possible to test 2 profiles with the same build")
@QuarkusScenario
public class ProdiIT {
@QuarkusApplication
static final RestService customized = new RestService()
.withProperty("quarkus.args", "start -t 60 -v")
.withProperty("quarkus.profile", "prod")
.setAutoStart(false);

@Test
public void verifyCustomizedCommandLineBehavior() {
String expectedOutput = "Service started with timeout: 60 and verbosity";
try {
runAsync(customized::start);
customized.logs().assertContains(expectedOutput);
} finally {
customized.stop();
}
}
}

0 comments on commit 19686f0

Please sign in to comment.