author | smarks |
Mon, 02 Apr 2018 16:11:53 -0700 | |
branch | JDK-8195649-branch |
changeset 56383 | 4872fc3de1b0 |
parent 56376 | 718b9df3e302 |
permissions | -rw-r--r-- |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
1 |
/* |
56383 | 2 |
* Copyright (c) 2013, 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 OptionalLong |
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 BasicLong |
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 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
30 |
import java.util.NoSuchElementException; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
31 |
import java.util.OptionalLong; |
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
32 |
import java.util.concurrent.atomic.AtomicBoolean; |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
33 |
import java.util.stream.LongStream; |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
34 |
|
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
35 |
import static org.testng.Assert.*; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
36 |
import org.testng.annotations.Test; |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
37 |
|
56383 | 38 |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
39 |
public class BasicLong { |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
40 |
|
56383 | 41 |
@Test(groups = "unit") |
42 |
public void testEmpty() { |
|
43 |
OptionalLong empty = OptionalLong.empty(); |
|
44 |
OptionalLong present = OptionalLong.of(1); |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
45 |
|
56383 | 46 |
// empty |
47 |
assertTrue(empty.equals(empty)); |
|
48 |
assertTrue(empty.equals(OptionalLong.empty())); |
|
49 |
assertTrue(!empty.equals(present)); |
|
50 |
assertTrue(0 == empty.hashCode()); |
|
51 |
assertTrue(!empty.toString().isEmpty()); |
|
52 |
assertTrue(!empty.isPresent()); |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
53 |
|
56383 | 54 |
empty.ifPresent(v -> { fail(); }); |
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
55 |
|
56383 | 56 |
AtomicBoolean emptyCheck = new AtomicBoolean(); |
57 |
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true)); |
|
58 |
assertTrue(emptyCheck.get()); |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
59 |
|
56383 | 60 |
try { |
61 |
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); }); |
|
62 |
fail(); |
|
63 |
} catch (ObscureException expected) { |
|
64 |
} catch (AssertionError e) { |
|
65 |
throw e; |
|
66 |
} catch (Throwable t) { |
|
67 |
fail(); |
|
68 |
} |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
69 |
|
56383 | 70 |
assertEquals(2, empty.orElse(2)); |
71 |
assertEquals(2, empty.orElseGet(()-> 2)); |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
72 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
73 |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
74 |
@Test(groups = "unit") |
56383 | 75 |
public void testIfPresentAndOrElseAndNull() { |
76 |
OptionalLong empty = OptionalLong.empty(); |
|
77 |
OptionalLong present = OptionalLong.of(1); |
|
78 |
||
79 |
// No NPE |
|
80 |
present.ifPresentOrElse(v -> {}, null); |
|
81 |
empty.ifPresent(null); |
|
82 |
empty.ifPresentOrElse(null, () -> {}); |
|
83 |
||
84 |
// NPE |
|
85 |
try { |
|
86 |
present.ifPresent(null); |
|
87 |
fail(); |
|
88 |
} catch (NullPointerException ex) {} |
|
89 |
try { |
|
90 |
present.ifPresentOrElse(null, () -> {}); |
|
91 |
fail(); |
|
92 |
} catch (NullPointerException ex) {} |
|
93 |
try { |
|
94 |
empty.ifPresentOrElse(v -> {}, null); |
|
95 |
fail(); |
|
96 |
} catch (NullPointerException ex) {} |
|
97 |
} |
|
98 |
||
99 |
@Test(expectedExceptions=NoSuchElementException.class) |
|
100 |
public void testEmptyGet() { |
|
101 |
OptionalLong empty = OptionalLong.empty(); |
|
102 |
||
103 |
long got = empty.getAsLong(); |
|
104 |
} |
|
105 |
||
106 |
@Test(expectedExceptions=NullPointerException.class) |
|
107 |
public void testEmptyOrElseGetNull() { |
|
108 |
OptionalLong empty = OptionalLong.empty(); |
|
109 |
||
110 |
long got = empty.orElseGet(null); |
|
111 |
} |
|
112 |
||
113 |
@Test(expectedExceptions=NullPointerException.class) |
|
114 |
public void testEmptyOrElseThrowNull() throws Throwable { |
|
115 |
OptionalLong empty = OptionalLong.empty(); |
|
116 |
||
117 |
long got = empty.orElseThrow(null); |
|
118 |
} |
|
119 |
||
120 |
@Test(expectedExceptions=ObscureException.class) |
|
121 |
public void testEmptyOrElseThrow() throws Exception { |
|
122 |
OptionalLong empty = OptionalLong.empty(); |
|
123 |
||
124 |
long got = empty.orElseThrow(ObscureException::new); |
|
125 |
} |
|
126 |
||
127 |
@Test(expectedExceptions=NoSuchElementException.class) |
|
128 |
public void testEmptyOrElseThrowNoArg() throws Exception { |
|
129 |
OptionalLong empty = OptionalLong.empty(); |
|
130 |
||
131 |
long got = empty.orElseThrow(); |
|
48328
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
132 |
} |
7acf5700d542
8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents:
47216
diff
changeset
|
133 |
|
28963
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
134 |
@Test(groups = "unit") |
8498cdb7c54b
8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents:
28754
diff
changeset
|
135 |
public void testPresent() { |
56383 | 136 |
OptionalLong empty = OptionalLong.empty(); |
137 |
OptionalLong present = OptionalLong.of(1L); |
|
138 |
||
139 |
// present |
|
140 |
assertTrue(present.equals(present)); |
|
141 |
assertFalse(present.equals(OptionalLong.of(0L))); |
|
142 |
assertTrue(present.equals(OptionalLong.of(1L))); |
|
143 |
assertFalse(present.equals(empty)); |
|
144 |
assertTrue(Long.hashCode(1) == present.hashCode()); |
|
145 |
assertFalse(present.toString().isEmpty()); |
|
146 |
assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString())); |
|
147 |
assertTrue(-1 != present.toString().indexOf(Long.toString(present.orElseThrow()).toString())); |
|
148 |
assertEquals(1L, present.getAsLong()); |
|
149 |
assertEquals(1L, present.orElseThrow()); |
|
150 |
||
151 |
AtomicBoolean presentCheck = new AtomicBoolean(); |
|
152 |
present.ifPresent(v -> presentCheck.set(true)); |
|
153 |
assertTrue(presentCheck.get()); |
|
154 |
presentCheck.set(false); |
|
155 |
present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail()); |
|
156 |
assertTrue(presentCheck.get()); |
|
157 |
||
158 |
try { |
|
159 |
present.ifPresent(v -> { throw new ObscureException(); }); |
|
160 |
fail(); |
|
161 |
} catch (ObscureException expected) { |
|
162 |
} catch (AssertionError e) { |
|
163 |
throw e; |
|
164 |
} catch (Throwable t) { |
|
165 |
fail(); |
|
166 |
} |
|
167 |
try { |
|
168 |
present.ifPresentOrElse(v -> { |
|
169 |
throw new ObscureException(); |
|
170 |
}, () -> fail()); |
|
171 |
fail(); |
|
172 |
} catch (ObscureException expected) { |
|
173 |
} catch (AssertionError e) { |
|
174 |
throw e; |
|
175 |
} catch (Throwable t) { |
|
176 |
fail(); |
|
177 |
} |
|
178 |
||
179 |
assertEquals(1, present.orElse(2)); |
|
180 |
assertEquals(1, present.orElseGet(null)); |
|
181 |
assertEquals(1, present.orElseGet(()-> 2)); |
|
182 |
assertEquals(1, present.orElseGet(()-> 3)); |
|
183 |
assertEquals(1, present.<RuntimeException>orElseThrow(null)); |
|
184 |
assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new)); |
|
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
185 |
} |
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
186 |
|
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
187 |
@Test(groups = "unit") |
56383 | 188 |
public void testStream() { |
189 |
{ |
|
190 |
LongStream s = OptionalLong.empty().stream(); |
|
191 |
||
192 |
long[] es = s.toArray(); |
|
193 |
assertEquals(es.length, 0); |
|
194 |
} |
|
195 |
||
196 |
{ |
|
197 |
LongStream s = OptionalLong.of(42L).stream(); |
|
198 |
||
199 |
long[] es = s.toArray(); |
|
200 |
assertEquals(es.length, 1); |
|
201 |
assertEquals(es[0], 42L); |
|
202 |
} |
|
56376 | 203 |
} |
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
204 |
|
56383 | 205 |
private static class ObscureException extends RuntimeException { |
206 |
||
28754
8fe59917548d
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents:
16489
diff
changeset
|
207 |
} |
16489
53619efef3fb
8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff
changeset
|
208 |
} |