69 assertFalse(empty.isPresent()); |
55 assertFalse(empty.isPresent()); |
70 assertEquals(empty.hashCode(), 0); |
56 assertEquals(empty.hashCode(), 0); |
71 assertEquals(empty.orElse("x"), "x"); |
57 assertEquals(empty.orElse("x"), "x"); |
72 assertEquals(empty.orElseGet(() -> "y"), "y"); |
58 assertEquals(empty.orElseGet(() -> "y"), "y"); |
73 |
59 |
74 assertThrows(() -> empty.get(), NoSuchElementException.class); |
60 assertThrows(NoSuchElementException.class, () -> empty.get()); |
75 assertThrows(() -> empty.orElseThrow(), NoSuchElementException.class); |
61 assertThrows(NoSuchElementException.class, () -> empty.orElseThrow()); |
76 assertThrows(() -> empty.orElseThrow(ObscureException::new), ObscureException.class); |
62 assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new)); |
77 |
63 |
78 var b = new AtomicBoolean(); |
64 var b = new AtomicBoolean(); |
79 empty.ifPresent(s -> b.set(true)); |
65 empty.ifPresent(s -> b.set(true)); |
80 assertFalse(b.get()); |
66 assertFalse(b.get()); |
81 |
67 |
121 assertFalse(b2.get()); |
107 assertFalse(b2.get()); |
122 |
108 |
123 assertEquals(opt.toString(), "Optional[" + expected + "]"); |
109 assertEquals(opt.toString(), "Optional[" + expected + "]"); |
124 } |
110 } |
125 |
111 |
126 @Test |
112 @Test(groups = "unit") |
127 public void testEmpty() { |
113 public void testEmpty() { |
128 checkEmpty(Optional.empty()); |
114 checkEmpty(Optional.empty()); |
129 } |
115 } |
130 |
116 |
131 @Test |
117 @Test(groups = "unit") |
132 public void testOfNull() { |
118 public void testOfNull() { |
133 assertThrows(() -> Optional.of(null), NullPointerException.class); |
119 assertThrows(NullPointerException.class, () -> Optional.of(null)); |
134 } |
120 } |
135 |
121 |
136 @Test |
122 @Test(groups = "unit") |
137 public void testOfPresent() { |
123 public void testOfPresent() { |
138 checkPresent(Optional.of("xyzzy"), "xyzzy"); |
124 checkPresent(Optional.of("xyzzy"), "xyzzy"); |
139 } |
125 } |
140 |
126 |
141 @Test |
127 @Test(groups = "unit") |
142 public void testOfNullableNull() { |
128 public void testOfNullableNull() { |
143 checkEmpty(Optional.ofNullable(null)); |
129 checkEmpty(Optional.ofNullable(null)); |
144 } |
130 } |
145 |
131 |
146 @Test |
132 @Test(groups = "unit") |
147 public void testOfNullablePresent() { |
133 public void testOfNullablePresent() { |
148 checkPresent(Optional.of("xyzzy"), "xyzzy"); |
134 checkPresent(Optional.of("xyzzy"), "xyzzy"); |
149 } |
135 } |
150 |
136 |
151 @Test |
137 @Test(groups = "unit") |
152 public void testFilterEmpty() { |
138 public void testFilterEmpty() { |
153 checkEmpty(Optional.<String>empty().filter(s -> { fail(); return true; })); |
139 checkEmpty(Optional.<String>empty().filter(s -> { fail(); return true; })); |
154 } |
140 } |
155 |
141 |
156 @Test |
142 @Test(groups = "unit") |
157 public void testFilterFalse() { |
143 public void testFilterFalse() { |
158 checkEmpty(Optional.of("xyzzy").filter(s -> s.equals("plugh"))); |
144 checkEmpty(Optional.of("xyzzy").filter(s -> s.equals("plugh"))); |
159 } |
145 } |
160 |
146 |
161 @Test |
147 @Test(groups = "unit") |
162 public void testFilterTrue() { |
148 public void testFilterTrue() { |
163 checkPresent(Optional.of("xyzzy").filter(s -> s.equals("xyzzy")), "xyzzy"); |
149 checkPresent(Optional.of("xyzzy").filter(s -> s.equals("xyzzy")), "xyzzy"); |
164 } |
150 } |
165 |
151 |
166 @Test |
152 @Test(groups = "unit") |
167 public void testMapEmpty() { |
153 public void testMapEmpty() { |
168 checkEmpty(Optional.empty().map(s -> { fail(); return ""; })); |
154 checkEmpty(Optional.empty().map(s -> { fail(); return ""; })); |
169 } |
155 } |
170 |
156 |
171 @Test |
157 @Test(groups = "unit") |
172 public void testMapPresent() { |
158 public void testMapPresent() { |
173 checkPresent(Optional.of("xyzzy").map(s -> s.replace("xyzzy", "plugh")), "plugh"); |
159 checkPresent(Optional.of("xyzzy").map(s -> s.replace("xyzzy", "plugh")), "plugh"); |
174 } |
160 } |
175 |
161 |
176 @Test |
162 @Test(groups = "unit") |
177 public void testFlatMapEmpty() { |
163 public void testFlatMapEmpty() { |
178 checkEmpty(Optional.empty().flatMap(s -> { fail(); return Optional.of(""); })); |
164 checkEmpty(Optional.empty().flatMap(s -> { fail(); return Optional.of(""); })); |
179 } |
165 } |
180 |
166 |
181 @Test |
167 @Test(groups = "unit") |
182 public void testFlatMapPresentReturnEmpty() { |
168 public void testFlatMapPresentReturnEmpty() { |
183 checkEmpty(Optional.of("xyzzy") |
169 checkEmpty(Optional.of("xyzzy") |
184 .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.empty(); })); |
170 .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.empty(); })); |
185 } |
171 } |
186 |
172 |
187 @Test |
173 @Test(groups = "unit") |
188 public void testFlatMapPresentReturnPresent() { |
174 public void testFlatMapPresentReturnPresent() { |
189 checkPresent(Optional.of("xyzzy") |
175 checkPresent(Optional.of("xyzzy") |
190 .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.of("plugh"); }), |
176 .flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.of("plugh"); }), |
191 "plugh"); |
177 "plugh"); |
192 } |
178 } |
193 |
179 |
194 @Test |
180 @Test(groups = "unit") |
195 public void testOrEmptyEmpty() { |
181 public void testOrEmptyEmpty() { |
196 checkEmpty(Optional.<String>empty().or(() -> Optional.empty())); |
182 checkEmpty(Optional.<String>empty().or(() -> Optional.empty())); |
197 } |
183 } |
198 |
184 |
199 @Test |
185 @Test(groups = "unit") |
200 public void testOrEmptyPresent() { |
186 public void testOrEmptyPresent() { |
201 checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh"); |
187 checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh"); |
202 } |
188 } |
203 |
189 |
204 @Test |
190 @Test(groups = "unit") |
205 public void testOrPresentDontCare() { |
191 public void testOrPresentDontCare() { |
206 checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy"); |
192 checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy"); |
207 } |
193 } |
208 |
194 |
209 @Test |
195 @Test(groups = "unit") |
210 public void testStreamEmpty() { |
196 public void testStreamEmpty() { |
211 assertEquals(Optional.empty().stream().collect(toList()), List.of()); |
197 assertEquals(Optional.empty().stream().collect(toList()), List.of()); |
212 } |
198 } |
213 |
199 |
214 @Test |
200 @Test(groups = "unit") |
215 public void testStreamPresent() { |
201 public void testStreamPresent() { |
216 assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy")); |
202 assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy")); |
217 } |
203 } |
218 |
|
219 private static class ObscureException extends RuntimeException { } |
|
220 } |
204 } |