1+ package com .baeldung .java8 .optional ;
2+
3+ import org .junit .Before ;
4+ import org .junit .Test ;
5+
6+ import java .util .Optional ;
7+ import java .util .function .Supplier ;
8+ import java .util .stream .Stream ;
9+
10+ import static org .junit .Assert .*;
11+
12+ public class OptionalChainingUnitTest {
13+
14+ private boolean getEmptyEvaluated ;
15+ private boolean getHelloEvaluated ;
16+ private boolean getByeEvaluated ;
17+
18+ @ Before
19+ public void setUp () {
20+ getEmptyEvaluated = false ;
21+ getHelloEvaluated = false ;
22+ getByeEvaluated = false ;
23+ }
24+
25+ @ Test
26+ public void givenThreeOptionals_whenChaining_thenFirstNonEmptyIsReturned () {
27+ Optional <String > found = Stream .of (getEmpty (), getHello (), getBye ())
28+ .filter (Optional ::isPresent )
29+ .map (Optional ::get )
30+ .findFirst ();
31+
32+ assertEquals (getHello (), found );
33+ }
34+
35+ @ Test
36+ public void givenTwoEmptyOptionals_whenChaining_thenEmptyOptionalIsReturned () {
37+ Optional <String > found = Stream .of (getEmpty (), getEmpty ())
38+ .filter (Optional ::isPresent )
39+ .map (Optional ::get )
40+ .findFirst ();
41+
42+ assertFalse (found .isPresent ());
43+ }
44+
45+ @ Test
46+ public void givenTwoEmptyOptionals_whenChaining_thenDefaultIsReturned () {
47+ String found = Stream .<Supplier <Optional <String >>>of (
48+ () -> createOptional ("empty" ),
49+ () -> createOptional ("empty" )
50+ )
51+ .map (Supplier ::get )
52+ .filter (Optional ::isPresent )
53+ .map (Optional ::get )
54+ .findFirst ()
55+ .orElseGet (() -> "default" );
56+
57+ assertEquals ("default" , found );
58+ }
59+
60+ @ Test
61+ public void givenThreeOptionals_whenChaining_thenFirstNonEmptyIsReturnedAndRestNotEvaluated () {
62+ Optional <String > found = Stream .<Supplier <Optional <String >>>of (this ::getEmpty , this ::getHello , this ::getBye )
63+ .map (Supplier ::get )
64+ .filter (Optional ::isPresent )
65+ .map (Optional ::get )
66+ .findFirst ();
67+
68+ assertTrue (this .getEmptyEvaluated );
69+ assertTrue (this .getHelloEvaluated );
70+ assertFalse (this .getByeEvaluated );
71+ assertEquals (getHello (), found );
72+ }
73+
74+ @ Test
75+ public void givenTwoOptionalsReturnedByOneArgMethod_whenChaining_thenFirstNonEmptyIsReturned () {
76+ Optional <String > found = Stream .<Supplier <Optional <String >>>of (
77+ () -> createOptional ("empty" ),
78+ () -> createOptional ("hello" )
79+ )
80+ .map (Supplier ::get )
81+ .filter (Optional ::isPresent )
82+ .map (Optional ::get )
83+ .findFirst ();
84+
85+ assertEquals (createOptional ("hello" ), found );
86+ }
87+
88+ private Optional <String > getEmpty () {
89+ this .getEmptyEvaluated = true ;
90+ return Optional .empty ();
91+ }
92+
93+ private Optional <String > getHello () {
94+ this .getHelloEvaluated = true ;
95+ return Optional .of ("hello" );
96+ }
97+
98+ private Optional <String > getBye () {
99+ this .getByeEvaluated = true ;
100+ return Optional .of ("bye" );
101+ }
102+
103+ private Optional <String > createOptional (String input ) {
104+ if (input == null || input == "" || input == "empty" ) {
105+ return Optional .empty ();
106+ }
107+
108+ return Optional .of (input );
109+ }
110+ }
0 commit comments