From 6846f624960b4ddba001c5c7b54a4de865bbc8d2 Mon Sep 17 00:00:00 2001 From: Thorsten Schlathoelter Date: Mon, 18 Sep 2023 07:20:01 +0200 Subject: [PATCH] fix(#988): Add Hamcrest matchesPattern matcher --- .../matcher/hamcrest/HamcrestValidationMatcher.java | 6 +++--- .../matcher/hamcrest/HamcrestValidationMatcherTest.java | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/validation/citrus-validation-hamcrest/src/main/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcher.java b/validation/citrus-validation-hamcrest/src/main/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcher.java index 4fa347f2d4..8f983c1796 100644 --- a/validation/citrus-validation-hamcrest/src/main/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcher.java +++ b/validation/citrus-validation-hamcrest/src/main/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcher.java @@ -54,7 +54,7 @@ @SuppressWarnings("unchecked") public class HamcrestValidationMatcher implements ValidationMatcher, ControlExpressionParser { - private final List matchers = Arrays.asList( "equalTo", "equalToIgnoringCase", "equalToIgnoringWhiteSpace", "is", "not", "containsString", "startsWith", "endsWith" ); + private final List matchers = Arrays.asList( "equalTo", "equalToIgnoringCase", "equalToIgnoringWhiteSpace", "is", "not", "containsString", "startsWith", "endsWith", "matchesPattern" ); private final List collectionMatchers = Arrays.asList("hasSize", "hasItem", "hasItems", "contains", "containsInAnyOrder"); @@ -500,7 +500,7 @@ private static class Tokenizer { /** * Regular expression with three alternative parts (ored) to match: *
    - *
  1. `(sometext)` - Quoted parameter block of a matcher.
  2. + *
  3. (`sometext`) - Quoted parameter block of a matcher.
  4. *
  5. 'sometext' - Quoted text used as a parameter to a string matcher.
  6. *
  7. (unquotedtext) - Unquoted text used as a parameter to a string matcher. This expression is non-greedy, meaning the first closing bracket will terminate the match.
  8. *
@@ -513,7 +513,7 @@ private static class Tokenizer { * Therefore, the regex expressions explicitly match the escaped quote -> \\\\' */ private static final Pattern TEXT_PARAMETER_PATTERN = Pattern.compile( - "(?\\('(?:[^']|\\\\')*[^\\\\]'\\))" + "(?\\('(?:[^']|\\\\')*[^\\\\]'\\))" + "|(?('(?:[^']|\\\\')*[^\\\\]'))" + "|(?\\(((?:[^']|\\\\')*?)[^\\\\]?\\))" ); diff --git a/validation/citrus-validation-hamcrest/src/test/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcherTest.java b/validation/citrus-validation-hamcrest/src/test/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcherTest.java index 7d20faa75e..d50a19ac60 100644 --- a/validation/citrus-validation-hamcrest/src/test/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcherTest.java +++ b/validation/citrus-validation-hamcrest/src/test/java/org/citrusframework/validation/matcher/hamcrest/HamcrestValidationMatcherTest.java @@ -162,9 +162,11 @@ public Object[][] testData() { new Object[]{"foo", "unquoted text may not include brackets or commas", Collections.singletonList("anyOf(equalTo(unquoted text may not include brackets or commas), anyOf(isEmptyOrNullString()))")}, new Object[]{"foo", "quoted \\' text may not include brackets or commas", Collections.singletonList("anyOf(equalTo(quoted \\\\' text may not include brackets or commas), anyOf(isEmptyOrNullString()))")}, new Object[]{"foo", "value1", Collections.singletonList("anyOf(isEmptyOrNullString(),equalTo(value1))")}, - new Object[]{"foo", "INSERT INTO todo_entries (id, title, description, done) values (1, 'Invite for meeting', 'Invite the group for a lunch meeting', 'false')", Collections.singletonList("allOf(startsWith('INSERT INTO todo_entries (id, title, description, done)'))")}, + new Object[]{"foo", "value1", Collections.singletonList("matchesPattern([^2345]*)")}, + new Object[]{"foo", "value1 with quotes", Collections.singletonList("matchesPattern('[^2345]*')")}, + }; @@ -237,6 +239,8 @@ public Object[][] testDataFailed() { new Object[]{"foo", "notext containing a ' (quote) and a , (comma) ", Collections.singletonList("anyOf(equalTo('text containing a \\' (quote) and a , (comma) '), anyOf(isEmptyOrNullString()))")}, new Object[]{"foo", "notext containing a \\' (quote) and a , (comma) ", Collections.singletonList("anyOf(equalTo('text containing a \\\\' (quote) and a , (comma) '), anyOf(isEmptyOrNullString()))")}, new Object[]{"foo", "nounquoted text may not include brackets or commas", Collections.singletonList("anyOf(equalTo(unquoted text may not include brackets or commas), anyOf(isEmptyOrNullString()))")}, + new Object[]{"foo", "value1", Collections.singletonList("matchesPattern([^12345]*)")}, + new Object[]{"foo", "value1 with quotes", Collections.singletonList("matchesPattern('[^12345]*')")}, }; }