KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > junit > DefaultDrawingSupplierTests


1 /* ===========================================================
2  * JFreeChart : a free chart 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/jfreechart/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  * DefaultDrawingSupplierTests.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: DefaultDrawingSupplierTests.java,v 1.1.2.1 2006/10/03 15:41:28 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 25-Mar-2003 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.plot.junit;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.awt.Color JavaDoc;
47 import java.awt.Paint JavaDoc;
48 import java.awt.Shape JavaDoc;
49 import java.awt.Stroke JavaDoc;
50 import java.awt.geom.Rectangle2D JavaDoc;
51 import java.io.ByteArrayInputStream JavaDoc;
52 import java.io.ByteArrayOutputStream JavaDoc;
53 import java.io.ObjectInput JavaDoc;
54 import java.io.ObjectInputStream JavaDoc;
55 import java.io.ObjectOutput JavaDoc;
56 import java.io.ObjectOutputStream JavaDoc;
57
58 import junit.framework.Test;
59 import junit.framework.TestCase;
60 import junit.framework.TestSuite;
61
62 import org.jfree.chart.plot.DefaultDrawingSupplier;
63
64 /**
65  * Tests for the {@link DefaultDrawingSupplier} class.
66  */

67 public class DefaultDrawingSupplierTests extends TestCase {
68
69     /**
70      * Returns the tests as a test suite.
71      *
72      * @return The test suite.
73      */

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

83     public DefaultDrawingSupplierTests(String JavaDoc name) {
84         super(name);
85     }
86     
87     /**
88      * Check that the equals() method can distinguish all required fields.
89      */

90     public void testEquals() {
91         DefaultDrawingSupplier r1 = new DefaultDrawingSupplier();
92         DefaultDrawingSupplier r2 = new DefaultDrawingSupplier();
93         assertTrue(r1.equals(r2));
94         assertTrue(r2.equals(r1));
95         
96         // set up some objects...
97
Paint JavaDoc[] ps1A = new Paint JavaDoc[] {Color.red, Color.blue};
98         Paint JavaDoc[] ps2A = new Paint JavaDoc[] {Color.green, Color.yellow, Color.white};
99         Paint JavaDoc[] ops1A = new Paint JavaDoc[] {Color.lightGray, Color.blue};
100         Paint JavaDoc[] ops2A = new Paint JavaDoc[] {Color.black, Color.yellow, Color.cyan};
101         Stroke JavaDoc[] ss1A = new Stroke JavaDoc[] {new BasicStroke JavaDoc(1.1f)};
102         Stroke JavaDoc[] ss2A
103             = new Stroke JavaDoc[] {new BasicStroke JavaDoc(2.2f), new BasicStroke JavaDoc(3.3f)};
104         Stroke JavaDoc[] oss1A = new Stroke JavaDoc[] {new BasicStroke JavaDoc(4.4f)};
105         Stroke JavaDoc[] oss2A
106             = new Stroke JavaDoc[] {new BasicStroke JavaDoc(5.5f), new BasicStroke JavaDoc(6.6f)};
107         Shape JavaDoc[] shapes1A = new Shape JavaDoc[] {
108             new Rectangle2D.Double JavaDoc(1.0, 1.0, 1.0, 1.0)
109         };
110         Shape JavaDoc[] shapes2A = new Shape JavaDoc[] {
111             new Rectangle2D.Double JavaDoc(2.0, 2.0, 2.0, 2.0),
112             new Rectangle2D.Double JavaDoc(2.0, 2.0, 2.0, 2.0)
113         };
114         Paint JavaDoc[] ps1B = new Paint JavaDoc[] {Color.red, Color.blue};
115         Paint JavaDoc[] ps2B = new Paint JavaDoc[] {Color.green, Color.yellow, Color.white};
116         Paint JavaDoc[] ops1B = new Paint JavaDoc[] {Color.lightGray, Color.blue};
117         Paint JavaDoc[] ops2B = new Paint JavaDoc[] {Color.black, Color.yellow, Color.cyan};
118         Stroke JavaDoc[] ss1B = new Stroke JavaDoc[] {new BasicStroke JavaDoc(1.1f)};
119         Stroke JavaDoc[] ss2B
120             = new Stroke JavaDoc[] {new BasicStroke JavaDoc(2.2f), new BasicStroke JavaDoc(3.3f)};
121         Stroke JavaDoc[] oss1B = new Stroke JavaDoc[] {new BasicStroke JavaDoc(4.4f)};
122         Stroke JavaDoc[] oss2B
123             = new Stroke JavaDoc[] {new BasicStroke JavaDoc(5.5f), new BasicStroke JavaDoc(6.6f)};
124         Shape JavaDoc[] shapes1B = new Shape JavaDoc[] {
125             new Rectangle2D.Double JavaDoc(1.0, 1.0, 1.0, 1.0)
126         };
127         Shape JavaDoc[] shapes2B = new Shape JavaDoc[] {
128             new Rectangle2D.Double JavaDoc(2.0, 2.0, 2.0, 2.0),
129             new Rectangle2D.Double JavaDoc(2.0, 2.0, 2.0, 2.0)
130         };
131         
132         r1 = new DefaultDrawingSupplier(ps1A, ops1A, ss1A, oss1A, shapes1A);
133         r2 = new DefaultDrawingSupplier(ps1B, ops1B, ss1B, oss1B, shapes1B);
134         assertTrue(r1.equals(r2));
135         
136         // paint sequence
137
r1 = new DefaultDrawingSupplier(ps2A, ops1A, ss1A, oss1A, shapes1A);
138         assertFalse(r1.equals(r2));
139         r2 = new DefaultDrawingSupplier(ps2B, ops1B, ss1B, oss1B, shapes1B);
140         assertTrue(r1.equals(r2));
141         // outline paint sequence
142
r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss1A, oss1A, shapes1A);
143         assertFalse(r1.equals(r2));
144         r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss1B, oss1B, shapes1B);
145         assertTrue(r1.equals(r2));
146         // stroke sequence
147
r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss2A, oss1A, shapes1A);
148         assertFalse(r1.equals(r2));
149         r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss2B, oss1B, shapes1B);
150         assertTrue(r1.equals(r2));
151         // outline stroke sequence
152
r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss2A, oss2A, shapes1A);
153         assertFalse(r1.equals(r2));
154         r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss2B, oss2B, shapes1B);
155         assertTrue(r1.equals(r2));
156         // shape sequence
157
r1 = new DefaultDrawingSupplier(ps2A, ops2A, ss2A, oss2A, shapes2A);
158         assertFalse(r1.equals(r2));
159         r2 = new DefaultDrawingSupplier(ps2B, ops2B, ss2B, oss2B, shapes2B);
160         assertTrue(r1.equals(r2));
161         
162         // paint index
163
r1.getNextPaint();
164         assertFalse(r1.equals(r2));
165         r2.getNextPaint();
166         assertTrue(r1.equals(r2));
167         
168         // outline paint index
169
r1.getNextOutlinePaint();
170         assertFalse(r1.equals(r2));
171         r2.getNextOutlinePaint();
172         assertTrue(r1.equals(r2));
173         
174         // stroke index
175
r1.getNextStroke();
176         assertFalse(r1.equals(r2));
177         r2.getNextStroke();
178         assertTrue(r1.equals(r2));
179         
180         // outline stroke index
181
r1.getNextOutlineStroke();
182         assertFalse(r1.equals(r2));
183         r2.getNextOutlineStroke();
184         assertTrue(r1.equals(r2));
185         
186         // shape index
187
r1.getNextShape();
188         assertFalse(r1.equals(r2));
189         r2.getNextShape();
190         assertTrue(r1.equals(r2));
191     }
192
193     /**
194      * Some basic checks for the clone() method.
195      */

196     public void testCloning() {
197         DefaultDrawingSupplier r1 = new DefaultDrawingSupplier();
198         DefaultDrawingSupplier r2 = null;
199         try {
200             r2 = (DefaultDrawingSupplier) r1.clone();
201         }
202         catch (CloneNotSupportedException JavaDoc e) {
203             e.printStackTrace();
204             System.err.println("Failed to clone.");
205         }
206         assertTrue(r1 != r2);
207         assertTrue(r1.getClass() == r2.getClass());
208         assertTrue(r1.equals(r2));
209     }
210
211     /**
212      * Serialize an instance, restore it, and check for equality.
213      */

214     public void testSerialization() {
215
216         DefaultDrawingSupplier r1 = new DefaultDrawingSupplier();
217         DefaultDrawingSupplier r2 = null;
218
219         try {
220             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
221             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
222             out.writeObject(r1);
223             out.close();
224
225             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
226                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
227             );
228             r2 = (DefaultDrawingSupplier) in.readObject();
229             in.close();
230         }
231         catch (Exception JavaDoc e) {
232             System.out.println(e.toString());
233         }
234         assertEquals(r1, r2);
235
236     }
237
238 }
239
Popular Tags