diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-08-12 10:34:39 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-08-12 10:34:39 +0000 |
commit | 38285e0e1147042b519957958083e537e6d90fc9 (patch) | |
tree | 317bcc846acb2bc8365ca2aedb76d9066152de40 /yolean | |
parent | 104f7547cb92ab39251308f15ddc45d515a48ae2 (diff) |
Minor unification of tests.
Diffstat (limited to 'yolean')
4 files changed, 44 insertions, 101 deletions
diff --git a/yolean/pom.xml b/yolean/pom.xml index 742142fe167..f994d401c60 100644 --- a/yolean/pom.xml +++ b/yolean/pom.xml @@ -19,16 +19,6 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-library</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-core</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> diff --git a/yolean/src/test/java/com/yahoo/yolean/chain/ChainBuilderTest.java b/yolean/src/test/java/com/yahoo/yolean/chain/ChainBuilderTest.java index 70bb0ab1397..84467283398 100644 --- a/yolean/src/test/java/com/yahoo/yolean/chain/ChainBuilderTest.java +++ b/yolean/src/test/java/com/yahoo/yolean/chain/ChainBuilderTest.java @@ -4,13 +4,13 @@ package com.yahoo.yolean.chain; import org.junit.Test; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import static com.yahoo.yolean.chain.Dependencies.after; import static com.yahoo.yolean.chain.Dependencies.before; import static com.yahoo.yolean.chain.Dependencies.provides; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.collection.IsIterableContainingInOrder.contains; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -103,6 +103,13 @@ public class ChainBuilderTest { assertEquals("myChain", chain.id()); } + boolean equalOrder(Iterator<Filter> a, Iterator<Filter> b) { + while (a.hasNext() && b.hasNext()) { + if ( ! a.next().equals(b.next())) return false; + } + return a.hasNext() == b.hasNext(); + } + @Test public void filters_without_dependencies_are_not_reordered() { List<Filter> filters = new ArrayList<>(); @@ -114,7 +121,7 @@ public class ChainBuilderTest { chain.add(filter); } - assertThat(chain.build(), contains(filters.toArray())); + assertTrue(equalOrder(chain.build().iterator(), filters.iterator())); } @Test(expected = ChainCycleException.class) diff --git a/yolean/src/test/java/com/yahoo/yolean/chain/ContainsSameElements.java b/yolean/src/test/java/com/yahoo/yolean/chain/ContainsSameElements.java deleted file mode 100644 index d3fddeafbb3..00000000000 --- a/yolean/src/test/java/com/yahoo/yolean/chain/ContainsSameElements.java +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.yolean.chain; - -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeMatcher; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.IdentityHashMap; -import java.util.List; -import java.util.Set; - -import static java.util.Collections.sort; - -/** - * @author Tony Vaagenes - */ -class ContainsSameElements<T> extends TypeSafeMatcher<Collection<? super T>> { - - private final Set<T> identitySet; - - public static <T> Matcher<Collection<? super T>> containsSameElements(Collection<T> collection) { - return new ContainsSameElements<>(collection); - } - - public ContainsSameElements(Collection<T> collection) { - identitySet = toIdentitySet(collection); - } - - @SuppressWarnings("SuspiciousMethodCalls") - @Override - protected boolean matchesSafely(Collection<? super T> collection2) { - for (Object elem : collection2) { - if (!identitySet.contains(elem)) { - return false; - } - } - - return collection2.size() == identitySet.size(); - } - - @Override - public void describeTo(Description description) { - description.appendText("containsSameElements "); - appendCollection(description, identitySet); - } - - private void appendCollection(Description description, Collection<?> collection) { - description.appendValueList("{", ", ", "}", elementsToStringSorted(collection)); - } - - private List<String> elementsToStringSorted(Collection<?> collection) { - List<String> result = new ArrayList<>(); - for (Object o : collection) { - result.add(o.toString()); - } - sort(result); - return result; - } - - @Override - protected void describeMismatchSafely(Collection<? super T> collection, Description description) { - description.appendText("was "); - appendCollection(description, collection); - } - - public static <T> Set<T> toIdentitySet(Collection<? extends T> collection) { - Set<T> identitySet = Collections.newSetFromMap(new IdentityHashMap<T, Boolean>()); - identitySet.addAll(collection); - return identitySet; - } -} diff --git a/yolean/src/test/java/com/yahoo/yolean/chain/EnumeratedIdentitySetTest.java b/yolean/src/test/java/com/yahoo/yolean/chain/EnumeratedIdentitySetTest.java index 5f0692e186f..30951f88162 100644 --- a/yolean/src/test/java/com/yahoo/yolean/chain/EnumeratedIdentitySetTest.java +++ b/yolean/src/test/java/com/yahoo/yolean/chain/EnumeratedIdentitySetTest.java @@ -5,17 +5,14 @@ import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; import java.util.Set; -import static com.yahoo.yolean.chain.ContainsSameElements.containsSameElements; -import static com.yahoo.yolean.chain.ContainsSameElements.toIdentitySet; import static java.util.Collections.singleton; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; @@ -81,6 +78,28 @@ public class EnumeratedIdentitySetTest { assertTrue(collectedElements.containsKey(element)); } } + private static boolean containsSame(Object a, Collection<?> coll) { + for (Object b : coll) { + if (a == b) return true; + } + return false; + } + private static boolean containsSubsetSame(Collection<?> subSet, Collection<?> superSet) { + for (Object a : subSet) { + if ( ! containsSame(a, superSet)) return false; + } + return true; + } + + private static boolean containsAllSame(Collection<?> a, Collection<?> b) { + return containsSubsetSame(a, b) && containsSubsetSame(b, a); + } + + private static <T> Set<T> toIdentitySet(Collection<? extends T> collection) { + Set<T> identitySet = Collections.newSetFromMap(new IdentityHashMap<>()); + identitySet.addAll(collection); + return identitySet; + } @Test public void toArray() { @@ -89,8 +108,9 @@ public class EnumeratedIdentitySetTest { Object[] array = set.toArray(); Element[] array2 = set.toArray(new Element[0]); - assertThat(Arrays.asList(array), containsSameElements(set)); - assertThat(Arrays.asList(array2), containsSameElements(set)); + assertTrue(set.containsAll(Arrays.asList(array))); + assertTrue(containsAllSame(Arrays.asList(array), set)); + assertTrue(containsAllSame(Arrays.asList(array2), set)); } @Test @@ -123,7 +143,7 @@ public class EnumeratedIdentitySetTest { EnumeratedIdentitySet<Element> set = new EnumeratedIdentitySet<>(); set.addAll(elements); - assertThat(set, containsSameElements(elements)); + assertTrue(containsAllSame(set, elements)); } @Test @@ -134,7 +154,7 @@ public class EnumeratedIdentitySetTest { boolean changed = set.retainAll(toIdentitySet(elements.subList(3, 10))); assertTrue(changed); - assertThat(set, containsSameElements(elements.subList(3, 5))); + assertTrue(containsAllSame(set, elements.subList(3, 5))); changed = set.retainAll(toIdentitySet(elements)); assertFalse(changed); @@ -144,7 +164,7 @@ public class EnumeratedIdentitySetTest { public void removeAll() { EnumeratedIdentitySet<Element> set = new EnumeratedIdentitySet<>(elements); set.removeAll(elements.subList(0, 5)); - assertThat(set, containsSameElements(elements.subList(5, 10))); + assertTrue(containsAllSame(set, elements.subList(5, 10))); } @Test @@ -157,8 +177,8 @@ public class EnumeratedIdentitySetTest { @Test public void removeNulls() { Element[] singletonArray = { null, elements.get(0), null }; - assertThat(EnumeratedIdentitySet.removeNulls(singletonArray), - containsSameElements(Arrays.asList(elements.get(0)))); + assertTrue(containsAllSame(EnumeratedIdentitySet.removeNulls(singletonArray), + Arrays.asList(elements.get(0)))); Element[] elementsWithNull = new Element[20]; @@ -168,7 +188,7 @@ public class EnumeratedIdentitySetTest { copyElementsTo(iterator, elementsWithNull, 4, 8); copyElementsTo(iterator, elementsWithNull, 19, 1); - assertThat(EnumeratedIdentitySet.removeNulls(elementsWithNull), containsSameElements(elements)); + assertTrue(containsAllSame(EnumeratedIdentitySet.removeNulls(elementsWithNull), elements)); } private void copyElementsTo(Iterator<Element> iterator, Element[] array, int startIndex, int numItems) { @@ -203,7 +223,7 @@ public class EnumeratedIdentitySetTest { } set.add(new Element()); - assertThat(set.numbers(), containsSameElements(range(0, 10))); + assertTrue(containsAllSame(set.numbers(), range(0, 10))); } @Test @@ -218,7 +238,7 @@ public class EnumeratedIdentitySetTest { assertTrue(set.numbers().isEmpty()); set.add(new Element()); - assertThat(set.numbers(), containsSameElements(singleton(0))); + assertTrue(containsAllSame(set.numbers(), singleton(0))); } private List<Integer> range(int start, int endInclusive) { |