1 package com.atlassian.plugin.test;
2
3 import org.dom4j.Element;
4 import org.dom4j.util.NodeComparator;
5 import org.hamcrest.Description;
6 import org.hamcrest.FeatureMatcher;
7 import org.hamcrest.Matcher;
8 import org.hamcrest.TypeSafeMatcher;
9
10 import java.io.File;
11 import java.net.URI;
12 import java.util.Optional;
13 import java.util.stream.Collectors;
14
15 import static java.util.Arrays.spliterator;
16 import static java.util.stream.StreamSupport.stream;
17 import static org.hamcrest.CoreMatchers.allOf;
18 import static org.hamcrest.Matchers.equalTo;
19 import static org.hamcrest.core.Is.is;
20
21 public class Matchers {
22
23
24
25
26
27
28 public static Matcher<File> fileNamed(final String name) {
29 return new TypeSafeMatcher<File>() {
30 private final Matcher<String> nameMatcher = equalTo(name);
31
32 @Override
33 protected boolean matchesSafely(final File item) {
34 return nameMatcher.matches(item.getName());
35 }
36
37 @Override
38 public void describeTo(final Description description) {
39 description.appendText("File named ");
40 nameMatcher.describeTo(description);
41 }
42 };
43 }
44
45
46
47
48
49
50
51 public static Matcher<URI> uriWithPath(final String path) {
52 return new TypeSafeMatcher<URI>() {
53 private final Matcher<String> pathMatcher = equalTo(path);
54
55 @Override
56 protected boolean matchesSafely(final URI item) {
57 File f = new File(item);
58 return pathMatcher.matches(f.getPath());
59 }
60
61 @Override
62 public void describeTo(final Description description) {
63 description.appendText("URI with path ");
64 pathMatcher.describeTo(description);
65 }
66 };
67 }
68
69 @SuppressWarnings("unchecked")
70 static Matcher<String> containsAllStrings(final String... substrings) {
71 Matcher<String>[] matchers = stream(spliterator(substrings), false)
72 .map(org.hamcrest.Matchers::containsString)
73 .toArray(Matcher[]::new);
74 return allOf(matchers);
75 }
76
77
78
79
80
81
82 public static Matcher<Element> isElement(final Element expected) {
83 return new TypeSafeMatcher<Element>() {
84 @Override
85 protected boolean matchesSafely(final Element item) {
86 return new NodeComparator().compare(expected, item) == 0;
87 }
88
89 @Override
90 public void describeTo(final Description description) {
91 description.appendText("is ").appendText(expected.toString());
92 }
93 };
94 }
95
96
97
98
99 public static <T> Matcher<Optional<T>> isPresent() {
100 return new FeatureMatcher<Optional<T>, Boolean>(is(true), "an Optional whose isPresent", "isPresent") {
101 @Override
102 protected Boolean featureValueOf(final Optional<T> optional) {
103 return optional.isPresent();
104 }
105 };
106 }
107
108
109
110
111 public static <T> Matcher<Optional<T>> notPresent() {
112 return new FeatureMatcher<Optional<T>, Boolean>(is(false), "an Optional whose isPresent", "isPresent") {
113 @Override
114 protected Boolean featureValueOf(final Optional<T> optional) {
115 return optional.isPresent();
116 }
117 };
118 }
119
120
121
122
123 public static <T> Matcher<? super Optional<T>> presentWithValue(final Matcher<T> valueMatcher) {
124 return new TypeSafeMatcher<Optional<T>>() {
125 @Override
126 protected boolean matchesSafely(final Optional<T> optional) {
127 return optional.isPresent() && valueMatcher.matches(optional.get());
128 }
129
130 @Override
131 public void describeTo(final Description description) {
132 description.appendText("a present Optional whose value ");
133 description.appendDescriptionOf(valueMatcher);
134 }
135 };
136 }
137 }