View Javadoc
1   package io.atlassian.fugue;
2   
3   import org.hamcrest.Matchers;
4   import org.junit.Test;
5   
6   import java.util.Optional;
7   import java.util.concurrent.atomic.AtomicBoolean;
8   import java.util.function.Function;
9   
10  import static io.atlassian.fugue.Either.left;
11  import static io.atlassian.fugue.Either.right;
12  import static io.atlassian.fugue.EitherRightProjectionTest.reverseToEither;
13  import static io.atlassian.fugue.Option.none;
14  import static io.atlassian.fugue.Option.some;
15  import static io.atlassian.fugue.UtilityFunctions.addOne;
16  import static io.atlassian.fugue.UtilityFunctions.reverse;
17  import static java.util.stream.Collectors.toList;
18  import static org.hamcrest.Matchers.contains;
19  import static org.hamcrest.Matchers.empty;
20  import static org.hamcrest.Matchers.is;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertThat;
23  import static org.junit.Assert.fail;
24  
25  public class EitherRightBiasTest {
26    private final Either<String, Integer> l = left("heyaa!");
27    private final Either<String, Integer> r = right(12);
28  
29    @Test public void mapRight() {
30      assertThat(Either.<String, Integer> right(3).map(addOne), is(Either.<String, Integer> right(4)));
31    }
32  
33    @Test public void mapLeft() {
34      assertThat(Either.<String, Integer> left("foo").map(addOne), is(Either.<String, Integer> left("foo")));
35    }
36  
37    @Test public void flatMapRight() {
38      assertThat(Either.<Integer, String> right("!foo").flatMap(reverseToEither), is(Either.<Integer, String> right("oof!")));
39    }
40  
41    @Test public void flatMapLeft() {
42      assertThat(Either.<Integer, String> left(5).flatMap(reverseToEither), is(Either.<Integer, String> left(5)));
43    }
44  
45    @Test public void leftMapRight() {
46      assertThat(Either.<Integer, String> right("foo").leftMap(addOne), is(Either.<Integer, String> right("foo")));
47    }
48  
49    @Test public void leftMapLeft() {
50      assertThat(Either.<Integer, String> left(3).leftMap(addOne), is(Either.<Integer, String> left(4)));
51    }
52  
53    @Test public void getOrRight() {
54      assertThat(r.getOr(Suppliers.ofInstance(1)), is(12));
55    }
56  
57    @Test public void getOrLeft() {
58      assertThat(l.getOr(Suppliers.ofInstance(1)), is(1));
59    }
60  
61    @Test public void getOrElseSupplierRight() {
62      @SuppressWarnings("deprecation")
63      final Integer orElse = r.getOrElse(Suppliers.ofInstance(1));
64      assertThat(orElse, is(12));
65    }
66  
67    @Test public void getOrElseSupplierLeft() {
68      @SuppressWarnings("deprecation")
69      final Integer orElse = l.getOrElse(Suppliers.ofInstance(1));
70      assertThat(orElse, is(1));
71    }
72  
73    @Test public void getOrElseRight() {
74      assertThat(r.getOrElse(1), is(12));
75    }
76  
77    @Test public void getOrElseLeft() {
78      assertThat(l.getOrElse(1), is(1));
79    }
80  
81    @Test public void getOrNullRight() {
82      assertThat(r.getOrNull(), is(12));
83    }
84  
85    @Test public void getOrNullLeft() {
86      assertNull(l.getOrNull());
87    }
88  
89    @Test public void getOrErrorRight() {
90      assertThat(r.getOrError(Suppliers.ofInstance("Error message")), is(12));
91    }
92  
93    @Test(expected = AssertionError.class) public void getOrErrorLeft() {
94      l.getOrError(Suppliers.ofInstance("Error message"));
95    }
96  
97    @Test public void getOrErrorLeftMessage() {
98      try {
99        l.getOrError(Suppliers.ofInstance("Error message"));
100     } catch (final Error e) {
101       assertThat(e.getMessage(), is("Error message"));
102       return;
103     }
104 
105     fail("No error thrown");
106   }
107 
108   @Test public void getOrThrowRight() {
109     assertThat(r.getOrThrow(Suppliers.ofInstance(new RuntimeException("Run Error"))), is(12));
110   }
111 
112   private class CustomException extends RuntimeException {
113     private static final long serialVersionUID = -633224822465345980L;
114   }
115 
116   @Test(expected = CustomException.class) public void getOrThrowLeft() {
117     l.getOrThrow(Suppliers.ofInstance(new CustomException()));
118   }
119 
120   @Test public void existsRight() {
121     assertThat(r.exists(x -> x == 12), is(true));
122     assertThat(r.exists(x -> x == 11), is(false));
123   }
124 
125   @Test public void existsLeft() {
126     assertThat(l.exists(x -> x == 12), is(false));
127   }
128 
129   @Test public void forallRight() {
130     assertThat(r.forall(x -> x == 12), is(true));
131     assertThat(r.forall(x -> x == 11), is(false));
132   }
133 
134   @Test public void forallLeft() {
135     assertThat(l.forall(x -> x == 12), is(true));
136   }
137 
138   @Test public void foreachRight() {
139     final AtomicBoolean called = new AtomicBoolean(false);
140     @SuppressWarnings("deprecation")
141     final Effect<Integer> effect = integer -> called.set(true);
142 
143     r.forEach(effect);
144 
145     assertThat(called.get(), is(true));
146   }
147 
148   @Test public void foreachLeft() {
149     final AtomicBoolean called = new AtomicBoolean(false);
150     @SuppressWarnings("deprecation")
151     final Effect<Integer> effect = integer -> called.set(true);
152 
153     l.forEach(effect);
154 
155     assertThat(called.get(), is(false));
156   }
157 
158   @Test public void filterRight() {
159     assertThat(r.filter(x -> x == 12), is(some(r)));
160     assertThat(r.filter(x -> x == 11), is(Option.<Either<String, Integer>> none()));
161   }
162 
163   @Test public void filterLeft() {
164     assertThat(l.filter(x -> x == 12), is(Option.<Either<String, Integer>> none()));
165   }
166 
167   @Test public void filterOrElseUnsatisfiedHandlerRight() {
168     assertThat(r.filterOrElse(x -> x == 12, () -> "else"), is(r));
169     assertThat(r.filterOrElse(x -> x == 11, () -> "else"), is(left("else")));
170   }
171 
172   @Test public void filterOrElseUnsatisfiedHandlerLeft() {
173     assertThat(l.filterOrElse(x -> x == 12, () -> "else"), is(l));
174     assertThat(l.filterOrElse(x -> x == 11, () -> "else"), is(l));
175   }
176 
177   @Test public void orElseRightInstance() {
178     assertThat(r.orElse(Either.<String, Integer> right(44)), is(r));
179   }
180 
181   @Test public void orElseLeftInstance() {
182     assertThat(l.orElse(Either.<String, Integer> right(44)), is(Either.<String, Integer> right(44)));
183     assertThat(l.orElse(Either.<String, Integer> left("left")), is(Either.<String, Integer> left("left")));
184   }
185 
186   @Test public void orElseRightSupplier() {
187     assertThat(r.orElse(Suppliers.ofInstance(Either.<String, Integer> right(44))), is(r));
188   }
189 
190   @Test public void orElseLeftSupplier() {
191     assertThat(l.orElse(Suppliers.ofInstance(Either.<String, Integer> right(44))), is(Either.<String, Integer> right(44)));
192     assertThat(l.orElse(Suppliers.ofInstance(Either.<String, Integer> left("left"))), is(Either.<String, Integer> left("left")));
193   }
194 
195   @Test public void orElseChild() {
196     class Parent {}
197     class Child extends Parent {}
198 
199     final Parent p = new Parent();
200     final Parent pp = right(p).orElse(Suppliers.ofInstance(Either.<Integer, Child> right(new Child()))).getOrNull();
201     assertThat(pp, is(p));
202   }
203 
204   @Test public void rightOr_valueIsARight_returnsRightValue() {
205     assertThat(r.rightOr(Functions.constant(99)), is(12));
206   }
207 
208   @Test public void rightOr_valueIsALeft_returnsLeftTransformerResult() {
209     assertThat(l.rightOr(Functions.constant(99)), is(99));
210   }
211 
212   @Test public void leftOr_valueIsALeft_returnsLeftValue() {
213     assertThat(l.leftOr(Functions.constant("s")), is("heyaa!"));
214   }
215 
216   @Test public void leftOr_valueIsARight_returnsRightTransformerResult() {
217     assertThat(r.leftOr(Functions.constant("s")), is("s"));
218   }
219 
220   @Test public void flatMapSubTypesOnLeft() {
221     class ErrorType {}
222     class AnotherErrorType extends ErrorType {}
223 
224     final AnotherErrorType anotherErrorType = new AnotherErrorType();
225     final Either<AnotherErrorType, Long> l = Either.left(anotherErrorType);
226 
227     final Either<ErrorType, Long> longEither = Either.<ErrorType, Integer> right(1).flatMap(input -> l);
228 
229     final ErrorType errorType = longEither.left().get();
230 
231     assertThat(errorType, Matchers.<ErrorType> is(anotherErrorType));
232   }
233 
234   @Test public void flatMapWithUpcastAndSubtypesOnLeft() {
235     class ErrorType {}
236     class MyErrorType extends ErrorType {}
237     class AnotherErrorType extends ErrorType {}
238 
239     final AnotherErrorType anotherErrorType = new AnotherErrorType();
240 
241     final Either<MyErrorType, Boolean> l = Either.right(true);
242     final Either<AnotherErrorType, Long> l2 = Either.left(anotherErrorType);
243 
244     final Either<ErrorType, Long> either = Eithers.<ErrorType, MyErrorType, Boolean> upcastLeft(l).flatMap(input -> l2);
245 
246     final ErrorType errorType = either.left().get();
247 
248     assertThat(errorType, Matchers.<ErrorType> is(anotherErrorType));
249   }
250 
251   @Test public void toOptionRight() {
252     assertThat(r.toOption(), is(some(12)));
253   }
254 
255   @Test public void toOptionLeft() {
256     assertThat(l.toOption(), is(none()));
257   }
258 
259   @Test public void toStreamRight() {
260     assertThat(r.toStream().collect(toList()), contains(12));
261   }
262 
263   @Test public void toStreamLeft() {
264     assertThat(l.toStream().collect(toList()), empty());
265   }
266 
267   @Test public void toOptionalRight() {
268     assertThat(r.toOptional(), is(Optional.of(12)));
269   }
270 
271   @Test public void toOptionalLeft() {
272     assertThat(l.toOptional(), is(Optional.empty()));
273   }
274 
275   @Test public void sequenceDefined() {
276     final Either<String, Integer> e = right(98);
277     assertThat(r.sequence(e).right().get(), is(98));
278   }
279 
280   @Test public void sequenceNotDefined() {
281     final Either<String, Integer> e = left("bar");
282     assertThat(l.sequence(e).left().get(), is("heyaa!"));
283   }
284 
285   @Test public void apDefinedRight() {
286     final Either<String, Function<Integer, String>> func = right(reverse.compose(Object::toString));
287     assertThat(r.ap(func).right().get(), is("21"));
288   }
289 
290   @Test public void apDefinedLeft() {
291     final Either<String, Function<Integer, String>> func = left("woo");
292     assertThat(r.ap(func).left().get(), is("woo"));
293   }
294 
295   @Test public void apNotDefinedRight() {
296     final Either<String, Function<Integer, String>> func = right(reverse.compose(Object::toString));
297     assertThat(l.ap(func).left().get(), is("heyaa!"));
298   }
299 
300   @Test public void apNotDefinedLeft() {
301     final Either<String, Function<Integer, String>> func = left("woo");
302     assertThat(l.ap(func).left().get(), is("woo"));
303   }
304 
305 }