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: * StandardXYItemLabelGeneratorTests.java
029: * --------------------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StandardXYItemLabelGeneratorTests.java,v 1.1.2.3 2007/02/02 13:50:36 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Mar-2003 : Version 1 (DG);
040: * 26-Feb-2004 : Updates for new code (DG);
041: * 20-Jan-2006 : Renamed StandardXYItemLabelGeneratorTests.java (DG);
042: * 25-Jan-2007 : Added independence checks to testCloning() (DG);
043: *
044: */
045:
046: package org.jfree.chart.labels.junit;
047:
048: import java.io.ByteArrayInputStream;
049: import java.io.ByteArrayOutputStream;
050: import java.io.ObjectInput;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutput;
053: import java.io.ObjectOutputStream;
054: import java.text.DateFormat;
055: import java.text.DecimalFormat;
056: import java.text.NumberFormat;
057: import java.text.SimpleDateFormat;
058:
059: import junit.framework.Test;
060: import junit.framework.TestCase;
061: import junit.framework.TestSuite;
062:
063: import org.jfree.chart.labels.StandardXYItemLabelGenerator;
064:
065: /**
066: * Tests for the {@link StandardXYItemLabelGenerator} class.
067: */
068: public class StandardXYItemLabelGeneratorTests 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(StandardXYItemLabelGeneratorTests.class);
077: }
078:
079: /**
080: * Constructs a new set of tests.
081: *
082: * @param name the name of the tests.
083: */
084: public StandardXYItemLabelGeneratorTests(String name) {
085: super (name);
086: }
087:
088: /**
089: * A series of tests for the equals() method.
090: */
091: public void testEquals() {
092:
093: // some setup...
094: String f1 = "{1}";
095: String f2 = "{2}";
096: NumberFormat xnf1 = new DecimalFormat("0.00");
097: NumberFormat xnf2 = new DecimalFormat("0.000");
098: NumberFormat ynf1 = new DecimalFormat("0.00");
099: NumberFormat ynf2 = new DecimalFormat("0.000");
100:
101: StandardXYItemLabelGenerator g1 = null;
102: StandardXYItemLabelGenerator g2 = null;
103:
104: g1 = new StandardXYItemLabelGenerator(f1, xnf1, ynf1);
105: g2 = new StandardXYItemLabelGenerator(f1, xnf1, ynf1);
106: assertTrue(g1.equals(g2));
107: assertTrue(g2.equals(g1));
108:
109: g1 = new StandardXYItemLabelGenerator(f2, xnf1, ynf1);
110: assertFalse(g1.equals(g2));
111: g2 = new StandardXYItemLabelGenerator(f2, xnf1, ynf1);
112: assertTrue(g1.equals(g2));
113:
114: g1 = new StandardXYItemLabelGenerator(f2, xnf2, ynf1);
115: assertFalse(g1.equals(g2));
116: g2 = new StandardXYItemLabelGenerator(f2, xnf2, ynf1);
117: assertTrue(g1.equals(g2));
118:
119: g1 = new StandardXYItemLabelGenerator(f2, xnf2, ynf2);
120: assertFalse(g1.equals(g2));
121: g2 = new StandardXYItemLabelGenerator(f2, xnf2, ynf2);
122: assertTrue(g1.equals(g2));
123:
124: DateFormat xdf1 = new SimpleDateFormat("d-MMM");
125: DateFormat xdf2 = new SimpleDateFormat("d-MMM-yyyy");
126: DateFormat ydf1 = new SimpleDateFormat("d-MMM");
127: DateFormat ydf2 = new SimpleDateFormat("d-MMM-yyyy");
128:
129: g1 = new StandardXYItemLabelGenerator(f1, xdf1, ydf1);
130: g2 = new StandardXYItemLabelGenerator(f1, xdf1, ydf1);
131: assertTrue(g1.equals(g2));
132: assertTrue(g2.equals(g1));
133:
134: g1 = new StandardXYItemLabelGenerator(f1, xdf2, ydf1);
135: assertFalse(g1.equals(g2));
136: g2 = new StandardXYItemLabelGenerator(f1, xdf2, ydf1);
137: assertTrue(g1.equals(g2));
138:
139: g1 = new StandardXYItemLabelGenerator(f1, xdf2, ydf2);
140: assertFalse(g1.equals(g2));
141: g2 = new StandardXYItemLabelGenerator(f1, xdf2, ydf2);
142: assertTrue(g1.equals(g2));
143:
144: }
145:
146: /**
147: * Confirm that cloning works.
148: */
149: public void testCloning() {
150: StandardXYItemLabelGenerator g1 = new StandardXYItemLabelGenerator();
151: StandardXYItemLabelGenerator g2 = null;
152: try {
153: g2 = (StandardXYItemLabelGenerator) g1.clone();
154: } catch (CloneNotSupportedException e) {
155: e.printStackTrace();
156: }
157: assertTrue(g1 != g2);
158: assertTrue(g1.getClass() == g2.getClass());
159: assertTrue(g1.equals(g2));
160:
161: // check independence
162: g1.getXFormat().setMinimumIntegerDigits(2);
163: assertFalse(g1.equals(g2));
164: g2.getXFormat().setMinimumIntegerDigits(2);
165: assertTrue(g1.equals(g2));
166:
167: g1.getYFormat().setMinimumIntegerDigits(2);
168: assertFalse(g1.equals(g2));
169: g2.getYFormat().setMinimumIntegerDigits(2);
170: assertTrue(g1.equals(g2));
171:
172: // another test...
173: g1 = new StandardXYItemLabelGenerator("{0} {1} {2}", DateFormat
174: .getInstance(), DateFormat.getInstance());
175: try {
176: g2 = (StandardXYItemLabelGenerator) g1.clone();
177: } catch (CloneNotSupportedException e) {
178: e.printStackTrace();
179: }
180: assertTrue(g1 != g2);
181: assertTrue(g1.getClass() == g2.getClass());
182: assertTrue(g1.equals(g2));
183:
184: // check independence
185: g1.getXDateFormat().setNumberFormat(new DecimalFormat("0.000"));
186: assertFalse(g1.equals(g2));
187: g2.getXDateFormat().setNumberFormat(new DecimalFormat("0.000"));
188: assertTrue(g1.equals(g2));
189:
190: g1.getYDateFormat().setNumberFormat(new DecimalFormat("0.000"));
191: assertFalse(g1.equals(g2));
192: g2.getYDateFormat().setNumberFormat(new DecimalFormat("0.000"));
193: assertTrue(g1.equals(g2));
194:
195: }
196:
197: /**
198: * Serialize an instance, restore it, and check for equality.
199: */
200: public void testSerialization() {
201:
202: StandardXYItemLabelGenerator g1 = new StandardXYItemLabelGenerator();
203: StandardXYItemLabelGenerator g2 = null;
204:
205: try {
206: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
207: ObjectOutput out = new ObjectOutputStream(buffer);
208: out.writeObject(g1);
209: out.close();
210:
211: ObjectInput in = new ObjectInputStream(
212: new ByteArrayInputStream(buffer.toByteArray()));
213: g2 = (StandardXYItemLabelGenerator) in.readObject();
214: in.close();
215: } catch (Exception e) {
216: e.printStackTrace();
217: }
218: assertEquals(g1, g2);
219:
220: }
221:
222: }
|