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: * DefaultDrawingSupplierTests.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: DefaultDrawingSupplierTests.java,v 1.1.2.1 2006/10/03 15:41:28 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.plot.junit;
044:
045: import java.awt.BasicStroke;
046: import java.awt.Color;
047: import java.awt.Paint;
048: import java.awt.Shape;
049: import java.awt.Stroke;
050: import java.awt.geom.Rectangle2D;
051: import java.io.ByteArrayInputStream;
052: import java.io.ByteArrayOutputStream;
053: import java.io.ObjectInput;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutput;
056: import java.io.ObjectOutputStream;
057:
058: import junit.framework.Test;
059: import junit.framework.TestCase;
060: import junit.framework.TestSuite;
061:
062: import org.jfree.chart.plot.DefaultDrawingSupplier;
063:
064: /**
065: * Tests for the {@link DefaultDrawingSupplier} class.
066: */
067: public class DefaultDrawingSupplierTests 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(DefaultDrawingSupplierTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public DefaultDrawingSupplierTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Check that the equals() method can distinguish all required fields.
089: */
090: public void testEquals() {
091: DefaultDrawingSupplier r1 = new DefaultDrawingSupplier();
092: DefaultDrawingSupplier r2 = new DefaultDrawingSupplier();
093: assertTrue(r1.equals(r2));
094: assertTrue(r2.equals(r1));
095:
096: // set up some objects...
097: Paint[] ps1A = new Paint[] { Color.red, Color.blue };
098: Paint[] ps2A = new Paint[] { Color.green, Color.yellow,
099: Color.white };
100: Paint[] ops1A = new Paint[] { Color.lightGray, Color.blue };
101: Paint[] ops2A = new Paint[] { Color.black, Color.yellow,
102: Color.cyan };
103: Stroke[] ss1A = new Stroke[] { new BasicStroke(1.1f) };
104: Stroke[] ss2A = new Stroke[] { new BasicStroke(2.2f),
105: new BasicStroke(3.3f) };
106: Stroke[] oss1A = new Stroke[] { new BasicStroke(4.4f) };
107: Stroke[] oss2A = new Stroke[] { new BasicStroke(5.5f),
108: new BasicStroke(6.6f) };
109: Shape[] shapes1A = new Shape[] { new Rectangle2D.Double(1.0,
110: 1.0, 1.0, 1.0) };
111: Shape[] shapes2A = new Shape[] {
112: new Rectangle2D.Double(2.0, 2.0, 2.0, 2.0),
113: new Rectangle2D.Double(2.0, 2.0, 2.0, 2.0) };
114: Paint[] ps1B = new Paint[] { Color.red, Color.blue };
115: Paint[] ps2B = new Paint[] { Color.green, Color.yellow,
116: Color.white };
117: Paint[] ops1B = new Paint[] { Color.lightGray, Color.blue };
118: Paint[] ops2B = new Paint[] { Color.black, Color.yellow,
119: Color.cyan };
120: Stroke[] ss1B = new Stroke[] { new BasicStroke(1.1f) };
121: Stroke[] ss2B = new Stroke[] { new BasicStroke(2.2f),
122: new BasicStroke(3.3f) };
123: Stroke[] oss1B = new Stroke[] { new BasicStroke(4.4f) };
124: Stroke[] oss2B = new Stroke[] { new BasicStroke(5.5f),
125: new BasicStroke(6.6f) };
126: Shape[] shapes1B = new Shape[] { new Rectangle2D.Double(1.0,
127: 1.0, 1.0, 1.0) };
128: Shape[] shapes2B = new Shape[] {
129: new Rectangle2D.Double(2.0, 2.0, 2.0, 2.0),
130: new Rectangle2D.Double(2.0, 2.0, 2.0, 2.0) };
131:
132: r1 = new DefaultDrawingSupplier(ps1A, ops1A, ss1A, oss1A,
133: shapes1A);
134: r2 = new DefaultDrawingSupplier(ps1B, ops1B, ss1B, oss1B,
135: shapes1B);
136: assertTrue(r1.equals(r2));
137:
138: // paint sequence
139: r1 = new DefaultDrawingSupplier(ps2A, ops1A, ss1A, oss1A,
140: shapes1A);
141: assertFalse(r1.equals(r2));
142: r2 = new DefaultDrawingSupplier(ps2B, ops1B, ss1B, oss1B,
143: shapes1B);
144: assertTrue(r1.equals(r2));
145: // outline paint sequence
146: r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss1A, oss1A,
147: shapes1A);
148: assertFalse(r1.equals(r2));
149: r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss1B, oss1B,
150: shapes1B);
151: assertTrue(r1.equals(r2));
152: // stroke sequence
153: r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss2A, oss1A,
154: shapes1A);
155: assertFalse(r1.equals(r2));
156: r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss2B, oss1B,
157: shapes1B);
158: assertTrue(r1.equals(r2));
159: // outline stroke sequence
160: r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss2A, oss2A,
161: shapes1A);
162: assertFalse(r1.equals(r2));
163: r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss2B, oss2B,
164: shapes1B);
165: assertTrue(r1.equals(r2));
166: // shape sequence
167: r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss2A, oss2A,
168: shapes2A);
169: assertFalse(r1.equals(r2));
170: r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss2B, oss2B,
171: shapes2B);
172: assertTrue(r1.equals(r2));
173:
174: // paint index
175: r1.getNextPaint();
176: assertFalse(r1.equals(r2));
177: r2.getNextPaint();
178: assertTrue(r1.equals(r2));
179:
180: // outline paint index
181: r1.getNextOutlinePaint();
182: assertFalse(r1.equals(r2));
183: r2.getNextOutlinePaint();
184: assertTrue(r1.equals(r2));
185:
186: // stroke index
187: r1.getNextStroke();
188: assertFalse(r1.equals(r2));
189: r2.getNextStroke();
190: assertTrue(r1.equals(r2));
191:
192: // outline stroke index
193: r1.getNextOutlineStroke();
194: assertFalse(r1.equals(r2));
195: r2.getNextOutlineStroke();
196: assertTrue(r1.equals(r2));
197:
198: // shape index
199: r1.getNextShape();
200: assertFalse(r1.equals(r2));
201: r2.getNextShape();
202: assertTrue(r1.equals(r2));
203: }
204:
205: /**
206: * Some basic checks for the clone() method.
207: */
208: public void testCloning() {
209: DefaultDrawingSupplier r1 = new DefaultDrawingSupplier();
210: DefaultDrawingSupplier r2 = null;
211: try {
212: r2 = (DefaultDrawingSupplier) r1.clone();
213: } catch (CloneNotSupportedException e) {
214: e.printStackTrace();
215: System.err.println("Failed to clone.");
216: }
217: assertTrue(r1 != r2);
218: assertTrue(r1.getClass() == r2.getClass());
219: assertTrue(r1.equals(r2));
220: }
221:
222: /**
223: * Serialize an instance, restore it, and check for equality.
224: */
225: public void testSerialization() {
226:
227: DefaultDrawingSupplier r1 = new DefaultDrawingSupplier();
228: DefaultDrawingSupplier r2 = null;
229:
230: try {
231: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
232: ObjectOutput out = new ObjectOutputStream(buffer);
233: out.writeObject(r1);
234: out.close();
235:
236: ObjectInput in = new ObjectInputStream(
237: new ByteArrayInputStream(buffer.toByteArray()));
238: r2 = (DefaultDrawingSupplier) in.readObject();
239: in.close();
240: } catch (Exception e) {
241: System.out.println(e.toString());
242: }
243: assertEquals(r1, r2);
244:
245: }
246:
247: }
|