diff --git a/oleaster-matcher/README.md b/oleaster-matcher/README.md
index 5d6e7a8..a746b15 100644
--- a/oleaster-matcher/README.md
+++ b/oleaster-matcher/README.md
@@ -136,6 +136,42 @@ expect("foobar").toMatch("fo+\\w*");
For comparing Strings [StringMatcher](https://github.com/mscharhag/oleaster/blob/master/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/StringMatcher.java) will be used.
+## Collections
+
+```
+// check if a collection contains a specific item
+expect(myList).toContain("my item");
+expect(mySet).toNotContain(42);
+
+// check the size of a collection
+expect(myList).toBeEmpty();
+expect(mySet).toHaveSize(15);
+```
+
+### Maps
+
+```
+// check if a map contains a specific key
+expect(myMap).toContainKey("key");
+
+// check if a map contains a specific value
+expect(myMap).toContainValue("value");
+```
+
+## Date and Time
+
+```
+// check if a date is before or after another date
+expect(date).toBeAfter(otherDate);
+expect(date).toBeBefore(otherDate);
+
+// check if a date is between two other dates
+expect(date).toBeBetween(firstDate, otherDate);
+
+// check if a date is close to another date
+expect(date).toBeCloseTo(otherDate, deltaInMillis );
+```
+
## Exceptions
To test exceptions you just have to wrap the code that throws the expected exception into a lambda expression and pass
it to `expect()`. The lambda expression will be executed and thrown exceptions will be caught.
diff --git a/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/Matchers.java b/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/Matchers.java
index 0350e78..d60c323 100644
--- a/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/Matchers.java
+++ b/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/Matchers.java
@@ -210,15 +210,16 @@ public static CollectionMatcher expect(Collection value) {
*
The returned matcher can be used to check {@code Map} values.
*
* expect(map).toContainValue("foo");
- * expect(map).toHaveLength(3);
+ * expect(map).toHaveSize(3);
*
* @see com.mscharhag.oleaster.matcher.matchers.MapMatcher
* @param value the value that should be checked
* @return a new {@code MapMatcher} initialized with {@code value}
*/
- public static MapMatcher expect(Map value) {
- return new MapMatcher(value);
+ public static MapMatcher expect(Map value) {
+ return new MapMatcher<>(value);
}
+
/**
* Creates a new {@link com.mscharhag.oleaster.matcher.matchers.ObjectMatcher} initialized with
* the passed {@code value}.
diff --git a/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcher.java b/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcher.java
index 6d916e6..2e679d1 100644
--- a/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcher.java
+++ b/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcher.java
@@ -4,15 +4,16 @@
import java.util.Collection;
+import static com.mscharhag.oleaster.matcher.util.Expectations.expectNotNull;
import static com.mscharhag.oleaster.matcher.util.Expectations.expectTrue;
/**
* Matcher class to validate Collections
*/
public class CollectionMatcher extends ObjectMatcher {
+
public CollectionMatcher(final Collection value) {
super(value);
- Arguments.ensureNotNull(value, "Collection cannot be null");
}
/**
@@ -20,27 +21,71 @@ public CollectionMatcher(final Collection value) {
* This method throws an {@code AssertionError} if
*
* - the stored {@code Collection} does not contain the passed {@code item}
+ * - the given {@code Collection} is {@code null}
*
*
* @param item the {@code Object} which the {@code Collection} should contain.
*/
public void toContain(final Object item) {
+ expectNotNull(this.getValue(), "Expected null to contain '%s'", item);
expectTrue(this.getValue().contains(item), "Expected '%s' to contain '%s'", this.getValue(), item);
}
+ /**
+ * Checks if the stored {@code Collection} does not contain the provided item.
+ * This method throws an {@code AssertionError} if
+ *
+ * - the stored {@code Collection} does contain the passed {@code item}
+ * - the given {@code Collection} is {@code null}
+ *
+ *
+ * @param item the {@code Object} which the {@code Collection} should not contain.
+ */
public void toNotContain(final Object item) {
+ expectNotNull(this.getValue(), "Expected null to not contain '%s'", item);
expectTrue(!this.getValue().contains(item), "Expected '%s' to not contain '%s'", this.getValue(), item);
}
+ /**
+ * Checks if the given {@code Collection} is empty
+ * This method throws an {@code AssertionError} if
+ *
+ * - the stored {@code Collection} is not empty
+ * - the given {@code Collection} is {@code null}
+ *
+ */
public void toBeEmpty() {
- expectTrue(this.getValue().size() == 0, "Expected '%s' to be empty", this.getValue());
+ expectNotNull(this.getValue(), "Expected null to be empty");
+ expectTrue(this.getValue().isEmpty(), "Expected '%s' to be empty", this.getValue());
}
+ /**
+ * Checks if the given {@code Collection} is not empty
+ * This method throws an {@code AssertionError} if
+ *
+ * - the stored {@code Collection} is empty
+ * - the given {@code Collection} is {@code null}
+ *
+ */
public void toNotBeEmpty() {
- expectTrue(this.getValue().size() > 0, "Expected '%s' to not be empty", this.getValue());
+ expectNotNull(this.getValue(), "Expected null to not be empty");
+ expectTrue(!this.getValue().isEmpty(), "Expected '%s' to not be empty", this.getValue());
}
- public void toHaveLength(final int length) {
- expectTrue(this.getValue().size() == length, "Expected '%s' to have a length of %d, instead has a length of %d", this.getValue(), length, this.getValue().size());
+ /**
+ * Checks if the given {@code Collection} has the expected size
+ * This method throws an {@code AssertionError} if
+ *
+ * - the stored {@code Collection} has a different size than the passed value
+ * - the stored {@code Collection} is {@code null}
+ *
+ *
+ *
+ * @param size The expected size of the collection
+ */
+ public void toHaveSize(final int size) {
+ expectNotNull(this.getValue(), "Expected null to have size '%d'", size);
+ expectTrue(this.getValue().size() == size, "Expected '%s' to have a size of %d, " +
+ "instead has a size of %d", this.getValue(), size, this.getValue().size());
}
}
diff --git a/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/MapMatcher.java b/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/MapMatcher.java
index 10820db..976eac3 100644
--- a/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/MapMatcher.java
+++ b/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/MapMatcher.java
@@ -4,39 +4,106 @@
import com.mscharhag.oleaster.matcher.util.Arguments;
+import static com.mscharhag.oleaster.matcher.util.Expectations.expectNotNull;
import static com.mscharhag.oleaster.matcher.util.Expectations.expectTrue;
-public class MapMatcher extends ObjectMatcher