KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > junit > DefaultKeyedValueTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------------
27  * DefaultKeyedValueTests.java
28  * ---------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: DefaultKeyedValueTests.java,v 1.4 2005/03/29 12:56:53 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 13-Mar-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.data.junit;
43
44 import java.io.ByteArrayInputStream JavaDoc;
45 import java.io.ByteArrayOutputStream JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.io.ObjectOutput JavaDoc;
49 import java.io.ObjectOutputStream JavaDoc;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.data.DefaultKeyedValue;
56
57 /**
58  * Tests for the {@link DefaultKeyedValue} class.
59  */

60 public class DefaultKeyedValueTests extends TestCase {
61
62     /**
63      * Returns the tests as a test suite.
64      *
65      * @return The test suite.
66      */

67     public static Test suite() {
68         return new TestSuite(DefaultKeyedValueTests.class);
69     }
70
71     /**
72      * Constructs a new set of tests.
73      *
74      * @param name the name of the tests.
75      */

76     public DefaultKeyedValueTests(String JavaDoc name) {
77         super(name);
78     }
79
80     /**
81      * Confirm that the equals method can distinguish all the required fields.
82      */

83     public void testEquals() {
84         
85         DefaultKeyedValue v1 = new DefaultKeyedValue("Test", new Double JavaDoc(45.5));
86         DefaultKeyedValue v2 = new DefaultKeyedValue("Test", new Double JavaDoc(45.5));
87         assertTrue(v1.equals(v2));
88         assertTrue(v2.equals(v1));
89
90         v1 = new DefaultKeyedValue("Test 1", new Double JavaDoc(45.5));
91         v2 = new DefaultKeyedValue("Test 2", new Double JavaDoc(45.5));
92         assertFalse(v1.equals(v2));
93
94         v1 = new DefaultKeyedValue("Test", new Double JavaDoc(45.5));
95         v2 = new DefaultKeyedValue("Test", new Double JavaDoc(45.6));
96         assertFalse(v1.equals(v2));
97
98     }
99
100     /**
101      * Some checks for the clone() method.
102      */

103     public void testCloning() {
104         DefaultKeyedValue v1 = new DefaultKeyedValue("Test", new Double JavaDoc(45.5));
105         DefaultKeyedValue v2 = null;
106         try {
107             v2 = (DefaultKeyedValue) v1.clone();
108         }
109         catch (CloneNotSupportedException JavaDoc e) {
110             System.err.println("Failed to clone.");
111         }
112         assertTrue(v1 != v2);
113         assertTrue(v1.getClass() == v2.getClass());
114         assertTrue(v1.equals(v2));
115         
116         // confirm that the clone is independent of the original
117
v2.setValue(new Double JavaDoc(12.3));
118         assertFalse(v1.equals(v2));
119     }
120
121     /**
122      * Serialize an instance, restore it, and check for equality.
123      */

124     public void testSerialization() {
125
126         DefaultKeyedValue v1 = new DefaultKeyedValue("Test", new Double JavaDoc(25.3));
127         DefaultKeyedValue v2 = null;
128
129         try {
130             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
131             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
132             out.writeObject(v1);
133             out.close();
134
135             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
136                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
137             );
138             v2 = (DefaultKeyedValue) in.readObject();
139             in.close();
140         }
141         catch (Exception JavaDoc e) {
142             System.out.println(e.toString());
143         }
144         assertEquals(v1, v2);
145
146     }
147
148 }
149
Popular Tags