001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------------
028: * SerialUtilitiesTests.java
029: * -------------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: SerialUtilitiesTests.java,v 1.7 2005/10/18 13:16:15 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Sep-2003 : Version 1 (DG);
040: * 26-Oct-2004 : Added checks for serializing Line2D instances (DG);
041: * 04-Feb-2005 : Added tests for serializing Rectangle2D instances (DG);
042: *
043: */
044:
045: package org.jfree.io.junit;
046:
047: import java.awt.Color;
048: import java.awt.GradientPaint;
049: import java.awt.Paint;
050: import java.awt.TexturePaint;
051: import java.awt.font.TextAttribute;
052: import java.awt.geom.Arc2D;
053: import java.awt.geom.GeneralPath;
054: import java.awt.geom.Line2D;
055: import java.awt.geom.Rectangle2D;
056: import java.awt.image.BufferedImage;
057: import java.io.ByteArrayInputStream;
058: import java.io.ByteArrayOutputStream;
059: import java.io.ObjectInputStream;
060: import java.io.ObjectOutputStream;
061: import java.text.AttributedString;
062:
063: import javax.swing.UIManager;
064: import javax.swing.plaf.ColorUIResource;
065:
066: import junit.framework.Test;
067: import junit.framework.TestCase;
068: import junit.framework.TestSuite;
069:
070: import org.jfree.io.SerialUtilities;
071: import org.jfree.util.AttributedStringUtilities;
072: import org.jfree.util.ShapeUtilities;
073:
074: /**
075: * Tests for the {@link SerialUtilities} class.
076: */
077: public class SerialUtilitiesTests extends TestCase {
078:
079: /**
080: * Returns the tests as a test suite.
081: *
082: * @return The test suite.
083: */
084: public static Test suite() {
085: return new TestSuite(SerialUtilitiesTests.class);
086: }
087:
088: /**
089: * Constructs a new set of tests.
090: *
091: * @param name the name of the tests.
092: */
093: public SerialUtilitiesTests(final String name) {
094: super (name);
095: }
096:
097: /**
098: * Tests the isSerializable(Class) method for some common cases.
099: */
100: public void testIsSerializable() {
101: assertTrue(SerialUtilities.isSerializable(Color.class));
102: assertTrue(SerialUtilities
103: .isSerializable(ColorUIResource.class));
104: assertFalse(SerialUtilities.isSerializable(GradientPaint.class));
105: assertFalse(SerialUtilities.isSerializable(TexturePaint.class));
106: }
107:
108: /**
109: * Serialize a <code>Color</code> and check that it can be deserialized
110: * correctly.
111: */
112: public void testColorSerialization() {
113:
114: final Paint p1 = Color.blue;
115: Paint p2 = null;
116:
117: try {
118: final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
119: final ObjectOutputStream out = new ObjectOutputStream(
120: 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: } catch (Exception 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 p1 = UIManager.getColor("Panel.background");
142: Paint p2 = null;
143: try {
144: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
145: ObjectOutputStream out = new ObjectOutputStream(buffer);
146: SerialUtilities.writePaint(p1, out);
147: out.close();
148:
149: ByteArrayInputStream bias = new ByteArrayInputStream(buffer
150: .toByteArray());
151: ObjectInputStream in = new ObjectInputStream(bias);
152: p2 = SerialUtilities.readPaint(in);
153: in.close();
154: } catch (Exception e) {
155: fail(e.toString());
156: }
157: assertEquals(p1, p2);
158: }
159:
160: /**
161: * Serialize a <code>GradientPaint</code>, restore it, and check for
162: * equality.
163: */
164: public void testGradientPaintSerialization() {
165:
166: final Paint p1 = new GradientPaint(0.0f, 0.0f, Color.blue,
167: 100.0f, 200.0f, Color.red);
168: Paint p2 = null;
169:
170: try {
171: final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
172: final ObjectOutputStream out = new ObjectOutputStream(
173: buffer);
174: SerialUtilities.writePaint(p1, out);
175: out.close();
176:
177: final ByteArrayInputStream bias = new ByteArrayInputStream(
178: buffer.toByteArray());
179: final ObjectInputStream in = new ObjectInputStream(bias);
180: p2 = SerialUtilities.readPaint(in);
181: in.close();
182: } catch (Exception e) {
183: System.out.println(e.toString());
184: }
185:
186: // we want to check that the two objects are equal, but can't rely on
187: // GradientPaint's equals() method because it is just the default
188: // method inherited from Object...
189: final GradientPaint gp1 = (GradientPaint) p1;
190: final GradientPaint gp2 = (GradientPaint) p2;
191: assertEquals(gp1.getColor1(), gp2.getColor1());
192: assertEquals(gp1.getPoint1(), gp2.getPoint1());
193: assertEquals(gp1.getColor2(), gp2.getColor2());
194: assertEquals(gp1.getPoint2(), gp2.getPoint2());
195: assertEquals(gp1.isCyclic(), gp2.isCyclic());
196:
197: }
198:
199: /**
200: * Serialize a <code>TexturePaint</code>, restore it, and check for
201: * equality. Since this class is not serializable, we expect null as the
202: * result.
203: */
204: public void testTexturePaintSerialization() {
205:
206: final Paint p1 = new TexturePaint(new BufferedImage(5, 5,
207: BufferedImage.TYPE_INT_RGB), new Rectangle2D.Double(0,
208: 0, 5, 5));
209: Paint p2 = null;
210:
211: try {
212: final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
213: final ObjectOutputStream out = new ObjectOutputStream(
214: buffer);
215: SerialUtilities.writePaint(p1, out);
216: out.close();
217:
218: final ByteArrayInputStream bias = new ByteArrayInputStream(
219: buffer.toByteArray());
220: final ObjectInputStream in = new ObjectInputStream(bias);
221: p2 = SerialUtilities.readPaint(in);
222: in.close();
223: } catch (Exception e) {
224: System.out.println(e.toString());
225: }
226:
227: assertNull(p2);
228:
229: }
230:
231: /**
232: * Serialize a <code>Line2D.Float</code> instance, and check that it can be
233: * deserialized correctly.
234: */
235: public void testLine2DFloatSerialization() {
236: Line2D l1 = new Line2D.Float(1.0f, 2.0f, 3.0f, 4.0f);
237: Line2D l2 = null;
238: try {
239: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
240: ObjectOutputStream out = new ObjectOutputStream(buffer);
241: SerialUtilities.writeShape(l1, out);
242: out.close();
243:
244: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
245: .toByteArray());
246: ObjectInputStream in = new ObjectInputStream(bais);
247: l2 = (Line2D) SerialUtilities.readShape(in);
248: in.close();
249: } catch (Exception e) {
250: System.out.println(e.toString());
251: }
252: assertTrue(ShapeUtilities.equal(l1, l2));
253: }
254:
255: /**
256: * Serialize a <code>Line2D.Double</code> instance and check that it can be
257: * deserialized correctly.
258: */
259: public void testLine2DDoubleSerialization() {
260: Line2D l1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
261: Line2D l2 = null;
262: try {
263: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
264: ObjectOutputStream out = new ObjectOutputStream(buffer);
265: SerialUtilities.writeShape(l1, out);
266: out.close();
267:
268: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
269: .toByteArray());
270: ObjectInputStream in = new ObjectInputStream(bais);
271: l2 = (Line2D) SerialUtilities.readShape(in);
272: in.close();
273: } catch (Exception e) {
274: System.out.println(e.toString());
275: }
276: assertTrue(ShapeUtilities.equal(l1, l2));
277: }
278:
279: /**
280: * Serialize a <code>Rectangle2D.Float</code> instance, and check that it
281: * can be deserialized correctly.
282: */
283: public void testRectangle2DFloatSerialization() {
284: Rectangle2D r1 = new Rectangle2D.Float(1.0f, 2.0f, 3.0f, 4.0f);
285: Rectangle2D r2 = null;
286: try {
287: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
288: ObjectOutputStream out = new ObjectOutputStream(buffer);
289: SerialUtilities.writeShape(r1, out);
290: out.close();
291:
292: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
293: .toByteArray());
294: ObjectInputStream in = new ObjectInputStream(bais);
295: r2 = (Rectangle2D) SerialUtilities.readShape(in);
296: in.close();
297: } catch (Exception e) {
298: System.out.println(e.toString());
299: }
300: assertTrue(ShapeUtilities.equal(r1, r2));
301: }
302:
303: /**
304: * Serialize a <code>Rectangle2D.Double</code> instance and check that it
305: * can be deserialized correctly.
306: */
307: public void testRectangle2DDoubleSerialization() {
308: Rectangle2D r1 = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
309: Rectangle2D r2 = null;
310: try {
311: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
312: ObjectOutputStream out = new ObjectOutputStream(buffer);
313: SerialUtilities.writeShape(r1, out);
314: out.close();
315:
316: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
317: .toByteArray());
318: ObjectInputStream in = new ObjectInputStream(bais);
319: r2 = (Rectangle2D) SerialUtilities.readShape(in);
320: in.close();
321: } catch (Exception e) {
322: System.out.println(e.toString());
323: }
324: assertTrue(ShapeUtilities.equal(r1, r2));
325: }
326:
327: /**
328: * Serialize an <code>Arc2D.Float</code> instance and check that it
329: * can be deserialized correctly.
330: */
331: public void testArc2DFloatSerialization() {
332: Arc2D a1 = new Arc2D.Float(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
333: Arc2D.PIE);
334: Arc2D a2 = null;
335: try {
336: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
337: ObjectOutputStream out = new ObjectOutputStream(buffer);
338: SerialUtilities.writeShape(a1, out);
339: out.close();
340:
341: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
342: .toByteArray());
343: ObjectInputStream in = new ObjectInputStream(bais);
344: a2 = (Arc2D) SerialUtilities.readShape(in);
345: in.close();
346: } catch (Exception e) {
347: System.out.println(e.toString());
348: }
349: assertTrue(ShapeUtilities.equal(a1, a2));
350: }
351:
352: /**
353: * Serialize an <code>Arc2D.Double</code> instance and check that it
354: * can be deserialized correctly.
355: */
356: public void testArc2DDoubleSerialization() {
357: Arc2D a1 = new Arc2D.Double(1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
358: Arc2D.PIE);
359: Arc2D a2 = null;
360: try {
361: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
362: ObjectOutputStream out = new ObjectOutputStream(buffer);
363: SerialUtilities.writeShape(a1, out);
364: out.close();
365:
366: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
367: .toByteArray());
368: ObjectInputStream in = new ObjectInputStream(bais);
369: a2 = (Arc2D) SerialUtilities.readShape(in);
370: in.close();
371: } catch (Exception e) {
372: System.out.println(e.toString());
373: }
374: assertTrue(ShapeUtilities.equal(a1, a2));
375: }
376:
377: /**
378: * Some checks for the serialization of a GeneralPath instance.
379: */
380: public void testGeneralPathSerialization() {
381: GeneralPath g1 = new GeneralPath();
382: g1.moveTo(1.0f, 2.0f);
383: g1.lineTo(3.0f, 4.0f);
384: g1.curveTo(5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
385: g1.quadTo(1.0f, 2.0f, 3.0f, 4.0f);
386: g1.closePath();
387: GeneralPath g2 = null;
388: try {
389: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
390: ObjectOutputStream out = new ObjectOutputStream(buffer);
391: SerialUtilities.writeShape(g1, out);
392: out.close();
393:
394: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
395: .toByteArray());
396: ObjectInputStream in = new ObjectInputStream(bais);
397: g2 = (GeneralPath) SerialUtilities.readShape(in);
398: in.close();
399: } catch (Exception e) {
400: System.out.println(e.toString());
401: }
402: assertTrue(ShapeUtilities.equal(g1, g2));
403:
404: }
405:
406: /**
407: * Tests the serialization of an {@link AttributedString}.
408: */
409: public void testAttributedStringSerialization1() {
410: AttributedString s1 = new AttributedString("");
411: AttributedString s2 = null;
412: try {
413: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
414: ObjectOutputStream out = new ObjectOutputStream(buffer);
415: SerialUtilities.writeAttributedString(s1, out);
416: out.close();
417:
418: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
419: .toByteArray());
420: ObjectInputStream in = new ObjectInputStream(bais);
421: s2 = SerialUtilities.readAttributedString(in);
422: in.close();
423: } catch (Exception e) {
424: e.printStackTrace();
425: }
426: assertTrue(AttributedStringUtilities.equal(s1, s2));
427: }
428:
429: /**
430: * Tests the serialization of an {@link AttributedString}.
431: */
432: public void testAttributedStringSerialization2() {
433: AttributedString s1 = new AttributedString("ABC");
434: AttributedString s2 = null;
435: try {
436: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
437: ObjectOutputStream out = new ObjectOutputStream(buffer);
438: SerialUtilities.writeAttributedString(s1, out);
439: out.close();
440:
441: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
442: .toByteArray());
443: ObjectInputStream in = new ObjectInputStream(bais);
444: s2 = SerialUtilities.readAttributedString(in);
445: in.close();
446: } catch (Exception e) {
447: e.printStackTrace();
448: }
449: assertTrue(AttributedStringUtilities.equal(s1, s2));
450: }
451:
452: /**
453: * Tests the serialization of an {@link AttributedString}.
454: */
455: public void testAttributedStringSerialization3() {
456: AttributedString s1 = new AttributedString("ABC");
457: s1.addAttribute(TextAttribute.LANGUAGE, "English");
458: AttributedString s2 = null;
459: try {
460: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
461: ObjectOutputStream out = new ObjectOutputStream(buffer);
462: SerialUtilities.writeAttributedString(s1, out);
463: out.close();
464:
465: ByteArrayInputStream bais = new ByteArrayInputStream(buffer
466: .toByteArray());
467: ObjectInputStream in = new ObjectInputStream(bais);
468: s2 = SerialUtilities.readAttributedString(in);
469: in.close();
470: } catch (Exception e) {
471: e.printStackTrace();
472: }
473: assertTrue(AttributedStringUtilities.equal(s1, s2));
474: }
475: }
|