You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2023/02/07 07:32:47 UTC
[camel-quarkus] 01/18: DoBeforeEach does not work with Advice #4362
This is an automated email from the ASF dual-hosted git repository.
jamesnetherton pushed a commit to branch 2.13.x
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit 57f6c57e057481afd2c0f930f330d1fbf7bee214
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Tue Jan 3 16:12:34 2023 +0100
DoBeforeEach does not work with Advice #4362
---
docs/modules/ROOT/pages/user-guide/testing.adoc | 4 +-
.../ProducedRouteBuilderET.java} | 54 +++++++------
.../ProducedRouteBuilderTest.java | 61 +++++++++++++++
.../camel/quarkus/test/AfterAllCallback.java | 2 +-
.../camel/quarkus/test/AfterEachCallback.java | 8 +-
.../camel/quarkus/test/BeforeEachCallback.java | 1 +
.../quarkus/test/CamelQuarkusTestSupport.java | 21 ++++++
.../quarkus/test/junit5/CamelTestSupportTest.java | 2 -
.../junit5/patterns/IsMockEndpointsFileTest.java | 2 -
.../RouteBuilderConfigureExceptionTest.java | 2 -
.../AdviceInDoBeforeEachMethodsTest.java | 88 ++++++++++++++++++++++
11 files changed, 210 insertions(+), 35 deletions(-)
diff --git a/docs/modules/ROOT/pages/user-guide/testing.adoc b/docs/modules/ROOT/pages/user-guide/testing.adoc
index 6fa7b4e79a..c595e5596d 100644
--- a/docs/modules/ROOT/pages/user-guide/testing.adoc
+++ b/docs/modules/ROOT/pages/user-guide/testing.adoc
@@ -258,9 +258,9 @@ Be aware that execution of method `doAfterConstruct` differs from the execution
* To force Quarkus JUnit Extension to restart the application (and thus also `CamelContext`) for a given test class, you need to assign a unique `@io.quarkus.test.junit.TestProfile` to that class. Check the https://quarkus.io/guides/getting-started-testing#testing_different_profiles[Quarkus documentation] for how you can do that. (Note that `https://quarkus.io/guides/getting-started-testing#quarkus-test-resource[@io.quarkus.test.common.QuarkusTestResource]` has a similar effect.)
* Camel Quarkus executes the production of beans during the build phase. Because all the tests are
build together, exclusion behavior is implemented into `CamelQuarkusTestSupport`. If a producer of the specific type and name is used in one tests, the instance will be the same for the rest of the tests.
-* JUnit Jupiter callbacks (`BeforeEachCallback`, `AfterEachCallback`, `AfterAllCallback`, `BeforeAllCallback`, `BeforeTestExecutionCallback` and `AfterTestExecutionCallback`) might not work correctly. See the https://quarkus.io/guides/getting-started-testing#enrichment-via-quarkustestcallback[documentation].
+* JUnit Jupiter callbacks (`BeforeEachCallback`, `AfterEachCallback`, `AfterAllCallback`, `BeforeAllCallback`, `BeforeTestExecutionCallback` and `AfterTestExecutionCallback`) and JUnit Jupiter annotations (`@BeforeEach`, `@AfterEach`, `@AfterAll` and `@BeforeAll`) might not work correctly. See the https://quarkus.io/guides/getting-started-testing#enrichment-via-quarkustestcallback[documentation].
* If there is an unaffected route, when using advice with, it's important to execute method `CamelQuarkusTestSupport.startRouteDefinitions()` manually from the unit test after you are done doing all the advice with
-
+* Do not use `@Produces` with `RouteBuilder` use the overridden method `createRouteBuilder()` instead. `@Produces` with `RouteBuilder` might not work correctly.
[source,java]
----
diff --git a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/RouteBuilderConfigureExceptionTest.java b/test-framework/junit5-extension-tests/src/test/java/org/apache/camel/quarkus/test/extensions/producedRouteBuilder/ProducedRouteBuilderET.java
similarity index 51%
copy from test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/RouteBuilderConfigureExceptionTest.java
copy to test-framework/junit5-extension-tests/src/test/java/org/apache/camel/quarkus/test/extensions/producedRouteBuilder/ProducedRouteBuilderET.java
index cfb26bddca..184d46a2cb 100644
--- a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/RouteBuilderConfigureExceptionTest.java
+++ b/test-framework/junit5-extension-tests/src/test/java/org/apache/camel/quarkus/test/extensions/producedRouteBuilder/ProducedRouteBuilderET.java
@@ -14,43 +14,53 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.quarkus.test.junit5.patterns;
+package org.apache.camel.quarkus.test.extensions.producedRouteBuilder;
+
+import javax.enterprise.inject.Produces;
import io.quarkus.test.junit.QuarkusTest;
-import org.apache.camel.Predicate;
+import org.apache.camel.builder.AdviceWith;
+import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.quarkus.test.CamelQuarkusTestSupport;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
-
@QuarkusTest
-public class RouteBuilderConfigureExceptionTest extends CamelQuarkusTestSupport {
+public class ProducedRouteBuilderET extends CamelQuarkusTestSupport {
- private Predicate iAmNull;
-
- @Override
@BeforeEach
- public void setUp() {
- try {
- super.setUp();
- fail("Should have thrown exception");
- } catch (Exception e) {
- // expected
- }
- }
-
- @Test
- public void testFoo() {
+ public void doSomethingBefore() throws Exception {
+ AdviceWith.adviceWith(this.context, "sampleRoute",
+ ProducedRouteBuilderET::enhanceRoute);
}
- @Override
- protected RouteBuilder createRouteBuilder() {
+ @Produces
+ public RouteBuilder routes() {
return new RouteBuilder() {
+ @Override
public void configure() {
- from("direct:start").choice().when(iAmNull).to("mock:dead");
+ from("direct:in").routeId("sampleRoute").to("file:target/data/RouteBuilderET?filename=hello_true.txt");
}
};
}
+
+ @Test
+ public void firstTest() throws Exception {
+ Assertions.assertTrue(true);
+ }
+
+ @Test
+ public void secondTest() throws Exception {
+ Assertions.assertTrue(true);
+ }
+
+ private static void enhanceRoute(AdviceWithRouteBuilder route) {
+ route.replaceFromWith("direct:ftp");
+ route.interceptSendToEndpoint("file:.*samples.*")
+ .skipSendToOriginalEndpoint()
+ .to("mock:file:sample_requests");
+ }
+
}
diff --git a/test-framework/junit5-extension-tests/src/test/java/org/apache/camel/quarkus/test/extensions/producedRouteBuilder/ProducedRouteBuilderTest.java b/test-framework/junit5-extension-tests/src/test/java/org/apache/camel/quarkus/test/extensions/producedRouteBuilder/ProducedRouteBuilderTest.java
new file mode 100644
index 0000000000..17fa9029a2
--- /dev/null
+++ b/test-framework/junit5-extension-tests/src/test/java/org/apache/camel/quarkus/test/extensions/producedRouteBuilder/ProducedRouteBuilderTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.test.extensions.producedRouteBuilder;
+
+import java.util.function.Supplier;
+
+import io.quarkus.test.ContinuousTestingTestUtils;
+import io.quarkus.test.QuarkusDevModeTest;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+/**
+ * Test for https://github.com/apache/camel-quarkus/issues/4362
+ */
+public class ProducedRouteBuilderTest {
+
+ @RegisterExtension
+ static final QuarkusDevModeTest TEST = new QuarkusDevModeTest()
+ .setArchiveProducer(new Supplier<>() {
+ @Override
+ public JavaArchive get() {
+ return ShrinkWrap.create(JavaArchive.class)
+ .add(new StringAsset(
+ ContinuousTestingTestUtils.appProperties("camel-quarkus.junit5.message=Sheldon")),
+ "application.properties");
+ }
+ })
+ .setTestArchiveProducer(new Supplier<>() {
+ @Override
+ public JavaArchive get() {
+ return ShrinkWrap.create(JavaArchive.class).addClasses(ProducedRouteBuilderET.class);
+ }
+ });
+
+ @Test
+ public void checkTests() {
+ ContinuousTestingTestUtils utils = new ContinuousTestingTestUtils();
+ ContinuousTestingTestUtils.TestStatus ts = utils.waitForNextCompletion();
+
+ Assertions.assertEquals(1L, ts.getTestsFailed());
+ Assertions.assertEquals(1L, ts.getTestsPassed());
+ }
+}
diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java
index 011e008b70..f6cb1f84be 100644
--- a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java
+++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java
@@ -27,8 +27,8 @@ public class AfterAllCallback implements QuarkusTestAfterAllCallback {
CamelQuarkusTestSupport testInstance = (CamelQuarkusTestSupport) context.getTestInstance();
if (CallbackUtil.isPerClass(testInstance)) {
- CallbackUtil.resetContext(testInstance);
testInstance.internalAfterAll(context);
+ CallbackUtil.resetContext(testInstance);
}
try {
diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java
index dcf78de57c..eb4e43a089 100644
--- a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java
+++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java
@@ -26,15 +26,15 @@ public class AfterEachCallback implements QuarkusTestAfterEachCallback {
if (context.getTestInstance() instanceof CamelQuarkusTestSupport) {
CamelQuarkusTestSupport testInstance = (CamelQuarkusTestSupport) context.getTestInstance();
- if (!CallbackUtil.isPerClass(testInstance)) {
- CallbackUtil.resetContext(testInstance);
- }
-
try {
+ testInstance.tearDown();
testInstance.doAfterEach(context);
} catch (Exception e) {
throw new RuntimeException(e);
+ }
+ if (!CallbackUtil.isPerClass(testInstance)) {
+ CallbackUtil.resetContext(testInstance);
}
}
}
diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java
index f7903726b0..93ac9e34e7 100644
--- a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java
+++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java
@@ -36,6 +36,7 @@ public class BeforeEachCallback implements QuarkusTestBeforeEachCallback {
try {
testInstance.internalBeforeEach(mockContext);
testInstance.internalBeforeAll(mockContext);
+ testInstance.setUp();
testInstance.doBeforeEach(context);
} catch (Exception e) {
throw new RuntimeException(e);
diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java
index b0eccd5cb8..66276fa398 100644
--- a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java
+++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java
@@ -219,6 +219,27 @@ public class CamelQuarkusTestSupport extends CamelTestSupport
//in camel-quarkus, junit5 uses different classloader, necessary code was moved into quarkus's callback
}
+ /**
+ * Method {@link CamelTestSupport#setUp()} is triggered via annotation {@link org.junit.jupiter.api.BeforeEach}.
+ * Its execution is disabled (by using overriding method without any annotation) and is executed from
+ * {@link BeforeEachCallback}
+ */
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /**
+ * Method {@link CamelTestSupport#tearDown()} is triggered via annotation
+ * {@link org.junit.jupiter.api.AfterEach}.
+ * Its execution is disabled (by using overriding method without any annotation) and is executed from
+ * {@link AfterEachCallback}
+ */
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
/**
* This method stops the Camel context. Be aware that on of the limitation that Quarkus brings is that context
* can not be started (lifecycle f the context is bound to the application) .
diff --git a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/CamelTestSupportTest.java b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/CamelTestSupportTest.java
index 71caa86c36..7c5704eca8 100644
--- a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/CamelTestSupportTest.java
+++ b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/CamelTestSupportTest.java
@@ -22,7 +22,6 @@ import org.apache.camel.NoSuchEndpointException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.quarkus.test.CamelQuarkusTestSupport;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -33,7 +32,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class CamelTestSupportTest extends CamelQuarkusTestSupport {
@Override
- @BeforeEach
public void setUp() throws Exception {
replaceRouteFromWith("routeId", "direct:start");
super.setUp();
diff --git a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/IsMockEndpointsFileTest.java b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/IsMockEndpointsFileTest.java
index 3088f11e26..5629296622 100644
--- a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/IsMockEndpointsFileTest.java
+++ b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/IsMockEndpointsFileTest.java
@@ -21,7 +21,6 @@ import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.quarkus.test.CamelQuarkusTestSupport;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -30,7 +29,6 @@ import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
public class IsMockEndpointsFileTest extends CamelQuarkusTestSupport {
@Override
- @BeforeEach
public void setUp() throws Exception {
deleteDirectory("target/input");
deleteDirectory("target/messages");
diff --git a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/RouteBuilderConfigureExceptionTest.java b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/RouteBuilderConfigureExceptionTest.java
index cfb26bddca..263adf28f9 100644
--- a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/RouteBuilderConfigureExceptionTest.java
+++ b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/junit5/patterns/RouteBuilderConfigureExceptionTest.java
@@ -20,7 +20,6 @@ import io.quarkus.test.junit.QuarkusTest;
import org.apache.camel.Predicate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.quarkus.test.CamelQuarkusTestSupport;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
@@ -31,7 +30,6 @@ public class RouteBuilderConfigureExceptionTest extends CamelQuarkusTestSupport
private Predicate iAmNull;
@Override
- @BeforeEach
public void setUp() {
try {
super.setUp();
diff --git a/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/userTestCases/AdviceInDoBeforeEachMethodsTest.java b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/userTestCases/AdviceInDoBeforeEachMethodsTest.java
new file mode 100644
index 0000000000..159d352c49
--- /dev/null
+++ b/test-framework/junit5/src/test/java/org/apache/camel/quarkus/test/userTestCases/AdviceInDoBeforeEachMethodsTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.test.userTestCases;
+
+import javax.inject.Inject;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.TestProfile;
+import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.AdviceWith;
+import org.apache.camel.builder.AdviceWithRouteBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.quarkus.test.CamelQuarkusTestSupport;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for https://github.com/apache/camel-quarkus/issues/4362
+ */
+@QuarkusTest
+@TestProfile(AdviceInDoBeforeEachMethodsTest.class)
+public class AdviceInDoBeforeEachMethodsTest extends CamelQuarkusTestSupport {
+
+ @Produce("direct:ftp")
+ protected ProducerTemplate template;
+
+ @Inject
+ protected CamelContext context;
+
+ @EndpointInject("mock:file:sample_requests")
+ protected MockEndpoint fileMock;
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:start")
+ .routeId("sampleRoute")
+ .to("file:samples/");
+ }
+ };
+ }
+
+ @Override
+ protected void doBeforeEach(QuarkusTestMethodContext context) throws Exception {
+ AdviceWith.adviceWith(this.context, "sampleRoute",
+ AdviceInDoBeforeEachMethodsTest::enhanceRoute);
+ }
+
+ @Test
+ void testConsumeFtpWriteToFileOne() throws Exception {
+ fileMock.message(0).body().isEqualTo("Hello World");
+ template.sendBody("direct:ftp", "Hello World");
+ fileMock.assertIsSatisfied();
+ }
+
+ @Test
+ void testConsumeFtpWriteToFileTwo() throws Exception {
+ fileMock.message(0).body().isEqualTo("Hello World");
+ template.sendBody("direct:ftp", "Hello World");
+ fileMock.assertIsSatisfied();
+ }
+
+ private static void enhanceRoute(AdviceWithRouteBuilder route) {
+ route.replaceFromWith("direct:ftp");
+ route.interceptSendToEndpoint("file:.*samples.*")
+ .skipSendToOriginalEndpoint()
+ .to("mock:file:sample_requests");
+ }
+}