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: * CategoryLabelPositionsTests.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: CategoryLabelPositionsTests.java,v 1.1.2.1 2006/10/03 15:41:23 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 17-Feb-2004 : Version 1 (DG);
040: * 07-Jan-2005 : Added test for hashCode() (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.CategoryLabelPositions;
059: import org.jfree.text.TextBlockAnchor;
060: import org.jfree.ui.RectangleAnchor;
061:
062: /**
063: * Tests for the {@link CategoryLabelPositions} class.
064: */
065: public class CategoryLabelPositionsTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(CategoryLabelPositionsTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public CategoryLabelPositionsTests(String name) {
082: super (name);
083: }
084:
085: private static final RectangleAnchor RA_TOP = RectangleAnchor.TOP;
086: private static final RectangleAnchor RA_BOTTOM = RectangleAnchor.BOTTOM;
087:
088: /**
089: * Check that the equals method distinguishes all fields.
090: */
091: public void testEquals() {
092: CategoryLabelPositions p1 = new CategoryLabelPositions(
093: new CategoryLabelPosition(RA_TOP,
094: TextBlockAnchor.CENTER),
095: new CategoryLabelPosition(RA_TOP,
096: TextBlockAnchor.CENTER),
097: new CategoryLabelPosition(RA_TOP,
098: TextBlockAnchor.CENTER),
099: new CategoryLabelPosition(RA_TOP,
100: TextBlockAnchor.CENTER));
101: CategoryLabelPositions p2 = new CategoryLabelPositions(
102: new CategoryLabelPosition(RA_TOP,
103: TextBlockAnchor.CENTER),
104: new CategoryLabelPosition(RA_TOP,
105: TextBlockAnchor.CENTER),
106: new CategoryLabelPosition(RA_TOP,
107: TextBlockAnchor.CENTER),
108: new CategoryLabelPosition(RA_TOP,
109: TextBlockAnchor.CENTER));
110: assertEquals(p1, p2);
111:
112: p1 = new CategoryLabelPositions(new CategoryLabelPosition(
113: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
114: new CategoryLabelPosition(RA_TOP,
115: TextBlockAnchor.CENTER),
116: new CategoryLabelPosition(RA_TOP,
117: TextBlockAnchor.CENTER),
118: new CategoryLabelPosition(RA_TOP,
119: TextBlockAnchor.CENTER));
120: assertTrue(!p1.equals(p2));
121: p2 = new CategoryLabelPositions(new CategoryLabelPosition(
122: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
123: new CategoryLabelPosition(RA_TOP,
124: TextBlockAnchor.CENTER),
125: new CategoryLabelPosition(RA_TOP,
126: TextBlockAnchor.CENTER),
127: new CategoryLabelPosition(RA_TOP,
128: TextBlockAnchor.CENTER));
129: assertTrue(p1.equals(p2));
130:
131: p1 = new CategoryLabelPositions(new CategoryLabelPosition(
132: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
133: new CategoryLabelPosition(RA_BOTTOM,
134: TextBlockAnchor.TOP_CENTER),
135: new CategoryLabelPosition(RA_TOP,
136: TextBlockAnchor.CENTER),
137: new CategoryLabelPosition(RA_TOP,
138: TextBlockAnchor.CENTER));
139: assertTrue(!p1.equals(p2));
140: p2 = new CategoryLabelPositions(new CategoryLabelPosition(
141: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
142: new CategoryLabelPosition(RA_BOTTOM,
143: TextBlockAnchor.TOP_CENTER),
144: new CategoryLabelPosition(RA_TOP,
145: TextBlockAnchor.CENTER),
146: new CategoryLabelPosition(RA_TOP,
147: TextBlockAnchor.CENTER));
148: assertTrue(p1.equals(p2));
149:
150: p1 = new CategoryLabelPositions(new CategoryLabelPosition(
151: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
152: new CategoryLabelPosition(RA_BOTTOM,
153: TextBlockAnchor.TOP_CENTER),
154: new CategoryLabelPosition(RA_BOTTOM,
155: TextBlockAnchor.TOP_CENTER),
156: new CategoryLabelPosition(RA_TOP,
157: TextBlockAnchor.CENTER));
158: assertTrue(!p1.equals(p2));
159: p2 = new CategoryLabelPositions(new CategoryLabelPosition(
160: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
161: new CategoryLabelPosition(RA_BOTTOM,
162: TextBlockAnchor.TOP_CENTER),
163: new CategoryLabelPosition(RA_BOTTOM,
164: TextBlockAnchor.TOP_CENTER),
165: new CategoryLabelPosition(RA_TOP,
166: TextBlockAnchor.CENTER));
167: assertTrue(p1.equals(p2));
168:
169: p1 = new CategoryLabelPositions(new CategoryLabelPosition(
170: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
171: new CategoryLabelPosition(RA_BOTTOM,
172: TextBlockAnchor.TOP_CENTER),
173: new CategoryLabelPosition(RA_BOTTOM,
174: TextBlockAnchor.TOP_CENTER),
175: new CategoryLabelPosition(RA_BOTTOM,
176: TextBlockAnchor.TOP_CENTER));
177: assertTrue(!p1.equals(p2));
178: p2 = new CategoryLabelPositions(new CategoryLabelPosition(
179: RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
180: new CategoryLabelPosition(RA_BOTTOM,
181: TextBlockAnchor.TOP_CENTER),
182: new CategoryLabelPosition(RA_BOTTOM,
183: TextBlockAnchor.TOP_CENTER),
184: new CategoryLabelPosition(RA_BOTTOM,
185: TextBlockAnchor.TOP_CENTER));
186: assertTrue(p1.equals(p2));
187: }
188:
189: /**
190: * Two objects that are equal are required to return the same hashCode.
191: */
192: public void testHashCode() {
193: CategoryLabelPositions p1 = new CategoryLabelPositions(
194: new CategoryLabelPosition(RA_TOP,
195: TextBlockAnchor.CENTER),
196: new CategoryLabelPosition(RA_TOP,
197: TextBlockAnchor.CENTER),
198: new CategoryLabelPosition(RA_TOP,
199: TextBlockAnchor.CENTER),
200: new CategoryLabelPosition(RA_TOP,
201: TextBlockAnchor.CENTER));
202: CategoryLabelPositions p2 = new CategoryLabelPositions(
203: new CategoryLabelPosition(RA_TOP,
204: TextBlockAnchor.CENTER),
205: new CategoryLabelPosition(RA_TOP,
206: TextBlockAnchor.CENTER),
207: new CategoryLabelPosition(RA_TOP,
208: TextBlockAnchor.CENTER),
209: new CategoryLabelPosition(RA_TOP,
210: TextBlockAnchor.CENTER));
211: assertTrue(p1.equals(p2));
212: int h1 = p1.hashCode();
213: int h2 = p2.hashCode();
214: assertEquals(h1, h2);
215: }
216:
217: /**
218: * Serialize an instance, restore it, and check for equality.
219: */
220: public void testSerialization() {
221:
222: CategoryLabelPositions p1 = CategoryLabelPositions.STANDARD;
223: CategoryLabelPositions p2 = null;
224:
225: try {
226: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
227: ObjectOutput out = new ObjectOutputStream(buffer);
228: out.writeObject(p1);
229: out.close();
230:
231: ObjectInput in = new ObjectInputStream(
232: new ByteArrayInputStream(buffer.toByteArray()));
233: p2 = (CategoryLabelPositions) in.readObject();
234: in.close();
235: } catch (Exception e) {
236: System.out.println(e.toString());
237: }
238: assertEquals(p1, p2);
239: }
240:
241: }
|