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: * StandardPieSectionLabelGeneratorTests.java
029: * ------------------------------------------
030: * (C) Copyright 2003, 2004, 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StandardPieSectionLabelGeneratorTests.java,v 1.1.2.2 2006/11/24 16:24:29 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Mar-2003 : Version 1 (DG);
040: * 13-Aug-2003 : Added clone tests (DG);
041: * 04-Mar-2004 : Added test for equals() method (DG);
042: * 23-Nov-2006 : Extended equals() test (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.AttributedString;
055: import java.text.DecimalFormat;
056: import java.text.NumberFormat;
057:
058: import junit.framework.Test;
059: import junit.framework.TestCase;
060: import junit.framework.TestSuite;
061:
062: import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
063:
064: /**
065: * Tests for the {@link StandardPieSectionLabelGenerator} class.
066: */
067: public class StandardPieSectionLabelGeneratorTests 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(
076: StandardPieSectionLabelGeneratorTests.class);
077: }
078:
079: /**
080: * Constructs a new set of tests.
081: *
082: * @param name the name of the tests.
083: */
084: public StandardPieSectionLabelGeneratorTests(String name) {
085: super (name);
086: }
087:
088: /**
089: * Test that the equals() method distinguishes all fields.
090: */
091: public void testEquals() {
092: StandardPieSectionLabelGenerator g1 = new StandardPieSectionLabelGenerator();
093: StandardPieSectionLabelGenerator g2 = new StandardPieSectionLabelGenerator();
094: assertTrue(g1.equals(g2));
095: assertTrue(g2.equals(g1));
096:
097: g1 = new StandardPieSectionLabelGenerator("{0}",
098: new DecimalFormat("#,##0.00"), NumberFormat
099: .getPercentInstance());
100: assertFalse(g1.equals(g2));
101: g2 = new StandardPieSectionLabelGenerator("{0}",
102: new DecimalFormat("#,##0.00"), NumberFormat
103: .getPercentInstance());
104: assertTrue(g1.equals(g2));
105:
106: g1 = new StandardPieSectionLabelGenerator("{0} {1}",
107: new DecimalFormat("#,##0.00"), NumberFormat
108: .getPercentInstance());
109: assertFalse(g1.equals(g2));
110: g2 = new StandardPieSectionLabelGenerator("{0} {1}",
111: new DecimalFormat("#,##0.00"), NumberFormat
112: .getPercentInstance());
113: assertTrue(g1.equals(g2));
114:
115: g1 = new StandardPieSectionLabelGenerator("{0} {1}",
116: new DecimalFormat("#,##0"), NumberFormat
117: .getPercentInstance());
118: assertFalse(g1.equals(g2));
119: g2 = new StandardPieSectionLabelGenerator("{0} {1}",
120: new DecimalFormat("#,##0"), NumberFormat
121: .getPercentInstance());
122: assertTrue(g1.equals(g2));
123:
124: g1 = new StandardPieSectionLabelGenerator("{0} {1}",
125: new DecimalFormat("#,##0"), new DecimalFormat("0.000%"));
126: assertFalse(g1.equals(g2));
127: g2 = new StandardPieSectionLabelGenerator("{0} {1}",
128: new DecimalFormat("#,##0"), new DecimalFormat("0.000%"));
129: assertTrue(g1.equals(g2));
130:
131: AttributedString as = new AttributedString("XYZ");
132: g1.setAttributedLabel(0, as);
133: assertFalse(g1.equals(g2));
134: g2.setAttributedLabel(0, as);
135: assertTrue(g1.equals(g2));
136: }
137:
138: /**
139: * Confirm that cloning works.
140: */
141: public void testCloning() {
142: StandardPieSectionLabelGenerator g1 = new StandardPieSectionLabelGenerator();
143: StandardPieSectionLabelGenerator g2 = null;
144: try {
145: g2 = (StandardPieSectionLabelGenerator) g1.clone();
146: } catch (CloneNotSupportedException e) {
147: e.printStackTrace();
148: }
149: assertTrue(g1 != g2);
150: assertTrue(g1.getClass() == g2.getClass());
151: assertTrue(g1.equals(g2));
152: }
153:
154: /**
155: * Serialize an instance, restore it, and check for equality.
156: */
157: public void testSerialization() {
158:
159: StandardPieSectionLabelGenerator g1 = new StandardPieSectionLabelGenerator();
160: StandardPieSectionLabelGenerator g2 = null;
161:
162: try {
163: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
164: ObjectOutput out = new ObjectOutputStream(buffer);
165: out.writeObject(g1);
166: out.close();
167:
168: ObjectInput in = new ObjectInputStream(
169: new ByteArrayInputStream(buffer.toByteArray()));
170: g2 = (StandardPieSectionLabelGenerator) in.readObject();
171: in.close();
172: } catch (Exception e) {
173: e.printStackTrace();
174: }
175: assertEquals(g1, g2);
176:
177: }
178:
179: /**
180: * Runs the test suite using JUnit's text-based runner.
181: *
182: * @param args ignored.
183: */
184: public static void main(String[] args) {
185: junit.textui.TestRunner.run(suite());
186: }
187:
188: }
|