-
-
Notifications
You must be signed in to change notification settings - Fork 779
Expand file tree
/
Copy pathAbstractOptionalAssert.java
More file actions
447 lines (422 loc) · 18.4 KB
/
Copy pathAbstractOptionalAssert.java
File metadata and controls
447 lines (422 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2019 the original author or authors.
*/
package org.assertj.core.api;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.error.OptionalShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.OptionalShouldBePresent.shouldBePresent;
import static org.assertj.core.error.OptionalShouldContain.shouldContain;
import static org.assertj.core.error.OptionalShouldContain.shouldContainSame;
import static org.assertj.core.error.OptionalShouldContainInstanceOf.shouldContainInstanceOf;
import static org.assertj.core.util.Preconditions.checkArgument;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.internal.ComparisonStrategy;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.FieldByFieldComparator;
import org.assertj.core.internal.StandardComparisonStrategy;
import org.assertj.core.util.CheckReturnValue;
/**
* Assertions for {@link java.util.Optional}.
*
* @param <SELF> the "self" type of this assertion class.
* @param <VALUE> type of the value contained in the {@link java.util.Optional}.
*
* @author Jean-Christophe Gay
* @author Nicolai Parlog
* @author Grzegorz Piwowarek
*/
public abstract class AbstractOptionalAssert<SELF extends AbstractOptionalAssert<SELF, VALUE>, VALUE> extends
AbstractAssert<SELF, Optional<VALUE>> {
private ComparisonStrategy optionalValueComparisonStrategy;
protected AbstractOptionalAssert(Optional<VALUE> actual, Class<?> selfType) {
super(actual, selfType);
this.optionalValueComparisonStrategy = StandardComparisonStrategy.instance();
}
/**
* Verifies that there is a value present in the actual {@link java.util.Optional}.
* <p>
* Assertion will pass :
* <pre><code class='java'> assertThat(Optional.of("something")).isPresent();</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(Optional.empty()).isPresent();</code></pre>
*
* @return this assertion object.
*/
public SELF isPresent() {
assertValueIsPresent();
return myself;
}
/**
* Verifies that there is a value present in the actual {@link java.util.Optional}, it's an alias of {@link #isPresent()}.
* <p>
* Assertion will pass :
* <pre><code class='java'> assertThat(Optional.of("something")).isNotEmpty();</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(Optional.empty()).isNotEmpty();</code></pre>
*
* @return this assertion object.
*/
public SELF isNotEmpty() {
return isPresent();
}
/**
* Verifies that the actual {@link java.util.Optional} is empty.
* <p>
* Assertion will pass :
* <pre><code class='java'> assertThat(Optional.empty()).isEmpty();</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(Optional.of("something")).isEmpty();</code></pre>
*
* @return this assertion object.
*/
public SELF isEmpty() {
isNotNull();
if (actual.isPresent()) throwAssertionError(shouldBeEmpty(actual));
return myself;
}
/**
* Verifies that the actual {@link java.util.Optional} is empty (alias of {@link #isEmpty()}).
* <p>
* Assertion will pass :
* <pre><code class='java'> assertThat(Optional.empty()).isNotPresent();</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(Optional.of("something")).isNotPresent();</code></pre>
*
* @return this assertion object.
*/
public SELF isNotPresent() {
return isEmpty();
}
/**
* Verifies that the actual {@link java.util.Optional} contains the given value (alias of {@link #hasValue(Object)}).
* <p>
* Assertion will pass :
* <pre><code class='java'> assertThat(Optional.of("something")).contains("something");
* assertThat(Optional.of(10)).contains(10);</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(Optional.of("something")).contains("something else");
* assertThat(Optional.of(20)).contains(10);</code></pre>
*
* @param expectedValue the expected value inside the {@link java.util.Optional}.
* @return this assertion object.
*/
public SELF contains(VALUE expectedValue) {
isNotNull();
checkNotNull(expectedValue);
if (!actual.isPresent()) throwAssertionError(shouldContain(expectedValue));
if (!optionalValueComparisonStrategy.areEqual(actual.get(), expectedValue))
throw Failures.instance().failure(info, shouldContain(actual, expectedValue), actual.get(), expectedValue);
return myself;
}
/**
* Verifies that the actual {@link java.util.Optional} contains a value and gives this value to the given
* {@link java.util.function.Consumer} for further assertions. Should be used as a way of deeper asserting on the
* containing object, as further requirement(s) for the value.
* <p>
* Assertions will pass :
* <pre><code class='java'> // one requirement
* assertThat(Optional.of(10)).hasValueSatisfying(i -> { assertThat(i).isGreaterThan(9); });
*
* // multiple requirements
* assertThat(Optional.of(someString)).hasValueSatisfying(s -> {
* assertThat(s).isEqualTo("something");
* assertThat(s).startsWith("some");
* assertThat(s).endsWith("thing");
* }); </code></pre>
*
* Assertions will fail :
* <pre><code class='java'> assertThat(Optional.of("something")).hasValueSatisfying(s -> {
* assertThat(s).isEqualTo("something else");
* });
*
* // fail because optional is empty, there is no value to perform assertion on
* assertThat(Optional.empty()).hasValueSatisfying(o -> {});</code></pre>
*
* @param requirement to further assert on the object contained inside the {@link java.util.Optional}.
* @return this assertion object.
*/
public SELF hasValueSatisfying(Consumer<VALUE> requirement) {
assertValueIsPresent();
requirement.accept(actual.get());
return myself;
}
/**
* Verifies that the actual {@link Optional} contains a value which satisfies the given {@link Condition}.
* <p>
* Examples:
* <pre><code class='java'> Condition<TolkienCharacter> isAnElf = new Condition<>(character -> character.getRace() == ELF, "an elf");
*
* TolkienCharacter legolas = new TolkienCharacter("Legolas", 1000, ELF);
* TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
*
* // assertion succeeds
* assertThat(Optional.of(legolas)).hasValueSatisfying(isAnElf);
*
* // assertion fails
* assertThat(Optional.of(frodo)).hasValueSatisfying(isAnElf);</code></pre>
*
* @param condition the given condition.
* @return this assertion object.
* @throws AssertionError if the actual {@link Optional} is null or empty.
* @throws NullPointerException if the given condition is {@code null}.
* @throws AssertionError if the actual value does not satisfy the given condition.
* @since 3.6.0
*/
public SELF hasValueSatisfying(Condition<? super VALUE> condition) {
assertValueIsPresent();
conditions.assertIs(info, actual.get(), condition);
return myself;
}
/**
* Verifies that the actual {@link java.util.Optional} contains the given value (alias of {@link #contains(Object)}).
* <p>
* Assertion will pass :
* <pre><code class='java'> assertThat(Optional.of("something")).hasValue("something");
* assertThat(Optional.of(10)).contains(10);</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(Optional.of("something")).hasValue("something else");
* assertThat(Optional.of(20)).contains(10);</code></pre>
*
* @param expectedValue the expected value inside the {@link java.util.Optional}.
* @return this assertion object.
*/
public SELF hasValue(VALUE expectedValue) {
return contains(expectedValue);
}
/**
* Verifies that the actual {@link Optional} contains a value that is an instance of the argument.
* <p>
* Assertions will pass:
*
* <pre><code class='java'> assertThat(Optional.of("something")).containsInstanceOf(String.class)
* .containsInstanceOf(Object.class);
*
* assertThat(Optional.of(10)).containsInstanceOf(Integer.class);</code></pre>
*
* Assertion will fail:
*
* <pre><code class='java'> assertThat(Optional.of("something")).containsInstanceOf(Integer.class);</code></pre>
*
* @param clazz the expected class of the value inside the {@link Optional}.
* @return this assertion object.
*/
public SELF containsInstanceOf(Class<?> clazz) {
assertValueIsPresent();
if (!clazz.isInstance(actual.get())) throwAssertionError(shouldContainInstanceOf(actual, clazz));
return myself;
}
/**
* Use field/property by field/property comparison (including inherited fields/properties) instead of relying on
* actual type A <code>equals</code> method to compare the {@link Optional} value's object for incoming assertion
* checks. Private fields are included but this can be disabled using
* {@link Assertions#setAllowExtractingPrivateFields(boolean)}.
* <p>
* This can be handy if <code>equals</code> method of the {@link Optional} value's object to compare does not suit
* you.
* </p>
* Note that the comparison is <b>not</b> recursive, if one of the fields/properties is an Object, it will be
* compared to the other field/property using its <code>equals</code> method.
* <p>
* Example:
*
* <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
* TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
*
* // Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references
* assertThat(Optional.of(frodo)).contains(frodoClone);
*
* // frodo and frodoClone are equals when doing a field by field comparison.
* assertThat(Optional.of(frodo)).usingFieldByFieldValueComparator().contains(frodoClone);</code></pre>
*
* @return {@code this} assertion object.
*/
@CheckReturnValue
public SELF usingFieldByFieldValueComparator() {
return usingValueComparator(new FieldByFieldComparator());
}
/**
* Use given custom comparator instead of relying on actual type A <code>equals</code> method to compare the
* {@link Optional} value's object for incoming assertion checks.
* <p>
* Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default
* comparison strategy.
* <p>
* Examples :
*
* <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
* TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
*
* // Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references
* assertThat(Optional.of(frodo)).contains(frodoClone);
*
* // frodo and frodoClone are equals when doing a field by field comparison.
* assertThat(Optional.of(frodo)).usingValueComparator(new FieldByFieldComparator()).contains(frodoClone);</code></pre>
*
* @param customComparator the comparator to use for incoming assertion checks.
* @throws NullPointerException if the given comparator is {@code null}.
* @return {@code this} assertion object.
*/
@CheckReturnValue
public SELF usingValueComparator(Comparator<? super VALUE> customComparator) {
optionalValueComparisonStrategy = new ComparatorBasedComparisonStrategy(customComparator);
return myself;
}
/**
* Revert to standard comparison for incoming assertion {@link Optional} value checks.
* <p>
* This method should be used to disable a custom comparison strategy set by calling
* {@link #usingValueComparator(Comparator)}.
*
* @return {@code this} assertion object.
*/
@CheckReturnValue
public SELF usingDefaultValueComparator() {
// fall back to default strategy to compare actual with other objects.
optionalValueComparisonStrategy = StandardComparisonStrategy.instance();
return myself;
}
/**
* Verifies that the actual {@link java.util.Optional} contains the instance given as an argument (i.e. it must be the
* same instance).
* <p>
* Assertion will pass :
*
* <pre><code class='java'> String someString = "something";
* assertThat(Optional.of(someString)).containsSame(someString);
*
* // Java will create the same 'Integer' instance when boxing small ints
* assertThat(Optional.of(10)).containsSame(10);</code></pre>
*
* Assertion will fail :
*
* <pre><code class='java'> // not even equal:
* assertThat(Optional.of("something")).containsSame("something else");
* assertThat(Optional.of(20)).containsSame(10);
*
* // equal but not the same:
* assertThat(Optional.of(new String("something"))).containsSame(new String("something"));
* assertThat(Optional.of(new Integer(10))).containsSame(new Integer(10));</code></pre>
*
* @param expectedValue the expected value inside the {@link java.util.Optional}.
* @return this assertion object.
*/
public SELF containsSame(VALUE expectedValue) {
isNotNull();
checkNotNull(expectedValue);
if (!actual.isPresent()) throwAssertionError(shouldContain(expectedValue));
if (actual.get() != expectedValue) throwAssertionError(shouldContainSame(actual, expectedValue));
return myself;
}
/**
* Call {@link Optional#flatMap(Function) flatMap} on the {@code Optional} under test, assertions chained afterwards are performed on the {@code Optional} resulting from the flatMap call.
* <p>
* Examples:
* <pre><code class='java'> Function<String, Optional<String>> UPPER_CASE_OPTIONAL_STRING =
* s -> s == null ? Optional.empty() : Optional.of(s.toUpperCase());
*
* // assertions succeed
* assertThat(Optional.of("something")).contains("something")
* .flatMap(UPPER_CASE_OPTIONAL_STRING)
* .contains("SOMETHING");
*
* assertThat(Optional.<String>empty()).flatMap(UPPER_CASE_OPTIONAL_STRING)
* .isEmpty();
*
* assertThat(Optional.<String>ofNullable(null)).flatMap(UPPER_CASE_OPTIONAL_STRING)
* .isEmpty();
*
* // assertion fails
* assertThat(Optional.of("something")).flatMap(UPPER_CASE_OPTIONAL_STRING)
* .contains("something");</code></pre>
*
* @param <U> the type wrapped in the {@link Optional} after the {@link Optional#flatMap(Function) flatMap} operation.
* @param mapper the {@link Function} to use in the {@link Optional#flatMap(Function) flatMap} operation.
* @return a new {@link AbstractOptionalAssert} for assertions chaining on the flatMap of the Optional.
* @throws AssertionError if the actual {@link Optional} is null.
* @since 3.6.0
*/
@CheckReturnValue
public <U> AbstractOptionalAssert<?, U> flatMap(Function<? super VALUE, Optional<U>> mapper) {
isNotNull();
return assertThat(actual.flatMap(mapper));
}
/**
* Call {@link Optional#map(Function) map} on the {@code Optional} under test, assertions chained afterwards are performed on the {@code Optional} resulting from the map call.
* <p>
* Examples:
* <pre><code class='java'> // assertions succeed
* assertThat(Optional.<String>empty()).map(String::length)
* .isEmpty();
*
* assertThat(Optional.of("42")).contains("42")
* .map(String::length)
* .contains(2);
*
* // assertion fails
* assertThat(Optional.of("42")).map(String::length)
* .contains(3);</code></pre>
*
* @param <U> the type wrapped in the {@link Optional} after the {@link Optional#map(Function) map} operation.
* @param mapper the {@link Function} to use in the {@link Optional#map(Function) map} operation.
* @return a new {@link AbstractOptionalAssert} for assertions chaining on the map of the Optional.
* @throws AssertionError if the actual {@link Optional} is null.
* @since 3.6.0
*/
@CheckReturnValue
public <U> AbstractOptionalAssert<?, U> map(Function<? super VALUE, ? extends U> mapper) {
isNotNull();
return assertThat(actual.map(mapper));
}
/**
* Verifies that the actual {@link Optional} is not {@code null} and not empty and returns an Object assertion
* that allows chaining (object) assertions on the optional value.
* <p>
* Note that it is only possible to return Object assertions after calling this method due to java generics limitations.
* <p>
* Example:
* <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
* TolkienCharacter sam = new TolkienCharacter("Sam", 38, null);
*
* // assertion succeeds since all frodo's fields are set
* assertThat(Optional.of(frodo)).get().hasNoNullFields();
*
* // assertion does not succeed because sam does not have its race set
* assertThat(Optional.of(sam)).get().hasNoNullFields();</code></pre>
*
* @return a new {@link AbstractObjectAssert} for assertions chaining on the value of the Optional.
* @throws AssertionError if the actual {@link Optional} is null.
* @throws AssertionError if the actual {@link Optional} is empty.
* @since 3.9.0
*/
@CheckReturnValue
public AbstractObjectAssert<?, VALUE> get() {
isPresent();
return assertThat(actual.get());
}
private void checkNotNull(Object expectedValue) {
checkArgument(expectedValue != null, "The expected value should not be <null>.");
}
private void assertValueIsPresent() {
isNotNull();
if (!actual.isPresent()) throwAssertionError(shouldBePresent(actual));
}
}