Commit | Line | Data |
---|---|---|
d4a8d935 | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2011, 2013 Ericsson |
d4a8d935 BH |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial design and implementation | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
d4a8d935 BH |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.tests.util; | |
15 | ||
6e1886bc AM |
16 | import static org.junit.Assert.assertEquals; |
17 | import static org.junit.Assert.assertTrue; | |
d4a8d935 BH |
18 | |
19 | import org.eclipse.linuxtools.tmf.core.util.Pair; | |
6e1886bc | 20 | import org.junit.Test; |
d4a8d935 BH |
21 | |
22 | /** | |
23 | * Test case for Pair class. | |
24 | * | |
25 | * @author Bernd Hufmann | |
26 | */ | |
cad06250 | 27 | @SuppressWarnings("javadoc") |
6e1886bc | 28 | public class PairTest { |
d4a8d935 BH |
29 | |
30 | // ------------------------------------------------------------------------ | |
31 | // Field(s) | |
32 | // ------------------------------------------------------------------------ | |
6e1886bc | 33 | |
d4a8d935 BH |
34 | Pair<String, Long> fPair1 = new Pair<String, Long>("String 1", 1L); |
35 | Pair<String, Long> fPair2 = new Pair<String, Long>("String 2", 2L); | |
36 | ||
d4a8d935 BH |
37 | // ------------------------------------------------------------------------ |
38 | // to String | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
6e1886bc | 41 | @Test |
d4a8d935 BH |
42 | public void testToString() { |
43 | String result = fPair1.toString(); | |
44 | assertEquals("(String 1, 1)", result); | |
45 | } | |
46 | ||
47 | // ------------------------------------------------------------------------ | |
48 | // Setters/Getters | |
49 | // ------------------------------------------------------------------------ | |
50 | ||
6e1886bc | 51 | @Test |
d4a8d935 BH |
52 | public void testAccessors() { |
53 | Pair<String, Long> myPair = new Pair<String, Long>("String 1", 1L); | |
54 | assertEquals("String 1", myPair.getFirst()); | |
55 | assertEquals(Long.valueOf(1L), myPair.getSecond()); | |
56 | ||
57 | myPair.setFirst("Hello"); | |
58 | assertEquals("Hello", myPair.getFirst()); | |
59 | ||
60 | myPair.setSecond(123L); | |
61 | assertEquals(Long.valueOf(123L), myPair.getSecond()); | |
62 | } | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // equals | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
6e1886bc | 68 | @Test |
d4a8d935 BH |
69 | public void testEqualsReflexivity() { |
70 | assertTrue("equals", fPair1.equals(fPair1)); | |
71 | assertTrue("equals", fPair2.equals(fPair2)); | |
72 | ||
73 | assertTrue("equals", !fPair1.equals(fPair2)); | |
74 | assertTrue("equals", !fPair2.equals(fPair1)); | |
75 | } | |
76 | ||
6e1886bc | 77 | @Test |
d4a8d935 BH |
78 | public void testEqualsSymmetry() { |
79 | Pair<String, Long> info1 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue()); | |
80 | Pair<String, Long> info2 = new Pair<String, Long>(fPair2.getFirst(), fPair2.getSecond().longValue()); | |
81 | ||
82 | assertTrue("equals", info1.equals(fPair1)); | |
83 | assertTrue("equals", fPair1.equals(info1)); | |
84 | ||
85 | assertTrue("equals", info2.equals(fPair2)); | |
86 | assertTrue("equals", fPair2.equals(info2)); | |
87 | } | |
88 | ||
6e1886bc | 89 | @Test |
d4a8d935 BH |
90 | public void testEqualsTransivity() { |
91 | Pair<String, Long> info1 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue()); | |
92 | Pair<String, Long> info2 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue()); | |
93 | Pair<String, Long> info3 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue()); | |
94 | ||
95 | assertTrue("equals", info1.equals(info2)); | |
96 | assertTrue("equals", info2.equals(info3)); | |
97 | assertTrue("equals", info1.equals(info3)); | |
98 | } | |
99 | ||
6e1886bc | 100 | @Test |
d4a8d935 BH |
101 | public void testEqualsNull() { |
102 | assertTrue("equals", !fPair1.equals(null)); | |
103 | assertTrue("equals", !fPair2.equals(null)); | |
104 | } | |
105 | ||
6e1886bc | 106 | @Test |
d4a8d935 BH |
107 | public void testEqualsDifferentObj() { |
108 | Pair<Long, String> info = new Pair<Long, String>(1L, "String1"); | |
109 | assertTrue("equals", !fPair1.equals(info)); | |
110 | } | |
111 | ||
112 | // ------------------------------------------------------------------------ | |
113 | // hashCode | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
6e1886bc | 116 | @Test |
d4a8d935 BH |
117 | public void testHashCode() { |
118 | Pair<String, Long> info1 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue()); | |
119 | Pair<String, Long> info2 = new Pair<String, Long>(fPair2.getFirst(), fPair2.getSecond().longValue()); | |
120 | ||
121 | assertTrue("hashCode", fPair1.hashCode() == info1.hashCode()); | |
122 | assertTrue("hashCode", fPair2.hashCode() == info2.hashCode()); | |
123 | ||
124 | assertTrue("hashCode", fPair1.hashCode() != info2.hashCode()); | |
125 | assertTrue("hashCode", fPair2.hashCode() != info1.hashCode()); | |
126 | } | |
127 | } |