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  * SerialUtilitiesTest.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: SerialUtilitiesTest.java,v 1.11 2011/10/11 12:45:00 matinh 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  * 10-Oct-2011 : Added tests for serializing Composite instances (MH);
43  *
44  */
45 
46 package org.jfree.io;
47 
48 import java.awt.AlphaComposite;
49 import java.awt.Color;
50 import java.awt.Composite;
51 import java.awt.GradientPaint;
52 import java.awt.Paint;
53 import java.awt.TexturePaint;
54 import java.awt.font.TextAttribute;
55 import java.awt.geom.Arc2D;
56 import java.awt.geom.GeneralPath;
57 import java.awt.geom.Line2D;
58 import java.awt.geom.Rectangle2D;
59 import java.awt.image.BufferedImage;
60 import java.io.ByteArrayInputStream;
61 import java.io.ByteArrayOutputStream;
62 import java.io.ObjectInputStream;
63 import java.io.ObjectOutputStream;
64 import java.text.AttributedString;
65 
66 import javax.swing.UIManager;
67 import javax.swing.plaf.ColorUIResource;
68 
69 import junit.framework.Test;
70 import junit.framework.TestCase;
71 import junit.framework.TestSuite;
72 
73 import org.jfree.util.AttributedStringUtilities;
74 import org.jfree.util.ShapeUtilities;
75 
76 /**
77  * Tests for the {@link SerialUtilities} class.
78  */
79 public class SerialUtilitiesTest extends TestCase {
80 
81     /**
82      * Returns the tests as a test suite.
83      *
84      * @return The test suite.
85      */
suite()86     public static Test suite() {
87         return new TestSuite(SerialUtilitiesTest.class);
88     }
89 
90     /**
91      * Constructs a new set of tests.
92      *
93      * @param name  the name of the tests.
94      */
SerialUtilitiesTest(final String name)95     public SerialUtilitiesTest(final String name) {
96         super(name);
97     }
98 
99     /**
100      * Tests the isSerializable(Class) method for some common cases.
101      */
testIsSerializable()102     public void testIsSerializable() {
103         assertTrue(SerialUtilities.isSerializable(Color.class));
104         assertTrue(SerialUtilities.isSerializable(ColorUIResource.class));
105         assertFalse(SerialUtilities.isSerializable(GradientPaint.class));
106         assertFalse(SerialUtilities.isSerializable(TexturePaint.class));
107     }
108 
109     /**
110      * Serialize a <code>Color</code> and check that it can be deserialized
111      * correctly.
112      */
testColorSerialization()113     public void testColorSerialization() {
114 
115         final Paint p1 = Color.blue;
116         Paint p2 = null;
117 
118         try {
119             final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
120             final ObjectOutputStream out = new ObjectOutputStream(buffer);
121             SerialUtilities.writePaint(p1, out);
122             out.close();
123 
124             final ByteArrayInputStream bias = new ByteArrayInputStream(
125                     buffer.toByteArray());
126             final ObjectInputStream in = new ObjectInputStream(bias);
127             p2 = SerialUtilities.readPaint(in);
128             in.close();
129         }
130         catch (Exception e) {
131             e.printStackTrace();
132         }
133         assertEquals(p1, p2);
134 
135     }
136 
137     /**
138      * Serialize a <code>ColorUIResource</code> and check that it can be
139      * deserialized correctly.
140      */
testColorUIResourceSerialization()141     public void testColorUIResourceSerialization() {
142         Paint p1 = UIManager.getColor("Panel.background");
143         Paint p2 = null;
144         try {
145             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
146             ObjectOutputStream out = new ObjectOutputStream(buffer);
147             SerialUtilities.writePaint(p1, out);
148             out.close();
149 
150             ByteArrayInputStream bias = new ByteArrayInputStream(
151                     buffer.toByteArray());
152             ObjectInputStream in = new ObjectInputStream(bias);
153             p2 = SerialUtilities.readPaint(in);
154             in.close();
155         }
156         catch (Exception 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      */
testGradientPaintSerialization()166     public void testGradientPaintSerialization() {
167 
168         final Paint p1 = new GradientPaint(0.0f, 0.0f, Color.blue,
169                 100.0f, 200.0f, Color.red);
170         Paint p2 = null;
171 
172         try {
173             final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
174             final ObjectOutputStream out = new ObjectOutputStream(buffer);
175             SerialUtilities.writePaint(p1, out);
176             out.close();
177 
178             final ByteArrayInputStream bias = new ByteArrayInputStream(
179                     buffer.toByteArray());
180             final ObjectInputStream in = new ObjectInputStream(bias);
181             p2 = SerialUtilities.readPaint(in);
182             in.close();
183         }
184         catch (Exception e) {
185             e.printStackTrace();
186         }
187 
188         // we want to check that the two objects are equal, but can't rely on
189         // GradientPaint's equals() method because it is just the default
190         // method inherited from Object...
191         final GradientPaint gp1 = (GradientPaint) p1;
192         final GradientPaint gp2 = (GradientPaint) p2;
193         assertEquals(gp1.getColor1(), gp2.getColor1());
194         assertEquals(gp1.getPoint1(), gp2.getPoint1());
195         assertEquals(gp1.getColor2(), gp2.getColor2());
196         assertEquals(gp1.getPoint2(), gp2.getPoint2());
197         assertEquals(gp1.isCyclic(), gp2.isCyclic());
198 
199     }
200 
201     /**
202      * Serialize an <code>AlphaComposite</code>, restore it, and check for
203      * equality.
204      */
testAlphaCompositeSerialization()205     public void testAlphaCompositeSerialization() {
206 
207         final Composite c1 = AlphaComposite.getInstance(2, 0.345f);
208         Composite c2 = null;
209 
210         try {
211             final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
212             final ObjectOutputStream out = new ObjectOutputStream(buffer);
213             SerialUtilities.writeComposite(c1, out);
214             out.close();
215 
216             final ByteArrayInputStream bias = new ByteArrayInputStream(
217                     buffer.toByteArray());
218             final ObjectInputStream in = new ObjectInputStream(bias);
219             c2 = SerialUtilities.readComposite(in);
220             in.close();
221         }
222         catch (Exception e) {
223             e.printStackTrace();
224         }
225 
226         // we want to check that the two objects are equal, but can't rely on
227         // AlphaComposite's equals() method because it is just the default
228         // method inherited from Object...
229         final AlphaComposite ac1 = (AlphaComposite) c1;
230         final AlphaComposite ac2 = (AlphaComposite) c2;
231         assertEquals(ac1.getRule(), ac2.getRule());
232         assertEquals(ac1.getAlpha(), ac2.getAlpha(), 0.001f);
233     }
234 
235     /**
236      * Serialize a <code>TexturePaint</code>, restore it, and check for
237      * equality.  Since this class is not serializable, we expect null as the
238      * result.
239      */
testTexturePaintSerialization()240     public void testTexturePaintSerialization() {
241 
242         final Paint p1 = new TexturePaint(
243                 new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB),
244                 new Rectangle2D.Double(0, 0, 5, 5));
245         Paint p2 = null;
246 
247         try {
248             final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
249             final ObjectOutputStream out = new ObjectOutputStream(buffer);
250             SerialUtilities.writePaint(p1, out);
251             out.close();
252 
253             final ByteArrayInputStream bias = new ByteArrayInputStream(
254                     buffer.toByteArray());
255             final ObjectInputStream in = new ObjectInputStream(bias);
256             p2 = SerialUtilities.readPaint(in);
257             in.close();
258         }
259         catch (Exception e) {
260             e.printStackTrace();
261         }
262 
263         assertNull(p2);
264 
265     }
266 
267     /**
268      * Serialize a <code>Line2D.Float</code> instance, and check that it can be
269      * deserialized correctly.
270      */
testLine2DFloatSerialization()271     public void testLine2DFloatSerialization() {
272         Line2D l1 = new Line2D.Float(1.0f, 2.0f, 3.0f, 4.0f);
273         Line2D l2 = null;
274         try {
275             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
276             ObjectOutputStream out = new ObjectOutputStream(buffer);
277             SerialUtilities.writeShape(l1, out);
278             out.close();
279 
280             ByteArrayInputStream bais = new ByteArrayInputStream(
281                     buffer.toByteArray());
282             ObjectInputStream in = new ObjectInputStream(bais);
283             l2 = (Line2D) SerialUtilities.readShape(in);
284             in.close();
285         }
286         catch (Exception e) {
287             e.printStackTrace();
288         }
289         assertTrue(ShapeUtilities.equal(l1, l2));
290     }
291 
292     /**
293      * Serialize a <code>Line2D.Double</code> instance and check that it can be
294      * deserialized correctly.
295      */
testLine2DDoubleSerialization()296     public void testLine2DDoubleSerialization() {
297         Line2D l1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
298         Line2D l2 = null;
299         try {
300             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
301             ObjectOutputStream out = new ObjectOutputStream(buffer);
302             SerialUtilities.writeShape(l1, out);
303             out.close();
304 
305             ByteArrayInputStream bais = new ByteArrayInputStream(
306                     buffer.toByteArray());
307             ObjectInputStream in = new ObjectInputStream(bais);
308             l2 = (Line2D) SerialUtilities.readShape(in);
309             in.close();
310         }
311         catch (Exception e) {
312             e.printStackTrace();
313         }
314         assertTrue(ShapeUtilities.equal(l1, l2));
315     }
316 
317     /**
318      * Serialize a <code>Rectangle2D.Float</code> instance, and check that it
319      * can be deserialized correctly.
320      */
testRectangle2DFloatSerialization()321     public void testRectangle2DFloatSerialization() {
322         Rectangle2D r1 = new Rectangle2D.Float(1.0f, 2.0f, 3.0f, 4.0f);
323         Rectangle2D r2 = null;
324         try {
325             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
326             ObjectOutputStream out = new ObjectOutputStream(buffer);
327             SerialUtilities.writeShape(r1, out);
328             out.close();
329 
330             ByteArrayInputStream bais = new ByteArrayInputStream(
331                     buffer.toByteArray());
332             ObjectInputStream in = new ObjectInputStream(bais);
333             r2 = (Rectangle2D) SerialUtilities.readShape(in);
334             in.close();
335         }
336         catch (Exception e) {
337             e.printStackTrace();
338         }
339         assertTrue(ShapeUtilities.equal(r1, r2));
340     }
341 
342     /**
343      * Serialize a <code>Rectangle2D.Double</code> instance and check that it
344      * can be deserialized correctly.
345      */
testRectangle2DDoubleSerialization()346     public void testRectangle2DDoubleSerialization() {
347         Rectangle2D r1 = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
348         Rectangle2D r2 = null;
349         try {
350             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
351             ObjectOutputStream out = new ObjectOutputStream(buffer);
352             SerialUtilities.writeShape(r1, out);
353             out.close();
354 
355             ByteArrayInputStream bais = new ByteArrayInputStream(
356                     buffer.toByteArray());
357             ObjectInputStream in = new ObjectInputStream(bais);
358             r2 = (Rectangle2D) SerialUtilities.readShape(in);
359             in.close();
360         }
361         catch (Exception e) {
362             e.printStackTrace();
363         }
364         assertTrue(ShapeUtilities.equal(r1, r2));
365     }
366 
367     /**
368      * Serialize an <code>Arc2D.Float</code> instance and check that it
369      * can be deserialized correctly.
370      */
testArc2DFloatSerialization()371     public void testArc2DFloatSerialization() {
372         Arc2D a1 = new Arc2D.Float(
373             1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, Arc2D.PIE
374         );
375         Arc2D a2 = null;
376         try {
377             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
378             ObjectOutputStream out = new ObjectOutputStream(buffer);
379             SerialUtilities.writeShape(a1, out);
380             out.close();
381 
382             ByteArrayInputStream bais = new ByteArrayInputStream(
383                     buffer.toByteArray());
384             ObjectInputStream in = new ObjectInputStream(bais);
385             a2 = (Arc2D) SerialUtilities.readShape(in);
386             in.close();
387         }
388         catch (Exception e) {
389             e.printStackTrace();
390         }
391         assertTrue(ShapeUtilities.equal(a1, a2));
392     }
393 
394     /**
395      * Serialize an <code>Arc2D.Double</code> instance and check that it
396      * can be deserialized correctly.
397      */
testArc2DDoubleSerialization()398     public void testArc2DDoubleSerialization() {
399         Arc2D a1 = new Arc2D.Double(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, Arc2D.PIE);
400         Arc2D a2 = null;
401         try {
402             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
403             ObjectOutputStream out = new ObjectOutputStream(buffer);
404             SerialUtilities.writeShape(a1, out);
405             out.close();
406 
407             ByteArrayInputStream bais = new ByteArrayInputStream(
408                     buffer.toByteArray());
409             ObjectInputStream in = new ObjectInputStream(bais);
410             a2 = (Arc2D) SerialUtilities.readShape(in);
411             in.close();
412         }
413         catch (Exception e) {
414             e.printStackTrace();
415         }
416         assertTrue(ShapeUtilities.equal(a1, a2));
417     }
418 
419     /**
420      * Some checks for the serialization of a GeneralPath instance.
421      */
testGeneralPathSerialization()422     public void testGeneralPathSerialization() {
423         GeneralPath g1 = new GeneralPath();
424         g1.moveTo(1.0f, 2.0f);
425         g1.lineTo(3.0f, 4.0f);
426         g1.curveTo(5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
427         g1.quadTo(1.0f, 2.0f, 3.0f, 4.0f);
428         g1.closePath();
429         GeneralPath g2 = null;
430         try {
431             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
432             ObjectOutputStream out = new ObjectOutputStream(buffer);
433             SerialUtilities.writeShape(g1, out);
434             out.close();
435 
436             ByteArrayInputStream bais = new ByteArrayInputStream(
437                     buffer.toByteArray());
438             ObjectInputStream in = new ObjectInputStream(bais);
439             g2 = (GeneralPath) SerialUtilities.readShape(in);
440             in.close();
441         }
442         catch (Exception e) {
443             e.printStackTrace();
444         }
445         assertTrue(ShapeUtilities.equal(g1, g2));
446 
447     }
448 
449     /**
450      * Tests the serialization of an {@link AttributedString}.
451      */
testAttributedStringSerialization1()452     public void testAttributedStringSerialization1() {
453         AttributedString s1 = new AttributedString("");
454         AttributedString s2 = null;
455         try {
456             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
457             ObjectOutputStream out = new ObjectOutputStream(buffer);
458             SerialUtilities.writeAttributedString(s1, out);
459             out.close();
460 
461             ByteArrayInputStream bais = new ByteArrayInputStream(
462                 buffer.toByteArray()
463             );
464             ObjectInputStream in = new ObjectInputStream(bais);
465             s2 = SerialUtilities.readAttributedString(in);
466             in.close();
467         }
468         catch (Exception e) {
469             e.printStackTrace();
470         }
471         assertTrue(AttributedStringUtilities.equal(s1, s2));
472     }
473 
474     /**
475      * Tests the serialization of an {@link AttributedString}.
476      */
testAttributedStringSerialization2()477     public void testAttributedStringSerialization2() {
478         AttributedString s1 = new AttributedString("ABC");
479         AttributedString s2 = null;
480         try {
481             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
482             ObjectOutputStream out = new ObjectOutputStream(buffer);
483             SerialUtilities.writeAttributedString(s1, out);
484             out.close();
485 
486             ByteArrayInputStream bais = new ByteArrayInputStream(
487                     buffer.toByteArray());
488             ObjectInputStream in = new ObjectInputStream(bais);
489             s2 = SerialUtilities.readAttributedString(in);
490             in.close();
491         }
492         catch (Exception e) {
493             e.printStackTrace();
494         }
495         assertTrue(AttributedStringUtilities.equal(s1, s2));
496     }
497 
498     /**
499      * Tests the serialization of an {@link AttributedString}.
500      */
testAttributedStringSerialization3()501     public void testAttributedStringSerialization3() {
502         AttributedString s1 = new AttributedString("ABC");
503         s1.addAttribute(TextAttribute.LANGUAGE, "English");
504         AttributedString s2 = null;
505         try {
506             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
507             ObjectOutputStream out = new ObjectOutputStream(buffer);
508             SerialUtilities.writeAttributedString(s1, out);
509             out.close();
510 
511             ByteArrayInputStream bais = new ByteArrayInputStream(
512                     buffer.toByteArray());
513             ObjectInputStream in = new ObjectInputStream(bais);
514             s2 = SerialUtilities.readAttributedString(in);
515             in.close();
516         }
517         catch (Exception e) {
518             e.printStackTrace();
519         }
520         assertTrue(AttributedStringUtilities.equal(s1, s2));
521     }
522 
523 }
524