001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * CyclicAxisTests.java
029: * --------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: Nicolas Brodu
033: * Contributor(s): -;
034: *
035: * $Id: CyclicNumberAxisTests.java,v 1.1.2.3 2007/02/02 14:33:11 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-Nov-2003 : First version (NB);
040: * 05-Oct-2004 : Removed extension of NumberAxisTests (DG);
041: * 07-Jan-2004 : Added test for hashCode() (DG);
042: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
043: *
044: */
045:
046: package org.jfree.chart.axis.junit;
047:
048: import java.awt.BasicStroke;
049: import java.awt.Color;
050: import java.awt.GradientPaint;
051: import java.awt.Stroke;
052: import java.io.ByteArrayInputStream;
053: import java.io.ByteArrayOutputStream;
054: import java.io.ObjectInput;
055: import java.io.ObjectInputStream;
056: import java.io.ObjectOutput;
057: import java.io.ObjectOutputStream;
058:
059: import junit.framework.Test;
060: import junit.framework.TestCase;
061: import junit.framework.TestSuite;
062:
063: import org.jfree.chart.axis.CyclicNumberAxis;
064:
065: /**
066: * Tests for the {@link CyclicNumberAxis} class.
067: */
068: public class CyclicNumberAxisTests extends TestCase {
069:
070: /**
071: * Returns the tests as a test suite.
072: *
073: * @return The test suite.
074: */
075: public static Test suite() {
076: return new TestSuite(CyclicNumberAxisTests.class);
077: }
078:
079: /**
080: * Constructs a new set of tests.
081: *
082: * @param name the name of the tests.
083: */
084: public CyclicNumberAxisTests(String name) {
085: super (name);
086: }
087:
088: /**
089: * Confirm that cloning works.
090: */
091: public void testCloning() {
092: CyclicNumberAxis a1 = new CyclicNumberAxis(10, 0, "Test");
093: CyclicNumberAxis a2 = null;
094: try {
095: a2 = (CyclicNumberAxis) a1.clone();
096: } catch (CloneNotSupportedException e) {
097: System.err.println("Failed to clone.");
098: }
099: assertTrue(a1 != a2);
100: assertTrue(a1.getClass() == a2.getClass());
101: assertTrue(a1.equals(a2));
102: }
103:
104: /**
105: * Confirm that the equals method can distinguish all the required fields.
106: */
107: public void testEquals() {
108:
109: CyclicNumberAxis a1 = new CyclicNumberAxis(10, 0, "Test");
110: CyclicNumberAxis a2 = new CyclicNumberAxis(10, 0, "Test");
111: assertTrue(a1.equals(a2));
112:
113: // period
114: a1.setPeriod(5);
115: assertFalse(a1.equals(a2));
116: a2.setPeriod(5);
117: assertTrue(a1.equals(a2));
118:
119: // offset
120: a1.setOffset(2.0);
121: assertFalse(a1.equals(a2));
122: a2.setOffset(2.0);
123: assertTrue(a1.equals(a2));
124:
125: // advance line Paint
126: a1.setAdvanceLinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
127: 3.0f, 4.0f, Color.black));
128: assertFalse(a1.equals(a2));
129: a2.setAdvanceLinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
130: 3.0f, 4.0f, Color.black));
131: assertTrue(a1.equals(a2));
132:
133: // advance line Stroke
134: Stroke stroke = new BasicStroke(0.2f);
135: a1.setAdvanceLineStroke(stroke);
136: assertFalse(a1.equals(a2));
137: a2.setAdvanceLineStroke(stroke);
138: assertTrue(a1.equals(a2));
139:
140: // advance line Visible
141: a1.setAdvanceLineVisible(!a1.isAdvanceLineVisible());
142: assertFalse(a1.equals(a2));
143: a2.setAdvanceLineVisible(a1.isAdvanceLineVisible());
144: assertTrue(a1.equals(a2));
145:
146: // cycle bound mapping
147: a1.setBoundMappedToLastCycle(!a1.isBoundMappedToLastCycle());
148: assertFalse(a1.equals(a2));
149: a2.setBoundMappedToLastCycle(a1.isBoundMappedToLastCycle());
150: assertTrue(a1.equals(a2));
151:
152: }
153:
154: /**
155: * Two objects that are equal are required to return the same hashCode.
156: */
157: public void testHashCode() {
158: CyclicNumberAxis a1 = new CyclicNumberAxis(10, 0, "Test");
159: CyclicNumberAxis a2 = new CyclicNumberAxis(10, 0, "Test");
160: assertTrue(a1.equals(a2));
161: int h1 = a1.hashCode();
162: int h2 = a2.hashCode();
163: assertEquals(h1, h2);
164: }
165:
166: /**
167: * Serialize an instance, restore it, and check for equality.
168: */
169: public void testSerialization() {
170:
171: CyclicNumberAxis a1 = new CyclicNumberAxis(10, 0, "Test Axis");
172: CyclicNumberAxis a2 = null;
173:
174: try {
175: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
176: ObjectOutput out = new ObjectOutputStream(buffer);
177: out.writeObject(a1);
178: out.close();
179:
180: ObjectInput in = new ObjectInputStream(
181: new ByteArrayInputStream(buffer.toByteArray()));
182: a2 = (CyclicNumberAxis) in.readObject();
183: in.close();
184: } catch (Exception e) {
185: System.out.println(e.toString());
186: }
187: assertEquals(a1, a2);
188:
189: }
190:
191: }
|