KickJava   Java API By Example, From Geeks To Geeks.

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


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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------------------
28  * PeriodAxisLabelInfoTests.java
29  * -----------------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: PeriodAxisLabelInfoTests.java,v 1.1.2.1 2006/10/03 15:41:23 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-Jun-2003 : Version 1 (DG);
40  * 07-Jan-2005 : Added test for hashCode() (DG);
41  *
42  */

43
44 package org.jfree.chart.axis.junit;
45
46 import java.awt.BasicStroke JavaDoc;
47 import java.awt.Color JavaDoc;
48 import java.awt.Font JavaDoc;
49 import java.awt.Paint JavaDoc;
50 import java.awt.Stroke JavaDoc;
51 import java.io.ByteArrayInputStream JavaDoc;
52 import java.io.ByteArrayOutputStream JavaDoc;
53 import java.io.ObjectInput JavaDoc;
54 import java.io.ObjectInputStream JavaDoc;
55 import java.io.ObjectOutput JavaDoc;
56 import java.io.ObjectOutputStream JavaDoc;
57 import java.text.DateFormat JavaDoc;
58 import java.text.SimpleDateFormat JavaDoc;
59
60 import junit.framework.Test;
61 import junit.framework.TestCase;
62 import junit.framework.TestSuite;
63
64 import org.jfree.chart.axis.PeriodAxisLabelInfo;
65 import org.jfree.data.time.Day;
66 import org.jfree.data.time.Month;
67 import org.jfree.ui.RectangleInsets;
68
69 /**
70  * Tests for the {@link PeriodAxisLabelInfo} class.
71  */

72 public class PeriodAxisLabelInfoTests extends TestCase {
73
74     /**
75      * Returns the tests as a test suite.
76      *
77      * @return The test suite.
78      */

79     public static Test suite() {
80         return new TestSuite(PeriodAxisLabelInfoTests.class);
81     }
82
83     /**
84      * Constructs a new set of tests.
85      *
86      * @param name the name of the tests.
87      */

88     public PeriodAxisLabelInfoTests(String JavaDoc name) {
89         super(name);
90     }
91
92     /**
93      * Confirm that the equals method can distinguish all the required fields.
94      */

95     public void testEquals() {
96         PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
97             Day.class, new SimpleDateFormat JavaDoc("d")
98         );
99         PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(
100             Day.class, new SimpleDateFormat JavaDoc("d")
101         );
102         assertTrue(info1.equals(info2));
103         assertTrue(info2.equals(info1));
104         
105         Class JavaDoc c1 = Day.class;
106         Class JavaDoc c2 = Month.class;
107         DateFormat JavaDoc df1 = new SimpleDateFormat JavaDoc("d");
108         DateFormat JavaDoc df2 = new SimpleDateFormat JavaDoc("MMM");
109         RectangleInsets sp1 = new RectangleInsets(1, 1, 1, 1);
110         RectangleInsets sp2 = new RectangleInsets(2, 2, 2, 2);
111         Font JavaDoc lf1 = new Font JavaDoc("SansSerif", Font.PLAIN, 10);
112         Font JavaDoc lf2 = new Font JavaDoc("SansSerif", Font.BOLD, 9);
113         Paint JavaDoc lp1 = Color.black;
114         Paint JavaDoc lp2 = Color.blue;
115         boolean b1 = true;
116         boolean b2 = false;
117         Stroke JavaDoc s1 = new BasicStroke JavaDoc(0.5f);
118         Stroke JavaDoc s2 = new BasicStroke JavaDoc(0.25f);
119         Paint JavaDoc dp1 = Color.red;
120         Paint JavaDoc dp2 = Color.green;
121         
122         info1 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
123         info2 = new PeriodAxisLabelInfo(c1, df1, sp1, lf1, lp1, b1, s1, dp1);
124         assertFalse(info1.equals(info2));
125         info2 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
126         assertTrue(info1.equals(info2));
127         
128         info1 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
129         assertFalse(info1.equals(info2));
130         info2 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
131         assertTrue(info1.equals(info2));
132
133         info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
134         assertFalse(info1.equals(info2));
135         info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
136         assertTrue(info1.equals(info2));
137
138         info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
139         assertFalse(info1.equals(info2));
140         info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
141         assertTrue(info1.equals(info2));
142
143         info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
144         assertFalse(info1.equals(info2));
145         info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
146         assertTrue(info1.equals(info2));
147
148         info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
149         assertFalse(info1.equals(info2));
150         info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
151         assertTrue(info1.equals(info2));
152
153         info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
154         assertFalse(info1.equals(info2));
155         info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
156         assertTrue(info1.equals(info2));
157
158         info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
159         assertFalse(info1.equals(info2));
160         info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
161         assertTrue(info1.equals(info2));
162
163     }
164    
165     /**
166      * Two objects that are equal are required to return the same hashCode.
167      */

168     public void testHashCode() {
169         PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
170             Day.class, new SimpleDateFormat JavaDoc("d")
171         );
172         PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(
173             Day.class, new SimpleDateFormat JavaDoc("d")
174         );
175         assertTrue(info1.equals(info2));
176         int h1 = info1.hashCode();
177         int h2 = info2.hashCode();
178         assertEquals(h1, h2);
179     }
180     
181     /**
182      * Confirm that cloning works.
183      */

184     public void testCloning() {
185         PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
186             Day.class, new SimpleDateFormat JavaDoc("d")
187         );
188         PeriodAxisLabelInfo info2 = null;
189         try {
190             info2 = (PeriodAxisLabelInfo) info1.clone();
191         }
192         catch (CloneNotSupportedException JavaDoc e) {
193             System.err.println("Failed to clone.");
194         }
195         assertTrue(info1 != info2);
196         assertTrue(info1.getClass() == info2.getClass());
197         assertTrue(info1.equals(info2));
198     }
199
200     /**
201      * Serialize an instance, restore it, and check for equality.
202      */

203     public void testSerialization() {
204         PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(
205             Day.class, new SimpleDateFormat JavaDoc("d")
206         );
207         PeriodAxisLabelInfo info2 = null;
208         try {
209             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
210             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
211             out.writeObject(info1);
212             out.close();
213
214             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
215                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
216             );
217             info2 = (PeriodAxisLabelInfo) in.readObject();
218             in.close();
219         }
220         catch (Exception JavaDoc e) {
221             System.out.println(e.toString());
222         }
223         boolean b = info1.equals(info2);
224         assertTrue(b);
225     }
226
227 }
228
Popular Tags