001: /* ========================================================================
002: * JCommon : a free general purpose class 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/jcommon/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: * PaintListTests.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: PaintListTests.java,v 1.3 2005/10/18 13:25:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 13-Aug-2003 : Version 1 (DG);
040: * 27-Jun-2005 : Added test for equals() where the list contains
041: * GradientPaint instances (DG);
042: */
043:
044: package org.jfree.util.junit;
045:
046: import java.awt.Color;
047: import java.awt.GradientPaint;
048: import java.awt.Paint;
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.util.PaintList;
061:
062: /**
063: * Some tests for the {@link PaintList} class.
064: */
065: public class PaintListTests 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(PaintListTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public PaintListTests(final String name) {
082: super (name);
083: }
084:
085: /**
086: * Tests the equals() method.
087: */
088: public void testEquals() {
089: final PaintList l1 = new PaintList();
090: l1.setPaint(0, Color.red);
091: l1.setPaint(1, Color.blue);
092: l1.setPaint(2, null);
093:
094: final PaintList l2 = new PaintList();
095: l2.setPaint(0, Color.red);
096: l2.setPaint(1, Color.blue);
097: l2.setPaint(2, null);
098:
099: assertTrue(l1.equals(l2));
100: assertTrue(l2.equals(l2));
101: }
102:
103: /**
104: * Tests the equals method.
105: */
106: public void testEquals2() {
107: // check two separate (but equal) colors
108: final PaintList l1 = new PaintList();
109: final Color color1 = new Color(200, 200, 200);
110: l1.setPaint(0, color1);
111: final PaintList l2 = new PaintList();
112: final Color color2 = new Color(200, 200, 200);
113: l2.setPaint(0, color2);
114: assertEquals(l1, l2);
115: }
116:
117: /**
118: * Tests the equals() method when the list contains a GradientPaint
119: * instance.
120: */
121: public void testEquals3() {
122: // check two separate (but equal) colors
123: PaintList l1 = new PaintList();
124: Paint p1 = new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
125: Color.blue);
126: l1.setPaint(0, p1);
127: PaintList l2 = new PaintList();
128: Paint p2 = new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
129: Color.blue);
130: l2.setPaint(0, p2);
131: assertEquals(l1, l2);
132: }
133:
134: /**
135: * Confirm that cloning works.
136: */
137: public void testCloning() {
138:
139: final PaintList l1 = new PaintList();
140: l1.setPaint(0, Color.red);
141: l1.setPaint(1, Color.blue);
142: l1.setPaint(2, null);
143:
144: PaintList l2 = null;
145: try {
146: l2 = (PaintList) l1.clone();
147: } catch (CloneNotSupportedException e) {
148: System.err
149: .println("PaintListTests.testCloning: failed to clone.");
150: }
151: assertTrue(l1 != l2);
152: assertTrue(l1.getClass() == l2.getClass());
153: assertTrue(l1.equals(l2));
154:
155: l2.setPaint(0, Color.green);
156: assertFalse(l1.equals(l2));
157:
158: }
159:
160: /**
161: * Serialize an instance, restore it, and check for equality.
162: */
163: public void testSerialization() {
164:
165: final PaintList l1 = new PaintList();
166: l1.setPaint(0, Color.red);
167: l1.setPaint(1, Color.blue);
168: l1.setPaint(2, null);
169:
170: PaintList l2 = null;
171:
172: try {
173: final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
174: final ObjectOutput out = new ObjectOutputStream(buffer);
175: out.writeObject(l1);
176: out.close();
177:
178: final ObjectInput in = new ObjectInputStream(
179: new ByteArrayInputStream(buffer.toByteArray()));
180: l2 = (PaintList) in.readObject();
181: in.close();
182: } catch (Exception e) {
183: System.out.println(e.toString());
184: }
185: assertEquals(l1, l2);
186:
187: }
188:
189: }
|