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
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
33
34
35
36
37 @Deprecated
38 public static <T> Matcher<Optional<T>> empty() {
39 return notPresent();
40 }
41
42
43
44
45
46
47
48
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
66
67
68
69
70
71
72
73
74
75
76 @Deprecated
77 public static <T> Matcher<Optional<T>> contains(T content) {
78 return present(content);
79 }
80
81
82
83
84
85
86
87
88
89
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
109
110
111
112
113
114
115
116
117
118
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
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
145
146
147
148
149 @Deprecated
150 public static Matcher<OptionalInt> emptyInt() {
151 return notPresentInt();
152 }
153
154
155
156
157
158
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
176
177
178
179
180
181
182
183 @Deprecated
184 public static Matcher<OptionalInt> containsInt(int content) {
185 return presentInt(content);
186 }
187
188
189
190
191
192
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
211
212
213
214
215
216
217
218 @Deprecated
219 public static Matcher<OptionalInt> containsInt(Matcher<Integer> matcher) {
220 return presentInt(matcher);
221 }
222
223
224
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
242
243
244
245
246 @Deprecated
247 public static Matcher<OptionalLong> emptyLong() {
248 return notPresentLong();
249 }
250
251
252
253
254
255
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
273
274
275
276
277
278
279
280 @Deprecated
281 public static Matcher<OptionalLong> containsLong(long content) {
282 return presentLong(content);
283 }
284
285
286
287
288
289
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
308
309
310
311
312
313
314
315 @Deprecated
316 public static Matcher<OptionalLong> containsLong(Matcher<Long> matcher) {
317 return presentLong(matcher);
318 }
319
320
321
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
339
340
341
342
343 @Deprecated
344 public static Matcher<OptionalDouble> emptyDouble() {
345 return notPresentDouble();
346 }
347
348
349
350
351
352
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
370
371
372
373
374
375
376
377 @Deprecated
378 public static Matcher<OptionalDouble> containsDouble(double content) {
379 return presentDouble(content);
380 }
381
382
383
384
385
386
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
406
407
408
409
410
411
412
413 @Deprecated
414 public static Matcher<OptionalDouble> containsDouble(
415 Matcher<Double> matcher) {
416 return presentDouble(matcher);
417 }
418 }