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.junit.Test;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.function.Function;
27  
28  import static io.atlassian.fugue.Option.none;
29  import static io.atlassian.fugue.Option.option;
30  import static io.atlassian.fugue.Option.some;
31  import static io.atlassian.fugue.Suppliers.ofInstance;
32  import static org.hamcrest.Matchers.is;
33  import static org.hamcrest.Matchers.sameInstance;
34  import static org.junit.Assert.assertThat;
35  
36  public class OptionSomeTest {
37    private static final Integer ORIGINAL_VALUE = 1;
38    private static final Integer NOT_IN_SOME = 3;
39    Option<Integer> some = some(ORIGINAL_VALUE);
40  
41    @Test public void get() {
42      assertThat(some.get(), is(ORIGINAL_VALUE));
43    }
44  
45    @Test public void isSet() {
46      assertThat(some.isDefined(), is(true));
47    }
48  
49    @Test public void getOrElse() {
50      assertThat(some.getOrElse(NOT_IN_SOME), is(ORIGINAL_VALUE));
51    }
52  
53    @Test public void getOrNull() {
54      assertThat(some.getOrNull(), is(ORIGINAL_VALUE));
55    }
56  
57    @Test(expected = NullPointerException.class) public void someNull() {
58      some(null);
59    }
60  
61    @Test(expected = NullPointerException.class) public void mapForNull() {
62      some.map(null);
63    }
64  
65    @Test(expected = NullPointerException.class) public void filterForNull() {
66      some.filter(null);
67    }
68  
69    @Test public void positiveFilter() {
70      assertThat(some.filter(x -> true).get(), is(ORIGINAL_VALUE));
71    }
72  
73    @Test public void negativeFilter() {
74      assertThat(some.filter(x -> false).isDefined(), is(false));
75    }
76  
77    @Test public void existsTrueReturnsTrue() {
78      assertThat(some.exists(x -> true), is(true));
79    }
80  
81    @Test public void existsFalseReturnsFalse() {
82      assertThat(some.exists(x -> false), is(false));
83    }
84  
85    @Test public void toLeftReturnsLeft() {
86      assertThat(some.toLeft(ofInstance("")).isLeft(), is(true));
87    }
88  
89    @Test public void toRightReturnsRight() {
90      assertThat(some.toRight(ofInstance("")).isRight(), is(true));
91    }
92  
93    @Test public void iteratorHasNext() {
94      assertThat(some.iterator().hasNext(), is(true));
95    }
96  
97    @Test public void iteratorNext() {
98      final Iterator<Integer> iterator = some.iterator();
99      assertThat(iterator.next(), is(ORIGINAL_VALUE));
100     assertThat(iterator.hasNext(), is(false));
101   }
102 
103   @Test(expected = UnsupportedOperationException.class) public void iteratorImmutable() {
104     final Iterator<Integer> iterator = some.iterator();
105     iterator.next();
106     iterator.remove();
107   }
108 
109   @Test public void foreach() {
110     assertThat(Count.countEach(some), is(1));
111   }
112 
113   @Test public void forallTrue() {
114     assertThat(some.forall(x -> true), is(true));
115   }
116 
117   @Test public void forallFalse() {
118     assertThat(some.forall(x -> false), is(false));
119   }
120 
121   @Test public void toStringTest() {
122     assertThat(some.toString(), is("some(1)"));
123   }
124 
125   @Test public void equalsItself() {
126     assertThat(some.equals(some), is(true));
127   }
128 
129   @Test public void notEqualsNone() {
130     assertThat(some.equals(none(Integer.class)), is(false));
131   }
132 
133   @Test public void notEqualsNull() {
134     assertThat(some.equals(null), is(false));
135   }
136 
137   @Test public void hashDoesNotThrowException() {
138     some.hashCode();
139   }
140 
141   static class MyException extends Exception {
142     private static final long serialVersionUID = -1056362494708225175L;
143   }
144 
145   @Test public void getOrThrow() throws MyException {
146     assertThat(some.getOrThrow(MyException::new), is(ORIGINAL_VALUE));
147   }
148 
149   @Test public void map() {
150     assertThat(some.map(i -> i + 1).get(), is(2));
151   }
152 
153   @Test public void superTypesPermittedOnFilter() {
154     final ArrayList<Integer> list = new ArrayList<>(2);
155     Collections.addAll(list, 1, 2);
156     final Option<ArrayList<Integer>> option = option(list);
157     final Option<ArrayList<Integer>> nopt = option.filter(x -> true);
158     assertThat(nopt, sameInstance(option));
159   }
160 
161   @Test public void superTypesPermittedOnMap() {
162     final ArrayList<Integer> list = new ArrayList<>(2);
163     Collections.addAll(list, 1, 2);
164     final Option<ArrayList<Integer>> option = option(list);
165     final Option<Set<Number>> set = option.map(new Function<List<Integer>, Set<Number>>() {
166       public Set<Number> apply(final List<Integer> list) {
167         return new HashSet<>(list);
168       }
169     });
170     assertThat(set.get().size(), is(option.get().size()));
171   }
172 }