001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * PeriodAxisTests.java
029: * --------------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: PeriodAxisTests.java,v 1.1.2.1 2006/10/03 15:41:22 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jun-2003 : Version 1 (DG);
040: * 07-Jan-2005 : Added test for hashCode() method (DG);
041: *
042: */
043:
044: package org.jfree.chart.axis.junit;
045:
046: import java.awt.BasicStroke;
047: import java.awt.Color;
048: import java.awt.Stroke;
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055: import java.text.SimpleDateFormat;
056: import java.util.SimpleTimeZone;
057: import java.util.TimeZone;
058:
059: import junit.framework.Test;
060: import junit.framework.TestCase;
061: import junit.framework.TestSuite;
062:
063: import org.jfree.chart.axis.PeriodAxis;
064: import org.jfree.chart.axis.PeriodAxisLabelInfo;
065: import org.jfree.data.time.Day;
066: import org.jfree.data.time.Minute;
067: import org.jfree.data.time.Month;
068: import org.jfree.data.time.Quarter;
069: import org.jfree.data.time.Second;
070: import org.jfree.data.time.Year;
071:
072: /**
073: * Tests for the {@link PeriodAxis} class.
074: */
075: public class PeriodAxisTests extends TestCase {
076:
077: /**
078: * Returns the tests as a test suite.
079: *
080: * @return The test suite.
081: */
082: public static Test suite() {
083: return new TestSuite(PeriodAxisTests.class);
084: }
085:
086: /**
087: * Constructs a new set of tests.
088: *
089: * @param name the name of the tests.
090: */
091: public PeriodAxisTests(String name) {
092: super (name);
093: }
094:
095: /**
096: * Confirm that the equals() method can distinguish all the required fields.
097: */
098: public void testEquals() {
099:
100: PeriodAxis a1 = new PeriodAxis("Test");
101: PeriodAxis a2 = new PeriodAxis("Test");
102: assertTrue(a1.equals(a2));
103: assertTrue(a2.equals(a1));
104:
105: a1.setFirst(new Year(2000));
106: assertFalse(a1.equals(a2));
107: a2.setFirst(new Year(2000));
108: assertTrue(a1.equals(a2));
109:
110: a1.setLast(new Year(2004));
111: assertFalse(a1.equals(a2));
112: a2.setLast(new Year(2004));
113: assertTrue(a1.equals(a2));
114:
115: a1.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
116: assertFalse(a1.equals(a2));
117: a2.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
118: assertTrue(a1.equals(a2));
119:
120: a1.setAutoRangeTimePeriodClass(Quarter.class);
121: assertFalse(a1.equals(a2));
122: a2.setAutoRangeTimePeriodClass(Quarter.class);
123: assertTrue(a1.equals(a2));
124:
125: PeriodAxisLabelInfo info[] = new PeriodAxisLabelInfo[1];
126: info[0] = new PeriodAxisLabelInfo(Month.class,
127: new SimpleDateFormat("MMM"));
128:
129: a1.setLabelInfo(info);
130: assertFalse(a1.equals(a2));
131: a2.setLabelInfo(info);
132: assertTrue(a1.equals(a2));
133:
134: a1.setMajorTickTimePeriodClass(Minute.class);
135: assertFalse(a1.equals(a2));
136: a2.setMajorTickTimePeriodClass(Minute.class);
137: assertTrue(a1.equals(a2));
138:
139: a1.setMinorTickMarksVisible(!a1.isMinorTickMarksVisible());
140: assertFalse(a1.equals(a2));
141: a2.setMinorTickMarksVisible(a1.isMinorTickMarksVisible());
142: assertTrue(a1.equals(a2));
143:
144: a1.setMinorTickTimePeriodClass(Minute.class);
145: assertFalse(a1.equals(a2));
146: a2.setMinorTickTimePeriodClass(Minute.class);
147: assertTrue(a1.equals(a2));
148:
149: Stroke s = new BasicStroke(1.23f);
150: a1.setMinorTickMarkStroke(s);
151: assertFalse(a1.equals(a2));
152: a2.setMinorTickMarkStroke(s);
153: assertTrue(a1.equals(a2));
154:
155: a1.setMinorTickMarkPaint(Color.blue);
156: assertFalse(a1.equals(a2));
157: a2.setMinorTickMarkPaint(Color.blue);
158: assertTrue(a1.equals(a2));
159:
160: }
161:
162: /**
163: * Two objects that are equal are required to return the same hashCode.
164: */
165: public void testHashCode() {
166: PeriodAxis a1 = new PeriodAxis("Test");
167: PeriodAxis a2 = new PeriodAxis("Test");
168: assertTrue(a1.equals(a2));
169: int h1 = a1.hashCode();
170: int h2 = a2.hashCode();
171: assertEquals(h1, h2);
172: }
173:
174: /**
175: * Confirm that cloning works.
176: */
177: public void testCloning() {
178: PeriodAxis a1 = new PeriodAxis("Test");
179: PeriodAxis a2 = null;
180: try {
181: a2 = (PeriodAxis) a1.clone();
182: } catch (CloneNotSupportedException e) {
183: System.err.println("Failed to clone.");
184: }
185: assertTrue(a1 != a2);
186: assertTrue(a1.getClass() == a2.getClass());
187: assertTrue(a1.equals(a2));
188:
189: // some checks that the clone is independent of the original
190: a1.setLabel("New Label");
191: assertFalse(a1.equals(a2));
192: a2.setLabel("New Label");
193: assertTrue(a1.equals(a2));
194:
195: a1.setFirst(new Year(1920));
196: assertFalse(a1.equals(a2));
197: a2.setFirst(new Year(1920));
198: assertTrue(a1.equals(a2));
199:
200: a1.setLast(new Year(2020));
201: assertFalse(a1.equals(a2));
202: a2.setLast(new Year(2020));
203: assertTrue(a1.equals(a2));
204:
205: PeriodAxisLabelInfo[] info = new PeriodAxisLabelInfo[2];
206: info[0] = new PeriodAxisLabelInfo(Day.class,
207: new SimpleDateFormat("d"));
208: info[1] = new PeriodAxisLabelInfo(Year.class,
209: new SimpleDateFormat("yyyy"));
210: a1.setLabelInfo(info);
211: assertFalse(a1.equals(a2));
212: a2.setLabelInfo(info);
213: assertTrue(a1.equals(a2));
214:
215: a1.setAutoRangeTimePeriodClass(Second.class);
216: assertFalse(a1.equals(a2));
217: a2.setAutoRangeTimePeriodClass(Second.class);
218: assertTrue(a1.equals(a2));
219:
220: a1.setTimeZone(new SimpleTimeZone(123, "Bogus"));
221: assertFalse(a1.equals(a2));
222: a2.setTimeZone(new SimpleTimeZone(123, "Bogus"));
223: assertTrue(a1.equals(a2));
224:
225: }
226:
227: /**
228: * Serialize an instance, restore it, and check for equality.
229: */
230: public void testSerialization() {
231: PeriodAxis a1 = new PeriodAxis("Test Axis");
232: PeriodAxis a2 = null;
233: try {
234: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
235: ObjectOutput out = new ObjectOutputStream(buffer);
236: out.writeObject(a1);
237: out.close();
238:
239: ObjectInput in = new ObjectInputStream(
240: new ByteArrayInputStream(buffer.toByteArray()));
241: a2 = (PeriodAxis) in.readObject();
242: in.close();
243: } catch (Exception e) {
244: System.out.println(e.toString());
245: }
246: boolean b = a1.equals(a2);
247: assertTrue(b);
248: }
249:
250: }
|