diff options
author | Henri Sara <henri.sara@itmill.com> | 2010-12-01 14:28:47 +0000 |
---|---|---|
committer | Henri Sara <henri.sara@itmill.com> | 2010-12-01 14:28:47 +0000 |
commit | 477667763afb1a69e22c708425605326bcbc0620 (patch) | |
tree | 2f2ee5e82b3c7c949d27dcf4120d478250a75ebc /tests | |
parent | 11abe3b0730a645ebe0aa6ca6f339d2dc61a12fe (diff) | |
download | vaadin-framework-477667763afb1a69e22c708425605326bcbc0620.tar.gz vaadin-framework-477667763afb1a69e22c708425605326bcbc0620.zip |
#6074 support item id resolvers in BeanContainer: basic mechanisms, more container tests
svn changeset:16255/svn branch:6.5
Diffstat (limited to 'tests')
3 files changed, 551 insertions, 73 deletions
diff --git a/tests/src/com/vaadin/tests/server/container/AbstractBeanContainerTest.java b/tests/src/com/vaadin/tests/server/container/AbstractBeanContainerTest.java new file mode 100644 index 0000000000..c90f46fb21 --- /dev/null +++ b/tests/src/com/vaadin/tests/server/container/AbstractBeanContainerTest.java @@ -0,0 +1,80 @@ +package com.vaadin.tests.server.container; + +import com.vaadin.data.util.AbstractBeanContainer; +import com.vaadin.data.util.BeanContainer; +import com.vaadin.data.util.BeanItemContainer; + +/** + * Automated test for {@link AbstractBeanContainer}. + * + * Only a limited subset of the functionality is tested here, the rest in tests + * of subclasses including {@link BeanItemContainer} and {@link BeanContainer}. + */ +public abstract class AbstractBeanContainerTest extends AbstractContainerTest { + + public static class Person { + private String name; + + public Person(String name) { + setName(name); + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + public static class ClassName { + // field names match constants in parent test class + private String fullyQualifiedName; + private String simpleName; + private String reverseFullyQualifiedName; + private Integer idNumber; + + public ClassName(String fullyQualifiedName, Integer idNumber) { + this.fullyQualifiedName = fullyQualifiedName; + simpleName = AbstractContainerTest + .getSimpleName(fullyQualifiedName); + reverseFullyQualifiedName = reverse(fullyQualifiedName); + this.idNumber = idNumber; + } + + public String getFullyQualifiedName() { + return fullyQualifiedName; + } + + public void setFullyQualifiedName(String fullyQualifiedName) { + this.fullyQualifiedName = fullyQualifiedName; + } + + public String getSimpleName() { + return simpleName; + } + + public void setSimpleName(String simpleName) { + this.simpleName = simpleName; + } + + public String getReverseFullyQualifiedName() { + return reverseFullyQualifiedName; + } + + public void setReverseFullyQualifiedName( + String reverseFullyQualifiedName) { + this.reverseFullyQualifiedName = reverseFullyQualifiedName; + } + + public Integer getIdNumber() { + return idNumber; + } + + public void setIdNumber(Integer idNumber) { + this.idNumber = idNumber; + } + } + +} diff --git a/tests/src/com/vaadin/tests/server/container/BeanContainerTest.java b/tests/src/com/vaadin/tests/server/container/BeanContainerTest.java new file mode 100644 index 0000000000..c683e4f598 --- /dev/null +++ b/tests/src/com/vaadin/tests/server/container/BeanContainerTest.java @@ -0,0 +1,380 @@ +package com.vaadin.tests.server.container; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +import junit.framework.Assert; + +import com.vaadin.data.Container; +import com.vaadin.data.Item; +import com.vaadin.data.util.AbstractBeanContainer.BeanIdResolver; +import com.vaadin.data.util.BeanContainer; + +public class BeanContainerTest extends AbstractBeanContainerTest { + + protected static class PersonNameResolver implements + BeanIdResolver<String, Person> { + + public String getIdForBean(Person bean) { + return bean != null ? bean.getName() : null; + } + + } + + protected static class NullResolver implements + BeanIdResolver<String, Person> { + + public String getIdForBean(Person bean) { + return null; + } + + } + + private Map<String, ClassName> nameToBean = new LinkedHashMap<String, ClassName>(); + + private BeanContainer<String, ClassName> getContainer() { + return new BeanContainer<String, 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) { + BeanContainer<String, ClassName> beanItemContainer = (BeanContainer<String, ClassName>) container; + + beanItemContainer.removeAllItems(); + + for (Entry<String, ClassName> entry : nameToBean.entrySet()) { + beanItemContainer.addItem(entry.getKey(), entry.getValue()); + } + } + + @Override + protected boolean isFilteredOutItemNull() { + return false; + } + + public void testBasicOperations() { + testBasicContainerOperations(getContainer()); + } + + public void testFiltering() { + testContainerFiltering(getContainer()); + } + + public void testSorting() { + testContainerSorting(getContainer()); + } + + public void testSortingAndFiltering() { + testContainerSortingAndFiltering(getContainer()); + } + + // duplicated from parent class and modified - adding items to + // BeanContainer differs from other containers + public void testContainerOrdered() { + BeanContainer<String, String> container = new BeanContainer<String, String>( + String.class); + + String id = "test1"; + + Item item = container.addItem(id, "value"); + assertNotNull(item); + + assertEquals(id, container.firstItemId()); + assertEquals(id, container.lastItemId()); + + // isFirstId + assertTrue(container.isFirstId(id)); + assertTrue(container.isFirstId(container.firstItemId())); + // isLastId + assertTrue(container.isLastId(id)); + assertTrue(container.isLastId(container.lastItemId())); + + // Add a new item before the first + // addItemAfter + String newFirstId = "newFirst"; + item = container.addItemAfter(null, newFirstId, "newFirstValue"); + assertNotNull(item); + assertNotNull(container.getItem(newFirstId)); + + // isFirstId + assertTrue(container.isFirstId(newFirstId)); + assertTrue(container.isFirstId(container.firstItemId())); + // isLastId + assertTrue(container.isLastId(id)); + assertTrue(container.isLastId(container.lastItemId())); + + // nextItemId + assertEquals(id, container.nextItemId(newFirstId)); + assertNull(container.nextItemId(id)); + assertNull(container.nextItemId("not-in-container")); + + // prevItemId + assertEquals(newFirstId, container.prevItemId(id)); + assertNull(container.prevItemId(newFirstId)); + assertNull(container.prevItemId("not-in-container")); + + // addItemAfter(IDTYPE, IDTYPE, BT) + String newSecondItemId = "newSecond"; + item = container.addItemAfter(newFirstId, newSecondItemId, + "newSecondValue"); + // order is now: newFirstId, newSecondItemId, id + assertNotNull(item); + assertNotNull(container.getItem(newSecondItemId)); + assertEquals(id, container.nextItemId(newSecondItemId)); + assertEquals(newFirstId, container.prevItemId(newSecondItemId)); + + // addItemAfter(IDTYPE, IDTYPE, BT) + String fourthId = "id of the fourth item"; + Item fourth = container.addItemAfter(newFirstId, fourthId, + "fourthValue"); + // order is now: newFirstId, fourthId, newSecondItemId, id + assertNotNull(fourth); + assertEquals(fourth, container.getItem(fourthId)); + assertEquals(newSecondItemId, container.nextItemId(fourthId)); + assertEquals(newFirstId, container.prevItemId(fourthId)); + + // addItemAfter(IDTYPE, IDTYPE, BT) + String fifthId = "fifth"; + Item fifth = container.addItemAfter(null, fifthId, "fifthValue"); + // order is now: fifthId, newFirstId, fourthId, newSecondItemId, id + assertNotNull(fifth); + assertEquals(fifth, container.getItem(fifthId)); + assertEquals(newFirstId, container.nextItemId(fifthId)); + assertNull(container.prevItemId(fifthId)); + + } + + // TODO test Container.Indexed interface operation - testContainerIndexed()? + + public void testAddItemAt() { + BeanContainer<String, String> container = new BeanContainer<String, String>( + String.class); + + container.addItem("id1", "value1"); + // id1 + container.addItemAt(0, "id2", "value2"); + // id2, id1 + container.addItemAt(1, "id3", "value3"); + // id2, id3, id1 + container.addItemAt(container.size(), "id4", "value4"); + // id2, id3, id1, id4 + + assertNull(container.addItemAt(-1, "id5", "value5")); + assertNull(container.addItemAt(container.size() + 1, "id6", "value6")); + + assertEquals(4, container.size()); + assertEquals("id2", container.getIdByIndex(0)); + assertEquals("id3", container.getIdByIndex(1)); + assertEquals("id1", container.getIdByIndex(2)); + assertEquals("id4", container.getIdByIndex(3)); + } + + public void testUnsupportedMethods() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + container.addItem("John", new Person("John")); + + try { + container.addItem(); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addItem(null); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addItemAfter(null, null); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addItemAfter(new Person("Jane")); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addItemAt(0); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addItemAt(0, new Person("Jane")); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addContainerProperty("lastName", String.class, ""); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.removeContainerProperty("name"); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + assertEquals(1, container.size()); + } + + public void testAddNullBeans() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + + assertNull(container.addItem("id1", null)); + assertNull(container.addItemAfter(null, "id2", null)); + assertNull(container.addItemAt(0, "id3", null)); + + assertEquals(0, container.size()); + } + + public void testAddNullId() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + + Person john = new Person("John"); + + assertNull(container.addItem(null, john)); + assertNull(container.addItemAfter(null, null, john)); + assertNull(container.addItemAt(0, null, john)); + + assertEquals(0, container.size()); + } + + public void testEmptyContainer() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + + assertNull(container.firstItemId()); + assertNull(container.lastItemId()); + + assertEquals(0, container.size()); + + // could test more about empty container + } + + public void testAddBeanWithoutResolver() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + + try { + container.addBean(new Person("John")); + Assert.fail(); + } catch (IllegalStateException e) { + // should get exception + } + try { + container.addBeanAfter(null, new Person("Jane")); + Assert.fail(); + } catch (IllegalStateException e) { + // should get exception + } + try { + container.addBeanAt(0, new Person("Jack")); + Assert.fail(); + } catch (IllegalStateException e) { + // should get exception + } + try { + container + .addAll(Arrays.asList(new Person[] { new Person("Jack") })); + Assert.fail(); + } catch (IllegalStateException e) { + // should get exception + } + + assertEquals(0, container.size()); + } + + public void testAddBeanWithNullResolver() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + // resolver that returns null as item id + container.setIdResolver(new NullResolver()); + + try { + container.addBean(new Person("John")); + Assert.fail(); + } catch (IllegalArgumentException e) { + // should get exception + } + try { + container.addBeanAfter(null, new Person("Jane")); + Assert.fail(); + } catch (IllegalArgumentException e) { + // should get exception + } + try { + container.addBeanAt(0, new Person("Jack")); + Assert.fail(); + } catch (IllegalArgumentException e) { + // should get exception + } + + assertEquals(0, container.size()); + } + + public void testAddBeanWithResolver() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + container.setIdResolver(new PersonNameResolver()); + + assertNotNull(container.addBean(new Person("John"))); + assertNotNull(container.addBeanAfter(null, new Person("Jane"))); + assertNotNull(container.addBeanAt(0, new Person("Jack"))); + + container.addAll(Arrays.asList(new Person[] { new Person("Jill"), + new Person("Joe") })); + + assertTrue(container.containsId("John")); + assertTrue(container.containsId("Jane")); + assertTrue(container.containsId("Jack")); + assertTrue(container.containsId("Jill")); + assertTrue(container.containsId("Joe")); + assertEquals(3, container.indexOfId("Jill")); + assertEquals(4, container.indexOfId("Joe")); + assertEquals(5, container.size()); + } + + public void testAddNullBeansWithResolver() { + BeanContainer<String, Person> container = new BeanContainer<String, Person>( + Person.class); + container.setIdResolver(new PersonNameResolver()); + + assertNull(container.addBean(null)); + assertNull(container.addBeanAfter(null, null)); + assertNull(container.addBeanAt(0, null)); + + assertEquals(0, container.size()); + } + +} diff --git a/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java b/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java index 7fb93b5152..e82c6a5984 100644 --- a/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java +++ b/tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java @@ -23,59 +23,10 @@ import com.vaadin.data.util.BeanItemContainer; * * Most sorting related tests are in {@link BeanItemContainerSortTest}. */ -public class BeanItemContainerTest extends AbstractContainerTest { +public class BeanItemContainerTest extends AbstractBeanContainerTest { // basics from the common container test - public class ClassName { - // field names match constants in parent test class - private String fullyQualifiedName; - private String simpleName; - private String reverseFullyQualifiedName; - private Integer idNumber; - - public ClassName(String fullyQualifiedName, Integer idNumber) { - this.fullyQualifiedName = fullyQualifiedName; - simpleName = AbstractContainerTest - .getSimpleName(fullyQualifiedName); - reverseFullyQualifiedName = reverse(fullyQualifiedName); - this.idNumber = idNumber; - } - - public String getFullyQualifiedName() { - return fullyQualifiedName; - } - - public void setFullyQualifiedName(String fullyQualifiedName) { - this.fullyQualifiedName = fullyQualifiedName; - } - - public String getSimpleName() { - return simpleName; - } - - public void setSimpleName(String simpleName) { - this.simpleName = simpleName; - } - - public String getReverseFullyQualifiedName() { - return reverseFullyQualifiedName; - } - - public void setReverseFullyQualifiedName( - String reverseFullyQualifiedName) { - this.reverseFullyQualifiedName = reverseFullyQualifiedName; - } - - public Integer getIdNumber() { - return idNumber; - } - - public void setIdNumber(Integer idNumber) { - this.idNumber = idNumber; - } - } - private Map<String, ClassName> nameToBean = new LinkedHashMap<String, ClassName>(); private BeanItemContainer<ClassName> getContainer() { @@ -220,14 +171,16 @@ public class BeanItemContainerTest extends AbstractContainerTest { 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() { + @SuppressWarnings("deprecation") + public void testCollectionConstructors() { 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)); + // note that this constructor is problematic, users should use the + // version that + // takes the bean class as a parameter BeanItemContainer<ClassName> container = new BeanItemContainer<ClassName>( classNames); @@ -235,20 +188,27 @@ public class BeanItemContainerTest extends AbstractContainerTest { Assert.assertEquals(classNames.get(0), container.firstItemId()); Assert.assertEquals(classNames.get(1), container.getIdByIndex(1)); Assert.assertEquals(classNames.get(2), container.lastItemId()); + + BeanItemContainer<ClassName> container2 = new BeanItemContainer<ClassName>( + ClassName.class, classNames); + + Assert.assertEquals(3, container2.size()); + Assert.assertEquals(classNames.get(0), container2.firstItemId()); + Assert.assertEquals(classNames.get(1), container2.getIdByIndex(1)); + Assert.assertEquals(classNames.get(2), container2.lastItemId()); } // this only applies to the collection constructor with no type parameter + @SuppressWarnings("deprecation") public void testEmptyCollectionConstructor() { try { - BeanItemContainer<ClassName> container = new BeanItemContainer<ClassName>( - (Collection<ClassName>) null); + 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>()); + new BeanItemContainer<ClassName>(new ArrayList<ClassName>()); Assert.fail("Initializing BeanItemContainer from an empty collection should not work!"); } catch (IllegalArgumentException e) { // success @@ -381,22 +341,6 @@ public class BeanItemContainerTest extends AbstractContainerTest { }.listenerTest(); } - protected static class Person { - private String name; - - public Person(String name) { - setName(name); - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - } - public void testAddRemoveWhileFiltering() { BeanItemContainer<Person> container = new BeanItemContainer<Person>( Person.class); @@ -537,4 +481,78 @@ public class BeanItemContainerTest extends AbstractContainerTest { assertEquals(jack, container.nextItemId(matthew)); assertEquals(michael, container.nextItemId(jack)); } + + public void testUnsupportedMethods() { + BeanItemContainer<Person> container = new BeanItemContainer<Person>( + Person.class); + container.addBean(new Person("John")); + + try { + container.addItem(); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addItemAfter(new Person("Jane")); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addItemAt(0); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.addContainerProperty("lastName", String.class, ""); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + try { + container.removeContainerProperty("name"); + Assert.fail(); + } catch (UnsupportedOperationException e) { + // should get exception + } + + assertEquals(1, container.size()); + } + + public void testAddNullBean() { + BeanItemContainer<Person> container = new BeanItemContainer<Person>( + Person.class); + Person john = new Person("John"); + container.addBean(john); + + assertNull(container.addItem(null)); + assertNull(container.addItemAfter(null, null)); + assertNull(container.addItemAfter(john, null)); + assertNull(container.addItemAt(0, null)); + + assertEquals(1, container.size()); + } + + public void testBeanIdResolver() { + BeanItemContainer<Person> container = new BeanItemContainer<Person>( + Person.class); + Person john = new Person("John"); + + assertSame(john, container.getIdResolver().getIdForBean(john)); + } + + public void testNullBeanClass() { + try { + new BeanItemContainer<Object>((Class<Object>) null); + } catch (IllegalArgumentException e) { + // should get exception + } + } + } |