1 44 45 package org.jfree.io.junit; 46 47 import java.awt.Color ; 48 import java.awt.GradientPaint ; 49 import java.awt.Paint ; 50 import java.awt.TexturePaint ; 51 import java.awt.font.TextAttribute ; 52 import java.awt.geom.Arc2D ; 53 import java.awt.geom.GeneralPath ; 54 import java.awt.geom.Line2D ; 55 import java.awt.geom.Rectangle2D ; 56 import java.awt.image.BufferedImage ; 57 import java.io.ByteArrayInputStream ; 58 import java.io.ByteArrayOutputStream ; 59 import java.io.ObjectInputStream ; 60 import java.io.ObjectOutputStream ; 61 import java.text.AttributedString ; 62 63 import javax.swing.UIManager ; 64 import javax.swing.plaf.ColorUIResource ; 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 77 public class SerialUtilitiesTests extends TestCase { 78 79 84 public static Test suite() { 85 return new TestSuite(SerialUtilitiesTests.class); 86 } 87 88 93 public SerialUtilitiesTests(final String name) { 94 super(name); 95 } 96 97 100 public void testIsSerializable() { 101 assertTrue(SerialUtilities.isSerializable(Color .class)); 102 assertTrue(SerialUtilities.isSerializable(ColorUIResource .class)); 103 assertFalse(SerialUtilities.isSerializable(GradientPaint .class)); 104 assertFalse(SerialUtilities.isSerializable(TexturePaint .class)); 105 } 106 107 111 public void testColorSerialization() { 112 113 final Paint p1 = Color.blue; 114 Paint p2 = null; 115 116 try { 117 final ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 118 final ObjectOutputStream out = new ObjectOutputStream (buffer); 119 SerialUtilities.writePaint(p1, out); 120 out.close(); 121 122 final ByteArrayInputStream bias = new ByteArrayInputStream ( 123 buffer.toByteArray() 124 ); 125 final ObjectInputStream in = new ObjectInputStream (bias); 126 p2 = SerialUtilities.readPaint(in); 127 in.close(); 128 } 129 catch (Exception e) { 130 System.out.println(e.toString()); 131 } 132 assertEquals(p1, p2); 133 134 } 135 136 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 ( 150 buffer.toByteArray() 151 ); 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 166 public void testGradientPaintSerialization() { 167 168 final Paint p1 = new GradientPaint ( 169 0.0f, 0.0f, Color.blue, 100.0f, 200.0f, Color.red 170 ); 171 Paint p2 = null; 172 173 try { 174 final ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 175 final ObjectOutputStream out = new ObjectOutputStream (buffer); 176 SerialUtilities.writePaint(p1, out); 177 out.close(); 178 179 final ByteArrayInputStream bias = new ByteArrayInputStream ( 180 buffer.toByteArray() 181 ); 182 final ObjectInputStream in = new ObjectInputStream (bias); 183 p2 = SerialUtilities.readPaint(in); 184 in.close(); 185 } 186 catch (Exception e) { 187 System.out.println(e.toString()); 188 } 189 190 final GradientPaint gp1 = (GradientPaint ) p1; 194 final GradientPaint gp2 = (GradientPaint ) 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 208 public void testTexturePaintSerialization() { 209 210 final Paint p1 = new TexturePaint ( 211 new BufferedImage (5, 5, BufferedImage.TYPE_INT_RGB), 212 new Rectangle2D.Double (0, 0, 5, 5) 213 ); 214 Paint p2 = null; 215 216 try { 217 final ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 218 final ObjectOutputStream out = new ObjectOutputStream (buffer); 219 SerialUtilities.writePaint(p1, out); 220 out.close(); 221 222 final ByteArrayInputStream bias = new ByteArrayInputStream ( 223 buffer.toByteArray() 224 ); 225 final ObjectInputStream in = new ObjectInputStream (bias); 226 p2 = SerialUtilities.readPaint(in); 227 in.close(); 228 } 229 catch (Exception e) { 230 System.out.println(e.toString()); 231 } 232 233 assertNull(p2); 234 235 } 236 237 241 public void testLine2DFloatSerialization() { 242 Line2D l1 = new Line2D.Float (1.0f, 2.0f, 3.0f, 4.0f); 243 Line2D l2 = null; 244 try { 245 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 246 ObjectOutputStream out = new ObjectOutputStream (buffer); 247 SerialUtilities.writeShape(l1, out); 248 out.close(); 249 250 ByteArrayInputStream bais = new ByteArrayInputStream ( 251 buffer.toByteArray() 252 ); 253 ObjectInputStream in = new ObjectInputStream (bais); 254 l2 = (Line2D ) SerialUtilities.readShape(in); 255 in.close(); 256 } 257 catch (Exception e) { 258 System.out.println(e.toString()); 259 } 260 assertTrue(ShapeUtilities.equal(l1, l2)); 261 } 262 263 267 public void testLine2DDoubleSerialization() { 268 Line2D l1 = new Line2D.Double (1.0, 2.0, 3.0, 4.0); 269 Line2D l2 = null; 270 try { 271 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 272 ObjectOutputStream out = new ObjectOutputStream (buffer); 273 SerialUtilities.writeShape(l1, out); 274 out.close(); 275 276 ByteArrayInputStream bais = new ByteArrayInputStream ( 277 buffer.toByteArray() 278 ); 279 ObjectInputStream in = new ObjectInputStream (bais); 280 l2 = (Line2D ) SerialUtilities.readShape(in); 281 in.close(); 282 } 283 catch (Exception e) { 284 System.out.println(e.toString()); 285 } 286 assertTrue(ShapeUtilities.equal(l1, l2)); 287 } 288 289 293 public void testRectangle2DFloatSerialization() { 294 Rectangle2D r1 = new Rectangle2D.Float (1.0f, 2.0f, 3.0f, 4.0f); 295 Rectangle2D r2 = null; 296 try { 297 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 298 ObjectOutputStream out = new ObjectOutputStream (buffer); 299 SerialUtilities.writeShape(r1, out); 300 out.close(); 301 302 ByteArrayInputStream bais = new ByteArrayInputStream ( 303 buffer.toByteArray() 304 ); 305 ObjectInputStream in = new ObjectInputStream (bais); 306 r2 = (Rectangle2D ) SerialUtilities.readShape(in); 307 in.close(); 308 } 309 catch (Exception e) { 310 System.out.println(e.toString()); 311 } 312 assertTrue(ShapeUtilities.equal(r1, r2)); 313 } 314 315 319 public void testRectangle2DDoubleSerialization() { 320 Rectangle2D r1 = new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0); 321 Rectangle2D r2 = null; 322 try { 323 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 324 ObjectOutputStream out = new ObjectOutputStream (buffer); 325 SerialUtilities.writeShape(r1, out); 326 out.close(); 327 328 ByteArrayInputStream bais = new ByteArrayInputStream ( 329 buffer.toByteArray() 330 ); 331 ObjectInputStream in = new ObjectInputStream (bais); 332 r2 = (Rectangle2D ) SerialUtilities.readShape(in); 333 in.close(); 334 } 335 catch (Exception e) { 336 System.out.println(e.toString()); 337 } 338 assertTrue(ShapeUtilities.equal(r1, r2)); 339 } 340 341 345 public void testArc2DFloatSerialization() { 346 Arc2D a1 = new Arc2D.Float ( 347 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, Arc2D.PIE 348 ); 349 Arc2D a2 = null; 350 try { 351 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 352 ObjectOutputStream out = new ObjectOutputStream (buffer); 353 SerialUtilities.writeShape(a1, out); 354 out.close(); 355 356 ByteArrayInputStream bais = new ByteArrayInputStream ( 357 buffer.toByteArray() 358 ); 359 ObjectInputStream in = new ObjectInputStream (bais); 360 a2 = (Arc2D ) SerialUtilities.readShape(in); 361 in.close(); 362 } 363 catch (Exception e) { 364 System.out.println(e.toString()); 365 } 366 assertTrue(ShapeUtilities.equal(a1, a2)); 367 } 368 369 373 public void testArc2DDoubleSerialization() { 374 Arc2D a1 = new Arc2D.Double (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, Arc2D.PIE); 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 ); 385 ObjectInputStream in = new ObjectInputStream (bais); 386 a2 = (Arc2D ) SerialUtilities.readShape(in); 387 in.close(); 388 } 389 catch (Exception e) { 390 System.out.println(e.toString()); 391 } 392 assertTrue(ShapeUtilities.equal(a1, a2)); 393 } 394 395 398 public void testGeneralPathSerialization() { 399 GeneralPath g1 = new GeneralPath (); 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 g2 = null; 406 try { 407 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 408 ObjectOutputStream out = new ObjectOutputStream (buffer); 409 SerialUtilities.writeShape(g1, out); 410 out.close(); 411 412 ByteArrayInputStream bais = new ByteArrayInputStream ( 413 buffer.toByteArray() 414 ); 415 ObjectInputStream in = new ObjectInputStream (bais); 416 g2 = (GeneralPath ) SerialUtilities.readShape(in); 417 in.close(); 418 } 419 catch (Exception e) { 420 System.out.println(e.toString()); 421 } 422 assertTrue(ShapeUtilities.equal(g1, g2)); 423 424 } 425 426 429 public void testAttributedStringSerialization1() { 430 AttributedString s1 = new AttributedString (""); 431 AttributedString s2 = null; 432 try { 433 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 434 ObjectOutputStream out = new ObjectOutputStream (buffer); 435 SerialUtilities.writeAttributedString(s1, out); 436 out.close(); 437 438 ByteArrayInputStream bais = new ByteArrayInputStream ( 439 buffer.toByteArray() 440 ); 441 ObjectInputStream in = new ObjectInputStream (bais); 442 s2 = SerialUtilities.readAttributedString(in); 443 in.close(); 444 } 445 catch (Exception e) { 446 e.printStackTrace(); 447 } 448 assertTrue(AttributedStringUtilities.equal(s1, s2)); 449 } 450 451 454 public void testAttributedStringSerialization2() { 455 AttributedString s1 = new AttributedString ("ABC"); 456 AttributedString s2 = null; 457 try { 458 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 459 ObjectOutputStream out = new ObjectOutputStream (buffer); 460 SerialUtilities.writeAttributedString(s1, out); 461 out.close(); 462 463 ByteArrayInputStream bais = new ByteArrayInputStream ( 464 buffer.toByteArray() 465 ); 466 ObjectInputStream in = new ObjectInputStream (bais); 467 s2 = SerialUtilities.readAttributedString(in); 468 in.close(); 469 } 470 catch (Exception e) { 471 e.printStackTrace(); 472 } 473 assertTrue(AttributedStringUtilities.equal(s1, s2)); 474 } 475 476 479 public void testAttributedStringSerialization3() { 480 AttributedString s1 = new AttributedString ("ABC"); 481 s1.addAttribute(TextAttribute.LANGUAGE, "English"); 482 AttributedString s2 = null; 483 try { 484 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 485 ObjectOutputStream out = new ObjectOutputStream (buffer); 486 SerialUtilities.writeAttributedString(s1, out); 487 out.close(); 488 489 ByteArrayInputStream bais = new ByteArrayInputStream ( 490 buffer.toByteArray() 491 ); 492 ObjectInputStream in = new ObjectInputStream (bais); 493 s2 = SerialUtilities.readAttributedString(in); 494 in.close(); 495 } 496 catch (Exception e) { 497 e.printStackTrace(); 498 } 499 assertTrue(AttributedStringUtilities.equal(s1, s2)); 500 } 501 } 502 | Popular Tags |