Package ca.odell.glazedlists

Source Code of ca.odell.glazedlists.BasicEventListTest

/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists;

import ca.odell.glazedlists.event.ListEventAssembler;
import ca.odell.glazedlists.event.ListEventPublisher;
import ca.odell.glazedlists.impl.testing.GlazedListsTests;
import ca.odell.glazedlists.impl.testing.GlazedListsTests.SerializableListener;
import ca.odell.glazedlists.impl.testing.GlazedListsTests.UnserializableListener;
import ca.odell.glazedlists.util.concurrent.LockFactory;
import ca.odell.glazedlists.util.concurrent.ReadWriteLock;

import junit.framework.TestCase;

import java.io.IOException;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* Makes sure that {@link BasicEventList} works above and beyond its duties as
* an {@link EventList}. This includes support for {@link Serializable}.
*
* @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
*/
public class BasicEventListTest extends TestCase {

    public void testSimpleSerialization() throws IOException, ClassNotFoundException {
        EventList<String> serializableList = new BasicEventList<String>();
        serializableList.addAll(GlazedListsTests.stringToList("Saskatchewan Roughriders"));
        EventList<String> serializedCopy = GlazedListsTests.serialize(serializableList);
        assertEquals(serializableList, serializedCopy);

        serializedCopy.addAll(GlazedListsTests.stringToList("Hamilton Tiger-Cats"));
        assertFalse(serializableList.equals(serializedCopy));
    }

    public void testSerializeEmpty() throws IOException, ClassNotFoundException {
        EventList serializableList = new BasicEventList();
        EventList serializedCopy = GlazedListsTests.serialize(serializableList);
        assertEquals(serializableList, serializedCopy);
    }

    public void testSerializableListeners() throws IOException, ClassNotFoundException {
        EventList<String> serializableList = new BasicEventList<String>();
        SerializableListener listener = new SerializableListener();
        serializableList.addListEventListener(listener);

        serializableList.addAll(GlazedListsTests.stringToList("Szarka"));
        assertEquals(serializableList, SerializableListener.getLastSource());

        EventList<String> serializedCopy = GlazedListsTests.serialize(serializableList);
        assertEquals(serializableList, serializedCopy);

        assertEquals(serializableList, SerializableListener.getLastSource());
        serializedCopy.addAll(GlazedListsTests.stringToList("McCalla"));
        assertEquals(serializedCopy, SerializableListener.getLastSource());
    }

    public void testUnserializableListeners() throws IOException, ClassNotFoundException {
        EventList<String> serializableList = new BasicEventList<String>();
        UnserializableListener listener = new UnserializableListener();
        serializableList.addListEventListener(listener);

        serializableList.addAll(GlazedListsTests.stringToList("Keith"));
        assertEquals(serializableList, UnserializableListener.getLastSource());

        EventList<String> serializedCopy = GlazedListsTests.serialize(serializableList);
        assertEquals(serializableList, serializedCopy);

        assertEquals(serializableList, UnserializableListener.getLastSource());
        serializedCopy.addAll(GlazedListsTests.stringToList("Holmes"));
        assertEquals(serializableList, UnserializableListener.getLastSource());
    }

    public void testSerialVersionUID() {
        assertEquals(4883958173323072345L, ObjectStreamClass.lookup(BasicEventList.class).getSerialVersionUID());
    }

    /**
     * Ensures the serialization format as of October 3, 2005 is still valid today.
     * We created a {@link BasicEventList} containing a sequence of one character
     * Strings, and dumped that to bytes.
     */
    public void testVersion20051003() throws IOException, ClassNotFoundException {
        byte[] serializedBytes = {
            -0x54, -0x130x000x050x730x720x000x230x630x610x2e0x6f0x640x650x6c0x6c,
             0x2e0x670x6c0x610x7a0x650x640x6c0x690x730x740x730x2e0x420x610x73,
             0x690x630x450x760x650x6e0x740x4c0x690x730x740x43, -0x390x4e0x150x12,
            -0x37, -0x6d0x590x030x000x010x4c0x000x040x640x610x740x610x740x000x10,
             0x4c0x6a0x610x760x610x2f0x750x740x690x6c0x2f0x4c0x690x730x740x3b,
             0x780x700x750x720x000x130x5b0x4c0x6a0x610x760x610x2e0x6c0x610x6e,
             0x670x2e0x4f0x620x6a0x650x630x740x3b, -0x70, -0x320x58, -0x610x100x730x29,
             0x6c0x020x000x000x780x700x000x000x000x100x740x000x010x4f0x740x00,
             0x010x630x740x000x010x740x740x000x010x6f0x740x000x010x620x740x00,
             0x010x650x740x000x010x720x740x000x010x200x740x000x010x310x740x00,
             0x010x300x740x000x010x2c0x740x000x010x200x740x000x010x320x740x00,
             0x010x300x740x000x010x300x740x000x010x350x750x720x000x2f0x5b0x4c,
             0x630x610x2e0x6f0x640x650x6c0x6c0x2e0x670x6c0x610x7a0x650x640x6c,
             0x690x730x740x730x2e0x650x760x650x6e0x740x2e0x4c0x690x730x740x45,
             0x760x650x6e0x740x4c0x690x730x740x650x6e0x650x720x3b0x320x430x6c,
             0x62, -0x520x4f0x320x0d0x020x000x000x780x700x000x000x000x000x78,
        };

        List<String> expected = new ArrayList<String>();
        expected.addAll(GlazedListsTests.stringToList("October 10, 2005"));

        Object deserialized = GlazedListsTests.fromBytes(serializedBytes);
        assertEquals(expected, deserialized);
        assertEquals("[O, c, t, o, b, e, r,  , 1, 0, ,,  , 2, 0, 0, 5]", deserialized.toString());
    }

    /**
     * This test adds 4 BasicEventList<String> to a serialization container
     * (a simple ArrayList). All 4 BasicEventLists are constructed to share the
     * SAME Locks and Publisher. The test then serializes/deserializes the
     * container and ensures that the 4 BasicEventLists still share common
     * Locks and Publisher, though the identity is not expected or required
     * to be preserved after deserialization.
     */
    public void testSerializableLocksAndPublisher() throws IOException, ClassNotFoundException {
        // 1. create the Lock and Publisher that will be shared by all BasicEventLists
        final ReadWriteLock sharedLock = LockFactory.DEFAULT.createReadWriteLock();
        final ListEventPublisher sharedPublisher = ListEventAssembler.createListEventPublisher();

        // 2. add 4 BasicEventLists to a container, each of which shares a common Publisher and ReadWriteLocks
        final List<EventList<String>> serializationContainer = new ArrayList<EventList<String>>();
        for (int i = 0; i < 4; i++) {
            final EventList<String> eventList = new BasicEventList<String>(sharedPublisher, sharedLock);
            eventList.add("Test " + i);
            serializationContainer.add(eventList);
        }

        // 3. serialize/deserialize the container
        final List<EventList<String>> serializedCopy = GlazedListsTests.serialize(serializationContainer);
        assertEquals(serializationContainer, serializedCopy);

        // 4. ensure deserialized lists still share the lock and publisher
        final ListEventPublisher publisher = serializedCopy.get(0).getPublisher();
        final ReadWriteLock lock = serializedCopy.get(0).getReadWriteLock();       
        final CompositeList<String> compositeList = new CompositeList<String>(publisher, lock);
        for (int i = 0; i < 4; i++) {
            // explicitly check the identity of the publisher and lock
            final EventList<String> eventList = serializedCopy.get(i);
            assertSame(publisher, eventList.getPublisher());
            assertSame(lock, eventList.getReadWriteLock());

            // as a result, CompositeList should accept the BasicEventList for use
            compositeList.addMemberList(eventList);
        }
    }
}
TOP

Related Classes of ca.odell.glazedlists.BasicEventListTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.