diff options
author | Henri Sara <henri.sara@itmill.com> | 2010-11-24 13:30:46 +0000 |
---|---|---|
committer | Henri Sara <henri.sara@itmill.com> | 2010-11-24 13:30:46 +0000 |
commit | 32a84eb54bd05c54263cca7d0d1be6899364e1e6 (patch) | |
tree | 18e1244479b75d97a2b47ac0312f5ea4e8db0196 | |
parent | 3f543a9585ec2f2f5622bb61b0474d2dac61806a (diff) | |
download | vaadin-framework-32a84eb54bd05c54263cca7d0d1be6899364e1e6.tar.gz vaadin-framework-32a84eb54bd05c54263cca7d0d1be6899364e1e6.zip |
More JUnit tests for BeanItemContainer and IndexedContainer
svn changeset:16129/svn branch:6.5
4 files changed, 275 insertions, 9 deletions
diff --git a/tests/src/com/vaadin/tests/server/container/AbstractContainerTest.java b/tests/src/com/vaadin/tests/server/container/AbstractContainerTest.java index 80cdfc1b6d..2b8dd1eecf 100644 --- a/tests/src/com/vaadin/tests/server/container/AbstractContainerTest.java +++ b/tests/src/com/vaadin/tests/server/container/AbstractContainerTest.java @@ -3,6 +3,7 @@ package com.vaadin.tests.server.container; import java.util.ArrayList;
import java.util.List;
+import junit.framework.Assert;
import junit.framework.TestCase;
import com.vaadin.data.Container;
@@ -172,6 +173,83 @@ public abstract class AbstractContainerTest extends TestCase { }
+ protected void testContainerIndexed(Container.Indexed container,
+ Object itemId, int itemPosition, boolean testAddEmptyItemAt,
+ Object newItemId, boolean testAddItemAtWithId) {
+ initializeContainer(container);
+
+ // indexOfId
+ Assert.assertEquals(itemPosition, container.indexOfId(itemId));
+
+ // getIdByIndex
+ Assert.assertEquals(itemId, container.getIdByIndex(itemPosition));
+
+ // addItemAt
+ if (testAddEmptyItemAt) {
+ Object addedId = container.addItemAt(itemPosition);
+ Assert.assertEquals(itemPosition, container.indexOfId(addedId));
+ Assert.assertEquals(itemPosition + 1, container.indexOfId(itemId));
+ Assert.assertEquals(addedId, container.getIdByIndex(itemPosition));
+ Assert.assertEquals(itemId,
+ container.getIdByIndex(itemPosition + 1));
+
+ Object newFirstId = container.addItemAt(0);
+ Assert.assertEquals(0, container.indexOfId(newFirstId));
+ Assert.assertEquals(itemPosition + 2, container.indexOfId(itemId));
+ Assert.assertEquals(newFirstId, container.firstItemId());
+ Assert.assertEquals(newFirstId, container.getIdByIndex(0));
+ Assert.assertEquals(itemId,
+ container.getIdByIndex(itemPosition + 2));
+
+ Object newLastId = container.addItemAt(container.size());
+ Assert.assertEquals(container.size() - 1,
+ container.indexOfId(newLastId));
+ Assert.assertEquals(itemPosition + 2, container.indexOfId(itemId));
+ Assert.assertEquals(newLastId, container.lastItemId());
+ Assert.assertEquals(newLastId,
+ container.getIdByIndex(container.size() - 1));
+ Assert.assertEquals(itemId,
+ container.getIdByIndex(itemPosition + 2));
+
+ container.removeItem(addedId);
+ container.removeItem(newFirstId);
+ container.removeItem(newLastId);
+ }
+
+ // addItemAt
+ if (testAddItemAtWithId) {
+ container.addItemAt(itemPosition, newItemId);
+ Assert.assertEquals(itemPosition, container.indexOfId(newItemId));
+ Assert.assertEquals(itemPosition + 1, container.indexOfId(itemId));
+ Assert.assertEquals(newItemId, container.getIdByIndex(itemPosition));
+ Assert.assertEquals(itemId,
+ container.getIdByIndex(itemPosition + 1));
+ container.removeItem(newItemId);
+ Assert.assertFalse(container.containsId(newItemId));
+
+ container.addItemAt(0, newItemId);
+ Assert.assertEquals(0, container.indexOfId(newItemId));
+ Assert.assertEquals(itemPosition + 1, container.indexOfId(itemId));
+ Assert.assertEquals(newItemId, container.firstItemId());
+ Assert.assertEquals(newItemId, container.getIdByIndex(0));
+ Assert.assertEquals(itemId,
+ container.getIdByIndex(itemPosition + 1));
+ container.removeItem(newItemId);
+ Assert.assertFalse(container.containsId(newItemId));
+
+ container.addItemAt(container.size(), newItemId);
+ Assert.assertEquals(container.size() - 1,
+ container.indexOfId(newItemId));
+ Assert.assertEquals(itemPosition, container.indexOfId(itemId));
+ Assert.assertEquals(newItemId, container.lastItemId());
+ Assert.assertEquals(newItemId,
+ container.getIdByIndex(container.size() - 1));
+ Assert.assertEquals(itemId, container.getIdByIndex(itemPosition));
+ container.removeItem(newItemId);
+ Assert.assertFalse(container.containsId(newItemId));
+ }
+ }
+
protected void testContainerFiltering(Container.Filterable container) {
initializeContainer(container);
@@ -528,5 +606,4 @@ public abstract class AbstractContainerTest extends TestCase { "com.vaadin.ui.VerticalLayout", "com.vaadin.ui.Window",
"com.vaadin.util.SerializerHelper", "org.vaadin.test.LastClass" };
- // TODO testContainerIndexing(Container.Indexed) & use in subclasses
}
diff --git a/tests/src/com/vaadin/tests/server/container/AbstractHierarchicalContainerTest.java b/tests/src/com/vaadin/tests/server/container/AbstractHierarchicalContainerTest.java index 7d0aa6f5e5..5c4dfc658f 100644 --- a/tests/src/com/vaadin/tests/server/container/AbstractHierarchicalContainerTest.java +++ b/tests/src/com/vaadin/tests/server/container/AbstractHierarchicalContainerTest.java @@ -7,7 +7,8 @@ import com.vaadin.data.Container.Hierarchical; import com.vaadin.data.Container.Sortable;
import com.vaadin.data.Item;
-public class AbstractHierarchicalContainerTest extends AbstractContainerTest {
+public abstract class AbstractHierarchicalContainerTest extends
+ AbstractContainerTest {
/**
* @param container
diff --git a/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java b/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java index 3fc03c97ea..fa76499860 100644 --- a/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java +++ b/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java @@ -1,9 +1,19 @@ package com.vaadin.tests.server.container; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; +import junit.framework.Assert; + +import org.easymock.EasyMock; + import com.vaadin.data.Container; +import com.vaadin.data.Container.ItemSetChangeEvent; +import com.vaadin.data.Container.ItemSetChangeListener; import com.vaadin.data.Item; import com.vaadin.data.util.BeanItemContainer; @@ -65,24 +75,32 @@ public class BeanItemContainerTest extends AbstractContainerTest { } } - private Map<String, ClassName> nameToBean = new HashMap<String, ClassName>(); + private Map<String, ClassName> nameToBean = new LinkedHashMap<String, ClassName>(); private BeanItemContainer<ClassName> getContainer() { return new BeanItemContainer<ClassName>(ClassName.class); } @Override + public void setUp() { + nameToBean.clear(); + + for (int i = 0; i < sampleData.length; i++) { + ClassName className = new ClassName(sampleData[i], i); + nameToBean.put(sampleData[i], className); + } + } + + @Override @SuppressWarnings("unchecked") protected void initializeContainer(Container container) { BeanItemContainer<ClassName> beanItemContainer = (BeanItemContainer<ClassName>) container; beanItemContainer.removeAllItems(); - nameToBean.clear(); - for (int i = 0; i < sampleData.length; i++) { - ClassName className = new ClassName(sampleData[i], i); - nameToBean.put(sampleData[i], className); - beanItemContainer.addBean(className); + Iterator<ClassName> it = nameToBean.values().iterator(); + while (it.hasNext()) { + beanItemContainer.addBean(it.next()); } } @@ -196,4 +214,169 @@ public class BeanItemContainerTest extends AbstractContainerTest { } + public void testContainerIndexed() { + testContainerIndexed(getContainer(), nameToBean.get(sampleData[2]), 2, + false, new ClassName("org.vaadin.test.Test", 8888), true); + } + + // note that the constructor tested here is problematic, and should also + // take the bean class as a parameter + public void testCollectionConstructor() { + List<ClassName> classNames = new ArrayList<ClassName>(); + classNames.add(new ClassName("a.b.c.Def", 1)); + classNames.add(new ClassName("a.b.c.Fed", 2)); + classNames.add(new ClassName("b.c.d.Def", 3)); + + BeanItemContainer<ClassName> container = new BeanItemContainer<ClassName>( + classNames); + + Assert.assertEquals(3, container.size()); + Assert.assertEquals(classNames.get(0), container.firstItemId()); + Assert.assertEquals(classNames.get(1), container.getIdByIndex(1)); + Assert.assertEquals(classNames.get(2), container.lastItemId()); + } + + // this only applies to the collection constructor with no type parameter + public void testEmptyCollectionConstructor() { + try { + BeanItemContainer<ClassName> container = new BeanItemContainer<ClassName>( + (Collection<ClassName>) null); + Assert.fail("Initializing BeanItemContainer from a null collection should not work!"); + } catch (IllegalArgumentException e) { + // success + } + try { + BeanItemContainer<ClassName> container = new BeanItemContainer<ClassName>( + new ArrayList<ClassName>()); + Assert.fail("Initializing BeanItemContainer from an empty collection should not work!"); + } catch (IllegalArgumentException e) { + // success + } + } + + protected abstract class ItemSetChangeListenerTester { + public void listenerTest() { + listenerTest(true); + } + + public void listenerTest(boolean expectChangeEvent) { + BeanItemContainer<ClassName> container = prepareContainer(); + + ItemSetChangeListener listener = EasyMock + .createStrictMock(ItemSetChangeListener.class); + + // Expectations and start test + if (expectChangeEvent) { + listener.containerItemSetChange(EasyMock + .isA(ItemSetChangeEvent.class)); + } + EasyMock.replay(listener); + + // Add listener and add a property -> should end up in listener + // once + container.addListener(listener); + performModification(container); + + // Ensure listener was called once + EasyMock.verify(listener); + + // Remove the listener + container.removeListener(listener); + performModification(container); + + // Ensure listener has not been called again + EasyMock.verify(listener); + } + + protected BeanItemContainer<ClassName> prepareContainer() { + BeanItemContainer<ClassName> container = getContainer(); + initializeContainer(container); + return container; + } + + protected abstract void performModification( + BeanItemContainer<ClassName> container); + } + + public void testItemSetChangeListeners() { + new ItemSetChangeListenerTester() { + @Override + protected void performModification( + BeanItemContainer<ClassName> container) { + container.addBean(new ClassName("com.example.Test", 1111)); + } + }.listenerTest(); + + new ItemSetChangeListenerTester() { + @Override + protected void performModification( + BeanItemContainer<ClassName> container) { + container.removeItem(nameToBean.get(sampleData[0])); + } + }.listenerTest(); + + new ItemSetChangeListenerTester() { + @Override + protected void performModification( + BeanItemContainer<ClassName> container) { + container.removeItem(new ClassName("com.example.Test", 1111)); + } + }.listenerTest(false); + + new ItemSetChangeListenerTester() { + @Override + protected void performModification( + BeanItemContainer<ClassName> container) { + // this test does not check that there would be no second + // notification because the collection is already empty + container.removeAllItems(); + } + }.listenerTest(); + + new ItemSetChangeListenerTester() { + private int propertyIndex = 0; + + @Override + protected void performModification( + BeanItemContainer<ClassName> container) { + Collection<String> containerPropertyIds = container + .getContainerPropertyIds(); + container.addContainerFilter(new ArrayList<String>( + containerPropertyIds).get(propertyIndex++), "a", true, + false); + } + }.listenerTest(); + + new ItemSetChangeListenerTester() { + @Override + protected void performModification( + BeanItemContainer<ClassName> container) { + container.removeContainerFilters(SIMPLE_NAME); + } + + @Override + protected BeanItemContainer<ClassName> prepareContainer() { + BeanItemContainer<ClassName> container = super + .prepareContainer(); + container.addContainerFilter(SIMPLE_NAME, "a", true, false); + return container; + }; + }.listenerTest(); + + new ItemSetChangeListenerTester() { + @Override + protected void performModification( + BeanItemContainer<ClassName> container) { + container.removeAllContainerFilters(); + } + + @Override + protected BeanItemContainer<ClassName> prepareContainer() { + BeanItemContainer<ClassName> container = super + .prepareContainer(); + container.addContainerFilter(SIMPLE_NAME, "a", true, false); + return container; + }; + }.listenerTest(); + } } diff --git a/tests/src/com/vaadin/tests/server/container/TestIndexedContainer.java b/tests/src/com/vaadin/tests/server/container/TestIndexedContainer.java index 13e8e17c48..1bb80b25b3 100644 --- a/tests/src/com/vaadin/tests/server/container/TestIndexedContainer.java +++ b/tests/src/com/vaadin/tests/server/container/TestIndexedContainer.java @@ -24,4 +24,9 @@ public class TestIndexedContainer extends AbstractContainerTest { testContainerOrdered(new IndexedContainer());
}
+ public void testContainerIndexed() {
+ testContainerIndexed(new IndexedContainer(), sampleData[2], 2, true,
+ "newItemId", true);
+ }
+
}
|