author | smarks |
Wed, 28 Mar 2018 23:43:54 -0700 | |
branch | JDK-8195649-branch |
changeset 56364 | ed0bfd119607 |
parent 56360 | 44f4a81c3b96 |
child 56375 | e0530ee8af48 |
permissions | -rw-r--r-- |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
1 |
/* |
56357 | 2 |
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
4 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
8 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
13 |
* accompanied this code). |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
14 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
18 |
* |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
21 |
* questions. |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
22 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
23 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
24 |
/* @test |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
25 |
* @summary Basic functional test of Optional |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
26 |
* @author Mike Duigou |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
27 |
* @run testng Basic |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
28 |
*/ |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
29 |
|
56357 | 30 |
import java.util.List; |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
31 |
import java.util.NoSuchElementException; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
32 |
import java.util.Optional; |
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
33 |
import java.util.concurrent.atomic.AtomicBoolean; |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
18819
diff
changeset
|
34 |
import java.util.stream.Stream; |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
35 |
|
56357 | 36 |
import static java.util.stream.Collectors.toList; |
37 |
||
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
38 |
import static org.testng.Assert.*; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
39 |
import org.testng.annotations.Test; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
40 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
41 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
42 |
public class Basic { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
43 |
|
56357 | 44 |
/** |
45 |
* Asserts that the runnable throws an exception of the given type. |
|
46 |
* Fails if no exception is thrown, or if an exception of the wrong |
|
47 |
* type is thrown. This is used instead of @Test(expectedExceptions) |
|
48 |
* because that can be applied only at the granularity of a test. |
|
49 |
*/ |
|
50 |
void assertThrows(Runnable r, Class<? extends Exception> clazz) { |
|
51 |
try { |
|
52 |
r.run(); |
|
53 |
fail(); |
|
54 |
} catch (Exception ex) { |
|
55 |
assertTrue(clazz.isInstance(ex)); |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
/** |
|
60 |
* Checks a block of assertions over an empty Optional. |
|
61 |
*/ |
|
62 |
void checkEmpty(Optional<String> empty) { |
|
63 |
assertTrue(empty.equals(Optional.empty())); |
|
64 |
assertTrue(Optional.empty().equals(empty)); |
|
56364 | 65 |
assertFalse(empty.equals(Optional.of("unexpected"))); |
66 |
assertFalse(Optional.of("unexpected").equals(empty)); |
|
67 |
assertFalse(empty.equals("unexpected")); |
|
68 |
||
56357 | 69 |
assertFalse(empty.isPresent()); |
70 |
assertEquals(empty.hashCode(), 0); |
|
71 |
assertEquals(empty.orElse("x"), "x"); |
|
72 |
assertEquals(empty.orElseGet(() -> "y"), "y"); |
|
73 |
||
74 |
assertThrows(() -> empty.get(), NoSuchElementException.class); |
|
75 |
assertThrows(() -> empty.orElseThrow(), NoSuchElementException.class); |
|
76 |
assertThrows(() -> empty.orElseThrow(ObscureException::new), ObscureException.class); |
|
77 |
||
78 |
var b = new AtomicBoolean(); |
|
79 |
empty.ifPresent(s -> b.set(true)); |
|
80 |
assertFalse(b.get()); |
|
81 |
||
82 |
var b1 = new AtomicBoolean(false); |
|
83 |
var b2 = new AtomicBoolean(false); |
|
84 |
empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
|
85 |
assertFalse(b1.get()); |
|
86 |
assertTrue(b2.get()); |
|
56360
44f4a81c3b96
restore Optional.or tests; add asserts for Optional.toString
smarks
parents:
56359
diff
changeset
|
87 |
|
44f4a81c3b96
restore Optional.or tests; add asserts for Optional.toString
smarks
parents:
56359
diff
changeset
|
88 |
assertEquals(empty.toString(), "Optional.empty"); |
56357 | 89 |
} |
90 |
||
91 |
/** |
|
92 |
* Checks a block of assertions over an Optional that is expected to |
|
93 |
* have a particular value present. |
|
94 |
*/ |
|
95 |
void checkPresent(Optional<String> opt, String expected) { |
|
96 |
assertFalse(opt.equals(Optional.empty())); |
|
97 |
assertFalse(Optional.empty().equals(opt)); |
|
98 |
assertTrue(opt.equals(Optional.of(expected))); |
|
99 |
assertTrue(Optional.of(expected).equals(opt)); |
|
100 |
assertFalse(opt.equals(Optional.of("unexpected"))); |
|
101 |
assertFalse(Optional.of("unexpected").equals(opt)); |
|
56364 | 102 |
assertFalse(opt.equals("unexpected")); |
103 |
||
56357 | 104 |
assertTrue(opt.isPresent()); |
105 |
assertEquals(opt.hashCode(), expected.hashCode()); |
|
106 |
assertEquals(opt.orElse("unexpected"), expected); |
|
107 |
assertEquals(opt.orElseGet(() -> "unexpected"), expected); |
|
108 |
||
109 |
assertEquals(opt.get(), expected); |
|
110 |
assertEquals(opt.orElseThrow(), expected); |
|
111 |
assertEquals(opt.orElseThrow(ObscureException::new), expected); |
|
112 |
||
113 |
var b = new AtomicBoolean(false); |
|
114 |
opt.ifPresent(s -> b.set(true)); |
|
115 |
assertTrue(b.get()); |
|
116 |
||
117 |
var b1 = new AtomicBoolean(false); |
|
118 |
var b2 = new AtomicBoolean(false); |
|
119 |
opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
|
120 |
assertTrue(b1.get()); |
|
121 |
assertFalse(b2.get()); |
|
56360
44f4a81c3b96
restore Optional.or tests; add asserts for Optional.toString
smarks
parents:
56359
diff
changeset
|
122 |
|
44f4a81c3b96
restore Optional.or tests; add asserts for Optional.toString
smarks
parents:
56359
diff
changeset
|
123 |
assertEquals(opt.toString(), "Optional[" + expected + "]"); |
56357 | 124 |
} |
125 |
||
126 |
@Test |
|
127 |
public void testEmpty() { |
|
128 |
checkEmpty(Optional.empty()); |
|
129 |
} |
|
130 |
||
131 |
@Test |
|
132 |
public void testOfNull() { |
|
133 |
assertThrows(() -> Optional.of(null), NullPointerException.class); |
|
134 |
} |
|
135 |
||
136 |
@Test |
|
137 |
public void testOfPresent() { |
|
138 |
checkPresent(Optional.of("xyzzy"), "xyzzy"); |
|
139 |
} |
|
140 |
||
141 |
@Test |
|
142 |
public void testOfNullableNull() { |
|
143 |
checkEmpty(Optional.ofNullable(null)); |
|
144 |
} |
|
145 |
||
146 |
@Test |
|
147 |
public void testOfNullablePresent() { |
|
148 |
checkPresent(Optional.of("xyzzy"), "xyzzy"); |
|
149 |
} |
|
150 |
||
151 |
@Test |
|
152 |
public void testFilterEmpty() { |
|
153 |
checkEmpty(Optional.<String>empty().filter(s -> { fail(); return true; })); |
|
154 |
} |
|
155 |
||
156 |
@Test |
|
157 |
public void testFilterFalse() { |
|
158 |
checkEmpty(Optional.of("xyzzy").filter(s -> s.equals("plugh"))); |
|
159 |
} |
|
160 |
||
161 |
@Test |
|
162 |
public void testFilterTrue() { |
|
163 |
checkPresent(Optional.of("xyzzy").filter(s -> s.equals("xyzzy")), "xyzzy"); |
|
164 |
} |
|
165 |
||
166 |
@Test |
|
167 |
public void testMapEmpty() { |
|
168 |
checkEmpty(Optional.empty().map(s -> { fail(); return ""; })); |
|
169 |
} |
|
170 |
||
171 |
@Test |
|
172 |
public void testMapPresent() { |
|
173 |
checkPresent(Optional.of("xyzzy").map(s -> s.replace("xyzzy", "plugh")), "plugh"); |
|
174 |
} |
|
175 |
||
176 |
@Test |
|
177 |
public void testFlatMapEmpty() { |
|
178 |
checkEmpty(Optional.empty().flatMap(s -> { fail(); return Optional.of(""); })); |
|
179 |
} |
|
180 |
||
181 |
@Test |
|
182 |
public void testFlatMapPresentReturnEmpty() { |
|
183 |
checkEmpty(Optional.of("xyzzy") |
|
184 |
.flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.empty(); })); |
|
185 |
} |
|
186 |
||
187 |
@Test |
|
188 |
public void testFlatMapPresentReturnPresent() { |
|
189 |
checkPresent(Optional.of("xyzzy") |
|
190 |
.flatMap(s -> { assertEquals(s, "xyzzy"); return Optional.of("plugh"); }), |
|
191 |
"plugh"); |
|
192 |
} |
|
193 |
||
194 |
@Test |
|
195 |
public void testOrEmptyEmpty() { |
|
196 |
checkEmpty(Optional.<String>empty().or(() -> Optional.empty())); |
|
197 |
} |
|
198 |
||
199 |
@Test |
|
200 |
public void testOrEmptyPresent() { |
|
201 |
checkPresent(Optional.<String>empty().or(() -> Optional.of("plugh")), "plugh"); |
|
202 |
} |
|
203 |
||
204 |
@Test |
|
205 |
public void testOrPresentDontCare() { |
|
206 |
checkPresent(Optional.of("xyzzy").or(() -> { fail(); return Optional.of("plugh"); }), "xyzzy"); |
|
207 |
} |
|
208 |
||
209 |
@Test |
|
210 |
public void testStreamEmpty() { |
|
211 |
assertEquals(Optional.empty().stream().collect(toList()), List.of()); |
|
212 |
} |
|
213 |
||
214 |
@Test |
|
215 |
public void testStreamPresent() { |
|
216 |
assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy")); |
|
217 |
} |
|
218 |
||
56358 | 219 |
private static class ObscureException extends RuntimeException { } |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
220 |
} |