]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/CaseInsensitiveStringCollectionMatcher.java
Fix dst18-vagrantfile to download the box by itself
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / CaseInsensitiveStringCollectionMatcher.java
1 package dst.ass1.jpa;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.hamcrest.Description;
7 import org.hamcrest.Factory;
8 import org.hamcrest.TypeSafeDiagnosingMatcher;
9
10 /**
11  * Matcher that finds items in string collections in a case-insensitive way.
12  */
13 public class CaseInsensitiveStringCollectionMatcher extends TypeSafeDiagnosingMatcher<List<String>> {
14
15     private final String[] items;
16
17     public CaseInsensitiveStringCollectionMatcher(String... items) {
18         this.items = items;
19     }
20
21     @Override
22     protected boolean matchesSafely(List<String> collection, Description description) {
23         List<String> missing = new ArrayList<>();
24
25         for (String item : items) {
26             if (collection.stream().noneMatch(i -> i.equalsIgnoreCase(item))) {
27                 missing.add(item);
28             }
29         }
30
31         if (!missing.isEmpty()) {
32             if (missing.size() == items.length) {
33                 description.appendValueList("was [", ", ", "]", collection);
34             } else {
35                 description.appendValueList("missing [", ", ", "]", missing)
36                         .appendValueList(" in [", ", ", "]", collection);
37             }
38         }
39
40         return missing.isEmpty();
41     }
42
43     @Override
44     public void describeTo(Description description) {
45         description.appendValueList("collection containing ", " and ", "", items);
46     }
47
48     @Factory
49     public static CaseInsensitiveStringCollectionMatcher hasItems(String... items) {
50         return new CaseInsensitiveStringCollectionMatcher(items);
51     }
52 }