davsclaus commented on code in PR #27173: URL: https://github.com/apache/camel/pull/27173#discussion_r4266913147
########## components/camel-sjms/src/test/java/org/apache/camel/component/sjms/batch/BatchConsumerExceptionHandlerTest.java: ########## @@ -0,0 +1,137 @@ +/* + * 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.component.sjms.batch; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.sjms.support.JmsCommonTestSupport; +import org.apache.camel.spi.ExceptionHandler; +import org.apache.camel.test.infra.artemis.services.ArtemisService; +import org.apache.camel.test.infra.artemis.services.ArtemisServiceFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class BatchConsumerExceptionHandlerTest extends JmsCommonTestSupport { + + private static final String SJMS_QUEUE_NAME = "sjms:queue:batch.consumer.BatchConsumerExceptionHandlerTest"; + private static final String MOCK_START = "mock:start"; + private static final String MOCK_FINISH = "mock:finish"; + private static final String EXCEPTION_MESSAGE = "Always throw an exception!"; + + private CapturingExceptionHandler exceptionHandler; + + @RegisterExtension + public static ArtemisService service = ArtemisServiceFactory.createSingletonVMService(); + + protected final Logger log = LoggerFactory.getLogger(getClass()); + + @Override + protected String getBrokerUri() { + return service.serviceAddress(); + } + + /** + * Set up the Broker + */ + @BeforeEach + protected final void setupBrokerUri() { + brokerUri = getBrokerUri(); + } + + @Override + protected void setupFactoryExternal(ActiveMQConnectionFactory factory) { + setupFactoryExternal(factory, service); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext camelContext = super.createCamelContext(); + + exceptionHandler = new CapturingExceptionHandler(); + camelContext.getRegistry().bind("myHandler", exceptionHandler); + + return camelContext; + } + + @Test + public void testBatchConsumerExceptionHandler() throws InterruptedException { + MockEndpoint mockStart = getMockEndpoint(MOCK_START); + MockEndpoint mockFinish = getMockEndpoint(MOCK_FINISH); + + mockStart.expectedMessageCount(1); + mockFinish.expectedMessageCount(0); + + template.sendBodyAndHeader(SJMS_QUEUE_NAME, "Hello there!", "cheese", "y"); + + assertTrue(exceptionHandler.latch.await(5, TimeUnit.SECONDS), + "An exchange exception should be reported to the endpoint exceptionHandler"); + assertNotNull(exceptionHandler.captured.get(), "The exceptionHandler should always get a cause"); + assertEquals("java.lang.IllegalArgumentException: " + EXCEPTION_MESSAGE, exceptionHandler.captured.get().getMessage()); Review Comment: This compares the message of the `RuntimeCamelException` wrapper, which is just `cause.toString()`. It breaks if the wrapping changes, and it doesn't show which exception actually got to the handler. Assert on the cause instead: ```java Throwable captured = exceptionHandler.captured.get(); assertNotNull(captured, "The exceptionHandler should always get a cause"); IllegalArgumentException cause = assertInstanceOf(IllegalArgumentException.class, captured.getCause()); assertEquals(EXCEPTION_MESSAGE, cause.getMessage()); ``` With `AUTO_ACKNOWLEDGE` the batch is not redelivered, so the handler should be called exactly once. A call counter (an `AtomicInteger` in `CapturingExceptionHandler`), checked after `MockEndpoint.assertIsSatisfied(context)`, would assert that too. Right now the latch passes even if the handler is called on every redelivery. ########## components/camel-sjms/src/test/java/org/apache/camel/component/sjms/batch/BatchConsumerExceptionHandlerTest.java: ########## @@ -0,0 +1,137 @@ +/* + * 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.component.sjms.batch; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.sjms.support.JmsCommonTestSupport; +import org.apache.camel.spi.ExceptionHandler; +import org.apache.camel.test.infra.artemis.services.ArtemisService; +import org.apache.camel.test.infra.artemis.services.ArtemisServiceFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class BatchConsumerExceptionHandlerTest extends JmsCommonTestSupport { + + private static final String SJMS_QUEUE_NAME = "sjms:queue:batch.consumer.BatchConsumerExceptionHandlerTest"; + private static final String MOCK_START = "mock:start"; + private static final String MOCK_FINISH = "mock:finish"; + private static final String EXCEPTION_MESSAGE = "Always throw an exception!"; + + private CapturingExceptionHandler exceptionHandler; + + @RegisterExtension + public static ArtemisService service = ArtemisServiceFactory.createSingletonVMService(); + + protected final Logger log = LoggerFactory.getLogger(getClass()); Review Comment: Nit: `log` is never used here, so it can go along with the two slf4j imports. The `cheese` header on the sent message isn't used either; `template.sendBody(...)` is enough. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
