This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 8e28eda0b1dca1ac9700103bf2cefbee6af83ec4 Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Sat Jan 24 18:41:40 2026 +0000 CAMEL-21196: modernize exception-based assertions in camel-keycloak --- .../security/KeycloakSecurityHelperTest.java | 22 ++++++--------- .../TransactionalClientDataSourceAsyncTest.java | 16 +++++------ ...sactionalClientDataSourceNotTransactedTest.java | 14 ++++------ .../TransactionalClientDataSourceTest.java | 16 +++++------ ...ourceWithOnExceptionHandledAndRollbackTest.java | 9 ++---- ...lientDataSourceWithOnExceptionRollbackTest.java | 16 +++++------ ...ctionalClientDataSourceWithOnExceptionTest.java | 16 +++++------ .../TransactionalClientWithRollbackTest.java | 12 ++++---- .../SpringCamelContextStartingFailedEventTest.java | 18 ++++++------ .../spring/processor/SpringFilterNoChildTest.java | 18 ++++++------ .../processor/SpringTryCatchMisconfiguredTest.java | 32 ++++++++++------------ 11 files changed, 83 insertions(+), 106 deletions(-) diff --git a/components/camel-keycloak/src/test/java/org/apache/camel/component/keycloak/security/KeycloakSecurityHelperTest.java b/components/camel-keycloak/src/test/java/org/apache/camel/component/keycloak/security/KeycloakSecurityHelperTest.java index b17823f18179..2cbdc9ae61ba 100644 --- a/components/camel-keycloak/src/test/java/org/apache/camel/component/keycloak/security/KeycloakSecurityHelperTest.java +++ b/components/camel-keycloak/src/test/java/org/apache/camel/component/keycloak/security/KeycloakSecurityHelperTest.java @@ -132,23 +132,19 @@ public class KeycloakSecurityHelperTest { } @Test - void testParseAndVerifyAccessTokenWithInvalidToken() { + void testParseAndVerifyAccessTokenWithInvalidToken() throws Exception { // Test that verification fails with invalid token String invalidToken = "invalid.jwt.token"; String expectedIssuer = "http://localhost:8080/realms/test"; - try { - KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); - keyGen.initialize(2048); - KeyPair keyPair = keyGen.generateKeyPair(); - PublicKey publicKey = keyPair.getPublic(); - - assertThrows(VerificationException.class, () -> { - KeycloakSecurityHelper.parseAndVerifyAccessToken(invalidToken, publicKey, expectedIssuer); - }); - } catch (Exception e) { - fail("Failed to generate test keys: " + e.getMessage()); - } + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); + keyGen.initialize(2048); + KeyPair keyPair = keyGen.generateKeyPair(); + PublicKey publicKey = keyPair.getPublic(); + + assertThrows(VerificationException.class, () -> { + KeycloakSecurityHelper.parseAndVerifyAccessToken(invalidToken, publicKey, expectedIssuer); + }); } @Test diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceAsyncTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceAsyncTest.java index c83fb5f41e0c..caf0de4f4fad 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceAsyncTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceAsyncTest.java @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit test to demonstrate the transactional client pattern. @@ -39,15 +39,13 @@ public class TransactionalClientDataSourceAsyncTest extends TransactionalClientD MockEndpoint mock = getMockEndpoint("mock:error"); mock.expectedMessageCount(1); - try { + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> { template.sendBody("direct:fail", "Hello World"); - fail("Should have thrown exception"); - } catch (RuntimeCamelException e) { - // expected as we fail - assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); - assertTrue(e.getCause().getCause() instanceof IllegalArgumentException); - assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage()); - } + }); + // expected as we fail + assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); + assertTrue(e.getCause().getCause() instanceof IllegalArgumentException); + assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage()); assertMockEndpointsSatisfied(); diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceNotTransactedTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceNotTransactedTest.java index 38842e1ea4b1..f50acc8d087c 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceNotTransactedTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceNotTransactedTest.java @@ -23,7 +23,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Same route but not transacted @@ -33,14 +33,12 @@ public class TransactionalClientDataSourceNotTransactedTest extends Transactiona @Override @Test public void testTransactionRollback() throws Exception { - try { + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> { template.sendBody("direct:fail", "Hello World"); - fail("Should have thrown exception"); - } catch (RuntimeCamelException e) { - // expected as we fail - assertTrue(e.getCause() instanceof IllegalArgumentException); - assertEquals("We don't have Donkeys, only Camels", e.getCause().getMessage()); - } + }); + // expected as we fail + assertTrue(e.getCause() instanceof IllegalArgumentException); + assertEquals("We don't have Donkeys, only Camels", e.getCause().getMessage()); int count = jdbc.queryForObject("select count(*) from books", Integer.class); // should get 2 books as the first operation will succeed and we are not transacted diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTest.java index 8acdfd9aff69..adc7cfae1825 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit test to demonstrate the transactional client pattern. @@ -44,15 +44,13 @@ public class TransactionalClientDataSourceTest extends TransactionClientDataSour // START SNIPPET: e4 @Test public void testTransactionRollback() throws Exception { - try { + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> { template.sendBody("direct:fail", "Hello World"); - fail("Should have thrown exception"); - } catch (RuntimeCamelException e) { - // expected as we fail - assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); - assertTrue(e.getCause().getCause() instanceof IllegalArgumentException); - assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage()); - } + }); + // expected as we fail + assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); + assertTrue(e.getCause().getCause() instanceof IllegalArgumentException); + assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage()); int count = jdbc.queryForObject("select count(*) from books", Integer.class); assertEquals(1, count, "Number of books"); diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionHandledAndRollbackTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionHandledAndRollbackTest.java index 9ff113281566..80494d3e1c28 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionHandledAndRollbackTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionHandledAndRollbackTest.java @@ -24,7 +24,7 @@ import org.apache.camel.spring.spi.SpringTransactionPolicy; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit test to demonstrate the transactional client pattern. @@ -37,12 +37,9 @@ public class TransactionalClientDataSourceWithOnExceptionHandledAndRollbackTest MockEndpoint mock = getMockEndpoint("mock:error"); mock.expectedMessageCount(1); - try { + assertThrows(CamelExecutionException.class, () -> { template.requestBody("direct:fail", "Hello World", String.class); - fail("Should have thrown exception"); - } catch (CamelExecutionException e) { - // expected - } + }); assertMockEndpointsSatisfied(); diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionRollbackTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionRollbackTest.java index 07eb460e8e8b..eaf9302e4643 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionRollbackTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionRollbackTest.java @@ -25,7 +25,7 @@ import org.apache.camel.spring.spi.SpringTransactionPolicy; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit test to demonstrate the transactional client pattern. @@ -38,15 +38,13 @@ public class TransactionalClientDataSourceWithOnExceptionRollbackTest extends Tr MockEndpoint mock = getMockEndpoint("mock:error"); mock.expectedMessageCount(1); - try { + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> { template.sendBody("direct:fail", "Hello World"); - fail("Should have thrown exception"); - } catch (RuntimeCamelException e) { - // expected as we fail - assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); - RollbackExchangeException rollback = assertIsInstanceOf(RollbackExchangeException.class, e.getCause().getCause()); - assertEquals("Donkey in Action", rollback.getExchange().getIn().getBody()); - } + }); + // expected as we fail + assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); + RollbackExchangeException rollback = assertIsInstanceOf(RollbackExchangeException.class, e.getCause().getCause()); + assertEquals("Donkey in Action", rollback.getExchange().getIn().getBody()); assertMockEndpointsSatisfied(); diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionTest.java index eda42ba0f5fa..93fa621b010a 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceWithOnExceptionTest.java @@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit test to demonstrate the transactional client pattern. @@ -38,15 +38,13 @@ public class TransactionalClientDataSourceWithOnExceptionTest extends Transactio MockEndpoint mock = getMockEndpoint("mock:error"); mock.expectedMessageCount(1); - try { + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> { template.sendBody("direct:fail", "Hello World"); - fail("Should have thrown exception"); - } catch (RuntimeCamelException e) { - // expected as we fail - assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); - assertTrue(e.getCause().getCause() instanceof IllegalArgumentException); - assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage()); - } + }); + // expected as we fail + assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); + assertTrue(e.getCause().getCause() instanceof IllegalArgumentException); + assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage()); assertMockEndpointsSatisfied(); diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientWithRollbackTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientWithRollbackTest.java index 36e978694835..72af2771fc18 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientWithRollbackTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientWithRollbackTest.java @@ -32,7 +32,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Transactional client test with rollback in the DSL. @@ -66,13 +66,11 @@ public class TransactionalClientWithRollbackTest extends SpringTestSupport { @Test public void testTransactionRollback() throws Exception { - try { + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> { template.sendBody("direct:fail", "Hello World"); - fail("Should have thrown a RollbackExchangeException"); - } catch (RuntimeCamelException e) { - assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); - assertTrue(e.getCause().getCause() instanceof RollbackExchangeException); - } + }); + assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); + assertTrue(e.getCause().getCause() instanceof RollbackExchangeException); int count = jdbc.queryForObject("select count(*) from books", Integer.class); assertEquals(1, count, "Number of books"); diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java index 3fee75a8a96c..2bbf66b5d289 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java @@ -26,7 +26,7 @@ import org.springframework.context.support.AbstractXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; @DisabledOnOs(OS.AIX) public class SpringCamelContextStartingFailedEventTest extends SpringTestSupport { @@ -38,14 +38,14 @@ public class SpringCamelContextStartingFailedEventTest extends SpringTestSupport @Override protected AbstractXmlApplicationContext createApplicationContext() { - try (ClassPathXmlApplicationContext x = new ClassPathXmlApplicationContext( - "org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.xml")) { - fail("Should thrown an exception"); - } catch (Exception e) { - FailedToCreateRouteException ftcre = assertIsInstanceOf(FailedToCreateRouteException.class, e); - assertIsInstanceOf(NoSuchEndpointException.class, ftcre.getCause()); - // expected - } + Exception e = assertThrows(Exception.class, () -> { + try (ClassPathXmlApplicationContext x = new ClassPathXmlApplicationContext( + "org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.xml")) { + } + }); + FailedToCreateRouteException ftcre = assertIsInstanceOf(FailedToCreateRouteException.class, e); + assertIsInstanceOf(NoSuchEndpointException.class, ftcre.getCause()); + // expected // fallback to load another file that works return new ClassPathXmlApplicationContext("/org/apache/camel/spring/disableJmxConfig.xml"); diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringFilterNoChildTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringFilterNoChildTest.java index 03a9a8d8e494..fdee058d2c9a 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringFilterNoChildTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringFilterNoChildTest.java @@ -25,20 +25,20 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class SpringFilterNoChildTest extends SpringTestSupport { @Override protected AbstractXmlApplicationContext createApplicationContext() { - try (ClassPathXmlApplicationContext x - = new ClassPathXmlApplicationContext("org/apache/camel/spring/processor/filterNoChild.xml")) { - fail("Should thrown an exception"); - } catch (Exception e) { - FailedToCreateRouteException cause = assertIsInstanceOf(FailedToCreateRouteException.class, e); - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause()); - assertEquals("Definition has no children on Filter[xpath{$foo = 'bar'} -> []]", iae.getMessage()); - } + Exception e = assertThrows(Exception.class, () -> { + try (ClassPathXmlApplicationContext x + = new ClassPathXmlApplicationContext("org/apache/camel/spring/processor/filterNoChild.xml")) { + } + }); + FailedToCreateRouteException cause = assertIsInstanceOf(FailedToCreateRouteException.class, e); + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause()); + assertEquals("Definition has no children on Filter[xpath{$foo = 'bar'} -> []]", iae.getMessage()); return null; } diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java index 17e47e304f00..286994d5943a 100644 --- a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java @@ -23,31 +23,27 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class SpringTryCatchMisconfiguredTest extends ContextTestSupport { @Override protected CamelContext createCamelContext() throws Exception { - try { + Exception e1 = assertThrows(Exception.class, () -> { createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.xml"); - fail("Should have thrown exception"); - } catch (Exception e) { - FailedToCreateRouteException ftce = assertIsInstanceOf(FailedToCreateRouteException.class, e); - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, ftce.getCause()); - assertEquals( - "This doCatch should have a doTry as its parent on DoCatch[ [class java.io.IOException] -> [to[mock:fail]]]", - iae.getMessage()); - } - - try { + }); + FailedToCreateRouteException ftce = assertIsInstanceOf(FailedToCreateRouteException.class, e1); + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, ftce.getCause()); + assertEquals( + "This doCatch should have a doTry as its parent on DoCatch[ [class java.io.IOException] -> [to[mock:fail]]]", + iae.getMessage()); + + Exception e2 = assertThrows(Exception.class, () -> { createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringTryCatchMisconfiguredFinallyTest.xml"); - fail("Should have thrown exception"); - } catch (Exception e) { - FailedToCreateRouteException ftcre = assertIsInstanceOf(FailedToCreateRouteException.class, e); - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, ftcre.getCause()); - assertEquals("This doFinally should have a doTry as its parent on DoFinally[[to[mock:finally]]]", iae.getMessage()); - } + }); + FailedToCreateRouteException ftcre = assertIsInstanceOf(FailedToCreateRouteException.class, e2); + IllegalArgumentException iae2 = assertIsInstanceOf(IllegalArgumentException.class, ftcre.getCause()); + assertEquals("This doFinally should have a doTry as its parent on DoFinally[[to[mock:finally]]]", iae2.getMessage()); // return a working context instead, to let this test pass return createSpringCamelContext(this, "org/apache/camel/spring/processor/convertBody.xml");
