97 assertTrue(present.equals(Optional.of(Boolean.TRUE)));
98 assertTrue(!present.equals(empty));
99 assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
100 assertTrue(!present.toString().isEmpty());
101 assertTrue(!present.toString().equals(presentEmptyString.toString()));
102 assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
103 assertSame(Boolean.TRUE, present.get());
104 try {
105 present.ifPresent(v -> { throw new ObscureException(); });
106 fail();
107 } catch(ObscureException expected) {
108
109 }
110 assertSame(Boolean.TRUE, present.orElse(null));
111 assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
112 assertSame(Boolean.TRUE, present.orElseGet(null));
113 assertSame(Boolean.TRUE, present.orElseGet(()-> null));
114 assertSame(Boolean.TRUE, present.orElseGet(()-> Boolean.FALSE));
115 assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow( null));
116 assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
117 }
118
119 private static class ObscureException extends RuntimeException {
120
121 }
122 }
|
97 assertTrue(present.equals(Optional.of(Boolean.TRUE)));
98 assertTrue(!present.equals(empty));
99 assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
100 assertTrue(!present.toString().isEmpty());
101 assertTrue(!present.toString().equals(presentEmptyString.toString()));
102 assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
103 assertSame(Boolean.TRUE, present.get());
104 try {
105 present.ifPresent(v -> { throw new ObscureException(); });
106 fail();
107 } catch(ObscureException expected) {
108
109 }
110 assertSame(Boolean.TRUE, present.orElse(null));
111 assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
112 assertSame(Boolean.TRUE, present.orElseGet(null));
113 assertSame(Boolean.TRUE, present.orElseGet(()-> null));
114 assertSame(Boolean.TRUE, present.orElseGet(()-> Boolean.FALSE));
115 assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow( null));
116 assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
117 }
118
119 @Test(groups = "unit")
120 public void testOfNullable() {
121 Optional<String> instance = Optional.ofNullable(null);
122 assertFalse(instance.isPresent());
123
124 instance = Optional.ofNullable("Duke");
125 assertTrue(instance.isPresent());
126 assertEquals(instance.get(), "Duke");
127 }
128
129 @Test(groups = "unit")
130 public void testFilter() {
131 // Null mapper function
132 Optional<String> empty = Optional.empty();
133 Optional<String> duke = Optional.of("Duke");
134
135 try {
136 Optional<String> result = empty.filter(null);
137 fail("Should throw NPE on null mapping function");
138 } catch (NullPointerException npe) {
139 // expected
140 }
141
142 Optional<String> result = empty.filter(String::isEmpty);
143 assertFalse(result.isPresent());
144
145 result = duke.filter(String::isEmpty);
146 assertFalse(result.isPresent());
147 result = duke.filter(s -> s.startsWith("D"));
148 assertTrue(result.isPresent());
149 assertEquals(result.get(), "Duke");
150
151 Optional<String> emptyString = Optional.of("");
152 result = emptyString.filter(String::isEmpty);
153 assertTrue(result.isPresent());
154 assertEquals(result.get(), "");
155 }
156
157 @Test(groups = "unit")
158 public void testMap() {
159 Optional<String> empty = Optional.empty();
160 Optional<String> duke = Optional.of("Duke");
161
162 // Null mapper function
163 try {
164 Optional<Boolean> b = empty.map(null);
165 fail("Should throw NPE on null mapping function");
166 } catch (NullPointerException npe) {
167 // expected
168 }
169
170 // Map an empty value
171 Optional<Boolean> b = empty.map(String::isEmpty);
172 assertFalse(b.isPresent());
173
174 // Map into null
175 b = empty.map(n -> null);
176 assertFalse(b.isPresent());
177 b = duke.map(s -> null);
178 assertFalse(b.isPresent());
179
180 // Map to value
181 Optional<Integer> l = duke.map(String::length);
182 assertEquals(l.get().intValue(), 4);
183 }
184
185 @Test(groups = "unit")
186 public void testFlatMap() {
187 Optional<String> empty = Optional.empty();
188 Optional<String> duke = Optional.of("Duke");
189
190 // Null mapper function
191 try {
192 Optional<Boolean> b = empty.flatMap(null);
193 fail("Should throw NPE on null mapping function");
194 } catch (NullPointerException npe) {
195 // expected
196 }
197
198 // Map into null
199 try {
200 Optional<Boolean> b = duke.flatMap(s -> null);
201 fail("Should throw NPE when mapper return null");
202 } catch (NullPointerException npe) {
203 // expected
204 }
205
206 // Empty won't invoke mapper function
207 try {
208 Optional<Boolean> b = empty.flatMap(s -> null);
209 assertFalse(b.isPresent());
210 } catch (NullPointerException npe) {
211 fail("Mapper function should not be invoked");
212 }
213
214 // Map an empty value
215 Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));
216 assertFalse(l.isPresent());
217
218 // Map to value
219 Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);
220 l = duke.flatMap(s -> Optional.of(s.length()));
221 assertTrue(l.isPresent());
222 assertEquals(l.get().intValue(), 4);
223
224 // Verify same instance
225 l = duke.flatMap(s -> fixture);
226 assertSame(l, fixture);
227 }
228
229 private static class ObscureException extends RuntimeException {
230
231 }
232 }
|