All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.ca.odell.glazedlists.EventListTest Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists;

import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
import ca.odell.glazedlists.impl.testing.GlazedListsTests;
import ca.odell.glazedlists.impl.testing.ListConsistencyListener;
import ca.odell.glazedlists.matchers.Matchers;

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Verifies that EventList matches the List API.
 *
 * @author Jesse Wilson
 */
public class EventListTest extends TestCase {

    /**
     * Validates that removeAll() works.
     *
     * @see Bug 169
     */
    public void testRemoveAll() {
        List jesse = GlazedListsTests.stringToList("JESSE");
        List wilson = GlazedListsTests.stringToList("WILSON");

        // create the reference list
        List jesseArrayList = new ArrayList();
        jesseArrayList.addAll(jesse);
        jesseArrayList.removeAll(wilson);

        // test the BasicEventList list
        List jesseBasicEventList = new BasicEventList();
        installConsistencyListener(jesseBasicEventList);
        jesseBasicEventList.addAll(jesse);
        jesseBasicEventList.removeAll(wilson);
        assertEquals(jesseArrayList, jesseBasicEventList);

        // test the SortedList list
        List jesseSortedList = new SortedList(new BasicEventList(), null);
        jesseSortedList.addAll(jesse);
        jesseSortedList.removeAll(wilson);
        assertEquals(jesseArrayList, jesseSortedList);

        List removeMultipleTestList = GlazedListsTests.stringToList("booblah");
        removeMultipleTestList.removeAll(GlazedListsTests.stringToList("bo"));
        assertEquals(GlazedListsTests.stringToList("lah"), removeMultipleTestList);
    }

    /**
     * Validates that retainAll() works.
     */
    public void testRetainAll() {
        List jesse = GlazedListsTests.stringToList("JESSE");
        List wilson = GlazedListsTests.stringToList("WILSON");

        // create the reference list
        List jesseArrayList = new ArrayList();
        jesseArrayList.addAll(jesse);
        jesseArrayList.retainAll(wilson);

        // test the BasicEventList list
        List jesseBasicEventList = new BasicEventList();
        installConsistencyListener(jesseBasicEventList);
        jesseBasicEventList.addAll(jesse);
        jesseBasicEventList.retainAll(wilson);
        assertEquals(jesseArrayList, jesseBasicEventList);

        // test the SortedList list
        List jesseSortedList = new SortedList(new BasicEventList(), null);
        jesseSortedList.addAll(jesse);
        jesseSortedList.retainAll(wilson);
        assertEquals(jesseArrayList, jesseSortedList);
    }

    /**
     * Validates that contains() works with null.
     */
    public void testContainsNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();

            // test a list that doesn't contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            assertEquals(false, list.contains(null));
            assertEquals(true,  list.contains("Western"));

            // test a list that does contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            assertEquals(true, list.contains(null));
            assertEquals(true, list.contains("Western"));
            assertEquals(false, list.contains("Molson"));
        }
    }

    /**
     * Validates that containsAll() works with null.
     */
    public void testContainsAllNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();

            // test a list that doesn't contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            assertEquals(true, list.containsAll(Arrays.asList(new String[] { "Sleeman", "Molson" })));
            assertEquals(false, list.containsAll(Arrays.asList(new String[] { "Molson", null })));
            assertEquals(false, list.containsAll(Arrays.asList(new String[] { "Molson", "Busch" })));

            // test a list that does contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            assertEquals(false, list.containsAll(Arrays.asList(new String[] { "Sleeman", "Molson" })));
            assertEquals(true, list.containsAll(Arrays.asList(new String[] { "Sleeman", "Western" })));
            assertEquals(true, list.containsAll(Arrays.asList(new String[] { "Western", null })));
            assertEquals(true, list.containsAll(Arrays.asList(new String[] { null, null })));
        }
    }

    /**
     * Validates that indexOf() works with null.
     */
    public void testIndexOfNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();

            // test a list that doesn't contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            assertTrue(-1 == list.indexOf(null));
            assertTrue(-1 != list.indexOf("Western"));

            // test a list that does contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            assertTrue(-1 != list.indexOf(null));
            assertTrue(-1 != list.indexOf("Western"));
            assertTrue(-1 == list.indexOf("Molson"));
        }
    }

    /**
     * Validates that lastIndexOf() works with null.
     */
    public void testLastIndexOfNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();

            // test a list that doesn't contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            assertTrue(-1 == list.lastIndexOf(null));
            assertTrue(-1 != list.lastIndexOf("Western"));

            // test a list that does contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            assertTrue(-1 != list.lastIndexOf(null));
            assertTrue(-1 != list.lastIndexOf("Western"));
            assertTrue(-1 == list.lastIndexOf("Molson"));
        }
    }

    /**
     * Validates that remove() works with null.
     */
    public void testRemoveNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();
            installConsistencyListener(list);

            // test a list that doesn't contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            assertEquals(false, list.remove(null));
            assertEquals(true,  list.remove("Sleeman"));

            // test a list that does contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            assertEquals(true, list.remove(null));
            assertEquals(true, list.remove("Western"));
            assertEquals(false, list.remove("Molson"));
        }
    }

    /**
     * Validates that removeAll() works with null.
     */
    public void testRemoveAllNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();

            // test a list that doesn't contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            assertEquals(true, list.removeAll(Arrays.asList(new String[] { "Western", null })));
            assertEquals(false,  list.removeAll(Arrays.asList(new String[] { null, "Busch" })));

            // test a list that does contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            assertEquals(true, list.removeAll(Arrays.asList(new String[] { "Western", "Busch" })));
            assertEquals(true, list.removeAll(Arrays.asList(new String[] { "Sleeman", null })));
            assertEquals(false, list.removeAll(Arrays.asList(new String[] { "Western", null })));
        }
    }

    /**
     * Validates that retainAll() works with null.
     */
    public void testRetainAllNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();

            // test a list that doesn't contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            assertEquals(true,  list.retainAll(Arrays.asList(new String[] { "Western", null })));
            assertEquals(true, list.retainAll(Arrays.asList(new String[] { "Moslon", null })));

            // test a list that does contain nulls
            list.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            assertEquals(true,  list.retainAll(Arrays.asList(new String[] { "Western", null })));
            assertEquals(true, list.retainAll(Arrays.asList(new String[] { "Moslon", null })));
        }
    }

    /**
     * Validates that hashCode() works with null.
     */
    public void testHashCodeNull() {
        // get all different list types
        List> listTypes = new ArrayList>();
        listTypes.add(new ArrayList());
        listTypes.add(new BasicEventList());
        listTypes.add(SortedList.create(new BasicEventList()));

        // test all different list types
        for(Iterator> i = listTypes.iterator(); i.hasNext();) {
            List list = i.next();
            List copy = new ArrayList();

            // test a list that doesn't contain nulls
            list.clear();
            copy.clear();
            list.addAll(Arrays.asList(new String[] { "Molson", "Sleeman", "Labatts", "Western" }));
            copy.addAll(list);
            assertEquals(copy.hashCode(), list.hashCode());
            assertTrue(list.equals(copy));
            copy.set(0, "Busch");
            assertFalse(list.equals(copy));

            // test a list that does contain nulls
            list.clear();
            copy.clear();
            list.addAll(Arrays.asList(new String[] { null, "Sleeman", null, "Western" }));
            copy.addAll(list);
            assertEquals(copy.hashCode(), list.hashCode());
            assertTrue(list.equals(copy));
            copy.set(0, "Busch");
            assertFalse(list.equals(copy));
        }
    }

    /**
     * Test that the {@link GlazedLists#eventListOf(Object[])} factory
     * method works.
     */
    public void testGlazedListsEventListUsingVarArgs() {
        // make sure they have different backing stores
        EventList eventList = GlazedLists.eventListOf(new String[] {"A", "B"});
        assertEquals(Arrays.asList(new String[] {"A", "B"}), eventList);

        // make sure null is supported
        EventList empty = GlazedLists.eventListOf((String[]) null);
        assertEquals(Collections.EMPTY_LIST, empty);
    }

    /**
     * Test that the {@link GlazedLists#eventList(java.util.Collection)} factory
     * method works.
     *
     * @see Bug 234
     */
    public void testGlazedListsEventList() {
        // make sure they have different backing stores
        List list = new ArrayList();
        EventList eventList = GlazedLists.eventList(list);
        assertEquals(list, eventList);

        list.add("A");
        assertTrue(!list.equals(eventList));

        eventList.add("B");
        assertTrue(!list.equals(eventList));

        // make sure null is supported
        EventList empty = GlazedLists.eventList((Collection) null);
        assertEquals(Collections.EMPTY_LIST, empty);
    }

    /**
     * Tests the {@link GlazedLists#syncEventListToList(EventList, List)}
     * factory method.
     */
    public void testGlazedListsSync() {
        EventList source = new BasicEventList();
        source.add("McCallum");
        source.add("Keith");
        List target = new ArrayList();
        target.add("Greene");

        ListEventListener listener = GlazedLists.syncEventListToList(source, target);
        assertEquals(source, target);

        source.add("Szakra");
        assertEquals(source, target);

        source.addAll(Arrays.asList(new String[] { "Moore", "Holmes" }));
        assertEquals(source, target);

        source.add(1, "Burris");
        assertEquals(source, target);

        source.set(1, "Crandell");
        assertEquals(source, target);

        Collections.sort(source);
        assertEquals(source, target);

        source.clear();
        assertEquals(source, target);

        source.removeListEventListener(listener);
        source.add("Davis");
        assertFalse(source.equals(target));
    }

    public void testEventListTypeSafety() {
        EventList source = new BasicEventList();
        final Set acceptedTypes = new HashSet();
        acceptedTypes.add(null);
        acceptedTypes.add(Integer.class);
        acceptedTypes.add(String.class);
        ListEventListener typeSafetyListener = GlazedLists.typeSafetyListener(source, acceptedTypes);

        source.add(null);
        source.add(new Integer(0));
        source.add("Testing");

        try {
            source.add(new Long(23));
            fail("Expected an IllegalArgumentException for disallowed type");
        } catch (IllegalArgumentException e) {
            // expected
        }

        // the source list is in an inconsistent state so we rebuild the list
        source = new BasicEventList();
        typeSafetyListener = GlazedLists.typeSafetyListener(source, acceptedTypes);
        source.add(null);

        try {
            source.set(0, new Long(23));
            fail("Expected an IllegalArgumentException for disallowed type");
        } catch (IllegalArgumentException e) {
            // expected
        }

        // recover from the exception
        source.clear();

        source.removeListEventListener(typeSafetyListener);

        // these should now succeed now that we're not using the type safety checker any longer
        source.add(new Long(23));
        source.set(0, new Long(23));
    }

    public void testEventListLock() {
        final EventList source = new BasicEventList();

        // asymmetric unlocking of the readlock should fail-fast
        try {
            source.getReadWriteLock().readLock().unlock();
            fail("failed to receive an IllegalStateException when unlocking and unlocked readlock");
        } catch (IllegalMonitorStateException iae) {}

        // asymmetric unlocking of the writelock should fail-fast
        try {
            source.getReadWriteLock().writeLock().unlock();
            fail("failed to receive an IllegalStateException when unlocking and unlocked writelock");
        } catch (IllegalMonitorStateException iae) {}

        // symmetric locking/unlocking of the readlock should succeed
        source.getReadWriteLock().readLock().lock();
        source.getReadWriteLock().readLock().unlock();

        // symmetric locking/unlocking of the writelock should succeed
        source.getReadWriteLock().writeLock().lock();
        source.getReadWriteLock().writeLock().unlock();
    }

    public void testRemoveAllOnView() {
        EventList original = new BasicEventList();
        original.addAll(GlazedListsTests.stringToList("ABCDE"));
        FilterList filtered = new FilterList(original, Matchers.trueMatcher());
        filtered.removeAll(filtered);
        assertEquals(Collections.EMPTY_LIST, original);
    }

    public void testRetainAllOnSelf() {
        EventList original = new BasicEventList();
        original.addAll(GlazedListsTests.stringToList("ABCDE"));
        original.retainAll(original);
        assertEquals(GlazedListsTests.stringToList("ABCDE"), original);
    }

    public void testSublistClear() {
        EventList original = new BasicEventList();
        original.addAll(GlazedListsTests.stringToList("ABCDE"));

        Iterator iterator = original.subList(2, 4).iterator();
        iterator.next();
        iterator.remove();
        iterator.next();
        iterator.remove();

        assertEquals(GlazedListsTests.stringToList("ABE"), original);
    }

    public void testAddAllFromView() {
        EventList original = new BasicEventList();
        original.addAll(Arrays.asList(new Integer[] { new Integer(0), new Integer(10), new Integer(20), new Integer(30), new Integer(40) }));
        FilterList filtered = new FilterList(original, GlazedListsTests.matchAtLeast(20));
        original.addAll(filtered);
        assertEquals(Arrays.asList(new Integer[] { new Integer(0), new Integer(10), new Integer(20), new Integer(30), new Integer(40), new Integer(20), new Integer(30), new Integer(40) }), original);
    }

    public void testSimpleAddAll() {
        EventList source = new BasicEventList();
        installConsistencyListener(source);
        FilterList filterList = new FilterList(source, Matchers.trueMatcher());

        filterList.addAll(GlazedListsTests.stringToList("JESSE"));
        assertEquals(GlazedListsTests.stringToList("JESSE"), source);
        assertEquals(GlazedListsTests.stringToList("JESSE"), filterList);
    }

    public void testReplace() {
        EventList source = new BasicEventList();
        installConsistencyListener(source);
        source.addAll(GlazedListsTests.stringToList("ROUGHRIDERS"));
        source.set(2, "G");
        source.set(0, "T");
        source.set(4, "R");
        source.set(1, "I");
        source.set(3, "E");
        assertEquals(GlazedListsTests.stringToList("TIGERRIDERS"), source);
    }

    /**
     * This test case was generated from a problem that we received in the field.
     * It occured when a crazy amount of list events were being combined into one,
     * and we failed to create a simpler test case that still demonstrated the
     * problematic behaviour. This is probably due to the way that we sort list
     * events while processing them.
     */
    public void testCombineEvents() {
        TransactionList list = new TransactionList(new BasicEventList(), true);
        for (int i = 0; i < 16; i++)
             list.add(new Integer(0));

        ListConsistencyListener.install(list);

        list.beginEvent();

        for(int i = 0; i < 4; i++) list.add(8, new Object());
        for(int j = 7; j >= 0; j--) {
            for(int i = 0; i < 10; i++) list.add(j, new Object());
        }
        list.remove(55);
        list.remove(95);
        list.remove(14);
        list.remove(22);
        list.remove(27);
        list.remove(78);
        list.remove(1);
        list.remove(85);
        list.remove(52);
        list.remove(14);
        list.remove(39);
        list.remove(38);
        list.remove(61);
        list.remove(69);
        list.remove(8);
        list.remove(57);
        list.remove(10);
        list.remove(5);
        list.remove(71);
        list.remove(60);
        list.remove(42);
        list.remove(21);
        list.remove(15);
        list.remove(59);
        list.remove(15);
        list.remove(14);
        list.remove(24);
        list.remove(43);
        list.remove(35);
        list.remove(12);
        list.remove(11);
        list.remove(34);
        list.remove(42);
        list.remove(32);
        list.remove(19);
        list.add(32, new Integer(92));
        list.remove(44);
        list.remove(19);
        list.remove(45);
        list.remove(55);
        list.remove(23);
        list.remove(11);
        list.remove(8);
        list.remove(50);
        list.remove(29);
        list.remove(31);
        list.remove(33);
        list.remove(45);
        list.remove(15);
        list.remove(25);
        list.remove(8);
        list.add(40, new Integer(95));
        list.remove(32);
        list.remove(3);
        list.remove(26);
        list.remove(14);
        list.remove(36);
        list.add(39, new Integer(96));
        list.remove(34);
        list.remove(21);
        list.remove(13);
        list.remove(32);
        list.remove(30);
        list.add(36, new Integer(97));
        list.remove(43);
        list.remove(2);
        list.remove(34);
        list.remove(35);
        list.remove(17);
        list.add(39, new Integer(98));
        for(int i = 0; i < 5; i++) {
            list.remove(list.size() - 1);
        }
        list.add(29, new Integer(99));
        for(int i = 0; i < 5; i++) {
            list.remove(list.size() - 1);
        }
        list.add(22, new Integer(100));
        for(int i = 0; i < 5; i++) {
            list.remove(list.size() - 1);
        }
        list.set(25, new Integer(101)); // critical
        for(int j = 0; j < 4; j++) {
            for(int i = 0; i < 5; i++) list.remove(0);
            list.add(0, new Integer(102));
        }
        for(int i = 0; i < 10; i++) list.remove(0);
        list.add(0, new Integer(107));
        for(int i = 0; i < 2; i++) list.remove(0);

        list.commitEvent();
    }

    public void testGenericsOfListEvent() {
        final EventList source = GlazedLists.eventListOf((String[]) null);
        source.addListEventListener(new ListEventListener() {
            public void listChanged(ListEvent listChanges) {
                listChanges.next();
                Object o = listChanges.getSourceList().get(listChanges.getIndex());
                assertEquals(String.class, o.getClass());
            }
        });

        source.addListEventListener(new ListEventListener() {
            public void listChanged(ListEvent listChanges) {
                listChanges.next();
                String s = listChanges.getSourceList().get(listChanges.getIndex());
                assertEquals(String.class, s.getClass());
            }
        });

        ((EventList)source).add("Test");
    }

    /**
     * Install a consistency listener to the specified list.
     */
    private static void installConsistencyListener(List list) {
        if(list instanceof BasicEventList) {
            ListConsistencyListener listConsistencyListener = ListConsistencyListener.install((BasicEventList)list);
            listConsistencyListener.setPreviousElementTracked(true);
        }
    }
}