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: * CategoryTickTests.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: CategoryTickTests.java,v 1.1.2.1 2006/10/03 15:41:23 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 13-May-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.CategoryTick;
058: import org.jfree.text.TextBlock;
059: import org.jfree.text.TextBlockAnchor;
060: import org.jfree.text.TextLine;
061: import org.jfree.ui.TextAnchor;
062:
063: /**
064: * Tests for the {@link CategoryTick} class.
065: */
066: public class CategoryTickTests 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(CategoryTickTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public CategoryTickTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * Confirm that the equals method can distinguish all the required fields.
088: */
089: public void testEquals() {
090:
091: Comparable c1 = "C1";
092: Comparable c2 = "C2";
093: TextBlock tb1 = new TextBlock();
094: tb1.addLine(new TextLine("Block 1"));
095: TextBlock tb2 = new TextBlock();
096: tb1.addLine(new TextLine("Block 2"));
097: TextBlockAnchor tba1 = TextBlockAnchor.CENTER;
098: TextBlockAnchor tba2 = TextBlockAnchor.BOTTOM_CENTER;
099: TextAnchor ta1 = TextAnchor.CENTER;
100: TextAnchor ta2 = TextAnchor.BASELINE_LEFT;
101:
102: CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
103: CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
104: assertTrue(t1.equals(t2));
105:
106: t1 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f);
107: assertFalse(t1.equals(t2));
108: t2 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f);
109: assertTrue(t1.equals(t2));
110:
111: t1 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f);
112: assertFalse(t1.equals(t2));
113: t2 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f);
114: assertTrue(t1.equals(t2));
115:
116: t1 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f);
117: assertFalse(t1.equals(t2));
118: t2 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f);
119: assertTrue(t1.equals(t2));
120:
121: t1 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f);
122: assertFalse(t1.equals(t2));
123: t2 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f);
124: assertTrue(t1.equals(t2));
125:
126: t1 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f);
127: assertFalse(t1.equals(t2));
128: t2 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f);
129: assertTrue(t1.equals(t2));
130:
131: }
132:
133: /**
134: * Two objects that are equal are required to return the same hashCode.
135: */
136: public void testHashCode() {
137: Comparable c1 = "C1";
138: TextBlock tb1 = new TextBlock();
139: tb1.addLine(new TextLine("Block 1"));
140: tb1.addLine(new TextLine("Block 2"));
141: TextBlockAnchor tba1 = TextBlockAnchor.CENTER;
142: TextAnchor ta1 = TextAnchor.CENTER;
143:
144: CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
145: CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
146: assertTrue(t1.equals(t2));
147: int h1 = t1.hashCode();
148: int h2 = t2.hashCode();
149: assertEquals(h1, h2);
150: }
151:
152: /**
153: * Confirm that cloning works.
154: */
155: public void testCloning() {
156: CategoryTick t1 = new CategoryTick("C1", new TextBlock(),
157: TextBlockAnchor.CENTER, TextAnchor.CENTER, 1.5f);
158: CategoryTick t2 = null;
159: try {
160: t2 = (CategoryTick) t1.clone();
161: } catch (CloneNotSupportedException e) {
162: System.err.println("Failed to clone.");
163: }
164: assertTrue(t1 != t2);
165: assertTrue(t1.getClass() == t2.getClass());
166: assertTrue(t1.equals(t2));
167: }
168:
169: /**
170: * Serialize an instance, restore it, and check for equality.
171: */
172: public void testSerialization() {
173:
174: CategoryTick t1 = new CategoryTick("C1", new TextBlock(),
175: TextBlockAnchor.CENTER, TextAnchor.CENTER, 1.5f);
176: CategoryTick t2 = null;
177:
178: try {
179: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
180: ObjectOutput out = new ObjectOutputStream(buffer);
181: out.writeObject(t1);
182: out.close();
183:
184: ObjectInput in = new ObjectInputStream(
185: new ByteArrayInputStream(buffer.toByteArray()));
186: t2 = (CategoryTick) in.readObject();
187: in.close();
188: } catch (Exception e) {
189: System.out.println(e.toString());
190: }
191: assertEquals(t1, t2);
192:
193: }
194:
195: }
|