/*
 * Created on 24.08.2006
 *
 */
package org.jdesktop.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Support class to test serializable behaviour.
 * 
 *  @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
 */
public final class SerializableSupport {
    private SerializableSupport() {
        //prevent instantiation
    }
    
    /**
     * Serialize the specified object to bytes, then deserialize it back.
     */
    @SuppressWarnings("unchecked")
    public static <T> T serialize(T object) {
        try {
            return (T) fromBytes(toBytes(object));
        } catch (ClassNotFoundException shouldNeverHappen) {
            throw new Error(shouldNeverHappen);
        } catch (IOException shouldNeverHappen) {
            throw new Error(shouldNeverHappen);
        }
    }

    private static byte[] toBytes(Object object) throws IOException {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream objectsOut = new ObjectOutputStream(bytesOut);
        objectsOut.writeObject(object);
        return bytesOut.toByteArray();
    }

    private static Object fromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
        ObjectInputStream objectsIn = new ObjectInputStream(bytesIn);
        return objectsIn.readObject();
    }
}

@@ -56,16 +56,76 @@
 .test;%0A%0A
+import java.beans.XMLDecoder;%0Aimport java.beans.XMLEncoder;%0A
 import j
@@ -1572,11 +1572,796 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Encodes an object as XML and then decodes it.%0A     * %0A     * @param %3CT%3E%0A     *            the type to operate on%0A     * @param object%0A     *            the object to encode/decode%0A     * @return a copy of the original object that was encoded and decoded%0A     * @see XMLEncoder%0A     * @see XMLDecoder%0A     */%0A    @SuppressWarnings(%22unchecked%22)%0A    public static %3CT%3E T encode(T object) %7B%0A        ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        XMLEncoder encoder = new XMLEncoder(baos);%0A        encoder.writeObject(object);%0A        encoder.close();%0A        %0A        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(baos.toByteArray()));%0A        T copy = (T) decoder.readObject();%0A        decoder.close();%0A        %0A        return copy;%0A    %7D
 %0A%7D%0A

Add XML serialization options to SerializableSupport.