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: * CategoryLabelPositionTests.java
029: * -------------------------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CategoryLabelPositionTests.java,v 1.1.2.1 2006/10/03 15:41:22 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 17-Feb-2004 : Version 1 (DG);
040: * 07-Jan-2005 : Improved testEquals() code and added hashCode() test (DG);
041: *
042: */
043:
044: package org.jfree.chart.axis.junit;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.chart.axis.CategoryLabelPosition;
058: import org.jfree.chart.axis.CategoryLabelWidthType;
059: import org.jfree.text.TextBlockAnchor;
060: import org.jfree.ui.RectangleAnchor;
061: import org.jfree.ui.TextAnchor;
062:
063: /**
064: * Tests for the {@link CategoryLabelPosition} class.
065: */
066: public class CategoryLabelPositionTests extends TestCase {
067:
068: /**
069: * Returns the tests as a test suite.
070: *
071: * @return The test suite.
072: */
073: public static Test suite() {
074: return new TestSuite(CategoryLabelPositionTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public CategoryLabelPositionTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * Check that the equals() method can distinguish all fields.
088: */
089: public void testEquals() {
090: CategoryLabelPosition p1 = new CategoryLabelPosition(
091: RectangleAnchor.BOTTOM_LEFT,
092: TextBlockAnchor.CENTER_RIGHT, TextAnchor.BASELINE_LEFT,
093: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
094: CategoryLabelPosition p2 = new CategoryLabelPosition(
095: RectangleAnchor.BOTTOM_LEFT,
096: TextBlockAnchor.CENTER_RIGHT, TextAnchor.BASELINE_LEFT,
097: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
098: assertTrue(p1.equals(p2));
099: assertTrue(p2.equals(p1));
100:
101: p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
102: TextBlockAnchor.CENTER_RIGHT, TextAnchor.BASELINE_LEFT,
103: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
104: assertFalse(p1.equals(p2));
105: p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
106: TextBlockAnchor.CENTER_RIGHT, TextAnchor.BASELINE_LEFT,
107: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
108: assertTrue(p1.equals(p2));
109:
110: p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
111: TextBlockAnchor.CENTER, TextAnchor.BASELINE_LEFT,
112: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
113: assertFalse(p1.equals(p2));
114: p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
115: TextBlockAnchor.CENTER, TextAnchor.BASELINE_LEFT,
116: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
117: assertTrue(p1.equals(p2));
118:
119: p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
120: TextBlockAnchor.CENTER, TextAnchor.CENTER,
121: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
122: assertFalse(p1.equals(p2));
123: p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
124: TextBlockAnchor.CENTER, TextAnchor.CENTER,
125: Math.PI / 4.0, CategoryLabelWidthType.RANGE, 0.44f);
126: assertTrue(p1.equals(p2));
127:
128: p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
129: TextBlockAnchor.CENTER, TextAnchor.CENTER,
130: Math.PI / 6.0, CategoryLabelWidthType.RANGE, 0.44f);
131: assertFalse(p1.equals(p2));
132: p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
133: TextBlockAnchor.CENTER, TextAnchor.CENTER,
134: Math.PI / 6.0, CategoryLabelWidthType.RANGE, 0.44f);
135: assertTrue(p1.equals(p2));
136:
137: p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
138: TextBlockAnchor.CENTER, TextAnchor.CENTER,
139: Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.44f);
140: assertFalse(p1.equals(p2));
141: p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
142: TextBlockAnchor.CENTER, TextAnchor.CENTER,
143: Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.44f);
144: assertTrue(p1.equals(p2));
145:
146: p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
147: TextBlockAnchor.CENTER, TextAnchor.CENTER,
148: Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.55f);
149: assertFalse(p1.equals(p2));
150: p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
151: TextBlockAnchor.CENTER, TextAnchor.CENTER,
152: Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.55f);
153: assertTrue(p1.equals(p2));
154: }
155:
156: /**
157: * Two objects that are equal are required to return the same hashCode.
158: */
159: public void testHashCode() {
160: CategoryLabelPosition a1 = new CategoryLabelPosition();
161: CategoryLabelPosition a2 = new CategoryLabelPosition();
162: assertTrue(a1.equals(a2));
163: int h1 = a1.hashCode();
164: int h2 = a2.hashCode();
165: assertEquals(h1, h2);
166: }
167:
168: /**
169: * Serialize an instance, restore it, and check for equality.
170: */
171: public void testSerialization() {
172:
173: CategoryLabelPosition p1 = new CategoryLabelPosition();
174: CategoryLabelPosition p2 = null;
175:
176: try {
177: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
178: ObjectOutput out = new ObjectOutputStream(buffer);
179: out.writeObject(p1);
180: out.close();
181:
182: ObjectInput in = new ObjectInputStream(
183: new ByteArrayInputStream(buffer.toByteArray()));
184: p2 = (CategoryLabelPosition) in.readObject();
185: in.close();
186: } catch (Exception e) {
187: System.out.println(e.toString());
188: }
189: assertEquals(p1, p2);
190: }
191:
192: }
|