KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultKeyedValuesDatasetTests.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: DefaultKeyedValuesDatasetTests.java,v 1.4 2003/08/20 12:16:18 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 13-Mar-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.DefaultKeyedValuesDataset;
52 import org.jfree.data.KeyedValuesDataset;
53
54 /**
55  * Tests for the {@link DefaultKeyedValuesDataset} class.
56  *
57  * @author David Gilbert
58  */

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

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

75     public DefaultKeyedValuesDatasetTests(String JavaDoc name) {
76         super(name);
77     }
78
79     /**
80      * Confirm that cloning works.
81      */

82     public void testCloning() {
83         DefaultKeyedValuesDataset d1 = new DefaultKeyedValuesDataset();
84         d1.setValue("V1", new Integer JavaDoc(1));
85         d1.setValue("V2", null);
86         d1.setValue("V3", new Integer JavaDoc(3));
87         DefaultKeyedValuesDataset d2 = null;
88         try {
89             d2 = (DefaultKeyedValuesDataset) d1.clone();
90         }
91         catch (CloneNotSupportedException JavaDoc e) {
92             System.err.println("DefaultKeyedValuesDatasetTests.testCloning: failed to clone.");
93         }
94         assertTrue(d1 != d2);
95         assertTrue(d1.getClass() == d2.getClass());
96         assertTrue(d1.equals(d2));
97     }
98
99     /**
100      * Serialize an instance, restore it, and check for equality.
101      */

102     public void testSerialization() {
103
104         DefaultKeyedValuesDataset d1 = new DefaultKeyedValuesDataset();
105         d1.setValue("C1", new Double JavaDoc(234.2));
106         d1.setValue("C2", null);
107         d1.setValue("C3", new Double JavaDoc(345.9));
108         d1.setValue("C4", new Double JavaDoc(452.7));
109
110         KeyedValuesDataset d2 = null;
111
112         try {
113             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
114             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
115             out.writeObject(d1);
116             out.close();
117
118             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
119             d2 = (KeyedValuesDataset) in.readObject();
120             in.close();
121         }
122         catch (Exception JavaDoc e) {
123             System.out.println(e.toString());
124         }
125         assertEquals(d1, d2);
126
127     }
128
129 }
130
Popular Tags