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: * StandardXYToolTipGeneratorTests.java
029: * ------------------------------------
030: * (C) Copyright 2004, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StandardXYToolTipGeneratorTests.java,v 1.1.2.1 2006/10/03 15:41:36 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 11-May-2004 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.labels.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051: import java.text.DateFormat;
052: import java.text.DecimalFormat;
053: import java.text.NumberFormat;
054: import java.text.SimpleDateFormat;
055:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.chart.labels.StandardXYToolTipGenerator;
061:
062: /**
063: * Tests for the {@link StandardXYToolTipGenerator} class.
064: */
065: public class StandardXYToolTipGeneratorTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(StandardXYToolTipGeneratorTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public StandardXYToolTipGeneratorTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Tests the equals() method.
087: */
088: public void testEquals() {
089:
090: // some setup...
091: String f1 = "{1}";
092: String f2 = "{2}";
093: NumberFormat xnf1 = new DecimalFormat("0.00");
094: NumberFormat xnf2 = new DecimalFormat("0.000");
095: NumberFormat ynf1 = new DecimalFormat("0.00");
096: NumberFormat ynf2 = new DecimalFormat("0.000");
097:
098: StandardXYToolTipGenerator g1 = null;
099: StandardXYToolTipGenerator g2 = null;
100:
101: g1 = new StandardXYToolTipGenerator(f1, xnf1, ynf1);
102: g2 = new StandardXYToolTipGenerator(f1, xnf1, ynf1);
103: assertTrue(g1.equals(g2));
104: assertTrue(g2.equals(g1));
105:
106: g1 = new StandardXYToolTipGenerator(f2, xnf1, ynf1);
107: assertFalse(g1.equals(g2));
108: g2 = new StandardXYToolTipGenerator(f2, xnf1, ynf1);
109: assertTrue(g1.equals(g2));
110:
111: g1 = new StandardXYToolTipGenerator(f2, xnf2, ynf1);
112: assertFalse(g1.equals(g2));
113: g2 = new StandardXYToolTipGenerator(f2, xnf2, ynf1);
114: assertTrue(g1.equals(g2));
115:
116: g1 = new StandardXYToolTipGenerator(f2, xnf2, ynf2);
117: assertFalse(g1.equals(g2));
118: g2 = new StandardXYToolTipGenerator(f2, xnf2, ynf2);
119: assertTrue(g1.equals(g2));
120:
121: DateFormat xdf1 = new SimpleDateFormat("d-MMM");
122: DateFormat xdf2 = new SimpleDateFormat("d-MMM-yyyy");
123: DateFormat ydf1 = new SimpleDateFormat("d-MMM");
124: DateFormat ydf2 = new SimpleDateFormat("d-MMM-yyyy");
125:
126: g1 = new StandardXYToolTipGenerator(f1, xdf1, ydf1);
127: g2 = new StandardXYToolTipGenerator(f1, xdf1, ydf1);
128: assertTrue(g1.equals(g2));
129: assertTrue(g2.equals(g1));
130:
131: g1 = new StandardXYToolTipGenerator(f1, xdf2, ydf1);
132: assertFalse(g1.equals(g2));
133: g2 = new StandardXYToolTipGenerator(f1, xdf2, ydf1);
134: assertTrue(g1.equals(g2));
135:
136: g1 = new StandardXYToolTipGenerator(f1, xdf2, ydf2);
137: assertFalse(g1.equals(g2));
138: g2 = new StandardXYToolTipGenerator(f1, xdf2, ydf2);
139: assertTrue(g1.equals(g2));
140:
141: }
142:
143: /**
144: * Confirm that cloning works.
145: */
146: public void testCloning() {
147: StandardXYToolTipGenerator g1 = new StandardXYToolTipGenerator();
148: StandardXYToolTipGenerator g2 = null;
149: try {
150: g2 = (StandardXYToolTipGenerator) g1.clone();
151: } catch (CloneNotSupportedException e) {
152: System.err.println("Failed to clone.");
153: }
154: assertTrue(g1 != g2);
155: assertTrue(g1.getClass() == g2.getClass());
156: assertTrue(g1.equals(g2));
157: }
158:
159: /**
160: * Serialize an instance, restore it, and check for equality.
161: */
162: public void testSerialization() {
163:
164: StandardXYToolTipGenerator g1 = new StandardXYToolTipGenerator();
165: StandardXYToolTipGenerator g2 = null;
166:
167: try {
168: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
169: ObjectOutput out = new ObjectOutputStream(buffer);
170: out.writeObject(g1);
171: out.close();
172:
173: ObjectInput in = new ObjectInputStream(
174: new ByteArrayInputStream(buffer.toByteArray()));
175: g2 = (StandardXYToolTipGenerator) in.readObject();
176: in.close();
177: } catch (Exception e) {
178: System.out.println(e.toString());
179: }
180: assertEquals(g1, g2);
181:
182: }
183:
184: }
|