View Javadoc

1   // Copyright (C) 2009 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.util;
23  
24  import junit.framework.TestCase;
25  
26  import static net.grinder.testutility.AssertUtilities.assertNotEquals;
27  
28  
29  /**
30   * Unit test for {@link Pair}.
31   *
32   * @author Philip Aston
33   * @version $Revision:$
34   */
35  public class TestPair extends TestCase {
36  
37    public void testBasics() throws Exception {
38      final Pair<Integer, String> p1 = Pair.of(3, "123");
39  
40      assertEquals(new Integer(3), p1.getFirst());
41      assertEquals("123", p1.getSecond());
42  
43      assertEquals("(3, 123)", p1.toString());
44  
45      final Pair<Integer, String> p2 = Pair.of((Integer)null, "123");
46      assertNull(p2.getFirst());
47      assertEquals("(null, 123)", p2.toString());
48    }
49  
50    public void testEquality() throws Exception {
51  
52      final Pair<Integer, String> p1 = Pair.of(3, "123");
53  
54      assertEquals(p1, p1);
55      assertEquals(p1.hashCode(), p1.hashCode());
56      assertNotEquals(p1, null);
57      assertNotEquals(p1, this);
58  
59      final Pair<Integer, String> p2 = Pair.of(4, "123");
60  
61      assertNotEquals(p1, p2);
62      assertEquals(p2, p2);
63  
64      final Pair<Integer, String> p3 = Pair.of(3, "123");
65  
66      assertEquals(p1, p3);
67      assertEquals(p1.hashCode(), p3.hashCode());
68  
69      final Pair<Integer, String> p4 = Pair.of((Integer)null, (String)null);
70      assertEquals(p4, p4);
71      assertEquals(p4.hashCode(), p4.hashCode());
72      assertNotEquals(p4, p3);
73  
74      final Pair<Integer, String> p5 = Pair.of(3, (String)null);
75      assertNotEquals(p3, p5);
76      assertNotEquals(p5, p3);
77      assertEquals(p5, p5);
78      assertEquals(p5.hashCode(), p5.hashCode());
79    }
80  }