1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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
10 * Bernd Hufmann - Initial design and implementation
11 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.util
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertTrue
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.util
.Pair
;
20 import org
.junit
.Test
;
23 * Test case for Pair class.
25 * @author Bernd Hufmann
27 @SuppressWarnings("javadoc")
28 public class PairTest
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 Pair
<String
, Long
> fPair1
= new Pair
<String
, Long
>("String 1", 1L);
35 Pair
<String
, Long
> fPair2
= new Pair
<String
, Long
>("String 2", 2L);
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 public void testToString() {
43 String result
= fPair1
.toString();
44 assertEquals("(String 1, 1)", result
);
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
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());
57 myPair
.setFirst("Hello");
58 assertEquals("Hello", myPair
.getFirst());
60 myPair
.setSecond(123L);
61 assertEquals(Long
.valueOf(123L), myPair
.getSecond());
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 public void testEqualsReflexivity() {
70 assertTrue("equals", fPair1
.equals(fPair1
));
71 assertTrue("equals", fPair2
.equals(fPair2
));
73 assertTrue("equals", !fPair1
.equals(fPair2
));
74 assertTrue("equals", !fPair2
.equals(fPair1
));
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());
82 assertTrue("equals", info1
.equals(fPair1
));
83 assertTrue("equals", fPair1
.equals(info1
));
85 assertTrue("equals", info2
.equals(fPair2
));
86 assertTrue("equals", fPair2
.equals(info2
));
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());
95 assertTrue("equals", info1
.equals(info2
));
96 assertTrue("equals", info2
.equals(info3
));
97 assertTrue("equals", info1
.equals(info3
));
101 public void testEqualsNull() {
102 assertTrue("equals", !fPair1
.equals(null));
103 assertTrue("equals", !fPair2
.equals(null));
107 public void testEqualsDifferentObj() {
108 Pair
<Long
, String
> info
= new Pair
<Long
, String
>(1L, "String1");
109 assertTrue("equals", !fPair1
.equals(info
));
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
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());
121 assertTrue("hashCode", fPair1
.hashCode() == info1
.hashCode());
122 assertTrue("hashCode", fPair2
.hashCode() == info2
.hashCode());
124 assertTrue("hashCode", fPair1
.hashCode() != info2
.hashCode());
125 assertTrue("hashCode", fPair2
.hashCode() != info1
.hashCode());
This page took 0.032388 seconds and 5 git commands to generate.