aboutsummaryrefslogtreecommitdiffstats
path: root/testutil/src/main/java/com
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /testutil/src/main/java/com
Publish
Diffstat (limited to 'testutil/src/main/java/com')
-rw-r--r--testutil/src/main/java/com/yahoo/test/CollectionPatternMatcher.java45
-rw-r--r--testutil/src/main/java/com/yahoo/test/LinePatternMatcher.java43
-rw-r--r--testutil/src/main/java/com/yahoo/test/ManualClock.java36
-rw-r--r--testutil/src/main/java/com/yahoo/test/Matchers.java104
-rw-r--r--testutil/src/main/java/com/yahoo/test/OrderTester.java73
-rw-r--r--testutil/src/main/java/com/yahoo/test/PartialOrderTester.java29
-rw-r--r--testutil/src/main/java/com/yahoo/test/PatternMatcher.java38
-rw-r--r--testutil/src/main/java/com/yahoo/test/TotalOrderTester.java37
8 files changed, 405 insertions, 0 deletions
diff --git a/testutil/src/main/java/com/yahoo/test/CollectionPatternMatcher.java b/testutil/src/main/java/com/yahoo/test/CollectionPatternMatcher.java
new file mode 100644
index 00000000000..12cf0f671f9
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/CollectionPatternMatcher.java
@@ -0,0 +1,45 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+
+import java.util.Collection;
+
+/**
+ * Checks if a collection of strings contains at least one string with the expected regex pattern.
+ *
+ * @author gjoranv
+ * @since 5.1.8
+ */
+public class CollectionPatternMatcher extends BaseMatcher<Collection<String>> {
+
+ private final String pattern;
+
+ public CollectionPatternMatcher(String pattern) {
+ this.pattern = pattern;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("contains a string that matches expression '" + pattern + "'");
+ }
+
+ @Override
+ public boolean matches(Object o) {
+ @SuppressWarnings("unchecked")
+ Collection<String> strings = (Collection<String>) o;
+ for (String s : strings)
+ if (s.matches(pattern))
+ return true;
+ return false;
+ }
+
+ @Factory
+ public static <T> Matcher<Collection<String>> containsStringWithPattern(String pattern) {
+ return new CollectionPatternMatcher(pattern);
+ }
+
+}
diff --git a/testutil/src/main/java/com/yahoo/test/LinePatternMatcher.java b/testutil/src/main/java/com/yahoo/test/LinePatternMatcher.java
new file mode 100644
index 00000000000..c1cb8e4032c
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/LinePatternMatcher.java
@@ -0,0 +1,43 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+
+/**
+ * Checks if a multi-line string contains at least one line with the expected regex pattern.
+ *
+ * @author gjoranv
+ * @since 5.1.7
+ */
+public class LinePatternMatcher extends BaseMatcher<String> {
+
+ private final String pattern;
+
+ public LinePatternMatcher(String pattern) {
+ this.pattern = pattern;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("contains a line that matches expression '" + pattern + "'");
+ }
+
+ @Override
+ public boolean matches(Object o) {
+ String s = (String)o;
+ String[] lines = s.split("\n");
+ for (String line : lines)
+ if (line.matches(pattern))
+ return true;
+ return false;
+ }
+
+ @Factory
+ public static <T> Matcher<String> containsLineWithPattern(String pattern) {
+ return new LinePatternMatcher(pattern);
+ }
+
+} \ No newline at end of file
diff --git a/testutil/src/main/java/com/yahoo/test/ManualClock.java b/testutil/src/main/java/com/yahoo/test/ManualClock.java
new file mode 100644
index 00000000000..b8325b3e3fc
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/ManualClock.java
@@ -0,0 +1,36 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.temporal.TemporalAmount;
+
+/** A clock which initially has the time of its creation but can only be advanced by calling advance */
+public class ManualClock extends Clock {
+
+ private Instant currentTime = Instant.now();
+
+ public ManualClock() {}
+
+ public ManualClock(Instant currentTime) {
+ this.currentTime = currentTime;
+ }
+
+ public void advance(TemporalAmount temporal) {
+ currentTime = currentTime.plus(temporal);
+ }
+
+ @Override
+ public Instant instant() { return currentTime; }
+
+ @Override
+ public ZoneId getZone() { return null; }
+
+ @Override
+ public Clock withZone(ZoneId zone) { return null; }
+
+ @Override
+ public long millis() { return currentTime.toEpochMilli(); }
+
+}
diff --git a/testutil/src/main/java/com/yahoo/test/Matchers.java b/testutil/src/main/java/com/yahoo/test/Matchers.java
new file mode 100644
index 00000000000..80e01b43a18
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/Matchers.java
@@ -0,0 +1,104 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+
+import java.lang.reflect.Method;
+
+/**
+ * Some useful matchers.
+ *
+ * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
+ */
+public final class Matchers {
+
+ /**
+ * A match is found if at least <em>one</em> item in the Iterable has the given method, <em>and</em> the return value
+ * when calling it equals the given expected value.
+ *
+ * @param expected The expected value.
+ * @param methodName The name of the method to call. The method must take no parameters.
+ * @return A Matcher to match against an Iterable.
+ */
+ @SuppressWarnings("rawtypes")
+ public static org.hamcrest.Matcher<java.lang.Iterable> hasItemWithMethod(final Object expected, final String methodName) {
+ return hasItemWithMethod(new MethodResult(methodName, expected));
+ }
+
+ /**
+ * A match is found if at least <em>one</em> item in the Iterable has <em>all</em> given methods, <em>and</em>
+ * the return values when calling them equals the given expected values.
+ *
+ * @param results The pairs of method names and expected results to compare.
+ * @return A Matcher to match against an Iterable.
+ */
+ @SuppressWarnings("rawtypes")
+ public static org.hamcrest.Matcher<java.lang.Iterable> hasItemWithMethod(final MethodResult... results) {
+ return new BaseMatcher<Iterable>() {
+ @Override
+ public boolean matches(Object item) {
+ Iterable components = (Iterable) item;
+ if (!components.iterator().hasNext()) {
+ //empty collection
+ return false;
+ }
+ for (Object componentInList : components) {
+ boolean allMethodsMatch = false;
+ for (MethodResult result : results) {
+ Object strToMatch;
+ try {
+ Method method = componentInList.getClass().getMethod(result.getMethodName());
+ strToMatch = method.invoke(componentInList);
+ } catch (Exception e) {
+ allMethodsMatch = false;
+ break;
+ }
+ if (result.getExpectedResult().equals(strToMatch)) {
+ allMethodsMatch = true;
+ } else {
+ allMethodsMatch = false;
+ break;
+ }
+ }
+ if (allMethodsMatch) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ StringBuilder b = new StringBuilder();
+ for (MethodResult result : results) {
+ b.append(result).append(". ");
+ }
+ description.appendText(b.toString());
+ }
+ };
+ }
+
+ public static final class MethodResult {
+ private final String methodName;
+ private final Object expectedResult;
+
+ public MethodResult(String methodName, Object expectedResult) {
+ this.methodName = methodName;
+ this.expectedResult = expectedResult;
+ }
+
+ public Object getExpectedResult() {
+ return expectedResult;
+ }
+
+ public String getMethodName() {
+ return methodName;
+ }
+
+ @Override
+ public String toString() {
+ return "Method: '" + methodName + "\', expected result: '" + expectedResult + '\'';
+ }
+ }
+}
diff --git a/testutil/src/main/java/com/yahoo/test/OrderTester.java b/testutil/src/main/java/com/yahoo/test/OrderTester.java
new file mode 100644
index 00000000000..8e001761164
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/OrderTester.java
@@ -0,0 +1,73 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * OrderTester is a an abstract helper class in the spirit of EqualsTester that
+ * tests an objects total or partial ordering with respect to T#compareTo.
+ *
+ * @author Vegard Sjonfjell
+ * @see com.yahoo.test.TotalOrderTester
+ * @see com.yahoo.test.PartialOrderTester
+ *
+ */
+
+public abstract class OrderTester<T extends Comparable<T>> {
+ private ArrayList<List<T>> groups = new ArrayList<>();
+
+ abstract protected void lessTest(T a, T b);
+ abstract protected void greaterTest(T a, T b);
+ abstract protected void equalTest(T a, T b);
+
+ @SafeVarargs
+ private final OrderTester<T> addGroup(T... group) {
+ groups.add(Arrays.asList(group));
+ return this;
+ }
+
+ /**
+ * Add group of objects being "less" (wrt. compareTo) than all the objects which follow.
+ * @param group group of objects
+ * @return the {@link OrderTester} instance, for method chaining
+ */
+ @SafeVarargs
+ public final OrderTester<T> theseObjects(T... group) {
+ return addGroup(group);
+ }
+
+ /**
+ * Add group of objects being "less" (wrt. compareTo) than all the objects which follow.
+ * @param group group of objects
+ * @return the {@link OrderTester} instance, for method chaining
+ */
+ @SafeVarargs
+ public final OrderTester<T> areLessThan(T... group) {
+ return addGroup(group);
+ }
+
+ /**
+ * Test the ordering defined with {@link OrderTester#theseObjects} and {@link OrderTester#areLessThan}
+ * with respect to T#compareTo and the {@link OrderTester} subclass (e.g. {@link com.yahoo.test.TotalOrderTester}).
+ */
+ public void testOrdering() {
+ for (int i = 0; i < groups.size(); i++) {
+ for (T item : groups.get(i)) {
+ for (T otherItem : groups.get(i)) {
+ equalTest(item, otherItem);
+ }
+ }
+
+ for (int j = i+1; j < groups.size(); j++) {
+ for (T lessItem : groups.get(i)) {
+ for (T greaterItem : groups.get(j)) {
+ lessTest(lessItem, greaterItem);
+ greaterTest(greaterItem, lessItem);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/testutil/src/main/java/com/yahoo/test/PartialOrderTester.java b/testutil/src/main/java/com/yahoo/test/PartialOrderTester.java
new file mode 100644
index 00000000000..61ee538a815
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/PartialOrderTester.java
@@ -0,0 +1,29 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+
+/**
+ * PartialOrderTester implements a partial order test for OrderTester
+ *
+ * Usage: see {@link com.yahoo.test.TotalOrderTester}
+ *
+ * @author Vegard Sjonfjell
+ */
+
+public class PartialOrderTester<T extends Comparable<T>> extends OrderTester<T> {
+ protected void lessTest(T a, T b) throws AssertionError {
+ assertThat(a + " must be less than or equal to " + b, a.compareTo(b), lessThanOrEqualTo(0));
+ }
+
+ protected void greaterTest(T a, T b) throws AssertionError {
+ assertThat(a + " must be greater than or equal to " + b, a.compareTo(b), greaterThanOrEqualTo(0));
+ }
+
+ protected void equalTest(T a, T b) throws AssertionError {
+ assertThat(a + " must be compared equal to " + b, a.compareTo(b), is(0));
+ }
+}
diff --git a/testutil/src/main/java/com/yahoo/test/PatternMatcher.java b/testutil/src/main/java/com/yahoo/test/PatternMatcher.java
new file mode 100644
index 00000000000..84cc94e32a2
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/PatternMatcher.java
@@ -0,0 +1,38 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+
+/**
+ * Matches a string against an expected regex pattern.
+ *
+ * @author gjoranv
+ * @since 5.1.7
+ */
+public class PatternMatcher extends BaseMatcher<String> {
+
+ private final String pattern;
+
+ public PatternMatcher(String pattern) {
+ this.pattern = pattern;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("matches expression '" + pattern + "'");
+ }
+
+ @Override
+ public boolean matches(Object o) {
+ return ((String)o).matches(pattern);
+ }
+
+ @Factory
+ public static <T> Matcher<String> matchesPattern(String pattern) {
+ return new PatternMatcher(pattern);
+ }
+
+} \ No newline at end of file
diff --git a/testutil/src/main/java/com/yahoo/test/TotalOrderTester.java b/testutil/src/main/java/com/yahoo/test/TotalOrderTester.java
new file mode 100644
index 00000000000..bc9bb1494fa
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/TotalOrderTester.java
@@ -0,0 +1,37 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+
+/**
+ * TotalOrderTester implements a total order test for OrderTester
+ *
+ * Usage:
+ * <code>
+ * new TotalOrderTester&lt;Integer&gt;()
+ * .theseObjects(3, 3)
+ * .areLessThan(4)
+ * .areLessThan(5)
+ * .areLessThan(6)
+ * .testOrdering();
+ * </code>
+ *
+ * @author Vegard Sjonfjell
+ */
+
+public class TotalOrderTester<T extends Comparable<T>> extends OrderTester<T> {
+ protected void lessTest(T a, T b) throws AssertionError {
+ assertThat(a + " must be less than " + b, a.compareTo(b), lessThanOrEqualTo(-1));
+ }
+
+ protected void greaterTest(T a, T b) throws AssertionError {
+ assertThat(a + " must be greater than " + b, a.compareTo(b), greaterThanOrEqualTo(1));
+ }
+
+ protected void equalTest(T a, T b) throws AssertionError {
+ assertThat(a + " must be compared equal to " + b, a.compareTo(b), is(0));
+ }
+}