KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > audit > KeyedValueTest


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.audit;
11
12 import java.util.*;
13 import org.jgap.*;
14 import junit.framework.*;
15
16 /**
17  * Tests for KeyedValue class
18  *
19  * @author Klaus Meffert
20  * @since 2.2
21  */

22 public class KeyedValueTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
26
27   public void setUp() {
28     super.setUp();
29   }
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(KeyedValueTest.class);
33     return suite;
34   }
35
36   /**
37    * @throws Exception
38    *
39    * @author Klaus Meffert
40    * @since 2.6
41    */

42   public void testConstruct_0()
43       throws Exception JavaDoc {
44     Comparable JavaDoc key = new Double JavaDoc(2.3d);
45     Number JavaDoc value = new Double JavaDoc(4.5d);
46     KeyedValue kv = new KeyedValue(key, value);
47     assertSame(key, kv.getKey());
48     assertSame(value, kv.getValue());
49     value = new Double JavaDoc(8.5d);
50     kv.setValue(value);
51     assertSame(value, kv.getValue());
52   }
53
54   /**
55    * @throws Exception
56    *
57    * @author Klaus Meffert
58    * @since 3.0
59    */

60   public void testConstruct_1()
61       throws Exception JavaDoc {
62     Number JavaDoc value = new Double JavaDoc(4.5d);
63     KeyedValue kv = new KeyedValue(null, value);
64     assertNull(kv.getKey());
65     assertSame(value, kv.getValue());
66   }
67
68   /**
69    * @throws Exception
70    *
71    * @author Klaus Meffert
72    * @since 3.0
73    */

74   public void testConstruct_2()
75       throws Exception JavaDoc {
76     KeyedValue kv = new KeyedValue(null, null);
77     assertNull(kv.getKey());
78     assertNull(kv.getValue());
79     assertTrue(kv.hashCode() < 0);
80   }
81
82   /**
83    * @throws Exception
84    *
85    * @author Klaus Meffert
86    * @since 2.6
87    */

88   public void testClone_0()
89       throws Exception JavaDoc {
90     Comparable JavaDoc key = new Double JavaDoc(2.3d);
91     Number JavaDoc value = new Double JavaDoc(4.5d);
92     KeyedValue kv = new KeyedValue(key, value);
93     KeyedValue clone = (KeyedValue) kv.clone();
94     assertEquals(clone, kv);
95     assertSame(key, clone.getKey());
96     assertSame(value, clone.getValue());
97   }
98
99   /**
100    * @throws Exception
101    *
102    * @author Klaus Meffert
103    * @since 3.0
104    */

105   public void testEquals_0()
106       throws Exception JavaDoc {
107     Comparable JavaDoc key = new Double JavaDoc(2.3d);
108     Number JavaDoc value = new Double JavaDoc(4.5d);
109     KeyedValue kv = new KeyedValue(key, value);
110     assertFalse(kv.equals(null));
111     assertTrue(kv.equals(kv));
112     assertFalse(kv.equals(new Vector()));
113   }
114
115   /**
116    * @throws Exception
117    *
118    * @author Klaus Meffert
119    * @since 3.0
120    */

121   public void testEquals_1()
122       throws Exception JavaDoc {
123     Comparable JavaDoc key = new Double JavaDoc(2.3d);
124     Number JavaDoc value = new Double JavaDoc(4.5d);
125     KeyedValue kv = new KeyedValue(key, value);
126     Comparable JavaDoc key2 = new Double JavaDoc(2.3d);
127     Number JavaDoc value2 = new Double JavaDoc(14.5d);
128     KeyedValue kv2 = new KeyedValue(key2, value2);
129     assertFalse(kv.equals(kv2));
130     assertFalse(kv2.equals(kv));
131   }
132
133   /**
134    * @throws Exception
135    *
136    * @author Klaus Meffert
137    * @since 3.0
138    */

139   public void testEquals_2()
140       throws Exception JavaDoc {
141     Comparable JavaDoc key = new Double JavaDoc(2.3d);
142     Number JavaDoc value = new Double JavaDoc(4.5d);
143     KeyedValue kv = new KeyedValue(key, value);
144     KeyedValue kv2 = new KeyedValue(null, value);
145     assertFalse(kv.equals(kv2));
146     assertFalse(kv2.equals(kv));
147   }
148
149   /**
150     * @throws Exception
151     * @author Klaus Meffert
152     * @since 3.0
153     */

154    public void testHashcode_0()
155        throws Exception JavaDoc {
156      Comparable JavaDoc key = new Double JavaDoc(2.3d);
157      Number JavaDoc value = new Double JavaDoc(4.5d);
158      KeyedValue kv = new KeyedValue(key, value);
159      assertEquals(41 * key.hashCode() + value.hashCode(), kv.hashCode());
160    }
161 }
162
Popular Tags