Class TestSupport

java.lang.Object
org.apache.camel.test.junit5.TestSupport

public final class TestSupport extends Object
Provides utility methods for camel test purpose (builders, assertions, endpoint resolutions, file helpers).
  • Method Details

    • header

      public static org.apache.camel.builder.ValueBuilder header(String name)
      Returns a value builder for the given header.
    • exchangeProperty

      public static org.apache.camel.builder.ValueBuilder exchangeProperty(String name)
      Returns a value builder for the given exchange property.
    • body

      public static org.apache.camel.builder.ValueBuilder body()
      Returns a predicate and value builder for the inbound body on an exchange.
    • bodyAs

      public static <T> org.apache.camel.builder.ValueBuilder bodyAs(Class<T> type)
      Returns a predicate and value builder for the inbound message body as a specific type.
    • systemProperty

      public static org.apache.camel.builder.ValueBuilder systemProperty(String name)
      Returns a value builder for the given system property.
    • systemProperty

      public static org.apache.camel.builder.ValueBuilder systemProperty(String name, String defaultValue)
      Returns a value builder for the given system property.
    • assertIsInstanceOf

      public static <T> T assertIsInstanceOf(Class<T> expectedType, Object value)
      Asserts that a given value is of an expected type.
    • assertEndpointUri

      public static void assertEndpointUri(org.apache.camel.Endpoint endpoint, String expectedUri)
      Asserts that a given endpoint has an expected uri.
    • assertInMessageHeader

      public static Object assertInMessageHeader(org.apache.camel.Exchange exchange, String headerName, Object expectedValue)
      Asserts that the In message on the exchange contains an header with a given name and expected value.
    • assertOutMessageHeader

      public static Object assertOutMessageHeader(org.apache.camel.Exchange exchange, String headerName, Object expectedValue)
      Asserts that the message on the exchange contains an header with a given name and expected value.
    • assertInMessageBodyEquals

      public static void assertInMessageBodyEquals(org.apache.camel.Exchange exchange, Object expectedBody) throws org.apache.camel.InvalidPayloadException
      Asserts that the given exchange has a given expectedBody on the IN message.
      Throws:
      org.apache.camel.InvalidPayloadException
    • assertMessageBodyEquals

      public static void assertMessageBodyEquals(org.apache.camel.Exchange exchange, Object expectedBody) throws org.apache.camel.InvalidPayloadException
      Asserts that the given exchange has a given expectedBody on the message.
      Throws:
      org.apache.camel.InvalidPayloadException
    • assertMessageHeader

      public static Object assertMessageHeader(org.apache.camel.Message message, String headerName, Object expectedValue)
      Asserts that a given message contains an header with a given name and expected value.
    • assertExpression

      public static Object assertExpression(org.apache.camel.Expression expression, org.apache.camel.Exchange exchange, Object expectedAnswer)
      Asserts that the given expression when evaluated returns the given answer.
    • assertPredicateMatches

      public static void assertPredicateMatches(org.apache.camel.Predicate predicate, org.apache.camel.Exchange exchange)
      Asserts that a given predicate returns true on a given exchange.
    • assertPredicateDoesNotMatch

      public static void assertPredicateDoesNotMatch(org.apache.camel.Predicate predicate, org.apache.camel.Exchange exchange)
      Asserts that a given predicate returns false on a given exchange.
    • assertPredicate

      public static boolean assertPredicate(org.apache.camel.Predicate predicate, org.apache.camel.Exchange exchange, boolean expectedValue)
      Asserts that the predicate returns the expected value on the exchange.
    • assertExpression

      public static void assertExpression(org.apache.camel.CamelContext context, org.apache.camel.Exchange exchange, String languageName, String expressionText, Object expectedValue)
      Asserts that the given language name and expression evaluates to the given value on a specific exchange
    • assertOneElement

      public static <T> T assertOneElement(List<T> list)
      Asserts that a given list has a single element.
    • assertListSize

      public static <T> List<T> assertListSize(List<T> list, int expectedSize)
      Asserts that a given list has a given expected size.
    • assertListSize

      public static <T> List<T> assertListSize(String brokenAssertionMessagePrefix, List<T> list, int expectedSize)
      Asserts that a list is of the given size. When the assertion is broken, the error message starts with a given prefix.
    • assertCollectionSize

      public static <T> Collection<T> assertCollectionSize(Collection<T> list, int expectedSize)
      Asserts that a given collection has a given size.
    • assertCollectionSize

      public static <T> Collection<T> assertCollectionSize(String brokenAssertionMessagePrefix, Collection<T> list, int expectedSize)
      Asserts that a given collection has a given size. When the assertion is broken, the error message starts with a given prefix.
    • assertStringContains

      public static void assertStringContains(String text, String containedText)
      Asserts that the text contains the given string.
      Parameters:
      text - the text to compare
      containedText - the text which must be contained inside the other text parameter
    • assertDirectoryEquals

      public static void assertDirectoryEquals(String expected, String actual)
      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.
    • assertDirectoryEquals

      public static void assertDirectoryEquals(String message, String expected, String actual)
      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.
    • assertDirectoryExists

      public static void assertDirectoryExists(Path file)
      To be used to check is a directory is found in the file system
    • assertDirectoryExists

      public static void assertDirectoryExists(String filename)
      Asserts that a given directory is found in the file system.
    • assertFileExists

      public static void assertFileExists(Path file)
      To be used to check is a file is found in the file system
    • assertFileExists

      public static void assertFileExists(Path file, String content) throws IOException
      To be used to check is a file is found in the file system
      Throws:
      IOException
    • assertFileExists

      public static void assertFileExists(String filename)
      Asserts that a given file is found in the file system.
    • assertFileNotExists

      public static void assertFileNotExists(Path file)
      To be used to check is a file is not found in the file system
    • assertFileNotExists

      public static void assertFileNotExists(String filename)
      Asserts that a given file is not found in the file system.
    • assertResolveLanguage

      @Deprecated(since="4.7.0") public static org.apache.camel.spi.Language assertResolveLanguage(org.apache.camel.CamelContext context, String languageName)
      Deprecated.
      Asserts that the language name can be resolved
    • assertPredicate

      public static void assertPredicate(org.apache.camel.CamelContext context, String languageName, String expressionText, org.apache.camel.Exchange exchange, boolean expected)
      Asserts that the given language name and predicate expression evaluates to the expected value on the message exchange
    • resolveMandatoryEndpoint

      public static org.apache.camel.Endpoint resolveMandatoryEndpoint(org.apache.camel.CamelContext context, String endpointUri)
      Resolves an endpoint and asserts that it is found.
    • resolveMandatoryEndpoint

      public static <T extends org.apache.camel.Endpoint> T resolveMandatoryEndpoint(org.apache.camel.CamelContext context, String endpointUri, Class<T> endpointType)
      Resolves an endpoint and asserts that it is found.
    • createExchangeWithBody

      public static org.apache.camel.Exchange createExchangeWithBody(org.apache.camel.CamelContext camelContext, Object body)
      Creates an exchange with the given body.
    • getRouteList

      public static List<org.apache.camel.Route> getRouteList(org.apache.camel.builder.RouteBuilder builder) throws Exception
      A helper method to create a list of Route objects for a given route builder.
      Throws:
      Exception
    • deleteDirectory

      public static boolean deleteDirectory(Path file)
      Recursively delete a directory, useful to zapping test data. Deletion will be attempted up to five time before giving up.
      Parameters:
      file - the directory to be deleted
      Returns:
      false when an error occur while deleting directory
    • deleteDirectory

      public static boolean deleteDirectory(String file)
      Recursively delete a directory, useful to zapping test data. Deletion will be attempted up to five time before giving up.
      Parameters:
      file - the directory to be deleted
      Returns:
      false when an error occur while deleting directory
    • deleteDirectory

      public static boolean deleteDirectory(File file)
      Recursively delete a directory, useful to zapping test data. Deletion will be attempted up to five time before giving up.
      Parameters:
      file - the directory to be deleted
      Returns:
      false when an error occur while deleting directory
    • recursivelyDeleteDirectory

      public static void recursivelyDeleteDirectory(File file)
      Recursively delete a directory. Deletion will be attempted a single time before giving up.
      Parameters:
      file - the directory to be deleted
    • createCleanDirectory

      public static void createCleanDirectory(Path file)
      Creates a given directory.
      Parameters:
      file - the directory to be created
    • createDirectory

      public static void createDirectory(Path file)
      Creates a given directory.
      Parameters:
      file - the directory to be created
    • createDirectory

      public static void createDirectory(String file)
      Creates a given directory.
      Parameters:
      file - the directory to be created
    • isPlatform

      public static boolean isPlatform(String expectedPlatform)
      Tells whether the current Operating System is the given expected platform.

      Uses os.name from the system properties to determine the Operating System.

      Parameters:
      expectedPlatform - such as Windows
      Returns:
      true when the current Operating System is the expected platform, false otherwise.
    • isJavaVendor

      public static boolean isJavaVendor(String expectedVendor)
      Tells whether the current Java Virtual Machine has been issued by a given expected vendor.

      Uses java.vendor from the system properties to determine the vendor.

      Parameters:
      expectedVendor - such as IBM
      Returns:
      true when the current Java Virtual Machine has been issued by the expected vendor, false otherwise.
    • getJavaMajorVersion

      public static int getJavaMajorVersion()
      Returns the current major Java version e.g 8.

      Uses java.specification.version from the system properties to determine the major version.

      Returns:
      the current major Java version.
    • isCamelDebugPresent

      public static boolean isCamelDebugPresent()
      Indicates whether the component camel-debug is present in the classpath of the test.
      Returns:
      true if it is present, false otherwise.
    • fileUri

      public static String fileUri(Path testDirectory)
    • fileUri

      public static String fileUri(Path testDirectory, String query)
    • executeSlowly

      public static void executeSlowly(int count, long interval, TimeUnit timeUnit, IntConsumer task) throws Exception
      Throws:
      Exception
    • getMandatoryEndpoint

      public static <T extends org.apache.camel.Endpoint> T getMandatoryEndpoint(org.apache.camel.CamelContext context, String uri, Class<T> type)
    • getMandatoryEndpoint

      public static org.apache.camel.Endpoint getMandatoryEndpoint(org.apache.camel.CamelContext context, String uri)
    • sendBody

      public static void sendBody(org.apache.camel.ProducerTemplate template, String endpointUri, Object body, Map<String,Object> headers)
    • sendBody

      public static void sendBody(org.apache.camel.ProducerTemplate template, String endpointUri, Object body)
      Sends a message to the given endpoint URI with the body value
      Parameters:
      endpointUri - the URI of the endpoint to send to
      body - the body for the message
    • sendBodies

      public static void sendBodies(org.apache.camel.ProducerTemplate template, String endpointUri, Object... bodies)
      Sends a message to the given endpoint URI with the body value
      Parameters:
      template - the producer template to use to send the messages
      endpointUri - the URI of the endpoint to send to
      bodies - the bodies for the message
    • getMockEndpoint

      public static org.apache.camel.component.mock.MockEndpoint getMockEndpoint(org.apache.camel.CamelContext context, String uri, boolean create) throws org.apache.camel.NoSuchEndpointException
      Resolves the MockEndpoint using a URI of the form mock:someName, optionally creating it if it does not exist. This implementation will lookup existing mock endpoints and match on the mock queue name, eg mock:foo and mock:foo?retainFirst=5 would match as the queue name is foo.
      Parameters:
      uri - the URI which typically starts with "mock:" and has some name
      create - whether to allow the endpoint to be created if it doesn't exist
      Returns:
      the mock endpoint or an NoSuchEndpointException is thrown if it could not be resolved
      Throws:
      org.apache.camel.NoSuchEndpointException - is the mock endpoint does not exist
    • loadExternalPropertiesQuietly

      public static Properties loadExternalPropertiesQuietly(Class<?> clazz, String path)
    • loadExternalPropertiesQuietly

      public static Properties loadExternalPropertiesQuietly(ClassLoader loader, String path)
    • loadExternalPropertiesQuietly

      public static Properties loadExternalPropertiesQuietly(Properties properties, Class<?> clazz, String path)
    • loadExternalPropertiesQuietly

      public static Properties loadExternalPropertiesQuietly(Properties properties, ClassLoader loader, String path)
    • loadExternalProperties

      public static Properties loadExternalProperties(Class<?> clazz, String path) throws IOException
      Throws:
      IOException
    • loadExternalProperties

      public static Properties loadExternalProperties(ClassLoader loader, String path) throws IOException
      Throws:
      IOException
    • loadExternalProperties

      public static Properties loadExternalProperties(Properties properties, Class<?> clazz, String path) throws IOException
      Throws:
      IOException
    • loadExternalProperties

      public static Properties loadExternalProperties(Properties properties, ClassLoader loader, String path) throws IOException
      Throws:
      IOException