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: * CategoryAxisTests.java
029: * ----------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CategoryAxisTests.java,v 1.1.2.1 2006/10/03 15:41:22 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Mar-2003 : Version 1 (DG);
040: * 13-Aug-2003 : Added clone() test (DG);
041: * 07-Jan-2005 : Added hashCode() test (DG);
042: *
043: */
044:
045: package org.jfree.chart.axis.junit;
046:
047: import java.awt.Color;
048: import java.awt.Font;
049: import java.awt.GradientPaint;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.ObjectInput;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutput;
055: import java.io.ObjectOutputStream;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.axis.CategoryAxis;
062: import org.jfree.chart.axis.CategoryLabelPositions;
063:
064: /**
065: * Tests for the {@link CategoryAxis} class.
066: */
067: public class CategoryAxisTests 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(CategoryAxisTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public CategoryAxisTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Confirm that the equals method can distinguish all the required fields.
089: */
090: public void testEquals() {
091:
092: CategoryAxis a1 = new CategoryAxis("Test");
093: CategoryAxis a2 = new CategoryAxis("Test");
094: assertTrue(a1.equals(a2));
095:
096: // lowerMargin
097: a1.setLowerMargin(0.15);
098: assertFalse(a1.equals(a2));
099: a2.setLowerMargin(0.15);
100: assertTrue(a1.equals(a2));
101:
102: // upperMargin
103: a1.setUpperMargin(0.15);
104: assertFalse(a1.equals(a2));
105: a2.setUpperMargin(0.15);
106: assertTrue(a1.equals(a2));
107:
108: // categoryMargin
109: a1.setCategoryMargin(0.15);
110: assertFalse(a1.equals(a2));
111: a2.setCategoryMargin(0.15);
112: assertTrue(a1.equals(a2));
113:
114: // maxCategoryLabelWidthRatio
115: a1.setMaximumCategoryLabelWidthRatio(0.98f);
116: assertFalse(a1.equals(a2));
117: a2.setMaximumCategoryLabelWidthRatio(0.98f);
118: assertTrue(a1.equals(a2));
119:
120: // categoryLabelPositionOffset
121: a1.setCategoryLabelPositionOffset(11);
122: assertFalse(a1.equals(a2));
123: a2.setCategoryLabelPositionOffset(11);
124: assertTrue(a1.equals(a2));
125:
126: // categoryLabelPositions
127: a1.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
128: assertFalse(a1.equals(a2));
129: a2.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
130: assertTrue(a1.equals(a2));
131:
132: // categoryLabelToolTips
133: a1.addCategoryLabelToolTip("Test", "Check");
134: assertFalse(a1.equals(a2));
135: a2.addCategoryLabelToolTip("Test", "Check");
136: assertTrue(a1.equals(a2));
137:
138: // tickLabelFont
139: a1.setTickLabelFont("C1", new Font("Dialog", Font.PLAIN, 21));
140: assertFalse(a1.equals(a2));
141: a2.setTickLabelFont("C1", new Font("Dialog", Font.PLAIN, 21));
142: assertTrue(a1.equals(a2));
143:
144: // tickLabelPaint
145: a1.setTickLabelPaint("C1", Color.red);
146: assertFalse(a1.equals(a2));
147: a2.setTickLabelPaint("C1", Color.red);
148: assertTrue(a1.equals(a2));
149:
150: // tickLabelPaint2
151: a1.setTickLabelPaint("C1", new GradientPaint(1.0f, 2.0f,
152: Color.red, 3.0f, 4.0f, Color.yellow));
153: assertFalse(a1.equals(a2));
154: a2.setTickLabelPaint("C1", new GradientPaint(1.0f, 2.0f,
155: Color.red, 3.0f, 4.0f, Color.yellow));
156: assertTrue(a1.equals(a2));
157:
158: }
159:
160: /**
161: * Two objects that are equal are required to return the same hashCode.
162: */
163: public void testHashCode() {
164: CategoryAxis a1 = new CategoryAxis("Test");
165: CategoryAxis a2 = new CategoryAxis("Test");
166: assertTrue(a1.equals(a2));
167: int h1 = a1.hashCode();
168: int h2 = a2.hashCode();
169: assertEquals(h1, h2);
170: }
171:
172: /**
173: * Confirm that cloning works.
174: */
175: public void testCloning() {
176: CategoryAxis a1 = new CategoryAxis("Test");
177: CategoryAxis a2 = null;
178: try {
179: a2 = (CategoryAxis) a1.clone();
180: } catch (CloneNotSupportedException e) {
181: System.err.println("Failed to clone.");
182: }
183: assertTrue(a1 != a2);
184: assertTrue(a1.getClass() == a2.getClass());
185: assertTrue(a1.equals(a2));
186: }
187:
188: /**
189: * Confirm that cloning works. This test customises the font and paint
190: * per category label.
191: */
192: public void testCloning2() {
193: CategoryAxis a1 = new CategoryAxis("Test");
194: a1.setTickLabelFont("C1", new Font("Dialog", Font.PLAIN, 15));
195: a1.setTickLabelPaint("C1", new GradientPaint(1.0f, 2.0f,
196: Color.red, 3.0f, 4.0f, Color.white));
197: CategoryAxis a2 = null;
198: try {
199: a2 = (CategoryAxis) a1.clone();
200: } catch (CloneNotSupportedException e) {
201: System.err.println("Failed to clone.");
202: }
203: assertTrue(a1 != a2);
204: assertTrue(a1.getClass() == a2.getClass());
205: assertTrue(a1.equals(a2));
206:
207: // check that changing a tick label font in a1 doesn't change a2
208: a1.setTickLabelFont("C1", null);
209: assertFalse(a1.equals(a2));
210: a2.setTickLabelFont("C1", null);
211: assertTrue(a1.equals(a2));
212:
213: // check that changing a tick label paint in a1 doesn't change a2
214: a1.setTickLabelPaint("C1", Color.yellow);
215: assertFalse(a1.equals(a2));
216: a2.setTickLabelPaint("C1", Color.yellow);
217: assertTrue(a1.equals(a2));
218:
219: // check that changing a category label tooltip in a1 doesn't change a2
220: a1.addCategoryLabelToolTip("C1", "XYZ");
221: assertFalse(a1.equals(a2));
222: a2.addCategoryLabelToolTip("C1", "XYZ");
223: assertTrue(a1.equals(a2));
224: }
225:
226: /**
227: * Serialize an instance, restore it, and check for equality.
228: */
229: public void testSerialization() {
230: CategoryAxis a1 = new CategoryAxis("Test Axis");
231: a1.setTickLabelPaint("C1", new GradientPaint(1.0f, 2.0f,
232: Color.red, 3.0f, 4.0f, Color.white));
233: CategoryAxis a2 = null;
234:
235: try {
236: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
237: ObjectOutput out = new ObjectOutputStream(buffer);
238: out.writeObject(a1);
239: out.close();
240:
241: ObjectInput in = new ObjectInputStream(
242: new ByteArrayInputStream(buffer.toByteArray()));
243: a2 = (CategoryAxis) in.readObject();
244: in.close();
245: } catch (Exception e) {
246: System.out.println(e.toString());
247: }
248: assertEquals(a1, a2);
249: }
250:
251: }
|