This is an automated email from the ASF dual-hosted git repository. aldettinger pushed a commit to branch CAMEL-13826 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5c7e31802324b1fab6c5ae135a31f13d900b21dd Author: aldettinger <aldettin...@gmail.com> AuthorDate: Thu Aug 8 20:36:35 2019 +0200 CAMEL-13826: Turned TestSupport into a utility class --- .../apache/camel/test/junit5/CamelTestSupport.java | 53 +-- .../org/apache/camel/test/junit5/TestSupport.java | 444 ++++++++++----------- .../junit5/patterns/AdviceWithNotStartedTest.java | 1 + .../test/junit5/patterns/DebugJUnit5Test.java | 6 +- .../patterns/DebugNoLazyTypeConverterTest.java | 6 +- .../camel/test/junit5/patterns/DebugTest.java | 6 +- .../junit5/patterns/IsMockEndpointsFileTest.java | 2 + .../RouteProcessorDumpRouteCoverageTest.java | 1 + 8 files changed, 265 insertions(+), 254 deletions(-) diff --git a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java index ff5c85b..d324389 100644 --- a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java +++ b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java @@ -108,7 +108,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; * with some routes along with a {@link org.apache.camel.ProducerTemplate} for * use in the test case Do <tt>not</tt> use this class for Spring Boot testing. */ -public abstract class CamelTestSupport extends TestSupport implements BeforeEachCallback, AfterAllCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback { +public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback { /** * JVM system property which can be set to true to turn on dumping route @@ -357,9 +357,9 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach @BeforeEach public void setUp() throws Exception { - log.info("********************************************************************************"); - log.info("Testing: " + currentTestName + "(" + getClass().getName() + ")"); - log.info("********************************************************************************"); + LOG.info("********************************************************************************"); + LOG.info("Testing: " + currentTestName + "(" + getClass().getName() + ")"); + LOG.info("********************************************************************************"); if (isCreateCamelContextPerClass()) { INSTANCE.set(this); @@ -421,7 +421,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach } private void doSetUp() throws Exception { - log.debug("setUp test"); + LOG.debug("setUp test"); // jmx is enabled if we have configured to use it, or if dump route // coverage is enabled (it requires JMX) boolean jmx = useJmx() || isRouteCoverageEnabled(); @@ -445,7 +445,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach // set debugger if enabled if (isUseDebugger()) { if (context.getStatus().equals(ServiceStatus.Started)) { - log.info("Cannot setting the Debugger to the starting CamelContext, stop the CamelContext now."); + LOG.info("Cannot setting the Debugger to the starting CamelContext, stop the CamelContext now."); // we need to stop the context first to setup the debugger context.stop(); } @@ -492,7 +492,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach String include = getRouteFilterIncludePattern(); String exclude = getRouteFilterExcludePattern(); if (include != null || exclude != null) { - log.info("Route filtering pattern: include={}, exclude={}", include, exclude); + LOG.info("Route filtering pattern: include={}, exclude={}", include, exclude); context.getExtension(Model.class).setRouteFilterPattern(include, exclude); } @@ -502,23 +502,23 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach if (isUseRouteBuilder()) { RoutesBuilder[] builders = createRouteBuilders(); for (RoutesBuilder builder : builders) { - log.debug("Using created route builder: " + builder); + LOG.debug("Using created route builder: " + builder); context.addRoutes(builder); } replaceFromEndpoints(); boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext")); if (skip) { - log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true."); + LOG.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true."); } else if (isUseAdviceWith()) { - log.info("Skipping starting CamelContext as isUseAdviceWith is set to true."); + LOG.info("Skipping starting CamelContext as isUseAdviceWith is set to true."); } else { startCamelContext(); } } else { replaceFromEndpoints(); - log.debug("Using route builder from the created context: " + context); + LOG.debug("Using route builder from the created context: " + context); } - log.debug("Routing Rules are: " + context.getRoutes()); + LOG.debug("Routing Rules are: " + context.getRoutes()); assertValidContext(context); } @@ -542,9 +542,9 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach public void tearDown() throws Exception { long time = watch.taken(); - log.info("********************************************************************************"); - log.info("Testing done: " + currentTestName + "(" + getClass().getName() + ")"); - log.info("Took: " + TimeUtils.printDuration(time) + " (" + time + " millis)"); + LOG.info("********************************************************************************"); + LOG.info("Testing done: " + currentTestName + "(" + getClass().getName() + ")"); + LOG.info("Took: " + TimeUtils.printDuration(time) + " (" + time + " millis)"); // if we should dump route stats, then write that to a file if (isRouteCoverageEnabled()) { @@ -555,7 +555,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach ManagedCamelContext mc = context != null ? context.getExtension(ManagedCamelContext.class) : null; ManagedCamelContextMBean managedCamelContext = mc != null ? mc.getManagedCamelContext() : null; if (managedCamelContext == null) { - log.warn("Cannot dump route coverage to file as JMX is not enabled. " + LOG.warn("Cannot dump route coverage to file as JMX is not enabled. " + "Add camel-management-impl JAR as dependency and/or override useJmx() method to enable JMX in the unit test classes."); } else { logCoverageSummary(managedCamelContext); @@ -568,14 +568,14 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach file.mkdirs(); file = new File(dir, name); - log.info("Dumping route coverage to file: {}", file); + LOG.info("Dumping route coverage to file: {}", file); InputStream is = new ByteArrayInputStream(combined.getBytes()); OutputStream os = new FileOutputStream(file, false); IOHelper.copyAndCloseInput(is, os); IOHelper.close(os); } } - log.info("********************************************************************************"); + LOG.info("********************************************************************************"); if (isCreateCamelContextPerClass()) { // will tear down test specially in CamelTearDownRule @@ -677,7 +677,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach } builder.append(routesSummary); - log.info(builder.toString()); + LOG.info(builder.toString()); } /** @@ -953,7 +953,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach // jndi.properties is optional InputStream in = getClass().getClassLoader().getResourceAsStream("jndi.properties"); if (in != null) { - log.debug("Using jndi.properties from classpath root"); + LOG.debug("Using jndi.properties from classpath root"); properties.load(in); } else { properties.put("java.naming.factory.initial", "org.apache.camel.support.jndi.CamelInitialContextFactory"); @@ -1013,7 +1013,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach * @return the endpoint */ protected Endpoint resolveMandatoryEndpoint(String uri) { - return resolveMandatoryEndpoint(context, uri); + return TestSupport.resolveMandatoryEndpoint(context, uri); } /** @@ -1025,7 +1025,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach * @return the endpoint */ protected <T extends Endpoint> T resolveMandatoryEndpoint(String uri, Class<T> endpointType) { - return resolveMandatoryEndpoint(context, uri, endpointType); + return TestSupport.resolveMandatoryEndpoint(context, uri, endpointType); } /** @@ -1055,7 +1055,8 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach * @throws NoSuchEndpointException is the mock endpoint does not exists */ protected MockEndpoint getMockEndpoint(String uri, boolean create) throws NoSuchEndpointException { - // look for existing mock endpoints that have the same queue name, and to + // look for existing mock endpoints that have the same queue name, and + // to // do that we need to normalize uri and strip out query parameters and // whatnot String n; @@ -1144,7 +1145,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach * Creates an exchange with the given body */ protected Exchange createExchangeWithBody(Object body) { - return createExchangeWithBody(context, body); + return TestSupport.createExchangeWithBody(context, body); } /** @@ -1157,7 +1158,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach Expression expression = language.createExpression(expressionText); assertNotNull(expression, "No Expression could be created for text: " + expressionText + " language: " + language); - assertExpression(expression, exchange, expectedValue); + TestSupport.assertExpression(expression, exchange, expectedValue); } /** @@ -1170,7 +1171,7 @@ public abstract class CamelTestSupport extends TestSupport implements BeforeEach Predicate predicate = language.createPredicate(expressionText); assertNotNull(predicate, "No Predicate could be created for text: " + expressionText + " language: " + language); - assertPredicate(predicate, exchange, expected); + TestSupport.assertPredicate(predicate, exchange, expected); } /** diff --git a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestSupport.java b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestSupport.java index 47d296f..04b39b5 100644 --- a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestSupport.java +++ b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestSupport.java @@ -44,33 +44,37 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; /** - * A bunch of useful testing methods + * Provides utility methods for camel test purpose (builders, assertions, + * endpoint resolutions, file helpers). */ -public abstract class TestSupport { +public final class TestSupport { - protected static final String LS = System.lineSeparator(); private static final Logger LOG = LoggerFactory.getLogger(TestSupport.class); - protected Logger log = LoggerFactory.getLogger(getClass()); + private TestSupport() { + } + + // ------------------------------------------------------------------------- // Builder methods for expressions used when testing // ------------------------------------------------------------------------- /** - * Returns a value builder for the given header + * Returns a value builder for the given header. */ public static ValueBuilder header(String name) { return Builder.header(name); } /** - * Returns a value builder for the given exchange property + * Returns a value builder for the given exchange property. */ public static ValueBuilder exchangeProperty(String name) { return Builder.exchangeProperty(name); } /** - * Returns a predicate and value builder for the inbound body on an exchange + * Returns a predicate and value builder for the inbound body on an + * exchange. */ public static ValueBuilder body() { return Builder.body(); @@ -78,134 +82,139 @@ public abstract class TestSupport { /** * Returns a predicate and value builder for the inbound message body as a - * specific type + * specific type. */ public static <T> ValueBuilder bodyAs(Class<T> type) { return Builder.bodyAs(type); } /** - * Returns a value builder for the given system property + * Returns a value builder for the given system property. */ public static ValueBuilder systemProperty(String name) { return Builder.systemProperty(name); } /** - * Returns a value builder for the given system property + * Returns a value builder for the given system property. */ public static ValueBuilder systemProperty(String name, String defaultValue) { return Builder.systemProperty(name, defaultValue); } + // ----------------------------------------------------------------------- // Assertions // ----------------------------------------------------------------------- + /** + * Asserts that a given value is of an expected type. + */ public static <T> T assertIsInstanceOf(Class<T> expectedType, Object value) { assertNotNull(value, "Expected an instance of type: " + expectedType.getName() + " but was null"); assertTrue(expectedType.isInstance(value), "Object should be of type " + expectedType.getName() + " but was: " + value + " with the type: " + value.getClass().getName()); return expectedType.cast(value); } - public static void assertEndpointUri(Endpoint endpoint, String uri) { - assertNotNull(endpoint, "Endpoint is null when expecting endpoint for: " + uri); - assertEquals(endpoint.getEndpointUri(), "Endpoint uri for: " + endpoint, uri); + /** + * Asserts that a given endpoint has an expected uri. + */ + public static void assertEndpointUri(Endpoint endpoint, String expectedUri) { + assertNotNull(endpoint, "Endpoint is null when expecting endpoint for: " + expectedUri); + assertEquals(expectedUri, endpoint.getEndpointUri(), "Endpoint uri for: " + endpoint); } /** - * Asserts the In message on the exchange contains the expected value + * Asserts that the In message on the exchange contains an header with a + * given name and expected value. */ - public static Object assertInMessageHeader(Exchange exchange, String name, Object expected) { - return assertMessageHeader(exchange.getIn(), name, expected); + public static Object assertInMessageHeader(Exchange exchange, String headerName, Object expectedValue) { + return assertMessageHeader(exchange.getIn(), headerName, expectedValue); } /** - * Asserts the Out message on the exchange contains the expected value + * Asserts that the message on the exchange contains an header with a given + * name and expected value. */ - public static Object assertOutMessageHeader(Exchange exchange, String name, Object expected) { - return assertMessageHeader(exchange.getOut(), name, expected); + public static Object assertOutMessageHeader(Exchange exchange, String headerName, Object expectedValue) { + return assertMessageHeader(exchange.getMessage(), headerName, expectedValue); } /** - * Asserts that the given exchange has an OUT message of the given body - * value - * - * @param exchange the exchange which should have an OUT message - * @param expected the expected value of the OUT message - * @throws InvalidPayloadException is thrown if the payload is not the - * expected class type + * Asserts that the given exchange has a given expectedBody on the IN + * message. */ - public static void assertInMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException { - assertNotNull(exchange, "Should have a response exchange!"); + public static void assertInMessageBodyEquals(Exchange exchange, Object expectedBody) throws InvalidPayloadException { + assertNotNull(exchange, "Should have a response exchange"); - Object actual; - if (expected == null) { - actual = exchange.getIn().getMandatoryBody(); - assertEquals(expected, actual, "in body of: " + exchange); + Object actualBody; + if (expectedBody == null) { + actualBody = exchange.getIn().getMandatoryBody(); + assertEquals(expectedBody, actualBody, "in body of: " + exchange); } else { - actual = exchange.getIn().getMandatoryBody(expected.getClass()); + actualBody = exchange.getIn().getMandatoryBody(expectedBody.getClass()); } - assertEquals(expected, actual, "in body of: " + exchange); + assertEquals(expectedBody, actualBody, "in body of: " + exchange); LOG.debug("Received response: " + exchange + " with in: " + exchange.getIn()); } /** - * Asserts that the given exchange has an OUT message of the given body - * value - * - * @param exchange the exchange which should have an OUT message - * @param expected the expected value of the OUT message - * @throws InvalidPayloadException is thrown if the payload is not the - * expected class type + * Asserts that the given exchange has a given expectedBody on the message. */ - public static void assertOutMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException { + public static void assertMessageBodyEquals(Exchange exchange, Object expectedBody) throws InvalidPayloadException { assertNotNull(exchange, "Should have a response exchange!"); - Object actual; - if (expected == null) { - actual = exchange.getOut().getMandatoryBody(); - assertEquals(expected, actual, "output body of: " + exchange); + Object actualBody; + if (expectedBody == null) { + actualBody = exchange.getMessage().getMandatoryBody(); + assertEquals(expectedBody, actualBody, "output body of: " + exchange); } else { - actual = exchange.getOut().getMandatoryBody(expected.getClass()); + actualBody = exchange.getMessage().getMandatoryBody(expectedBody.getClass()); } - assertEquals(expected, actual, "output body of: " + exchange); + assertEquals(expectedBody, actualBody, "output body of: " + exchange); - LOG.debug("Received response: " + exchange + " with out: " + exchange.getOut()); + LOG.debug("Received response: " + exchange + " with out: " + exchange.getMessage()); } - public static Object assertMessageHeader(Message message, String name, Object expected) { - Object value = message.getHeader(name); - assertEquals(expected, value, "Header: " + name + " on Message: " + message); - return value; + /** + * Asserts that a given message contains an header with a given name and + * expected value. + */ + public static Object assertMessageHeader(Message message, String headerName, Object expectedValue) { + Object actualValue = message.getHeader(headerName); + assertEquals(expectedValue, actualValue, "Header: " + headerName + " on Message: " + message); + return actualValue; } /** - * Asserts that the given expression when evaluated returns the given answer + * Asserts that the given expression when evaluated returns the given + * answer. */ - public static Object assertExpression(Expression expression, Exchange exchange, Object expected) { - Object value; - if (expected != null) { - value = expression.evaluate(exchange, expected.getClass()); + public static Object assertExpression(Expression expression, Exchange exchange, Object expectedAnswer) { + Object actualAnswer; + if (expectedAnswer != null) { + actualAnswer = expression.evaluate(exchange, expectedAnswer.getClass()); } else { - value = expression.evaluate(exchange, Object.class); + actualAnswer = expression.evaluate(exchange, Object.class); } - LOG.debug("Evaluated expression: " + expression + " on exchange: " + exchange + " result: " + value); + LOG.debug("Evaluated expression: " + expression + " on exchange: " + exchange + " result: " + actualAnswer); - assertEquals(expected, value, "Expression: " + expression + " on Exchange: " + exchange); - return value; + assertEquals(expectedAnswer, actualAnswer, "Expression: " + expression + " on Exchange: " + exchange); + return actualAnswer; } /** - * Asserts that the predicate returns the expected value on the exchange + * Asserts that a given predicate returns <code>true</code> on a given + * exchange. */ public static void assertPredicateMatches(Predicate predicate, Exchange exchange) { assertPredicate(predicate, exchange, true); } /** - * Asserts that the predicate returns the expected value on the exchange + * Asserts that a given predicate returns <code>false</code> on a given + * exchange. */ public static void assertPredicateDoesNotMatch(Predicate predicate, Exchange exchange) { try { @@ -217,91 +226,163 @@ public abstract class TestSupport { } /** - * Asserts that the predicate returns the expected value on the exchange + * Asserts that the predicate returns the expected value on the exchange. */ - public static boolean assertPredicate(final Predicate predicate, Exchange exchange, boolean expected) { - if (expected) { + public static boolean assertPredicate(final Predicate predicate, Exchange exchange, boolean expectedValue) { + if (expectedValue) { PredicateAssertHelper.assertMatches(predicate, "Predicate failed: ", exchange); } - boolean value = predicate.matches(exchange); + boolean actualValue = predicate.matches(exchange); - LOG.debug("Evaluated predicate: " + predicate + " on exchange: " + exchange + " result: " + value); + LOG.debug("Evaluated predicate: " + predicate + " on exchange: " + exchange + " result: " + actualValue); - assertEquals(expected, value, "Predicate: " + predicate + " on Exchange: " + exchange); - return value; + assertEquals(expectedValue, actualValue, "Predicate: " + predicate + " on Exchange: " + exchange); + return actualValue; } /** - * Resolves an endpoint and asserts that it is found + * Asserts that a given list has a single element. */ - public static Endpoint resolveMandatoryEndpoint(CamelContext context, String uri) { - Endpoint endpoint = context.getEndpoint(uri); + public static <T> T assertOneElement(List<T> list) { + assertEquals(1, list.size(), "Size of list should be 1: " + list); + return list.get(0); + } - assertNotNull(endpoint, "No endpoint found for URI: " + uri); + /** + * Asserts that a given list has a given expected size. + */ + public static <T> List<T> assertListSize(List<T> list, int expectedSize) { + return assertListSize("List", list, expectedSize); + } - return endpoint; + /** + * Asserts that a list is of the given size. When the assertion is broken, + * the error message starts with a given prefix. + */ + public static <T> List<T> assertListSize(String brokenAssertionMessagePrefix, List<T> list, int expectedSize) { + assertEquals(expectedSize, list.size(), brokenAssertionMessagePrefix + " should be of size: " + expectedSize + " but is: " + list); + return list; } /** - * Resolves an endpoint and asserts that it is found + * Asserts that a given collection has a given size. */ - public static <T extends Endpoint> T resolveMandatoryEndpoint(CamelContext context, String uri, Class<T> endpointType) { - T endpoint = context.getEndpoint(uri, endpointType); + public static <T> Collection<T> assertCollectionSize(Collection<T> list, int expectedSize) { + return assertCollectionSize("List", list, expectedSize); + } - assertNotNull(endpoint, "No endpoint found for URI: " + uri); + /** + * Asserts that a given collection has a given size. When the assertion is + * broken, the error message starts with a given prefix. + */ + public static <T> Collection<T> assertCollectionSize(String brokenAssertionMessagePrefix, Collection<T> list, int expectedSize) { + assertEquals(expectedSize, list.size(), brokenAssertionMessagePrefix + " should be of size: " + expectedSize + " but is: " + list); + return list; + } - return endpoint; + /** + * Asserts that the text contains the given string. + * + * @param text the text to compare + * @param containedText the text which must be contained inside the other + * text parameter + */ + public static void assertStringContains(String text, String containedText) { + assertNotNull(text, "Text should not be null!"); + assertTrue(text.contains(containedText), "Text: " + text + " does not contain: " + containedText); } /** - * Creates an exchange with the given body + * Asserts that two given directories are equal. To be used for + * folder/directory comparison that works across different platforms such as + * Window, Mac and Linux. */ - protected Exchange createExchangeWithBody(CamelContext camelContext, Object body) { - Exchange exchange = new DefaultExchange(camelContext); - Message message = exchange.getIn(); - message.setHeader("testClass", getClass().getName()); - message.setBody(body); - return exchange; + public static void assertDirectoryEquals(String expected, String actual) { + assertDirectoryEquals(null, expected, actual); } - public static <T> T assertOneElement(List<T> list) { - assertEquals(1, list.size(), "Size of list should be 1: " + list); - return list.get(0); + /** + * Asserts that two given directories are equal. To be used for + * folder/directory comparison that works across different platforms such as + * Window, Mac and Linux. + */ + public static void assertDirectoryEquals(String message, String expected, String actual) { + // must use single / as path separators + String expectedPath = expected.replace('\\', '/'); + String actualPath = actual.replace('\\', '/'); + + if (message != null) { + assertEquals(message, expectedPath, actualPath); + } else { + assertEquals(expectedPath, actualPath); + } } /** - * Asserts that a list is of the given size + * Asserts that a given directory is found in the file system. */ - public static <T> List<T> assertListSize(List<T> list, int size) { - return assertListSize("List", list, size); + public static void assertDirectoryExists(String filename) { + File file = new File(filename); + assertTrue(file.exists(), "Directory " + filename + " should exist"); + assertTrue(file.isDirectory(), "Directory " + filename + " should be a directory"); } /** - * Asserts that a list is of the given size + * Asserts that a given file is found in the file system. */ - public static <T> List<T> assertListSize(String message, List<T> list, int size) { - assertEquals(size, list.size(), message + " should be of size: " + size + " but is: " + list); - return list; + public static void assertFileExists(String filename) { + File file = new File(filename); + assertTrue(file.exists(), "File " + filename + " should exist"); + assertTrue(file.isFile(), "File " + filename + " should be a file"); } /** - * Asserts that a list is of the given size + * Asserts that a given file is <b>not</b> found in the file system. */ - public static <T> Collection<T> assertCollectionSize(Collection<T> list, int size) { - return assertCollectionSize("List", list, size); + public static void assertFileNotExists(String filename) { + File file = new File(filename); + assertFalse(file.exists(), "File " + filename + " should not exist"); } + // ----------------------------------------------------------------------- + // Other helpers, resolution, file, getRouteList + // ----------------------------------------------------------------------- + /** - * Asserts that a list is of the given size + * Resolves an endpoint and asserts that it is found. */ - public static <T> Collection<T> assertCollectionSize(String message, Collection<T> list, int size) { - assertEquals(size, list.size(), message + " should be of size: " + size + " but is: " + list); - return list; + public static Endpoint resolveMandatoryEndpoint(CamelContext context, String endpointUri) { + Endpoint endpoint = context.getEndpoint(endpointUri); + + assertNotNull(endpoint, "No endpoint found for URI: " + endpointUri); + + return endpoint; + } + + /** + * Resolves an endpoint and asserts that it is found. + */ + public static <T extends Endpoint> T resolveMandatoryEndpoint(CamelContext context, String endpointUri, Class<T> endpointType) { + T endpoint = context.getEndpoint(endpointUri, endpointType); + + assertNotNull(endpoint, "No endpoint found for URI: " + endpointUri); + + return endpoint; + } + + /** + * Creates an exchange with the given body. + */ + public static Exchange createExchangeWithBody(CamelContext camelContext, Object body) { + Exchange exchange = new DefaultExchange(camelContext); + Message message = exchange.getIn(); + message.setBody(body); + return exchange; } /** * A helper method to create a list of Route objects for a given route - * builder + * builder. */ public static List<Route> getRouteList(RouteBuilder builder) throws Exception { CamelContext context = new DefaultCamelContext(); @@ -313,32 +394,22 @@ public abstract class TestSupport { } /** - * Asserts that the text contains the given string - * - * @param text the text to compare - * @param containedText the text which must be contained inside the other - * text parameter - */ - public static void assertStringContains(String text, String containedText) { - assertNotNull(text, "Text should not be null!"); - assertTrue(text.contains(containedText), "Text: " + text + " does not contain: " + containedText); - } - - /** - * Recursively delete a directory, useful to zapping test data + * Recursively delete a directory, useful to zapping test data. Deletion + * will be attempted up to five time before giving up. * * @param file the directory to be deleted - * @return <tt>false</tt> if error deleting directory + * @return <tt>false</tt> when an error occur while deleting directory */ public static boolean deleteDirectory(String file) { return deleteDirectory(new File(file)); } /** - * Recursively delete a directory, useful to zapping test data + * Recursively delete a directory, useful to zapping test data. Deletion + * will be attempted up to five time before giving up. * * @param file the directory to be deleted - * @return <tt>false</tt> if error deleting directory + * @return <tt>false</tt> when an error occur while deleting directory */ public static boolean deleteDirectory(File file) { int tries = 0; @@ -359,7 +430,13 @@ public abstract class TestSupport { return !exists; } - private static void recursivelyDeleteDirectory(File file) { + /** + * Recursively delete a directory. Deletion will be attempted a single time + * before giving up. + * + * @param file the directory to be deleted + */ + public static void recursivelyDeleteDirectory(File file) { if (!file.exists()) { return; } @@ -377,7 +454,7 @@ public abstract class TestSupport { } /** - * create the directory + * Creates a given directory. * * @param file the directory to be created */ @@ -387,122 +464,39 @@ public abstract class TestSupport { } /** - * To be used for folder/directory comparison that works across different - * platforms such as Window, Mac and Linux. - */ - public static void assertDirectoryEquals(String expected, String actual) { - assertDirectoryEquals(null, expected, actual); - } - - /** - * To be used for folder/directory comparison that works across different - * platforms such as Window, Mac and Linux. - */ - public static void assertDirectoryEquals(String message, String expected, String actual) { - // must use single / as path separators - String expectedPath = expected.replace('\\', '/'); - String actualPath = actual.replace('\\', '/'); - - if (message != null) { - assertEquals(message, expectedPath, actualPath); - } else { - assertEquals(expectedPath, actualPath); - } - } - - /** - * To be used to check is a directory is found in the file system - */ - public static void assertDirectoryExists(String filename) { - File file = new File(filename); - assertTrue(file.exists(), "Directory " + filename + " should exist"); - assertTrue(file.isDirectory(), "Directory " + filename + " should be a directory"); - } - - /** - * To be used to check is a file is found in the file system - */ - public static void assertFileExists(String filename) { - File file = new File(filename); - assertTrue(file.exists(), "File " + filename + " should exist"); - assertTrue(file.isFile(), "File " + filename + " should be a file"); - } - - /** - * To be used to check is a file is <b>not</b> found in the file system - */ - public static void assertFileNotExists(String filename) { - File file = new File(filename); - assertFalse(file.exists(), "File " + filename + " should not exist"); - } - - /** - * Is this OS the given platform. + * Tells whether the current Operating System is the given expected + * platform. * <p/> - * Uses <tt>os.name</tt> from the system properties to determine the OS. + * Uses <tt>os.name</tt> from the system properties to determine the + * Operating System. * - * @param platform such as Windows - * @return <tt>true</tt> if its that platform. + * @param expectedPlatform such as Windows + * @return <tt>true</tt> when the current Operating System is the expected + * platform, <tt>false</tt> otherwise. */ - public static boolean isPlatform(String platform) { + public static boolean isPlatform(String expectedPlatform) { String osName = System.getProperty("os.name").toLowerCase(Locale.US); - return osName.contains(platform.toLowerCase(Locale.US)); + return osName.contains(expectedPlatform.toLowerCase(Locale.US)); } /** - * Is this Java by the given vendor. + * Tells whether the current Java Virtual Machine has been issued by a given + * expected vendor. * <p/> * Uses <tt>java.vendor</tt> from the system properties to determine the * vendor. * - * @param vendor such as IBM - * @return <tt>true</tt> if its that vendor. + * @param expectedVendor such as IBM + * @return <tt>true</tt> when the current Java Virtual Machine has been + * issued by the expected vendor, <tt>false</tt> otherwise. */ - public static boolean isJavaVendor(String vendor) { + public static boolean isJavaVendor(String expectedVendor) { String javaVendor = System.getProperty("java.vendor").toLowerCase(Locale.US); - return javaVendor.contains(vendor.toLowerCase(Locale.US)); - } - - /** - * Is this Java 1.5 - * - * @return <tt>true</tt> if its Java 1.5, <tt>false</tt> if its not (for - * example Java 1.6 or better) - * @deprecated will be removed in the future as Camel requires JDK1.8+ - */ - @Deprecated - public static boolean isJava15() { - return getJavaMajorVersion() == 5; - } - - /** - * Is this Java 1.6 - * - * @return <tt>true</tt> if its Java 1.6, <tt>false</tt> if its not (for - * example Java 1.7 or better) - * @deprecated will be removed in the future as Camel requires JDK1.8+ - */ - @Deprecated - public static boolean isJava16() { - return getJavaMajorVersion() == 6; - - } - - /** - * Is this Java 1.7 - * - * @return <tt>true</tt> if its Java 1.7, <tt>false</tt> if its not (for - * example Java 1.6 or older) - * @deprecated will be removed in the future as Camel requires JDK1.8+ - */ - @Deprecated - public static boolean isJava17() { - return getJavaMajorVersion() == 7; - + return javaVendor.contains(expectedVendor.toLowerCase(Locale.US)); } /** - * Is this Java 1.8 + * Tells whether the current Java version is 1.8. * * @return <tt>true</tt> if its Java 1.8, <tt>false</tt> if its not (for * example Java 1.7 or older) @@ -513,7 +507,7 @@ public abstract class TestSupport { } /** - * Is this Java 1.9 + * Tells whether the current Java version is 1.9. * * @return <tt>true</tt> if its Java 1.9, <tt>false</tt> if its not (for * example Java 1.8 or older) diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/AdviceWithNotStartedTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/AdviceWithNotStartedTest.java index 68aae9c..b097165 100644 --- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/AdviceWithNotStartedTest.java +++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/AdviceWithNotStartedTest.java @@ -26,6 +26,7 @@ import org.apache.camel.reifier.RouteReifier; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; +import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.fail; public class AdviceWithNotStartedTest extends CamelTestSupport { diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java index 9a6811b..183d1d7 100644 --- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java +++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java @@ -22,9 +22,13 @@ import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DebugJUnit5Test extends CamelTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(DebugJUnit5Test.class); + // START SNIPPET: e1 @Override public boolean isUseDebugger() { @@ -38,7 +42,7 @@ public class DebugJUnit5Test extends CamelTestSupport { // processor // from your Java editor you can just add a breakpoint in the code line // below - log.info("Before " + definition + " with body " + exchange.getIn().getBody()); + LOG.info("Before " + definition + " with body " + exchange.getIn().getBody()); } // END SNIPPET: e1 diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java index 7584f15..a9cb78f 100644 --- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java +++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java @@ -22,9 +22,13 @@ import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DebugNoLazyTypeConverterTest extends CamelTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(DebugNoLazyTypeConverterTest.class); + @Override protected boolean isLazyLoadingTypeConverter() { return false; @@ -43,7 +47,7 @@ public class DebugNoLazyTypeConverterTest extends CamelTestSupport { // processor // from your Java editor you can just add a breakpoint in the code line // below - log.info("Before " + definition + " with body " + exchange.getIn().getBody()); + LOG.info("Before " + definition + " with body " + exchange.getIn().getBody()); } // END SNIPPET: e1 diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java index 38f75d5..87ae779 100644 --- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java +++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java @@ -22,9 +22,13 @@ import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DebugTest extends CamelTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(DebugTest.class); + // START SNIPPET: e1 @Override public boolean isUseDebugger() { @@ -38,7 +42,7 @@ public class DebugTest extends CamelTestSupport { // processor // from your Java editor you can just add a breakpoint in the code line // below - log.info("Before " + definition + " with body " + exchange.getIn().getBody()); + LOG.info("Before " + definition + " with body " + exchange.getIn().getBody()); } // END SNIPPET: e1 diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/IsMockEndpointsFileTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/IsMockEndpointsFileTest.java index 1407f13..fe621af 100644 --- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/IsMockEndpointsFileTest.java +++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/IsMockEndpointsFileTest.java @@ -23,6 +23,8 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.apache.camel.test.junit5.TestSupport.deleteDirectory; + public class IsMockEndpointsFileTest extends CamelTestSupport { @Override diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/RouteProcessorDumpRouteCoverageTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/RouteProcessorDumpRouteCoverageTest.java index fe820dd..b97959c 100644 --- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/RouteProcessorDumpRouteCoverageTest.java +++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/RouteProcessorDumpRouteCoverageTest.java @@ -22,6 +22,7 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import static org.apache.camel.test.junit5.TestSupport.assertFileExists; import static org.junit.jupiter.api.Assertions.assertEquals; public class RouteProcessorDumpRouteCoverageTest extends CamelTestSupport {