KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > io > junit > SerialUtilitiesTests


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  * SerialUtilitiesTests.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: SerialUtilitiesTests.java,v 1.7 2005/10/18 13:16:15 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 18-Sep-2003 : Version 1 (DG);
40  * 26-Oct-2004 : Added checks for serializing Line2D instances (DG);
41  * 04-Feb-2005 : Added tests for serializing Rectangle2D instances (DG);
42  *
43  */

44
45 package org.jfree.io.junit;
46
47 import java.awt.Color JavaDoc;
48 import java.awt.GradientPaint JavaDoc;
49 import java.awt.Paint JavaDoc;
50 import java.awt.TexturePaint JavaDoc;
51 import java.awt.font.TextAttribute JavaDoc;
52 import java.awt.geom.Arc2D JavaDoc;
53 import java.awt.geom.GeneralPath JavaDoc;
54 import java.awt.geom.Line2D JavaDoc;
55 import java.awt.geom.Rectangle2D JavaDoc;
56 import java.awt.image.BufferedImage JavaDoc;
57 import java.io.ByteArrayInputStream JavaDoc;
58 import java.io.ByteArrayOutputStream JavaDoc;
59 import java.io.ObjectInputStream JavaDoc;
60 import java.io.ObjectOutputStream JavaDoc;
61 import java.text.AttributedString JavaDoc;
62
63 import javax.swing.UIManager JavaDoc;
64 import javax.swing.plaf.ColorUIResource JavaDoc;
65
66 import junit.framework.Test;
67 import junit.framework.TestCase;
68 import junit.framework.TestSuite;
69
70 import org.jfree.io.SerialUtilities;
71 import org.jfree.util.AttributedStringUtilities;
72 import org.jfree.util.ShapeUtilities;
73
74 /**
75  * Tests for the {@link SerialUtilities} class.
76  */

77 public class SerialUtilitiesTests extends TestCase {
78
79     /**
80      * Returns the tests as a test suite.
81      *
82      * @return The test suite.
83      */

84     public static Test suite() {
85         return new TestSuite(SerialUtilitiesTests.class);
86     }
87
88     /**
89      * Constructs a new set of tests.
90      *
91      * @param name the name of the tests.
92      */

93     public SerialUtilitiesTests(final String JavaDoc name) {
94         super(name);
95     }
96
97     /**
98      * Tests the isSerializable(Class) method for some common cases.
99      */

100     public void testIsSerializable() {
101         assertTrue(SerialUtilities.isSerializable(Color JavaDoc.class));
102         assertTrue(SerialUtilities.isSerializable(ColorUIResource JavaDoc.class));
103         assertFalse(SerialUtilities.isSerializable(GradientPaint JavaDoc.class));
104         assertFalse(SerialUtilities.isSerializable(TexturePaint JavaDoc.class));
105     }
106     
107     /**
108      * Serialize a <code>Color</code> and check that it can be deserialized
109      * correctly.
110      */

111     public void testColorSerialization() {
112
113         final Paint JavaDoc p1 = Color.blue;
114         Paint JavaDoc p2 = null;
115
116         try {
117             final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
118             final ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
119             SerialUtilities.writePaint(p1, out);
120             out.close();
121
122             final ByteArrayInputStream JavaDoc bias = new ByteArrayInputStream JavaDoc(
123                 buffer.toByteArray()
124             );
125             final ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bias);
126             p2 = SerialUtilities.readPaint(in);
127             in.close();
128         }
129         catch (Exception JavaDoc e) {
130             System.out.println(e.toString());
131         }
132         assertEquals(p1, p2);
133
134     }
135
136     /**
137      * Serialize a <code>ColorUIResource</code> and check that it can be
138      * deserialized correctly.
139      */

140     public void testColorUIResourceSerialization() {
141         Paint JavaDoc p1 = UIManager.getColor("Panel.background");
142         Paint JavaDoc p2 = null;
143         try {
144             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
145             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
146             SerialUtilities.writePaint(p1, out);
147             out.close();
148
149             ByteArrayInputStream JavaDoc bias = new ByteArrayInputStream JavaDoc(
150                 buffer.toByteArray()
151             );
152             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bias);
153             p2 = SerialUtilities.readPaint(in);
154             in.close();
155         }
156         catch (Exception JavaDoc e) {
157             fail(e.toString());
158         }
159         assertEquals(p1, p2);
160     }
161
162     /**
163      * Serialize a <code>GradientPaint</code>, restore it, and check for
164      * equality.
165      */

166     public void testGradientPaintSerialization() {
167
168         final Paint JavaDoc p1 = new GradientPaint JavaDoc(
169             0.0f, 0.0f, Color.blue, 100.0f, 200.0f, Color.red
170         );
171         Paint JavaDoc p2 = null;
172
173         try {
174             final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
175             final ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
176             SerialUtilities.writePaint(p1, out);
177             out.close();
178
179             final ByteArrayInputStream JavaDoc bias = new ByteArrayInputStream JavaDoc(
180                 buffer.toByteArray()
181             );
182             final ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bias);
183             p2 = SerialUtilities.readPaint(in);
184             in.close();
185         }
186         catch (Exception JavaDoc e) {
187             System.out.println(e.toString());
188         }
189         
190         // we want to check that the two objects are equal, but can't rely on
191
// GradientPaint's equals() method because it is just the default
192
// method inherited from Object...
193
final GradientPaint JavaDoc gp1 = (GradientPaint JavaDoc) p1;
194         final GradientPaint JavaDoc gp2 = (GradientPaint JavaDoc) p2;
195         assertEquals(gp1.getColor1(), gp2.getColor1());
196         assertEquals(gp1.getPoint1(), gp2.getPoint1());
197         assertEquals(gp1.getColor2(), gp2.getColor2());
198         assertEquals(gp1.getPoint2(), gp2.getPoint2());
199         assertEquals(gp1.isCyclic(), gp2.isCyclic());
200
201     }
202
203     /**
204      * Serialize a <code>TexturePaint</code>, restore it, and check for
205      * equality. Since this class is not serializable, we expect null as the
206      * result.
207      */

208     public void testTexturePaintSerialization() {
209
210         final Paint JavaDoc p1 = new TexturePaint JavaDoc(
211             new BufferedImage JavaDoc(5, 5, BufferedImage.TYPE_INT_RGB),
212             new Rectangle2D.Double JavaDoc(0, 0, 5, 5)
213         );
214         Paint JavaDoc p2 = null;
215
216         try {
217             final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
218             final ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
219             SerialUtilities.writePaint(p1, out);
220             out.close();
221
222             final ByteArrayInputStream JavaDoc bias = new ByteArrayInputStream JavaDoc(
223                 buffer.toByteArray()
224             );
225             final ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bias);
226             p2 = SerialUtilities.readPaint(in);
227             in.close();
228         }
229         catch (Exception JavaDoc e) {
230             System.out.println(e.toString());
231         }
232         
233         assertNull(p2);
234
235     }
236
237     /**
238      * Serialize a <code>Line2D.Float</code> instance, and check that it can be
239      * deserialized correctly.
240      */

241     public void testLine2DFloatSerialization() {
242         Line2D JavaDoc l1 = new Line2D.Float JavaDoc(1.0f, 2.0f, 3.0f, 4.0f);
243         Line2D JavaDoc l2 = null;
244         try {
245             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
246             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
247             SerialUtilities.writeShape(l1, out);
248             out.close();
249
250             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
251                 buffer.toByteArray()
252             );
253             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
254             l2 = (Line2D JavaDoc) SerialUtilities.readShape(in);
255             in.close();
256         }
257         catch (Exception JavaDoc e) {
258             System.out.println(e.toString());
259         }
260         assertTrue(ShapeUtilities.equal(l1, l2));
261     }
262
263     /**
264      * Serialize a <code>Line2D.Double</code> instance and check that it can be
265      * deserialized correctly.
266      */

267     public void testLine2DDoubleSerialization() {
268         Line2D JavaDoc l1 = new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
269         Line2D JavaDoc l2 = null;
270         try {
271             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
272             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
273             SerialUtilities.writeShape(l1, out);
274             out.close();
275
276             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
277                 buffer.toByteArray()
278             );
279             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
280             l2 = (Line2D JavaDoc) SerialUtilities.readShape(in);
281             in.close();
282         }
283         catch (Exception JavaDoc e) {
284             System.out.println(e.toString());
285         }
286         assertTrue(ShapeUtilities.equal(l1, l2));
287     }
288
289     /**
290      * Serialize a <code>Rectangle2D.Float</code> instance, and check that it
291      * can be deserialized correctly.
292      */

293     public void testRectangle2DFloatSerialization() {
294         Rectangle2D JavaDoc r1 = new Rectangle2D.Float JavaDoc(1.0f, 2.0f, 3.0f, 4.0f);
295         Rectangle2D JavaDoc r2 = null;
296         try {
297             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
298             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
299             SerialUtilities.writeShape(r1, out);
300             out.close();
301
302             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
303                 buffer.toByteArray()
304             );
305             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
306             r2 = (Rectangle2D JavaDoc) SerialUtilities.readShape(in);
307             in.close();
308         }
309         catch (Exception JavaDoc e) {
310             System.out.println(e.toString());
311         }
312         assertTrue(ShapeUtilities.equal(r1, r2));
313     }
314
315     /**
316      * Serialize a <code>Rectangle2D.Double</code> instance and check that it
317      * can be deserialized correctly.
318      */

319     public void testRectangle2DDoubleSerialization() {
320         Rectangle2D JavaDoc r1 = new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
321         Rectangle2D JavaDoc r2 = null;
322         try {
323             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
324             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
325             SerialUtilities.writeShape(r1, out);
326             out.close();
327
328             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
329                 buffer.toByteArray()
330             );
331             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
332             r2 = (Rectangle2D JavaDoc) SerialUtilities.readShape(in);
333             in.close();
334         }
335         catch (Exception JavaDoc e) {
336             System.out.println(e.toString());
337         }
338         assertTrue(ShapeUtilities.equal(r1, r2));
339     }
340     
341     /**
342      * Serialize an <code>Arc2D.Float</code> instance and check that it
343      * can be deserialized correctly.
344      */

345     public void testArc2DFloatSerialization() {
346         Arc2D JavaDoc a1 = new Arc2D.Float JavaDoc(
347             1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, Arc2D.PIE
348         );
349         Arc2D JavaDoc a2 = null;
350         try {
351             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
352             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
353             SerialUtilities.writeShape(a1, out);
354             out.close();
355
356             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
357                 buffer.toByteArray()
358             );
359             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
360             a2 = (Arc2D JavaDoc) SerialUtilities.readShape(in);
361             in.close();
362         }
363         catch (Exception JavaDoc e) {
364             System.out.println(e.toString());
365         }
366         assertTrue(ShapeUtilities.equal(a1, a2));
367     }
368
369     /**
370      * Serialize an <code>Arc2D.Double</code> instance and check that it
371      * can be deserialized correctly.
372      */

373     public void testArc2DDoubleSerialization() {
374         Arc2D JavaDoc a1 = new Arc2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, Arc2D.PIE);
375         Arc2D JavaDoc a2 = null;
376         try {
377             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
378             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
379             SerialUtilities.writeShape(a1, out);
380             out.close();
381
382             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
383                 buffer.toByteArray()
384             );
385             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
386             a2 = (Arc2D JavaDoc) SerialUtilities.readShape(in);
387             in.close();
388         }
389         catch (Exception JavaDoc e) {
390             System.out.println(e.toString());
391         }
392         assertTrue(ShapeUtilities.equal(a1, a2));
393     }
394     
395     /**
396      * Some checks for the serialization of a GeneralPath instance.
397      */

398     public void testGeneralPathSerialization() {
399         GeneralPath JavaDoc g1 = new GeneralPath JavaDoc();
400         g1.moveTo(1.0f, 2.0f);
401         g1.lineTo(3.0f, 4.0f);
402         g1.curveTo(5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
403         g1.quadTo(1.0f, 2.0f, 3.0f, 4.0f);
404         g1.closePath();
405         GeneralPath JavaDoc g2 = null;
406         try {
407             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
408             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
409             SerialUtilities.writeShape(g1, out);
410             out.close();
411
412             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
413                 buffer.toByteArray()
414             );
415             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
416             g2 = (GeneralPath JavaDoc) SerialUtilities.readShape(in);
417             in.close();
418         }
419         catch (Exception JavaDoc e) {
420             System.out.println(e.toString());
421         }
422         assertTrue(ShapeUtilities.equal(g1, g2));
423   
424     }
425     
426     /**
427      * Tests the serialization of an {@link AttributedString}.
428      */

429     public void testAttributedStringSerialization1() {
430         AttributedString JavaDoc s1 = new AttributedString JavaDoc("");
431         AttributedString JavaDoc s2 = null;
432         try {
433             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
434             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
435             SerialUtilities.writeAttributedString(s1, out);
436             out.close();
437
438             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
439                 buffer.toByteArray()
440             );
441             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
442             s2 = SerialUtilities.readAttributedString(in);
443             in.close();
444         }
445         catch (Exception JavaDoc e) {
446             e.printStackTrace();
447         }
448         assertTrue(AttributedStringUtilities.equal(s1, s2));
449     }
450
451     /**
452      * Tests the serialization of an {@link AttributedString}.
453      */

454     public void testAttributedStringSerialization2() {
455         AttributedString JavaDoc s1 = new AttributedString JavaDoc("ABC");
456         AttributedString JavaDoc s2 = null;
457         try {
458             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
459             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
460             SerialUtilities.writeAttributedString(s1, out);
461             out.close();
462
463             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
464                 buffer.toByteArray()
465             );
466             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
467             s2 = SerialUtilities.readAttributedString(in);
468             in.close();
469         }
470         catch (Exception JavaDoc e) {
471             e.printStackTrace();
472         }
473         assertTrue(AttributedStringUtilities.equal(s1, s2));
474     }
475
476     /**
477      * Tests the serialization of an {@link AttributedString}.
478      */

479     public void testAttributedStringSerialization3() {
480         AttributedString JavaDoc s1 = new AttributedString JavaDoc("ABC");
481         s1.addAttribute(TextAttribute.LANGUAGE, "English");
482         AttributedString JavaDoc s2 = null;
483         try {
484             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
485             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
486             SerialUtilities.writeAttributedString(s1, out);
487             out.close();
488
489             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
490                 buffer.toByteArray()
491             );
492             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bais);
493             s2 = SerialUtilities.readAttributedString(in);
494             in.close();
495         }
496         catch (Exception JavaDoc e) {
497             e.printStackTrace();
498         }
499         assertTrue(AttributedStringUtilities.equal(s1, s2));
500     }
501 }
502
Popular Tags