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 { - public MapMatcher(final Map value) { +/** + * Matcher class to validate {@link Map}s + */ +public class MapMatcher extends ObjectMatcher> { + + public MapMatcher(final Map value) { super(value); - Arguments.ensureNotNull(value, "Map cannot be null"); } + /** + * Checks if the stored {@code Map} is empty. + *

This method throws an {@code AssertionError} if + *

    + *
  • the stored {@code Map} is not empty
  • + *
  • the stored {@code Map} 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 stored {@code Map} is not empty. + *

This method throws an {@code AssertionError} if + *

    + *
  • the stored {@code Map} is empty
  • + *
  • the stored {@code Map} 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 toContainKey(final Object key) { + /** + * Checks if the stored {@code Map} contains the given {@code key}. + *

This method throws an {@code AssertionError} if + *

    + *
  • the stored {@code Map} does not contain the given {@code key}
  • + *
  • the stored {@code Map} is {@code null}
  • + *
+ */ + public void toContainKey(final K key) { + expectNotNull(this.getValue(), "Expected null to contain the key '%s'", key); expectTrue(this.getValue().containsKey(key), "Expected '%s' to contain key '%s'", this.getValue(), key); } - public void toNotContainKey(final Object key) { + /** + * Checks if the stored {@code Map} does not contain the given {@code key}. + *

This method throws an {@code AssertionError} if + *

    + *
  • the stored {@code Map} contains the given {@code key}
  • + *
  • the stored {@code Map} is {@code null}
  • + *
+ */ + public void toNotContainKey(final K key) { + expectNotNull(this.getValue(), "Expected null to not contain the key '%s'", key); expectTrue(!this.getValue().containsKey(key), "Expected '%s' to not contain key '%s'", this.getValue(), key); } - public void toContainValue(final Object value) { + /** + * Checks if the stored {@code Map} contains the given {@code value}. + *

This method throws an {@code AssertionError} if + *

    + *
  • the stored {@code Map} does not contain the given {@code value}
  • + *
  • the stored {@code Map} is {@code null}
  • + *
+ */ + public void toContainValue(final V value) { + expectNotNull(this.getValue(), "Expected null to contain the value '%s'", value); expectTrue(this.getValue().containsValue(value), "Expected '%s' to contain value '%s'", this.getValue(), value); } - public void toNotContainValue(final Object value) { + /** + * Checks if the stored {@code Map} does not contain the given {@code value}. + *

This method throws an {@code AssertionError} if + *

    + *
  • the stored {@code Map} does not contain the given {@code value}
  • + *
  • the stored {@code Map} is {@code null}
  • + *
+ */ + public void toNotContainValue(final V value) { + expectNotNull(this.getValue(), "Expected null to not contain the value '%s'", value); expectTrue(!this.getValue().containsValue(value), "Expected '%s' to not contain value '%s'", this.getValue(), value); } - 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 stored {@code Map} has a given {@code size}. + *

This method throws an {@code AssertionError} if + *

    + *
  • the stored {@code Map} does not have the given {@code size}
  • + *
  • the stored {@code Map} is {@code null}
  • + *
+ */ + public void toHaveSize(final int size) { + expectNotNull(this.getValue(), "Expected null to have size '%s'", 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/test/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcherTest.java b/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcherTest.java index a3e83f1..6f38c87 100644 --- a/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcherTest.java +++ b/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcherTest.java @@ -27,6 +27,11 @@ public class CollectionMatcherTest {{ "Expected '[one, two]' to contain 'four'"); }); + it("fails if the stored collection is null", () -> { + expectAssertionError(() -> new CollectionMatcher(null).toContain("one"), + "Expected null to contain 'one'"); + }); + it("is ok if the stored collection contains the expected value", () -> { new CollectionMatcher(list).toContain("one"); new CollectionMatcher(set).toContain("two"); @@ -34,13 +39,18 @@ public class CollectionMatcherTest {{ }); describe("when toNotContain() is called", () -> { - it("fails if hte stored collection does contain the expected value", () -> { + it("fails if the stored collection does contain the expected value", () -> { expectAssertionError(() -> new CollectionMatcher(list).toNotContain("one"), "Expected '[one, two]' to not contain 'one'"); expectAssertionError(() -> new CollectionMatcher(set).toNotContain("one"), "Expected '[one, two]' to not contain 'one'"); }); + it("fails if the stored collection is null", () -> { + expectAssertionError(() -> new CollectionMatcher(null).toNotContain("one"), + "Expected null to not contain 'one'"); + }); + it("is ok if the stored collection not contains the expected value", () -> { new CollectionMatcher(list).toNotContain("three"); new CollectionMatcher(set).toNotContain("three"); @@ -55,6 +65,11 @@ public class CollectionMatcherTest {{ "Expected '[one, two]' to be empty"); }); + it("fails if the stored collection is null", () -> { + expectAssertionError(() -> new CollectionMatcher(null).toBeEmpty(), + "Expected null to be empty"); + }); + it("is ok if the stored collection is empty", () -> { new CollectionMatcher(new LinkedList()).toBeEmpty(); new CollectionMatcher(new HashSet()).toBeEmpty(); @@ -69,23 +84,32 @@ public class CollectionMatcherTest {{ "Expected '[]' to not be empty"); }); + it("fails if the stored collection is null", () -> { + expectAssertionError(() -> new CollectionMatcher(null).toNotBeEmpty(), + "Expected null to not be empty"); + }); + it("is ok if the stored collection is not empty", () -> { new CollectionMatcher(list).toNotBeEmpty(); new CollectionMatcher(set).toNotBeEmpty(); }); }); - describe("when toHaveLength() is called", () -> { - it("fails if the stored collection does not have the provided length", () -> { - expectAssertionError(() -> new CollectionMatcher(list).toHaveLength(1), - "Expected '[one, two]' to have a length of 1, instead has a length of 2"); - expectAssertionError(() -> new CollectionMatcher(set).toHaveLength(-1), - "Expected '[one, two]' to have a length of -1, instead has a length of 2"); + describe("when toHaveSize() is called", () -> { + it("fails if the stored collection does not have the provided size", () -> { + expectAssertionError(() -> new CollectionMatcher(list).toHaveSize(1), + "Expected '[one, two]' to have a size of 1, instead has a size of 2"); + expectAssertionError(() -> new CollectionMatcher(set).toHaveSize(-1), + "Expected '[one, two]' to have a size of -1, instead has a size of 2"); + }); + + it("fails if the stored collection is null", () -> { + expectAssertionError(() -> new CollectionMatcher(null).toHaveSize(1)); }); - it("is ok if hte stored collection has the provided length", () -> { - new CollectionMatcher(list).toHaveLength(2); - new CollectionMatcher(set).toHaveLength(2); + it("is ok if hte stored collection has the provided size", () -> { + new CollectionMatcher(list).toHaveSize(2); + new CollectionMatcher(set).toHaveSize(2); }); }); }); diff --git a/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/MapMatcherTest.java b/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/MapMatcherTest.java index 38d43d6..6a098ef 100644 --- a/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/MapMatcherTest.java +++ b/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/MapMatcherTest.java @@ -13,84 +13,120 @@ @RunWith(OleasterRunner.class) public class MapMatcherTest {{ describe("MapMatcher test", () -> { + final Map map = new HashMap<>(); map.put("one", 1); map.put("two", 2); describe("when toBeEmpty() is called", () -> { it("fails if the stored map is not empty", () -> { - expectAssertionError(() -> new MapMatcher(map).toBeEmpty(), + expectAssertionError(() -> new MapMatcher<>(map).toBeEmpty(), "Expected '{one=1, two=2}' to be empty"); }); + it("fails if the stored map is null", () -> { + expectAssertionError(() -> new MapMatcher<>(null).toBeEmpty(), + "Expected null to be empty"); + }); + it("is ok if the stored map is empty", () -> { - new MapMatcher(new HashMap()).toBeEmpty(); + new MapMatcher<>(new HashMap()).toBeEmpty(); }); }); describe("when toNotBeEmpty() is called", () -> { it("fails if the stored map is empty", () -> { - expectAssertionError(() -> new MapMatcher(new HashMap()).toNotBeEmpty(), + expectAssertionError(() -> new MapMatcher<>(new HashMap()).toNotBeEmpty(), "Expected '{}' to not be empty"); }); + it("fails if the stored map is null", () -> { + expectAssertionError(() -> new MapMatcher<>(null).toNotBeEmpty(), + "Expected null to not be empty"); + }); + it("is ok if the stored map is not empty", () -> { - new MapMatcher(map).toNotBeEmpty(); + new MapMatcher<>(map).toNotBeEmpty(); }); }); describe("when toContainKey() is called", () -> { it("fails if the stored map does not contain the expected key", () -> { - expectAssertionError(() -> new MapMatcher(map).toContainKey("three"), + expectAssertionError(() -> new MapMatcher<>(map).toContainKey("three"), "Expected '{one=1, two=2}' to contain key 'three'"); }); + it("fails if the stored map is null", () -> { + expectAssertionError(() -> new MapMatcher<>(null).toContainKey("one"), + "Expected null to contain the key 'one'"); + }); + it("is ok if the stored map contains the expected key", () -> { - new MapMatcher(map).toContainKey("one"); + new MapMatcher<>(map).toContainKey("one"); }); }); describe("when toNotContainKey() is called", () -> { it("fails if the stored map does not contain the expected key", () -> { - expectAssertionError(() -> new MapMatcher(map).toNotContainKey("one"), + expectAssertionError(() -> new MapMatcher<>(map).toNotContainKey("one"), "Expected '{one=1, two=2}' to not contain key 'one'"); }); + it("fails if the stored map is null", () -> { + expectAssertionError(() -> new MapMatcher<>(null).toNotContainKey("one"), + "Expected null to not contain the key 'one'"); + }); + it("is ok if the stored map contains the expected key", () -> { - new MapMatcher(map).toNotContainKey("three"); + new MapMatcher<>(map).toNotContainKey("three"); }); }); describe("when toContainValue() is called", () -> { it("fails if the stored map does not contain the expected value", () -> { - expectAssertionError(() -> new MapMatcher(map).toContainValue(3), + expectAssertionError(() -> new MapMatcher<>(map).toContainValue(3), "Expected '{one=1, two=2}' to contain value '3'"); }); + it("fails if the stored map is null", () -> { + expectAssertionError(() -> new MapMatcher<>(null).toContainValue(1), + "Expected null to contain the value '1'"); + }); + it("is ok if the stored map contains the expected value", () -> { - new MapMatcher(map).toContainValue(2); + new MapMatcher<>(map).toContainValue(2); }); }); describe("when toNotContainValue() is called", () -> { it("fails if the stored value does not contain the expected value", () -> { - expectAssertionError(() -> new MapMatcher(map).toNotContainValue(1), + expectAssertionError(() -> new MapMatcher<>(map).toNotContainValue(1), "Expected '{one=1, two=2}' to not contain value '1'"); }); + it("fails if the stored map is null", () -> { + expectAssertionError(() -> new MapMatcher<>(null).toNotContainValue(1), + "Expected null to not contain the value '1'"); + }); + it("is ok if the stored value contains the expected value", () -> { - new MapMatcher(map).toNotContainValue(3); + new MapMatcher<>(map).toNotContainValue(3); }); }); - describe("when toHaveLength() is called", () -> { - it("fails if the stored value does not have the provided length", () -> { - expectAssertionError(() -> new MapMatcher(map).toHaveLength(3), - "Expected '{one=1, two=2}' to have a length of 3, instead has a length of 2"); + describe("when toHaveSize() is called", () -> { + it("fails if the stored value does not have the provided size", () -> { + expectAssertionError(() -> new MapMatcher<>(map).toHaveSize(3), + "Expected '{one=1, two=2}' to have a size of 3, instead has a size of 2"); + }); + + it("fails if the stored map is null", () -> { + expectAssertionError(() -> new MapMatcher<>(null).toHaveSize(1), + "Expected null to have size '1'"); }); - it("is ok if the stored value does have the provided length", () -> { - new MapMatcher(map).toHaveLength(2); + it("is ok if the stored value does have the provided size", () -> { + new MapMatcher<>(map).toHaveSize(2); }); });