View Javadoc
1   /*
2      Copyright 2011 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue;
17  
18  import org.hamcrest.Matchers;
19  import org.junit.Test;
20  
21  import java.io.IOException;
22  import java.util.NoSuchElementException;
23  import java.util.Optional;
24  
25  import static io.atlassian.fugue.Either.right;
26  import static io.atlassian.fugue.Eithers.getOrThrow;
27  import static io.atlassian.fugue.Option.none;
28  import static io.atlassian.fugue.Option.some;
29  import static io.atlassian.fugue.UtilityFunctions.bool2String;
30  import static io.atlassian.fugue.UtilityFunctions.int2String;
31  import static java.lang.String.valueOf;
32  import static java.util.stream.Collectors.toList;
33  import static org.hamcrest.Matchers.contains;
34  import static org.hamcrest.Matchers.empty;
35  import static org.hamcrest.Matchers.is;
36  import static org.junit.Assert.assertThat;
37  
38  public class EitherRightTest {
39    private static final Integer ORIGINAL_VALUE = 1;
40    final Either<Boolean, Integer> either = right(ORIGINAL_VALUE);
41  
42    @Test public void rightGet() {
43      assertThat(either.right().get(), is(ORIGINAL_VALUE));
44    }
45  
46    @Test public void rightIsDefined() {
47      assertThat(either.right().isDefined(), is(true));
48    }
49  
50    @Test public void leftIsDefined() {
51      assertThat(either.left().isDefined(), is(false));
52    }
53  
54    @Test public void isRight() {
55      assertThat(either.isRight(), is(true));
56    }
57  
58    @Test public void isLeft() {
59      assertThat(either.isLeft(), is(false));
60    }
61  
62    @Test public void getRight() {
63      assertThat(either.getRight(), is(1));
64    }
65  
66    @Test(expected = NoSuchElementException.class) public void getLeft() {
67      either.left().get();
68    }
69  
70    @Test public void swapIsLeft() {
71      assertThat(either.swap().isLeft(), is(true));
72    }
73  
74    @Test public void swapLeftIsEitherRight() {
75      assertThat(either.swap().left().get(), is(either.right().get()));
76    }
77  
78    @Test public void swapLeftIsOriginal() {
79      assertThat(either.swap().left().get(), is(ORIGINAL_VALUE));
80    }
81  
82    @Test public void map() {
83      assertThat(either.fold(bool2String, int2String), is(valueOf(ORIGINAL_VALUE)));
84    }
85  
86    @Test public void mapLeft() {
87      assertThat(either.left().map(bool2String).left().isEmpty(), is(true));
88    }
89  
90    @Test public void mapRight() {
91      assertThat(either.right().map(int2String).right().get(), is(valueOf(ORIGINAL_VALUE)));
92    }
93  
94    @Test public void toStringTest() {
95      assertThat(either.toString(), is("Either.Right(1)"));
96    }
97  
98    @Test public void hashCodeTest() {
99      assertThat(either.hashCode(), is(ORIGINAL_VALUE.hashCode()));
100   }
101 
102   @Test public void equalsItself() {
103     assertThat(either.equals(either), is(true));
104   }
105 
106   @Test public void notEqualsNull() {
107     assertThat(either.equals(null), is(false));
108   }
109 
110   @Test public void rightPredicateMatches() {
111     assertThat(Eithers.<Boolean, Integer> isRight().test(either), is(true));
112   }
113 
114   @Test public void toOptionTest() {
115     assertThat(either.right().toOption(), is(some(ORIGINAL_VALUE)));
116     assertThat(either.left().toOption(), is(none()));
117   }
118 
119   @Test public void toOptionalTest() {
120     assertThat(either.right().toOptional(), is(Optional.of(ORIGINAL_VALUE)));
121     assertThat(either.left().toOptional(), is(Optional.empty()));
122   }
123 
124   @Test public void toStreamTest() {
125     assertThat(either.right().toStream().collect(toList()), contains(ORIGINAL_VALUE));
126     assertThat(either.left().toStream().collect(toList()), empty());
127   }
128 
129   @Test public void notThrowsException() throws Exception {
130     final Either<IOException, String> either = right("boo yaa!");
131     assertThat(getOrThrow(either), is("boo yaa!"));
132   }
133 
134   @Test public void upcastRightOnRight() {
135     final Either<String, Integer> e = Either.right(1);
136     final Either<String, Number> result = Eithers.<String, Number, Integer> upcastRight(e);
137     final Number expected = 1;
138     assertThat(result.getRight(), is(expected));
139   }
140 
141   @Test public void upcastRightOnLeft() {
142     final Either<String, Integer> e = Either.left("a");
143     final Either<String, Number> result = Eithers.<String, Number, Integer> upcastRight(e);
144     assertThat(result.left().get(), is("a"));
145   }
146 
147   @Test public void flatMapRightSubTypes() {
148     class Type {}
149     class AnotherType extends Type {}
150 
151     final AnotherType anotherType = new AnotherType();
152     final Either<Boolean, AnotherType> r = Either.right(anotherType);
153 
154     final Either<Boolean, AnotherType> either = Either.<Boolean, Type> right(new Type()).right().flatMap(input -> r);
155 
156     final Type type = either.right().get();
157 
158     assertThat(type, Matchers.<Type> is(anotherType));
159   }
160 
161   @Test public void flatMapRightWithUpcastAndSubtypes() {
162     class Type {}
163     class MyType extends Type {}
164     class AnotherType extends Type {}
165 
166     final MyType myType = new MyType();
167     final AnotherType anotherType = new AnotherType();
168 
169     final Either<Boolean, MyType> r = Either.right(myType);
170     final Either<Boolean, AnotherType> r2 = Either.right(anotherType);
171 
172     final Either<Boolean, AnotherType> either = Eithers.<Boolean, Type, MyType> upcastRight(r).right().flatMap(input -> r2);
173 
174     final Type errorType = either.right().get();
175 
176     assertThat(errorType, Matchers.<Type> is(anotherType));
177   }
178 }