001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * BubbleXYItemLabelGeneratorTests.java
029: * ------------------------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: BubbleXYItemLabelGeneratorTests.java,v 1.1.2.1 2006/10/03 15:41:36 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 20-Jan-2006 : 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.BubbleXYItemLabelGenerator;
061: import org.jfree.data.xy.XYSeries;
062: import org.jfree.data.xy.XYSeriesCollection;
063:
064: /**
065: * Tests for the {@link BubbleXYItemLabelGenerator} class.
066: */
067: public class BubbleXYItemLabelGeneratorTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(BubbleXYItemLabelGeneratorTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public BubbleXYItemLabelGeneratorTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * A series of tests for the equals() method.
089: */
090: public void testEquals() {
091:
092: // some setup...
093: String f1 = "{1}";
094: String f2 = "{2}";
095: NumberFormat xnf1 = new DecimalFormat("0.00");
096: NumberFormat xnf2 = new DecimalFormat("0.000");
097: NumberFormat ynf1 = new DecimalFormat("0.00");
098: NumberFormat ynf2 = new DecimalFormat("0.000");
099: NumberFormat znf1 = new DecimalFormat("0.00");
100: NumberFormat znf2 = new DecimalFormat("0.000");
101:
102: BubbleXYItemLabelGenerator g1 = null;
103: BubbleXYItemLabelGenerator g2 = null;
104:
105: g1 = new BubbleXYItemLabelGenerator(f1, xnf1, ynf1, znf1);
106: g2 = new BubbleXYItemLabelGenerator(f1, xnf1, ynf1, znf1);
107: assertTrue(g1.equals(g2));
108: assertTrue(g2.equals(g1));
109:
110: g1 = new BubbleXYItemLabelGenerator(f2, xnf1, ynf1, znf1);
111: assertFalse(g1.equals(g2));
112: g2 = new BubbleXYItemLabelGenerator(f2, xnf1, ynf1, znf1);
113: assertTrue(g1.equals(g2));
114:
115: g1 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf1, znf1);
116: assertFalse(g1.equals(g2));
117: g2 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf1, znf1);
118: assertTrue(g1.equals(g2));
119:
120: g1 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf1);
121: assertFalse(g1.equals(g2));
122: g2 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf1);
123: assertTrue(g1.equals(g2));
124:
125: g1 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf2);
126: assertFalse(g1.equals(g2));
127: g2 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf2);
128: assertTrue(g1.equals(g2));
129:
130: DateFormat xdf1 = new SimpleDateFormat("d-MMM");
131: DateFormat xdf2 = new SimpleDateFormat("d-MMM-yyyy");
132: DateFormat ydf1 = new SimpleDateFormat("d-MMM");
133: DateFormat ydf2 = new SimpleDateFormat("d-MMM-yyyy");
134: DateFormat zdf1 = new SimpleDateFormat("d-MMM");
135: DateFormat zdf2 = new SimpleDateFormat("d-MMM-yyyy");
136:
137: g1 = new BubbleXYItemLabelGenerator(f1, xdf1, ydf1, zdf1);
138: g2 = new BubbleXYItemLabelGenerator(f1, xdf1, ydf1, zdf1);
139: assertTrue(g1.equals(g2));
140: assertTrue(g2.equals(g1));
141:
142: g1 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf1, zdf1);
143: assertFalse(g1.equals(g2));
144: g2 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf1, zdf1);
145: assertTrue(g1.equals(g2));
146:
147: g1 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf1);
148: assertFalse(g1.equals(g2));
149: g2 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf1);
150: assertTrue(g1.equals(g2));
151:
152: g1 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf2);
153: assertFalse(g1.equals(g2));
154: g2 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf2);
155: assertTrue(g1.equals(g2));
156: }
157:
158: /**
159: * Confirm that cloning works.
160: */
161: public void testCloning() {
162: BubbleXYItemLabelGenerator g1 = new BubbleXYItemLabelGenerator();
163: BubbleXYItemLabelGenerator g2 = null;
164: try {
165: g2 = (BubbleXYItemLabelGenerator) g1.clone();
166: } catch (CloneNotSupportedException e) {
167: System.err.println("Clone failed.");
168: }
169: assertTrue(g1 != g2);
170: assertTrue(g1.getClass() == g2.getClass());
171: assertTrue(g1.equals(g2));
172: }
173:
174: /**
175: * Serialize an instance, restore it, and check for equality.
176: */
177: public void testSerialization() {
178:
179: BubbleXYItemLabelGenerator g1 = new BubbleXYItemLabelGenerator();
180: BubbleXYItemLabelGenerator g2 = null;
181:
182: try {
183: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
184: ObjectOutput out = new ObjectOutputStream(buffer);
185: out.writeObject(g1);
186: out.close();
187:
188: ObjectInput in = new ObjectInputStream(
189: new ByteArrayInputStream(buffer.toByteArray()));
190: g2 = (BubbleXYItemLabelGenerator) in.readObject();
191: in.close();
192: } catch (Exception e) {
193: System.out.println(e.toString());
194: }
195: assertEquals(g1, g2);
196:
197: }
198:
199: /**
200: * Some checks for the testGenerateLabel() method.
201: */
202: public void testGenerateLabel() {
203: // check handling when the dataset is a regular XYDataset, not an
204: // XYZDataset...
205: XYSeries s1 = new XYSeries("S1");
206: s1.add(1.0, 2.0);
207: s1.add(2.2, 3.3);
208: XYSeriesCollection dataset = new XYSeriesCollection(s1);
209: BubbleXYItemLabelGenerator g = new BubbleXYItemLabelGenerator();
210: assertEquals("{3}", g.generateLabel(dataset, 0, 0));
211: assertEquals("{3}", g.generateLabel(dataset, 0, 1));
212: }
213:
214: }
|