/** * Serialize an instance, restore it, and check for equality. In addition, test for a bug that was * reported where the listener list is 'null' after deserialization. */ public void testSerialization() { BarRenderer r1 = new BarRenderer(); r1.setBaseLegendTextFont(new Font("Dialog", Font.PLAIN, 4)); r1.setBaseLegendTextPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.green)); r1.setBaseLegendShape(new Line2D.Double(1.0, 2.0, 3.0, 4.0)); BarRenderer r2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(r1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); r2 = (BarRenderer) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(r1, r2); try { r2.notifyListeners(new RendererChangeEvent(r2)); } catch (NullPointerException e) { assertTrue(false); // failed } }
public static Object byteArray2Object(byte[] b) throws IOException, ClassNotFoundException { /* * http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array */ ByteArrayInputStream bis = new ByteArrayInputStream(b); ObjectInput oi = new ObjectInputStream(bis); Object nesne = oi.readObject(); bis.close(); oi.close(); return nesne; }