1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2014, 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  * PaintListTest.java
29  * ------------------
30  * (C) Copyright 2003-2014, by Object Refinery Limited and Contributors.
31  *
32  * Original Author:  David Gilbert (for Object Refinery Limited);
33  * Contributor(s):   -;
34  *
35  * $Id: PaintListTest.java,v 1.5 2011/11/03 20:53:47 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  * 03-Nov-2011 : Extended testEqual() to cover defect mentioned in patch
43  *               2999449 (DG);
44  *
45  */
46 
47 package org.jfree.util;
48 
49 import java.awt.Color;
50 import java.awt.GradientPaint;
51 import java.awt.Paint;
52 import java.io.ByteArrayInputStream;
53 import java.io.ByteArrayOutputStream;
54 import java.io.ObjectInput;
55 import java.io.ObjectInputStream;
56 import java.io.ObjectOutput;
57 import java.io.ObjectOutputStream;
58 
59 import junit.framework.Test;
60 import junit.framework.TestCase;
61 import junit.framework.TestSuite;
62 import org.jfree.util.PaintList;
63 
64 /**
65  * Some tests for the {@link PaintList} class.
66  */
67 public class PaintListTest extends TestCase {
68 
69     /**
70      * Returns the tests as a test suite.
71      *
72      * @return The test suite.
73      */
suite()74     public static Test suite() {
75         return new TestSuite(PaintListTest.class);
76     }
77 
78     /**
79      * Constructs a new set of tests.
80      *
81      * @param name  the name of the tests.
82      */
PaintListTest(final String name)83     public PaintListTest(final String name) {
84         super(name);
85     }
86 
87     /**
88      * Tests the equals() method.
89      */
testEquals()90     public void testEquals() {
91         final PaintList l1 = new PaintList();
92         l1.setPaint(0, Color.red);
93         l1.setPaint(1, Color.blue);
94         l1.setPaint(2, null);
95 
96         final PaintList l2 = new PaintList();
97         l2.setPaint(0, Color.red);
98         l2.setPaint(1, Color.blue);
99         l2.setPaint(2, null);
100 
101         assertTrue(l1.equals(l2));
102         assertTrue(l2.equals(l2));
103 
104         assertFalse(l1.equals("XYZ"));
105     }
106 
107     /**
108      * Tests the equals method.
109      */
testEquals2()110     public void testEquals2() {
111         // check two separate (but equal) colors
112         final PaintList l1 = new PaintList();
113         final Color color1 = new Color(200, 200, 200);
114         l1.setPaint(0, color1);
115         final PaintList l2 = new PaintList();
116         final Color color2 = new Color(200, 200, 200);
117         l2.setPaint(0, color2);
118         assertEquals(l1, l2);
119     }
120 
121     /**
122      * Tests the equals() method when the list contains a GradientPaint
123      * instance.
124      */
testEquals3()125     public void testEquals3() {
126         // check two separate (but equal) colors
127         PaintList l1 = new PaintList();
128         Paint p1 = new GradientPaint(1.0f, 2.0f, Color.red,
129                 3.0f, 4.0f, Color.blue);
130         l1.setPaint(0, p1);
131         PaintList l2 = new PaintList();
132         Paint p2 = new GradientPaint(1.0f, 2.0f, Color.red,
133                 3.0f, 4.0f, Color.blue);
134         l2.setPaint(0, p2);
135         assertEquals(l1, l2);
136     }
137 
138     /**
139      * Confirm that cloning works.
140      */
testCloning()141     public void testCloning() {
142 
143         final PaintList l1 = new PaintList();
144         l1.setPaint(0, Color.red);
145         l1.setPaint(1, Color.blue);
146         l1.setPaint(2, null);
147 
148         PaintList l2 = null;
149         try {
150             l2 = (PaintList) l1.clone();
151         }
152         catch (CloneNotSupportedException e) {
153             l2 = l1;  // silences Eclipse warning
154             fail("PaintListTests.testCloning: failed to clone.");
155         }
156         assertTrue(l1 != l2);
157         assertTrue(l1.getClass() == l2.getClass());
158         assertTrue(l1.equals(l2));
159 
160         l2.setPaint(0, Color.green);
161         assertFalse(l1.equals(l2));
162 
163     }
164 
165     /**
166      * Serialize an instance, restore it, and check for equality.
167      */
testSerialization()168     public void testSerialization() {
169 
170         final PaintList l1 = new PaintList();
171         l1.setPaint(0, Color.red);
172         l1.setPaint(1, Color.blue);
173         l1.setPaint(2, null);
174 
175         PaintList l2 = null;
176 
177         try {
178             final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
179             final ObjectOutput out = new ObjectOutputStream(buffer);
180             out.writeObject(l1);
181             out.close();
182 
183             final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
184             l2 = (PaintList) in.readObject();
185             in.close();
186         }
187         catch (Exception e) {
188             System.out.println(e.toString());
189         }
190         assertEquals(l1, l2);
191 
192     }
193 
194 }
195