/******************************************************************************* * Copyright (c) 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation ******************************************************************************/ package org.eclipse.core.tests.databinding; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.HashSet; import junit.framework.Assert; import org.eclipse.core.databinding.observable.list.WritableList; import org.eclipse.core.databinding.observable.set.WritableSet; import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase; /** * @since 3.2 * */ public class SerializableTest extends AbstractDefaultRealmTestCase{ public void testWritableList() throws Exception{ try{ WritableList list = new WritableList(new ArrayList(), Object.class); Object obj = doSerializationTest(list); Assert.assertNotNull(obj); }catch (Exception x){ x.printStackTrace(); throw x; } } public void testWritableSet() throws Exception{ try{ WritableSet set = new WritableSet(new HashSet(), Object.class); Object obj = doSerializationTest(set); Assert.assertNotNull(obj); }catch (Exception x){ x.printStackTrace(); throw x; } } private Object doSerializationTest(Serializable s) throws Exception{ byte[] bytes = serialize(s); Object obj = deserialize(bytes); return obj; } private byte[] serialize( Serializable s) throws IOException{ ByteArrayOutputStream bos = new ByteArrayOutputStream() ; ObjectOutputStream out = new ObjectOutputStream(bos) ; out.writeObject(s); out.close(); return bos.toByteArray(); } private Object deserialize( byte [] bytes) throws IOException,ClassNotFoundException{ ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream in = new ObjectInputStream(bis); Object result = in.readObject(); in.close(); return result; } }