View Javadoc
1   package dev.aherscu.qa.testing.utils;
2   
3   import java.util.Optional;
4   import java.util.OptionalDouble;
5   import java.util.OptionalInt;
6   import java.util.OptionalLong;
7   
8   import org.hamcrest.Description;
9   import org.hamcrest.Matcher;
10  import org.hamcrest.TypeSafeMatcher;
11  
12  public class OptionalMatchers {
13  
14      /**
15       * Matches a not present Optional.
16       */
17      public static <T> Matcher<Optional<T>> notPresent() {
18          return new TypeSafeMatcher<Optional<T>>() {
19              @Override
20              protected boolean matchesSafely(Optional<T> item) {
21                  return !item.isPresent();
22              }
23  
24              @Override
25              public void describeTo(Description description) {
26                  description.appendText("An Optional with no value");
27              }
28          };
29      }
30  
31      /**
32       * Matches a not present Optional.
33       *
34       * @deprecated name clashes with {@link org.hamcrest.Matchers#empty()}, use
35       *             {@link #notPresent()} instead
36       */
37      @Deprecated
38      public static <T> Matcher<Optional<T>> empty() {
39          return notPresent();
40      }
41  
42      /**
43       * Matches a present Optional with the given content
44       *
45       * @param content
46       *            Expected contents of the Optional
47       * @param <T>
48       *            The type of the Optional's content
49       */
50      public static <T> Matcher<Optional<T>> present(T content) {
51          return new TypeSafeMatcher<Optional<T>>() {
52              @Override
53              protected boolean matchesSafely(Optional<T> item) {
54                  return item.map(content::equals).orElse(false);
55              }
56  
57              @Override
58              public void describeTo(Description description) {
59                  description.appendText(Optional.of(content).toString());
60              }
61          };
62      }
63  
64      /**
65       * Matches a present Optional with the given content
66       *
67       * @param content
68       *            Expected contents of the Optional
69       * @param <T>
70       *            The type of the Optional's content
71       *
72       * @deprecated name clashes with
73       *             {@link org.hamcrest.Matchers#contains(Object...)}, use
74       *             {@link #present(Object)} instead
75       */
76      @Deprecated
77      public static <T> Matcher<Optional<T>> contains(T content) {
78          return present(content);
79      }
80  
81      /**
82       * Matches a present Optional with content matching the given matcher
83       *
84       * @param matcher
85       *            To match against the Optional's content
86       * @param <T>
87       *            The type of the Optional's content
88       * @param <S>
89       *            The type matched by the matcher, a subtype of T
90       */
91      public static <T, S extends T> Matcher<Optional<S>> present(
92          Matcher<T> matcher) {
93          return new TypeSafeMatcher<Optional<S>>() {
94              @Override
95              protected boolean matchesSafely(Optional<S> item) {
96                  return item.map(matcher::matches).orElse(false);
97              }
98  
99              @Override
100             public void describeTo(Description description) {
101                 description.appendText(
102                     "Optional with an item that matches " + matcher);
103             }
104         };
105     }
106 
107     /**
108      * Matches a present Optional with content matching the given matcher
109      *
110      * @param matcher
111      *            To match against the Optional's content
112      * @param <T>
113      *            The type of the Optional's content
114      * @param <S>
115      *            The type matched by the matcher, a subtype of T
116      * @deprecated name clashes with
117      *             {@link org.hamcrest.Matchers#contains(Matcher)}, use
118      *             {@link #present(Matcher)} instead
119      */
120     @Deprecated
121     public static <T, S extends T> Matcher<Optional<S>> contains(
122         Matcher<T> matcher) {
123         return present(matcher);
124     }
125 
126     /**
127      * Matches an not present OptionalInt.
128      */
129     public static Matcher<OptionalInt> notPresentInt() {
130         return new TypeSafeMatcher<OptionalInt>() {
131             @Override
132             protected boolean matchesSafely(OptionalInt item) {
133                 return !item.isPresent();
134             }
135 
136             @Override
137             public void describeTo(Description description) {
138                 description.appendText("An OptionalInt with no value");
139             }
140         };
141     }
142 
143     /**
144      * Matches a not present OptionalInt.
145      *
146      * @deprecated Matcher is replaced with {@link #notPresentInt()} to align
147      *             with naming of {@link #notPresent()}.
148      */
149     @Deprecated
150     public static Matcher<OptionalInt> emptyInt() {
151         return notPresentInt();
152     }
153 
154     /**
155      * Matches a present OptionalInt with the given content
156      *
157      * @param content
158      *            Expected contents of the Optional
159      */
160     public static Matcher<OptionalInt> presentInt(int content) {
161         return new TypeSafeMatcher<OptionalInt>() {
162             @Override
163             protected boolean matchesSafely(OptionalInt item) {
164                 return item.isPresent() && item.getAsInt() == content;
165             }
166 
167             @Override
168             public void describeTo(Description description) {
169                 description.appendText(Optional.of(content).toString());
170             }
171         };
172     }
173 
174     /**
175      * Matches a present OptionalInt with the given content
176      *
177      * @param content
178      *            Expected contents of the Optional
179      *
180      * @deprecated Matcher is replaced with {@link #presentInt(int)} to align
181      *             with naming of {@link #present(Object)}.
182      */
183     @Deprecated
184     public static Matcher<OptionalInt> containsInt(int content) {
185         return presentInt(content);
186     }
187 
188     /**
189      * Matches a present OptionalInt with content matching the given matcher
190      *
191      * @param matcher
192      *            To match against the OptionalInt's content
193      */
194     public static Matcher<OptionalInt> presentInt(Matcher<Integer> matcher) {
195         return new TypeSafeMatcher<OptionalInt>() {
196             @Override
197             protected boolean matchesSafely(OptionalInt item) {
198                 return item.isPresent() && matcher.matches(item.getAsInt());
199             }
200 
201             @Override
202             public void describeTo(Description description) {
203                 description.appendText(
204                     "OptionalInt with an item that matches " + matcher);
205             }
206         };
207     }
208 
209     /**
210      * Matches a present OptionalInt with content matching the given matcher
211      *
212      * @param matcher
213      *            To match against the OptionalInt's content
214      *
215      * @deprecated Matcher is replaced with {@link #presentInt(Matcher)} to
216      *             align with naming of {@link #present(Matcher)}.
217      */
218     @Deprecated
219     public static Matcher<OptionalInt> containsInt(Matcher<Integer> matcher) {
220         return presentInt(matcher);
221     }
222 
223     /**
224      * Matches an OptionalLong with no value.
225      */
226     public static Matcher<OptionalLong> notPresentLong() {
227         return new TypeSafeMatcher<OptionalLong>() {
228             @Override
229             protected boolean matchesSafely(OptionalLong item) {
230                 return !item.isPresent();
231             }
232 
233             @Override
234             public void describeTo(Description description) {
235                 description.appendText("An OptionalLong with no value");
236             }
237         };
238     }
239 
240     /**
241      * Matches a not present OptionalLong.
242      *
243      * @deprecated Matcher is replaced with {@link #notPresentLong()} to align
244      *             with naming of {@link #notPresent()}.
245      */
246     @Deprecated
247     public static Matcher<OptionalLong> emptyLong() {
248         return notPresentLong();
249     }
250 
251     /**
252      * Matches a present OptionalLong with the given content
253      *
254      * @param content
255      *            Expected contents of the Optional
256      */
257     public static Matcher<OptionalLong> presentLong(long content) {
258         return new TypeSafeMatcher<OptionalLong>() {
259             @Override
260             protected boolean matchesSafely(OptionalLong item) {
261                 return item.isPresent() && item.getAsLong() == content;
262             }
263 
264             @Override
265             public void describeTo(Description description) {
266                 description.appendText(Optional.of(content).toString());
267             }
268         };
269     }
270 
271     /**
272      * Matches a present OptionalLong with the given content
273      *
274      * @param content
275      *            Expected contents of the Optional
276      *
277      * @deprecated Matcher is replaced with {@link #presentLong(long)} to align
278      *             with naming of {@link #present(Object)}.
279      */
280     @Deprecated
281     public static Matcher<OptionalLong> containsLong(long content) {
282         return presentLong(content);
283     }
284 
285     /**
286      * Matches a present OptionalLong with content matching the given matcher
287      *
288      * @param matcher
289      *            To match against the OptionalLong's content
290      */
291     public static Matcher<OptionalLong> presentLong(Matcher<Long> matcher) {
292         return new TypeSafeMatcher<OptionalLong>() {
293             @Override
294             protected boolean matchesSafely(OptionalLong item) {
295                 return item.isPresent() && matcher.matches(item.getAsLong());
296             }
297 
298             @Override
299             public void describeTo(Description description) {
300                 description.appendText(
301                     "OptionalLong with an item that matches " + matcher);
302             }
303         };
304     }
305 
306     /**
307      * Matches a present OptionalLong with content matching the given matcher
308      *
309      * @param matcher
310      *            To match against the OptionalLong's content
311      *
312      * @deprecated Matcher is replaced with {@link #presentLong(Matcher)} to
313      *             align with naming of {@link #present(Matcher)}.
314      */
315     @Deprecated
316     public static Matcher<OptionalLong> containsLong(Matcher<Long> matcher) {
317         return presentLong(matcher);
318     }
319 
320     /**
321      * Matches a not present OptionalDouble.
322      */
323     public static Matcher<OptionalDouble> notPresentDouble() {
324         return new TypeSafeMatcher<OptionalDouble>() {
325             @Override
326             protected boolean matchesSafely(OptionalDouble item) {
327                 return !item.isPresent();
328             }
329 
330             @Override
331             public void describeTo(Description description) {
332                 description.appendText("An OptionalDouble with no value");
333             }
334         };
335     }
336 
337     /**
338      * Matches a not present OptionalDouble.
339      *
340      * @deprecated Matcher is replaced with {@link #notPresentDouble()} to align
341      *             with naming of {@link #notPresent()}.
342      */
343     @Deprecated
344     public static Matcher<OptionalDouble> emptyDouble() {
345         return notPresentDouble();
346     }
347 
348     /**
349      * Matches a present OptionalDouble with the given content
350      *
351      * @param content
352      *            Expected contents of the Optional
353      */
354     public static Matcher<OptionalDouble> presentDouble(double content) {
355         return new TypeSafeMatcher<OptionalDouble>() {
356             @Override
357             protected boolean matchesSafely(OptionalDouble item) {
358                 return item.isPresent() && item.getAsDouble() == content;
359             }
360 
361             @Override
362             public void describeTo(Description description) {
363                 description.appendText(Optional.of(content).toString());
364             }
365         };
366     }
367 
368     /**
369      * Matches a present OptionalDouble with the given content
370      *
371      * @param content
372      *            Expected contents of the Optional
373      *
374      * @deprecated Matcher is replaced with {@link #presentDouble(double)} to
375      *             align with naming of {@link #present(Object)}.
376      */
377     @Deprecated
378     public static Matcher<OptionalDouble> containsDouble(double content) {
379         return presentDouble(content);
380     }
381 
382     /**
383      * Matches a present OptionalDouble with content matching the given matcher
384      *
385      * @param matcher
386      *            To match against the OptionalDouble's content
387      */
388     public static Matcher<OptionalDouble> presentDouble(
389         Matcher<Double> matcher) {
390         return new TypeSafeMatcher<OptionalDouble>() {
391             @Override
392             protected boolean matchesSafely(OptionalDouble item) {
393                 return item.isPresent() && matcher.matches(item.getAsDouble());
394             }
395 
396             @Override
397             public void describeTo(Description description) {
398                 description.appendText(
399                     "OptionalDouble with an item that matches " + matcher);
400             }
401         };
402     }
403 
404     /**
405      * Matches a present OptionalDouble with content matching the given matcher
406      *
407      * @param matcher
408      *            To match against the OptionalDouble's content
409      *
410      * @deprecated Matcher is replaced with {@link #presentDouble(Matcher)} to
411      *             align with naming of {@link #present(Matcher)}.
412      */
413     @Deprecated
414     public static Matcher<OptionalDouble> containsDouble(
415         Matcher<Double> matcher) {
416         return presentDouble(matcher);
417     }
418 }