KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

58 public class DefaultKeyedValueDatasetTests extends TestCase {
59
60     /**
61      * Returns the tests as a test suite.
62      *
63      * @return the test suite.
64      */

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

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

81     public void testEquals() {
82         
83         DefaultKeyedValueDataset d1 = new DefaultKeyedValueDataset("Test", new Double JavaDoc(45.5));
84         DefaultKeyedValueDataset d2 = new DefaultKeyedValueDataset("Test", new Double JavaDoc(45.5));
85         assertTrue(d1.equals(d2));
86         assertTrue(d2.equals(d1));
87
88         d1 = new DefaultKeyedValueDataset("Test 1", new Double JavaDoc(45.5));
89         d2 = new DefaultKeyedValueDataset("Test 2", new Double JavaDoc(45.5));
90         assertFalse(d1.equals(d2));
91
92         d1 = new DefaultKeyedValueDataset("Test", new Double JavaDoc(45.5));
93         d2 = new DefaultKeyedValueDataset("Test", new Double JavaDoc(45.6));
94         assertFalse(d1.equals(d2));
95
96     }
97
98     /**
99      * Confirm that cloning works.
100      */

101     public void testCloning() {
102         DefaultKeyedValueDataset d1 = new DefaultKeyedValueDataset("Test", new Double JavaDoc(45.5));
103         DefaultKeyedValueDataset d2 = null;
104         try {
105             d2 = (DefaultKeyedValueDataset) d1.clone();
106         }
107         catch (CloneNotSupportedException JavaDoc e) {
108             System.err.println("DefaultKeyedValueDatasetTests.testCloning: failed to clone.");
109         }
110         assertTrue(d1 != d2);
111         assertTrue(d1.getClass() == d2.getClass());
112         assertTrue(d1.equals(d2));
113     }
114
115     /**
116      * Serialize an instance, restore it, and check for equality.
117      */

118     public void testSerialization() {
119
120         DefaultKeyedValueDataset d1 = new DefaultKeyedValueDataset("Test", new Double JavaDoc(25.3));
121         DefaultKeyedValueDataset d2 = null;
122
123         try {
124             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
125             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
126             out.writeObject(d1);
127             out.close();
128
129             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
130             d2 = (DefaultKeyedValueDataset) in.readObject();
131             in.close();
132         }
133         catch (Exception JavaDoc e) {
134             System.out.println(e.toString());
135         }
136         assertEquals(d1, d2);
137
138     }
139
140 }
141
Popular Tags