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.NoSuchElementException;
21  import java.util.concurrent.atomic.AtomicInteger;
22  import java.util.function.Supplier;
23  
24  import static org.hamcrest.MatcherAssert.assertThat;
25  import static org.hamcrest.Matchers.is;
26  
27  public class SuppliersTest {
28  
29    @Test public void ofInstance() {
30      final Integer instance = 29;
31      final Supplier<Integer> integerSupplier = Suppliers.ofInstance(instance);
32  
33      assertThat(integerSupplier.get(), is(29));
34  
35      // do it a few more times to ensure that it keeps returning what we want.
36      assertThat(integerSupplier.get(), is(29));
37      assertThat(integerSupplier.get(), is(29));
38    }
39  
40    @Test public void alwaysTrue() {
41      final Supplier<Boolean> alwaysTrue = Suppliers.alwaysTrue();
42  
43      assertThat(alwaysTrue.get(), is(true));
44      assertThat(alwaysTrue.get(), is(true));
45      assertThat(alwaysTrue.get(), is(true));
46    }
47  
48    @Test public void alwaysFalse() {
49      final Supplier<Boolean> alwaysFalse = Suppliers.alwaysFalse();
50  
51      assertThat(alwaysFalse.get(), is(false));
52      assertThat(alwaysFalse.get(), is(false));
53      assertThat(alwaysFalse.get(), is(false));
54    }
55  
56    @Test public void alwaysNull() {
57      final Supplier<Object> alwaysNull = Suppliers.alwaysNull();
58  
59      final Object nully = null;
60      assertThat(alwaysNull.get(), is(nully));
61      assertThat(alwaysNull.get(), is(nully));
62      assertThat(alwaysNull.get(), is(nully));
63    }
64  
65    @Test public void fromOptionCallsSome() {
66      assertThat(Suppliers.fromOption(Option.some("test")).get(), is("test"));
67    }
68  
69    @Test(expected = NoSuchElementException.class) public void fromOptionNoneThrows() {
70      Suppliers.fromOption(Option.none()).get();
71    }
72  
73    @Test public void fromFunctionInt() {
74      assertThat(Suppliers.fromFunction(UtilityFunctions.square, 4).get(), is(16));
75    }
76  
77    @Test public void fromFunctionString() {
78      assertThat(Suppliers.fromFunction(UtilityFunctions.reverse, "collywobble").get(), is("elbbowylloc"));
79    }
80  
81    @Test public void memoize() {
82      AtomicInteger intRef = new AtomicInteger(1);
83      Supplier<Integer> memoize = Suppliers.memoize(() -> intRef.get());
84  
85      assertThat(memoize.get(), is(1));
86      intRef.set(2);
87      assertThat(memoize.get(), is(1));
88    }
89  
90    @Test public void weakMemoize() {
91      AtomicInteger intRef = new AtomicInteger(Integer.valueOf(1));
92      Supplier<Integer> weakMemoized = Suppliers.weakMemoize(() -> intRef.get());
93  
94      assertThat(weakMemoized.get(), is(1));
95      intRef.set(Integer.valueOf(2));
96      assertThat(weakMemoized.get(), is(1));
97    }
98  
99  }