From 7b2b33ca715918993896c541cbe9846986455aff Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Fri, 10 Mar 2017 16:44:07 +0000 Subject: [PATCH] yaml-test assertions: adds "containsMatch" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit “matches: ” does not work as you might expect for multi-line, if you don't include "(?m)(?s)" at the start of the regex. The "containsMatch" is easier to use for (many/some?) use-cases. --- .../framework/TestFrameworkAssertions.java | 13 ++++++++--- .../TestFrameworkAssertionsTest.java | 23 ++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java index 68d51d87c7..f416da56b6 100644 --- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java +++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java @@ -25,6 +25,8 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; +import java.util.regex.Pattern; + import javax.annotation.Nullable; import org.apache.brooklyn.api.entity.Entity; @@ -62,6 +64,7 @@ public class TestFrameworkAssertions { public static final String EQUALS = "equals"; public static final String NOT_EQUAL = "notEqual"; public static final String MATCHES = "matches"; + public static final String CONTAINS_MATCH = "containsMatch"; public static final String CONTAINS = "contains"; public static final String IS_EMPTY = "isEmpty"; public static final String NOT_EMPTY = "notEmpty"; @@ -333,7 +336,11 @@ private static boolean conditionHolds(String condition, Object actual, Object ex case NOT_EMPTY: return isTrue(expected) == ((null != actual && Strings.isNonEmpty(actual.toString()))); case MATCHES: - return null != actual && actual.toString().matches(expected.toString()); + Pattern matchesPattern = Pattern.compile(expected.toString()); + return null != actual && matchesPattern.matcher(actual.toString()).matches(); + case CONTAINS_MATCH: + Pattern containsMatchPattern = Pattern.compile(expected.toString()); + return null != actual && containsMatchPattern.matcher(actual.toString()).find(); case HAS_TRUTH_VALUE: return isTrue(expected) == isTrue(actual); case GREATER_THAN: @@ -349,7 +356,7 @@ private static boolean knownCondition(String condition) { // Everything but UNKNOWN_CONDITION. The conditions should really be an enum! Set allConditions = ImmutableSet.of( IS_NULL, NOT_NULL, IS_EQUAL_TO, EQUAL_TO, EQUALS, NOT_EQUAL, - MATCHES, CONTAINS, IS_EMPTY, NOT_EMPTY, HAS_TRUTH_VALUE, + MATCHES, CONTAINS_MATCH, CONTAINS, IS_EMPTY, NOT_EMPTY, HAS_TRUTH_VALUE, GREATER_THAN, LESS_THAN); return allConditions.contains(condition); } @@ -362,7 +369,7 @@ private static boolean canCompare(@Nullable Object actual, @Nullable Object expe && actual.getClass().equals(expected.getClass()); } - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) private static int compare(@Nullable Object actual, @Nullable Object expected) { if (!canCompare(actual, expected)) { throw new IllegalArgumentException("Arguments are not comparable: " + actual + ", " + expected); diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java index 360f07270f..b698f0fb8f 100644 --- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java +++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java @@ -60,17 +60,30 @@ public Object[][] positiveTestsDP() { {"some-sensor-value", Arrays.asList(ImmutableMap.of("equals", "some-sensor-value"))}, {"some-sensor-value", Arrays.asList(ImmutableMap.of("notEqual", "other-sensor-value"))}, {10, Arrays.asList(ImmutableMap.of("notEqual", 20))}, - {"some-regex-value-to-match", Arrays.asList(ImmutableMap.of("matches", "some.*match", "isEqualTo", "some-regex-value-to-match"))}, + {null, Arrays.asList(ImmutableMap.of("isNull", Boolean.TRUE))}, {"some-non-null-value", Arrays.asList(ImmutableMap.of("isNull", Boolean.FALSE))}, {null, Arrays.asList(ImmutableMap.of("notNull", Boolean.FALSE))}, {"some-non-null-value", Arrays.asList(ImmutableMap.of("notNull", Boolean.TRUE))}, + {"

Im a H1 tag!

", Arrays.asList(ImmutableMap.of("contains", "Im a H1 tag!"))}, {"{\"a\":\"b\",\"c\":\"d\",\"e\":123,\"g\":false}", Arrays.asList(ImmutableMap.of("contains", "false"))}, + + {"some-regex-value-to-match", Arrays.asList(ImmutableMap.of("matches", "some.*match", "isEqualTo", "some-regex-value-to-match"))}, + {"line1\nline2\nline3\n", Arrays.asList(ImmutableMap.of("matches", "(?s).*line2.*"))}, + {"line1\nline2\nline3\n", Arrays.asList(ImmutableMap.of("matches", "(?m)line1\n^line2$\nline3\n"))}, + {"line1\nline2\nline3\n", Arrays.asList(ImmutableMap.of("matches", "(?m)(?s).*^line2$.*"))}, + {"line1\nline2\nline3\n", Arrays.asList(ImmutableMap.of("matches", "^line1\nline2\nline3\n$"))}, + + {"line1\nline2\nline3\n", Arrays.asList(ImmutableMap.of("containsMatch", "line1"))}, + {"line1\nline2\nline3\n", Arrays.asList(ImmutableMap.of("containsMatch", "lin.*1"))}, + {"line1\nline2\nline3\n", Arrays.asList(ImmutableMap.of("containsMatch", "lin.1\nlin.2"))}, + {"", Arrays.asList(ImmutableMap.of("isEmpty", Boolean.TRUE))}, {"some-non-null-value", Arrays.asList(ImmutableMap.of("isEmpty", Boolean.FALSE))}, {null, Arrays.asList(ImmutableMap.of("notEmpty", Boolean.FALSE))}, {"some-non-null-value", Arrays.asList(ImmutableMap.of("notEmpty", Boolean.TRUE))}, + {"true", Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.TRUE))}, {"false", Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.FALSE))}, @@ -145,6 +158,14 @@ public Object[][] negativeTestsDP() { {"

Im a H1 tag!

", "contains", "quack", Arrays.asList(ImmutableMap.of("contains", "quack"))}, {"{\"a\":\"b\",\"c\":\"d\",\"e\":123,\"g\":false}", "contains", "moo", Arrays.asList(ImmutableMap.of("contains", "moo"))}, + {"line1\nline2\nline3\n", "matches", "notthere", Arrays.asList(ImmutableMap.of("matches", "notthere"))}, + {"line1\nline2\nline3\n", "matches", ".*line2.*", Arrays.asList(ImmutableMap.of("matches", ".*line2.*"))}, // default is not DOTALL + {"line1\nline2\nline3\n", "matches", "line1\n^line2$\nline3\n", Arrays.asList(ImmutableMap.of("matches", "line1\n^line2$\nline3\n"))}, // default is not MULTILINE + + {"line1", "containsMatch", "quack", Arrays.asList(ImmutableMap.of("containsMatch", "quack"))}, + {"line1\nline2\nline3\n", "containsMatch", ".*line1.*line2", Arrays.asList(ImmutableMap.of("containsMatch", ".*line1.*line2"))}, // default is not DOTALL + {"line1\nline2\nline3\n", "containsMatch", "^line2$", Arrays.asList(ImmutableMap.of("containsMatch", "^line2$"))}, // default is not MULTILINE + {25, "lessThan", 24, Collections.singletonList(ImmutableMap.of("lessThan", 24))}, {"b", "lessThan", "a", Collections.singletonList(ImmutableMap.of("lessThan", "a"))}, {null, "lessThan", "a", Collections.singletonList(ImmutableMap.of("lessThan", "a"))},