KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > util > ValueLabelPairTest


1 /**
2  * Feb 23, 2005
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.util;
19
20 import junit.framework.TestCase;
21
22 /**
23  * Tests {@link net.sf.uitags.util.ValueLabelPair}.
24  *
25  * @author hgani
26  * @version $Id: ValueLabelPairTest.java,v 1.1.1.1 2006/02/26 11:39:22 hgani Exp $
27  */

28 public class ValueLabelPairTest extends TestCase {
29
30   /**
31    * Instance of the class under test.
32    */

33   private ValueLabelPair pair;
34
35   /**
36    * Main method for the tests.
37    *
38    * @param args main arguments
39    */

40   public static void main(String JavaDoc[] args) {
41     junit.textui.TestRunner.run(ValueLabelPairTest.class);
42   }
43
44   /** {@inheritDoc} */
45   protected void setUp() throws Exception JavaDoc {
46     super.setUp();
47     this.pair = new ValueLabelPair("value1", "label1");
48   }
49
50   /** {@inheritDoc} */
51   protected void tearDown() throws Exception JavaDoc {
52     super.tearDown();
53     this.pair = null;
54   }
55
56   /**
57    * Ensures that all getters of this class return proper values.
58    */

59   public void testGetStrings() {
60     assertEquals("value1", this.pair.getValueAsString());
61     assertEquals("label1", this.pair.getLabelAsString());
62     assertEquals(this.pair.getValueAsString(), this.pair.getKey());
63     assertEquals(this.pair.getLabelAsString(), this.pair.getValue());
64   }
65
66   /**
67    * Ensures that {@link ValueLabelPair#equals(Object)}
68    * correctly compares two objects.
69    */

70   public void testEquals() {
71     // same value, diff label
72
ValueLabelPair pair1 = new ValueLabelPair("value1", "label2");
73     // diff value, same label
74
ValueLabelPair pair2 = new ValueLabelPair("value2", "label1");
75     // same value, same label
76
ValueLabelPair pair3 = new ValueLabelPair("value1", "label1");
77     // diff value, diff label
78
ValueLabelPair pair4 = new ValueLabelPair("value2", "label2");
79
80     // use assertTrue instead of assertEquals, because in this case
81
// we just want to know both are equal, we don't have certain
82
// value that we expect to have
83
assertTrue(this.pair.equals(pair1));
84     assertTrue(!this.pair.equals(pair2));
85     assertTrue(this.pair.equals(pair3));
86     assertTrue(!this.pair.equals(pair4));
87   }
88
89   /**
90    * Ensures that {@link ValueLabelPair#hashCode()}
91    * returns the same hash code value when the objects are equal.
92    */

93   public void testHashCode() {
94     // we can only test objects with the same hashcode, because
95
// there is no guarantee that different objects will have
96
// different hash code
97
ValueLabelPair tempPair = new ValueLabelPair("value1", "label1");
98
99     assertEquals(this.pair.hashCode(), tempPair.hashCode());
100   }
101 }
102
Popular Tags