Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only invoke completed method on unknown arguments if the failure remedy is FAIL #2373

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class CamelMainUnknownArgumentFailTest {
@Test
public void unknownArgumentThrowsException() {
Exception exception = Assertions.assertThrows(RuntimeException.class, () -> {
main.parseArguments(new String[] { "-d", "10", "-foo", "bar" });
main.parseArguments(new String[] { "-d", "10", "-foo", "bar", "-t" });
});
assertEquals("CamelMain encountered unknown arguments", exception.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public void unknownArgumentIgnored() {
try (ByteArrayOutputStream sysout = new ByteArrayOutputStream()) {
System.setOut(new PrintStream(sysout));

main.parseArguments(new String[] { "-d", "10", "-foo", "bar" });
main.parseArguments(new String[] { "-d", "10", "-foo", "bar", "-t" });

String consoleContent = sysout.toString();
assertFalse(consoleContent.contains("Unknown option: -foo"));
assertFalse(consoleContent.contains("Unknown option: -foo bar"));
assertFalse(consoleContent.contains("Apache Camel Runner takes the following options"));
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import io.quarkus.test.QuarkusUnitTest;
import org.apache.camel.quarkus.main.CamelMain;
import org.apache.camel.util.StringHelper;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
Expand All @@ -53,10 +54,17 @@ public void unknownArgumentLogsWarning() {
try (ByteArrayOutputStream sysout = new ByteArrayOutputStream()) {
System.setOut(new PrintStream(sysout));

main.parseArguments(new String[] { "-d", "10", "-foo", "bar" });
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 150; i++) {
builder.append("test");
}

String longArg = builder.toString();
main.parseArguments(new String[] { "-d", "10", "-foo", "bar", "-t", longArg });

String consoleContent = sysout.toString();
assertTrue(consoleContent.contains("Unknown option: -foo"));
assertTrue(consoleContent
.contains("Unknown option: -foo bar " + String.format("%s...", StringHelper.limitLength(longArg, 97))));
assertTrue(consoleContent.contains("Apache Camel Runner takes the following options"));
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*/
package org.apache.camel.quarkus.main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import io.quarkus.runtime.Quarkus;
Expand All @@ -35,6 +37,7 @@
import org.apache.camel.quarkus.core.CamelConfig.FailureRemedy;
import org.apache.camel.spi.HasCamelContext;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -171,12 +174,12 @@ public int run(String[] args) throws Exception {
@Override
public void parseArguments(String[] arguments) {
LinkedList<String> args = new LinkedList<>(Arrays.asList(arguments));
List<String> unknownArgs = new ArrayList<>();

boolean valid = true;
while (!args.isEmpty()) {
initOptions();
String arg = args.removeFirst();

boolean handled = false;
for (Option option : options) {
if (option.processOption(arg, args)) {
Expand All @@ -185,17 +188,22 @@ public void parseArguments(String[] arguments) {
}
}
if (!handled && !failureRemedy.equals(FailureRemedy.ignore)) {
System.out.println("Unknown option: " + arg);
System.out.println();

if (arg.length() >= 100) {
// For long arguments, clean up formatting for console output
String truncatedArg = String.format("%s...", StringHelper.limitLength(arg, 97));
unknownArgs.add(truncatedArg);
} else {
unknownArgs.add(arg);
}
valid = false;
break;
}
}
if (!valid) {
System.out.println("Unknown option: " + String.join(" ", unknownArgs));
System.out.println();
showOptions();
completed();
if (failureRemedy.equals(FailureRemedy.fail)) {
completed();
throw new RuntimeException("CamelMain encountered unknown arguments");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ public class QuarkusProcessExecutor {
private final int httpPort = AvailablePortFinder.getNextAvailable();
private final int httpsPort = AvailablePortFinder.getNextAvailable();

public QuarkusProcessExecutor(String... args) {
LOGGER.infof("Executing process: %s", String.join(" ", command(args)));
public QuarkusProcessExecutor(String... jvmArgs) {
this(jvmArgs, null);
}

public QuarkusProcessExecutor(String[] jvmArgs, String... applicationArgs) {
List<String> command = command(jvmArgs);
if (applicationArgs != null) {
command.addAll(Arrays.asList(applicationArgs));
}

LOGGER.infof("Executing process: %s", String.join(" ", command));
executor = new ProcessExecutor()
.command(command(args))
.command(command)
.redirectOutput(System.out)
.readOutput(true);
}
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/main-command-mode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-timer</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-web</artifactId>
aldettinger marked this conversation as resolved.
Show resolved Hide resolved
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import java.util.concurrent.TimeoutException;

import org.apache.camel.quarkus.test.support.process.QuarkusProcessExecutor;
import org.apache.camel.util.StringHelper;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.zeroturnaround.exec.InvalidExitValueException;
import org.zeroturnaround.exec.ProcessResult;

import static org.assertj.core.api.Assertions.assertThat;

public class CommandModeTest {

@Test
Expand All @@ -34,4 +37,34 @@ void hello() throws InvalidExitValueException, IOException, InterruptedException
Assertions.assertThat(result.getExitValue()).isEqualTo(0);
Assertions.assertThat(result.outputUTF8()).contains("Hello Joe!");
}

@Test
void testMainWarnsOnUnknownArguments() throws InterruptedException, IOException, TimeoutException {
// Build a long fake classpath argument
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 150; i++) {
builder.append("jar-" + i + ".jar:");
}
String classpathArg = builder.toString();

final String[] jvmArgs = new String[] { "-Dgreeted.subject=Joe" };
final String[] applicationArgs = new String[] {
"-d",
"10",
"-cp",
classpathArg,
"-t"
};

final ProcessResult result = new QuarkusProcessExecutor(jvmArgs, applicationArgs).execute();

// Verify the application ran successfully
assertThat(result.getExitValue()).isEqualTo(0);
assertThat(result.outputUTF8()).contains("Hello Joe!");

// Verify warning for unknown arguments was printed to the console
String truncatedCpArg = String.format("%s...", StringHelper.limitLength(classpathArg, 97));
assertThat(result.outputUTF8()).contains("Unknown option: -cp " + truncatedCpArg);
assertThat(result.outputUTF8()).contains("Apache Camel Runner takes the following options");
}
}
183 changes: 183 additions & 0 deletions integration-tests/main-unknown-args-fail/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-tests</artifactId>
<version>1.8.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-quarkus-integration-test-main-unknown-args-fail</artifactId>
<name>Camel Quarkus :: Integration Tests :: Main Unknown Arguments Fail :: Tests</name>

<properties>
<quarkus.runner.jar>${project.build.directory}/quarkus-app/quarkus-run.jar</quarkus.runner.jar>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-log</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-timer</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-web</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-tests-process-executor-support</artifactId>
<scope>test</scope>
</dependency>

<!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-log-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-main-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-timer-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>


<profiles>

<profile>
<id>full</id>
<activation>
<property>
<name>!quickly</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<!-- Move surefire:test to integration-test phase to be able to run
java -jar target/*runner.jar from a test -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<systemProperties>
<quarkus.runner>${quarkus.runner.jar}</quarkus.runner>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
<quarkus.runner.jar>${project.build.directory}/${project.artifactId}-${project.version}-native-image-source-jar/${project.artifactId}-${project.version}-runner.jar</quarkus.runner.jar>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemProperties>
<quarkus.runner>${project.build.directory}/${project.artifactId}-${project.version}-runner</quarkus.runner>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>