KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > junit > ColorBarTests


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  * ColorBarTests.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: ColorBarTests.java,v 1.3 2005/03/16 12:10:45 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 26-Mar-2003 : Version 1 (DG);
39  * 07-Jan-2005 : Added test for hashCode() method (DG);
40  *
41  */

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

63 public class ColorBarTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(ColorBarTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public ColorBarTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Check that the equals() method can distinguish all fields.
85      */

86     public void testEquals() {
87         ColorBar c1 = new ColorBar("Test");
88         ColorBar c2 = new ColorBar("Test");
89         assertEquals(c1, c2);
90         
91         c1.setAxis(new NumberAxis("Axis 1"));
92         assertTrue(!c1.equals(c2));
93         c2.setAxis(new NumberAxis("Axis 1"));
94         assertTrue(c1.equals(c2));
95         
96         c1.setColorPalette(new GreyPalette());
97         assertTrue(!c1.equals(c2));
98         c2.setColorPalette(new GreyPalette());
99         assertTrue(c1.equals(c2));
100     }
101     
102     /**
103      * Two objects that are equal are required to return the same hashCode.
104      */

105     public void testHashCode() {
106         ColorBar c1 = new ColorBar("Test");
107         ColorBar c2 = new ColorBar("Test");
108         assertTrue(c1.equals(c2));
109         int h1 = c1.hashCode();
110         int h2 = c2.hashCode();
111         assertEquals(h1, h2);
112     }
113
114     /**
115      * Confirm that cloning works.
116      */

117     public void testCloning() {
118         ColorBar c1 = new ColorBar("Test");
119         ColorBar c2 = null;
120         try {
121             c2 = (ColorBar) c1.clone();
122         }
123         catch (CloneNotSupportedException JavaDoc e) {
124             System.err.println("Failed to clone.");
125         }
126         assertTrue(c1 != c2);
127         assertTrue(c1.getClass() == c2.getClass());
128         assertTrue(c1.equals(c2));
129     }
130
131     /**
132      * Serialize an instance, restore it, and check for equality.
133      */

134     public void testSerialization() {
135
136         ColorBar a1 = new ColorBar("Test Axis");
137         ColorBar a2 = null;
138
139         try {
140             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
141             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
142             out.writeObject(a1);
143             out.close();
144
145             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
146                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
147             );
148             a2 = (ColorBar) in.readObject();
149             in.close();
150         }
151         catch (Exception JavaDoc e) {
152             System.out.println(e.toString());
153         }
154         assertEquals(a1, a2);
155
156     }
157
158 }
159
Popular Tags