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: * ExtendedCategoryAxisTests.java
029: * ------------------------------
030: * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ExtendedCategoryAxisTests.java,v 1.1.2.1 2007/03/21 11:16:59 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 21-Mar-2007 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.axis.junit;
044:
045: import java.awt.Color;
046: import java.awt.Font;
047: import java.awt.GradientPaint;
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:
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: import org.jfree.chart.axis.ExtendedCategoryAxis;
060:
061: /**
062: * Tests for the {@link ExtendedCategoryAxis} class.
063: */
064: public class ExtendedCategoryAxisTests extends TestCase {
065:
066: /**
067: * Returns the tests as a test suite.
068: *
069: * @return The test suite.
070: */
071: public static Test suite() {
072: return new TestSuite(ExtendedCategoryAxisTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public ExtendedCategoryAxisTests(String name) {
081: super (name);
082: }
083:
084: /**
085: * Confirm that the equals method can distinguish all the required fields.
086: */
087: public void testEquals() {
088:
089: ExtendedCategoryAxis a1 = new ExtendedCategoryAxis("Test");
090: ExtendedCategoryAxis a2 = new ExtendedCategoryAxis("Test");
091: assertTrue(a1.equals(a2));
092:
093: a1.addSubLabel("C1", "C1-sublabel");
094: assertFalse(a1.equals(a2));
095: a2.addSubLabel("C1", "C1-sublabel");
096: assertTrue(a1.equals(a2));
097:
098: a1.setSubLabelFont(new Font("Dialog", Font.BOLD, 8));
099: assertFalse(a1.equals(a2));
100: a2.setSubLabelFont(new Font("Dialog", Font.BOLD, 8));
101: assertTrue(a1.equals(a2));
102:
103: a1.setSubLabelPaint(Color.red);
104: assertFalse(a1.equals(a2));
105: a2.setSubLabelPaint(Color.red);
106: assertTrue(a1.equals(a2));
107: }
108:
109: /**
110: * Two objects that are equal are required to return the same hashCode.
111: */
112: public void testHashCode() {
113: ExtendedCategoryAxis a1 = new ExtendedCategoryAxis("Test");
114: ExtendedCategoryAxis a2 = new ExtendedCategoryAxis("Test");
115: assertTrue(a1.equals(a2));
116: int h1 = a1.hashCode();
117: int h2 = a2.hashCode();
118: assertEquals(h1, h2);
119: }
120:
121: /**
122: * Confirm that cloning works.
123: */
124: public void testCloning() {
125: ExtendedCategoryAxis a1 = new ExtendedCategoryAxis("Test");
126: ExtendedCategoryAxis a2 = null;
127: try {
128: a2 = (ExtendedCategoryAxis) a1.clone();
129: } catch (CloneNotSupportedException e) {
130: e.printStackTrace();
131: }
132: assertTrue(a1 != a2);
133: assertTrue(a1.getClass() == a2.getClass());
134: assertTrue(a1.equals(a2));
135:
136: // check independence
137: a1.addSubLabel("C1", "ABC");
138: assertFalse(a1.equals(a2));
139: a2.addSubLabel("C1", "ABC");
140: assertTrue(a1.equals(a2));
141:
142: }
143:
144: /**
145: * Confirm that cloning works. This test customises the font and paint
146: * per category label.
147: */
148: public void testCloning2() {
149: ExtendedCategoryAxis a1 = new ExtendedCategoryAxis("Test");
150: a1.setTickLabelFont("C1", new Font("Dialog", Font.PLAIN, 15));
151: a1.setTickLabelPaint("C1", new GradientPaint(1.0f, 2.0f,
152: Color.red, 3.0f, 4.0f, Color.white));
153: ExtendedCategoryAxis a2 = null;
154: try {
155: a2 = (ExtendedCategoryAxis) a1.clone();
156: } catch (CloneNotSupportedException e) {
157: e.printStackTrace();
158: }
159: assertTrue(a1 != a2);
160: assertTrue(a1.getClass() == a2.getClass());
161: assertTrue(a1.equals(a2));
162:
163: // check that changing a tick label font in a1 doesn't change a2
164: a1.setTickLabelFont("C1", null);
165: assertFalse(a1.equals(a2));
166: a2.setTickLabelFont("C1", null);
167: assertTrue(a1.equals(a2));
168:
169: // check that changing a tick label paint in a1 doesn't change a2
170: a1.setTickLabelPaint("C1", Color.yellow);
171: assertFalse(a1.equals(a2));
172: a2.setTickLabelPaint("C1", Color.yellow);
173: assertTrue(a1.equals(a2));
174:
175: // check that changing a category label tooltip in a1 doesn't change a2
176: a1.addCategoryLabelToolTip("C1", "XYZ");
177: assertFalse(a1.equals(a2));
178: a2.addCategoryLabelToolTip("C1", "XYZ");
179: assertTrue(a1.equals(a2));
180: }
181:
182: /**
183: * Serialize an instance, restore it, and check for equality.
184: */
185: public void testSerialization() {
186: ExtendedCategoryAxis a1 = new ExtendedCategoryAxis("Test");
187: a1.setSubLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
188: 3.0f, 4.0f, Color.blue));
189: ExtendedCategoryAxis a2 = null;
190:
191: try {
192: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
193: ObjectOutput out = new ObjectOutputStream(buffer);
194: out.writeObject(a1);
195: out.close();
196:
197: ObjectInput in = new ObjectInputStream(
198: new ByteArrayInputStream(buffer.toByteArray()));
199: a2 = (ExtendedCategoryAxis) in.readObject();
200: in.close();
201: } catch (Exception e) {
202: e.printStackTrace();
203: }
204: assertEquals(a1, a2);
205: }
206:
207: }
|