|
31 | 31 | import java.time.LocalDate; |
32 | 32 | import java.time.ZoneId; |
33 | 33 | import java.util.Arrays; |
| 34 | +import java.util.Collection; |
34 | 35 | import java.util.Collections; |
35 | 36 | import java.util.Currency; |
36 | 37 | import java.util.Date; |
|
52 | 53 | import static org.assertj.core.api.Assertions.assertThat; |
53 | 54 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
54 | 55 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 56 | +import static org.springframework.util.ObjectUtils.getIfEmpty; |
| 57 | +import static org.springframework.util.ObjectUtils.getIfNull; |
55 | 58 | import static org.springframework.util.ObjectUtils.isEmpty; |
56 | 59 |
|
57 | 60 | /** |
@@ -111,6 +114,36 @@ void isCompatibleWithThrowsClause() { |
111 | 114 | assertThat(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), throwable)).isTrue(); |
112 | 115 | } |
113 | 116 |
|
| 117 | + @Test |
| 118 | + void testGetIfNull() { |
| 119 | + String value = UUID.randomUUID().toString(); |
| 120 | + String backup = UUID.randomUUID().toString(); |
| 121 | + assertThat(getIfNull(value, backup)).isEqualTo(value); |
| 122 | + assertThat(getIfNull(value, value)).isEqualTo(value); |
| 123 | + assertThat(getIfNull(backup, backup)).isEqualTo(backup); |
| 124 | + assertThat(getIfNull(null, value)).isEqualTo(value); |
| 125 | + assertThat(getIfNull(null, backup)).isEqualTo(backup); |
| 126 | + assertThat(getIfNull("null", backup)).isEqualTo("null"); |
| 127 | + assertThat(Optional.ofNullable(getIfNull(null, null))).isEmpty(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testGetIfEmpty() { |
| 132 | + Collection<String> empty = Collections.emptyList(); |
| 133 | + Collection<String> value = List.of(UUID.randomUUID().toString()); |
| 134 | + Collection<String> backup = List.of(UUID.randomUUID().toString()); |
| 135 | + assertThat(getIfEmpty(backup, backup)).isEqualTo(backup); |
| 136 | + assertThat(getIfEmpty(empty, backup)).isEqualTo(backup); |
| 137 | + assertThat(getIfEmpty(value, backup)).isEqualTo(value); |
| 138 | + assertThat(getIfEmpty(value, empty)).isEqualTo(value); |
| 139 | + assertThat(getIfEmpty(null, backup)).isEqualTo(backup); |
| 140 | + assertThat(getIfEmpty(empty, empty)).isEqualTo(empty); |
| 141 | + assertThat(getIfEmpty(backup, backup)).isEqualTo(backup); |
| 142 | + assertThat(getIfEmpty(null, empty)).isEqualTo(empty); |
| 143 | + assertThat(getIfEmpty(null, backup)).isEqualTo(backup); |
| 144 | + assertThat(Optional.ofNullable(getIfEmpty(null, null))).isEmpty(); |
| 145 | + } |
| 146 | + |
114 | 147 | @Test |
115 | 148 | void isEmptyNull() { |
116 | 149 | assertThat(isEmpty(null)).isTrue(); |
|
0 commit comments