}
@Test
public void testSerialization() throws IOException, ClassNotFoundException, PropertyVetoException {
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
listenerSupport.addListener(new VetoableChangeListener() {
@Override
public void vetoableChange(final PropertyChangeEvent e) {
}
});
listenerSupport.addListener(EasyMock.createNiceMock(VetoableChangeListener.class));
//serialize:
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(listenerSupport);
objectOutputStream.close();
//deserialize:
@SuppressWarnings("unchecked")
final
EventListenerSupport<VetoableChangeListener> deserializedListenerSupport = (EventListenerSupport<VetoableChangeListener>) new ObjectInputStream(
new ByteArrayInputStream(outputStream.toByteArray())).readObject();
//make sure we get a listener array back, of the correct component type, and that it contains only the serializable mock
final VetoableChangeListener[] listeners = deserializedListenerSupport.getListeners();
assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType());
assertEquals(1, listeners.length);
//now verify that the mock still receives events; we can infer that the proxy was correctly reconstituted
final VetoableChangeListener listener = listeners[0];
final PropertyChangeEvent evt = new PropertyChangeEvent(new Date(), "Day", 7, 9);
listener.vetoableChange(evt);
EasyMock.replay(listener);
deserializedListenerSupport.fire().vetoableChange(evt);
EasyMock.verify(listener);
//remove listener and verify we get an empty array of listeners