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.Arrays;
21  import java.util.List;
22  
23  import static io.atlassian.fugue.Pair.pair;
24  import static org.hamcrest.Matchers.equalTo;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertThat;
27  
28  public class PairTest {
29    @Test(expected = NullPointerException.class) public void testNullLeft() {
30      pair(null, "");
31    }
32  
33    @Test(expected = NullPointerException.class) public void testNullRight() {
34      pair("", null);
35    }
36  
37    @Test public void left() {
38      assertThat(pair("left", "right").left(), equalTo("left"));
39    }
40  
41    @Test public void right() {
42      assertThat(pair("left", "right").right(), equalTo("right"));
43    }
44  
45    @Test public void toStringTest() {
46      assertThat(pair("hello", 4).toString(), equalTo("Pair(hello, 4)"));
47    }
48  
49    @Test public void hashCodeTest() {
50      assertThat(pair(1, 3).hashCode(), equalTo(65539));
51    }
52  
53    @Test public void notEqualToNull() {
54      assertThat(pair(1, 3).equals(null), equalTo(false));
55    }
56  
57    @Test public void equalToSelf() {
58      final Pair<Integer, Integer> pair = pair(1, 3);
59      assertThat(pair.equals(pair), equalTo(true));
60    }
61  
62    @Test public void notEqualToArbitraryObject() {
63      assertThat(pair(1, 3).equals(new Object()), equalTo(false));
64    }
65  
66    @Test public void notEqualLeft() {
67      assertThat(pair(1, 3).equals(pair(0, 3)), equalTo(false));
68    }
69  
70    @Test public void notEqualRight() {
71      assertThat(pair(1, 3).equals(pair(1, 0)), equalTo(false));
72    }
73  
74    @Test public void equalsSameValue() {
75      assertThat(pair(1, 3).equals(pair(1, 3)), equalTo(true));
76    }
77  
78    @Test public void leftFunction() {
79      assertEquals(pairs().get(0).left().longValue(), 1);
80      assertEquals(pairs().get(1).left().longValue(), 2);
81      assertEquals(pairs().get(2).left().longValue(), 3);
82    }
83  
84    @Test public void rightFunction() {
85      assertEquals(pairs().get(0).right(), "1");
86      assertEquals(pairs().get(1).right(), "2");
87      assertEquals(pairs().get(2).right(), "3");
88    }
89  
90    private List<Pair<Integer, String>> pairs() {
91      return Arrays.asList(pair(1, "1"), pair(2, "2"), pair(3, "3"));
92    }
93  }