KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > junit > PaintListTests


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------
28  * PaintListTests.java
29  * -------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: PaintListTests.java,v 1.3 2005/10/18 13:25:14 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 13-Aug-2003 : Version 1 (DG);
40  * 27-Jun-2005 : Added test for equals() where the list contains
41  * GradientPaint instances (DG);
42  */

43
44 package org.jfree.util.junit;
45
46 import java.awt.Color JavaDoc;
47 import java.awt.GradientPaint JavaDoc;
48 import java.awt.Paint JavaDoc;
49 import java.io.ByteArrayInputStream JavaDoc;
50 import java.io.ByteArrayOutputStream JavaDoc;
51 import java.io.ObjectInput JavaDoc;
52 import java.io.ObjectInputStream JavaDoc;
53 import java.io.ObjectOutput JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.util.PaintList;
61
62 /**
63  * Some tests for the {@link PaintList} class.
64  */

65 public class PaintListTests extends TestCase {
66
67     /**
68      * Returns the tests as a test suite.
69      *
70      * @return The test suite.
71      */

72     public static Test suite() {
73         return new TestSuite(PaintListTests.class);
74     }
75
76     /**
77      * Constructs a new set of tests.
78      *
79      * @param name the name of the tests.
80      */

81     public PaintListTests(final String JavaDoc name) {
82         super(name);
83     }
84
85     /**
86      * Tests the equals() method.
87      */

88     public void testEquals() {
89         final PaintList l1 = new PaintList();
90         l1.setPaint(0, Color.red);
91         l1.setPaint(1, Color.blue);
92         l1.setPaint(2, null);
93         
94         final PaintList l2 = new PaintList();
95         l2.setPaint(0, Color.red);
96         l2.setPaint(1, Color.blue);
97         l2.setPaint(2, null);
98         
99         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 JavaDoc color1 = new Color JavaDoc(200, 200, 200);
110         l1.setPaint(0, color1);
111         final PaintList l2 = new PaintList();
112         final Color JavaDoc color2 = new Color JavaDoc(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 JavaDoc p1 = new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
125                 3.0f, 4.0f, Color.blue);
126         l1.setPaint(0, p1);
127         PaintList l2 = new PaintList();
128         Paint JavaDoc p2 = new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
129                 3.0f, 4.0f, 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         }
148         catch (CloneNotSupportedException JavaDoc e) {
149             System.err.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 JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
174             final ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
175             out.writeObject(l1);
176             out.close();
177
178             final ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
179             l2 = (PaintList) in.readObject();
180             in.close();
181         }
182         catch (Exception JavaDoc e) {
183             System.out.println(e.toString());
184         }
185         assertEquals(l1, l2);
186
187     }
188
189 }
190
Popular Tags