KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DateTickTests.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: DateTickTests.java,v 1.1.2.1 2006/10/03 15:41:23 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 13-May-2004 : Version 1 (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 import java.util.Date JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.chart.axis.DateTick;
58 import org.jfree.ui.TextAnchor;
59
60 /**
61  * Tests for the {@link DateTick} class.
62  */

63 public class DateTickTests 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(DateTickTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

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

86     public void testEquals() {
87         
88         Date JavaDoc d1 = new Date JavaDoc(0L);
89         Date JavaDoc d2 = new Date JavaDoc(1L);
90         String JavaDoc l1 = "Label 1";
91         String JavaDoc l2 = "Label 2";
92         TextAnchor ta1 = TextAnchor.CENTER;
93         TextAnchor ta2 = TextAnchor.BASELINE_LEFT;
94         
95         DateTick t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
96         DateTick t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
97         assertTrue(t1.equals(t2));
98         
99         t1 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0);
100         assertFalse(t1.equals(t2));
101         t2 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0);
102         assertTrue(t1.equals(t2));
103
104         t1 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0);
105         assertFalse(t1.equals(t2));
106         t2 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0);
107         assertTrue(t1.equals(t2));
108
109         t1 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0);
110         assertFalse(t1.equals(t2));
111         t2 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0);
112         assertTrue(t1.equals(t2));
113
114         t1 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0);
115         assertFalse(t1.equals(t2));
116         t2 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0);
117         assertTrue(t1.equals(t2));
118
119         t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0);
120         assertFalse(t1.equals(t2));
121         t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0);
122         assertTrue(t1.equals(t2));
123         
124     }
125
126     /**
127      * Two objects that are equal are required to return the same hashCode.
128      */

129     public void testHashCode() {
130         Date JavaDoc d1 = new Date JavaDoc(0L);
131         String JavaDoc l1 = "Label 1";
132         TextAnchor ta1 = TextAnchor.CENTER;
133         
134         DateTick t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
135         DateTick t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
136         assertTrue(t1.equals(t2));
137         int h1 = t1.hashCode();
138         int h2 = t2.hashCode();
139         assertEquals(h1, h2);
140     }
141
142     /**
143      * Confirm that cloning works.
144      */

145     public void testCloning() {
146         DateTick t1 = new DateTick(
147             new Date JavaDoc(0L), "Label", TextAnchor.CENTER, TextAnchor.CENTER, 10.0
148         );
149         DateTick t2 = null;
150         try {
151             t2 = (DateTick) t1.clone();
152         }
153         catch (CloneNotSupportedException JavaDoc e) {
154             System.err.println("Failed to clone.");
155         }
156         assertTrue(t1 != t2);
157         assertTrue(t1.getClass() == t2.getClass());
158         assertTrue(t1.equals(t2));
159     }
160
161     /**
162      * Serialize an instance, restore it, and check for equality.
163      */

164     public void testSerialization() {
165
166         DateTick t1 = new DateTick(
167             new Date JavaDoc(0L), "Label", TextAnchor.CENTER, TextAnchor.CENTER, 10.0
168         );
169         DateTick t2 = null;
170
171         try {
172             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
173             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
174             out.writeObject(t1);
175             out.close();
176
177             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
178                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
179             );
180             t2 = (DateTick) in.readObject();
181             in.close();
182         }
183         catch (Exception JavaDoc e) {
184             System.out.println(e.toString());
185         }
186         assertEquals(t1, t2);
187
188     }
189
190 }
191
Popular Tags