KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MonthDateFormatTests.java
29  * -------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: MonthDateFormatTests.java,v 1.1.2.1 2006/10/03 15:41:24 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-May-2005 : 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.text.SimpleDateFormat JavaDoc;
52 import java.util.Locale JavaDoc;
53 import java.util.TimeZone JavaDoc;
54
55 import junit.framework.Test;
56 import junit.framework.TestCase;
57 import junit.framework.TestSuite;
58
59 import org.jfree.chart.axis.MonthDateFormat;
60
61 /**
62  * Some tests for the {@link MonthDateFormat} class.
63  */

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

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

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

87     public void testEquals() {
88         MonthDateFormat mf1 = new MonthDateFormat();
89         MonthDateFormat mf2 = new MonthDateFormat();
90         assertTrue(mf1.equals(mf2));
91         assertTrue(mf2.equals(mf1));
92         
93         boolean[] showYear1 = new boolean [12];
94         showYear1[0] = true;
95         boolean[] showYear2 = new boolean [12];
96         showYear1[1] = true;
97         
98         // time zone
99
mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.US, 1,
100             showYear1, new SimpleDateFormat JavaDoc("yy"));
101         assertFalse(mf1.equals(mf2));
102         mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.US, 1,
103             showYear1, new SimpleDateFormat JavaDoc("yy"));
104         assertTrue(mf1.equals(mf2));
105         
106         // locale
107
mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 1,
108             showYear1, new SimpleDateFormat JavaDoc("yy"));
109         assertFalse(mf1.equals(mf2));
110         mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 1,
111             showYear1, new SimpleDateFormat JavaDoc("yy"));
112         assertTrue(mf1.equals(mf2));
113         
114         // chars
115
mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
116             showYear1, new SimpleDateFormat JavaDoc("yy"));
117         assertFalse(mf1.equals(mf2));
118         mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
119             showYear1, new SimpleDateFormat JavaDoc("yy"));
120         assertTrue(mf1.equals(mf2));
121         
122         // showYear[]
123
mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
124             showYear2, new SimpleDateFormat JavaDoc("yy"));
125         assertFalse(mf1.equals(mf2));
126         mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
127             showYear2, new SimpleDateFormat JavaDoc("yy"));
128         assertTrue(mf1.equals(mf2));
129         
130         // yearFormatter
131
mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
132             showYear2, new SimpleDateFormat JavaDoc("yyyy"));
133         assertFalse(mf1.equals(mf2));
134         mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
135             showYear2, new SimpleDateFormat JavaDoc("yyyy"));
136         assertTrue(mf1.equals(mf2));
137
138     }
139    
140     /**
141      * Two objects that are equal are required to return the same hashCode.
142      */

143     public void testHashCode() {
144         MonthDateFormat mf1 = new MonthDateFormat();
145         MonthDateFormat mf2 = new MonthDateFormat();
146         assertTrue(mf1.equals(mf2));
147         int h1 = mf1.hashCode();
148         int h2 = mf2.hashCode();
149         assertEquals(h1, h2);
150     }
151     
152     /**
153      * Confirm that cloning works.
154      */

155     public void testCloning() {
156         MonthDateFormat mf1 = new MonthDateFormat();
157         MonthDateFormat mf2 = null;
158         mf2 = (MonthDateFormat) mf1.clone();
159         assertTrue(mf1 != mf2);
160         assertTrue(mf1.getClass() == mf2.getClass());
161         assertTrue(mf1.equals(mf2));
162     }
163
164     /**
165      * Serialize an instance, restore it, and check for equality.
166      */

167     public void testSerialization() {
168         MonthDateFormat mf1 = new MonthDateFormat();
169         MonthDateFormat mf2 = null;
170         try {
171             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
172             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
173             out.writeObject(mf1);
174             out.close();
175
176             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
177                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
178             );
179             mf2 = (MonthDateFormat) in.readObject();
180             in.close();
181         }
182         catch (Exception JavaDoc e) {
183             fail(e.toString());
184         }
185         assertTrue(mf1.equals(mf2));
186     }
187
188 }
189
Popular Tags