35 import java.util.stream.LongStream; |
33 import java.util.stream.LongStream; |
36 |
34 |
37 import static org.testng.Assert.*; |
35 import static org.testng.Assert.*; |
38 import org.testng.annotations.Test; |
36 import org.testng.annotations.Test; |
39 |
37 |
|
38 |
40 public class BasicLong { |
39 public class BasicLong { |
41 static final long LONGVAL = 2_305_843_008_139_952_128L; |
40 |
42 static final long UNEXPECTED = 0xFEEDBEEFCAFEBABEL; |
41 @Test(groups = "unit") |
43 |
42 public void testEmpty() { |
44 /** |
43 OptionalLong empty = OptionalLong.empty(); |
45 * Checks a block of assertions over an empty OptionalLong. |
44 OptionalLong present = OptionalLong.of(1); |
46 */ |
45 |
47 void checkEmpty(OptionalLong empty) { |
46 // empty |
|
47 assertTrue(empty.equals(empty)); |
48 assertTrue(empty.equals(OptionalLong.empty())); |
48 assertTrue(empty.equals(OptionalLong.empty())); |
49 assertTrue(OptionalLong.empty().equals(empty)); |
49 assertTrue(!empty.equals(present)); |
50 assertFalse(empty.equals(OptionalLong.of(UNEXPECTED))); |
50 assertTrue(0 == empty.hashCode()); |
51 assertFalse(OptionalLong.of(UNEXPECTED).equals(empty)); |
51 assertTrue(!empty.toString().isEmpty()); |
52 assertFalse(empty.equals("unexpected")); |
52 assertTrue(!empty.isPresent()); |
53 |
53 |
54 assertFalse(empty.isPresent()); |
54 empty.ifPresent(v -> { fail(); }); |
55 assertEquals(empty.hashCode(), 0); |
55 |
56 assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); |
56 AtomicBoolean emptyCheck = new AtomicBoolean(); |
57 assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); |
57 empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true)); |
58 |
58 assertTrue(emptyCheck.get()); |
59 assertThrows(NoSuchElementException.class, () -> empty.getAsLong()); |
59 |
60 assertThrows(NoSuchElementException.class, () -> empty.orElseThrow()); |
60 try { |
61 assertThrows(ObscureException.class, () -> empty.orElseThrow(ObscureException::new)); |
61 empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); }); |
62 |
62 fail(); |
63 var b = new AtomicBoolean(); |
63 } catch (ObscureException expected) { |
64 empty.ifPresent(s -> b.set(true)); |
64 } catch (AssertionError e) { |
65 assertFalse(b.get()); |
65 throw e; |
66 |
66 } catch (Throwable t) { |
67 var b1 = new AtomicBoolean(false); |
67 fail(); |
68 var b2 = new AtomicBoolean(false); |
68 } |
69 empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
69 |
70 assertFalse(b1.get()); |
70 assertEquals(2, empty.orElse(2)); |
71 assertTrue(b2.get()); |
71 assertEquals(2, empty.orElseGet(()-> 2)); |
72 |
72 } |
73 assertEquals(empty.toString(), "OptionalLong.empty"); |
73 |
74 } |
74 @Test(groups = "unit") |
75 |
75 public void testIfPresentAndOrElseAndNull() { |
76 /** |
76 OptionalLong empty = OptionalLong.empty(); |
77 * Checks a block of assertions over an OptionalLong that is expected to |
77 OptionalLong present = OptionalLong.of(1); |
78 * have a particular value present. |
78 |
79 */ |
79 // No NPE |
80 void checkPresent(OptionalLong opt, long expected) { |
80 present.ifPresentOrElse(v -> {}, null); |
81 assertFalse(opt.equals(OptionalLong.empty())); |
81 empty.ifPresent(null); |
82 assertFalse(OptionalLong.empty().equals(opt)); |
82 empty.ifPresentOrElse(null, () -> {}); |
83 assertTrue(opt.equals(OptionalLong.of(expected))); |
83 |
84 assertTrue(OptionalLong.of(expected).equals(opt)); |
84 // NPE |
85 assertFalse(opt.equals(OptionalLong.of(UNEXPECTED))); |
85 try { |
86 assertFalse(OptionalLong.of(UNEXPECTED).equals(opt)); |
86 present.ifPresent(null); |
87 assertFalse(opt.equals("unexpected")); |
87 fail(); |
88 |
88 } catch (NullPointerException ex) {} |
89 assertTrue(opt.isPresent()); |
89 try { |
90 assertEquals(opt.hashCode(), Long.hashCode(expected)); |
90 present.ifPresentOrElse(null, () -> {}); |
91 assertEquals(opt.orElse(UNEXPECTED), expected); |
91 fail(); |
92 assertEquals(opt.orElseGet(() -> UNEXPECTED), expected); |
92 } catch (NullPointerException ex) {} |
93 |
93 try { |
94 assertEquals(opt.getAsLong(), expected); |
94 empty.ifPresentOrElse(v -> {}, null); |
95 assertEquals(opt.orElseThrow(), expected); |
95 fail(); |
96 assertEquals(opt.orElseThrow(ObscureException::new), expected); |
96 } catch (NullPointerException ex) {} |
97 |
97 } |
98 var b = new AtomicBoolean(false); |
98 |
99 opt.ifPresent(s -> b.set(true)); |
99 @Test(expectedExceptions=NoSuchElementException.class) |
100 assertTrue(b.get()); |
100 public void testEmptyGet() { |
101 |
101 OptionalLong empty = OptionalLong.empty(); |
102 var b1 = new AtomicBoolean(false); |
102 |
103 var b2 = new AtomicBoolean(false); |
103 long got = empty.getAsLong(); |
104 opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true)); |
104 } |
105 assertTrue(b1.get()); |
105 |
106 assertFalse(b2.get()); |
106 @Test(expectedExceptions=NullPointerException.class) |
107 |
107 public void testEmptyOrElseGetNull() { |
108 assertEquals(opt.toString(), "OptionalLong[" + expected + "]"); |
108 OptionalLong empty = OptionalLong.empty(); |
109 } |
109 |
110 |
110 long got = empty.orElseGet(null); |
111 @Test(groups = "unit") |
111 } |
112 public void testEmpty() { |
112 |
113 checkEmpty(OptionalLong.empty()); |
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(); |
114 } |
132 } |
115 |
133 |
116 @Test(groups = "unit") |
134 @Test(groups = "unit") |
117 public void testPresent() { |
135 public void testPresent() { |
118 checkPresent(OptionalLong.of(LONGVAL), LONGVAL); |
136 OptionalLong empty = OptionalLong.empty(); |
119 } |
137 OptionalLong present = OptionalLong.of(1L); |
120 |
138 |
121 @Test(groups = "unit") |
139 // present |
122 public void testStreamEmpty() { |
140 assertTrue(present.equals(present)); |
123 assertEquals(OptionalLong.empty().stream().toArray(), new long[] { }); |
141 assertFalse(present.equals(OptionalLong.of(0L))); |
124 } |
142 assertTrue(present.equals(OptionalLong.of(1L))); |
125 |
143 assertFalse(present.equals(empty)); |
126 @Test(groups = "unit") |
144 assertTrue(Long.hashCode(1) == present.hashCode()); |
127 public void testStreamPresent() { |
145 assertFalse(present.toString().isEmpty()); |
128 assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL }); |
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)); |
|
185 } |
|
186 |
|
187 @Test(groups = "unit") |
|
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 } |
|
203 } |
|
204 |
|
205 private static class ObscureException extends RuntimeException { |
|
206 |
129 } |
207 } |
130 } |
208 } |