Skip to content

Commit

Permalink
Only invoke completed method on unknown arguments if the failure reme…
Browse files Browse the repository at this point in the history
…dy is FAIL

Fixes #2293
  • Loading branch information
jamesnetherton committed Mar 24, 2021
1 parent 2937812 commit df76494
Show file tree
Hide file tree
Showing 17 changed files with 639 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public void parseArguments(String[] arguments) {
}
if (!valid) {
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>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
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 +36,19 @@ 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 {
final ProcessResult result = new QuarkusProcessExecutor(new String[] { "-Dgreeted.subject=Joe" }, "-d", "10", "-cp",
"foo.jar")
.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
assertThat(result.outputUTF8()).contains("Unknown option: -cp");
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/
package org.apache.camel.quarkus.main.unknown.args.fail;

import org.apache.camel.builder.RouteBuilder;

public class Routes extends RouteBuilder {

@Override
public void configure() throws Exception {
from("timer:tick?repeatCount=1&delay=-1")
.log("Timer tick!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------

# Terminate after 1 message is generated
camel.main.duration-max-messages = 1

quarkus.camel.main.arguments.on-unknown = fail
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/
package org.apache.camel.quarkus.main.unknown.args.fail;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
class MainUnknownArgumentFailIT extends MainUnknownArgumentFailTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/
package org.apache.camel.quarkus.main.unknown.args.fail;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import io.quarkus.test.junit.QuarkusTest;
import org.apache.camel.quarkus.test.support.process.QuarkusProcessExecutor;
import org.junit.jupiter.api.Test;
import org.zeroturnaround.exec.ProcessResult;

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

@QuarkusTest
public class MainUnknownArgumentFailTest {

@Test
public void testMainTerminatesOnUnknownArguments() throws InterruptedException, IOException, TimeoutException {
final ProcessResult result = new QuarkusProcessExecutor(new String[] {}, "-d", "10", "-cp", "foo.jar").execute();

// Verify the application did not run successfully
assertThat(result.getExitValue()).isEqualTo(1);
assertThat(result.outputUTF8()).doesNotContain("Timer tick!");

// Verify warning for unknown arguments was printed to the console
assertThat(result.outputUTF8()).contains("Unknown option: -cp");
assertThat(result.outputUTF8()).contains("Apache Camel Runner takes the following options");
}
}

0 comments on commit df76494

Please sign in to comment.