You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridAddRowBuiltinContainerTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.vaadin.v7.tests.server.component.grid;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.fail;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import com.vaadin.tests.data.bean.Person;
  7. import com.vaadin.v7.data.Container;
  8. import com.vaadin.v7.data.Item;
  9. import com.vaadin.v7.data.util.BeanItem;
  10. import com.vaadin.v7.data.util.BeanItemContainer;
  11. import com.vaadin.v7.data.util.MethodProperty.MethodException;
  12. import com.vaadin.v7.ui.Grid;
  13. public class GridAddRowBuiltinContainerTest {
  14. Grid grid = new Grid();
  15. Container.Indexed container;
  16. @Before
  17. public void setUp() {
  18. container = grid.getContainerDataSource();
  19. grid.addColumn("myColumn");
  20. }
  21. @Test
  22. public void testSimpleCase() {
  23. Object itemId = grid.addRow("Hello");
  24. assertEquals(Integer.valueOf(1), itemId);
  25. assertEquals("There should be one item in the container", 1,
  26. container.size());
  27. assertEquals("Hello", container.getItem(itemId)
  28. .getItemProperty("myColumn").getValue());
  29. }
  30. @Test(expected = IllegalArgumentException.class)
  31. public void testNullParameter() {
  32. // cast to Object[] to distinguish from one null varargs value
  33. grid.addRow((Object[]) null);
  34. }
  35. @Test
  36. public void testNullValue() {
  37. // cast to Object to distinguish from a null varargs array
  38. Object itemId = grid.addRow((Object) null);
  39. assertEquals(null, container.getItem(itemId).getItemProperty("myColumn")
  40. .getValue());
  41. }
  42. @Test(expected = IllegalArgumentException.class)
  43. public void testAddInvalidType() {
  44. grid.addRow(Integer.valueOf(5));
  45. }
  46. @Test
  47. public void testMultipleProperties() {
  48. grid.addColumn("myOther", Integer.class);
  49. Object itemId = grid.addRow("Hello", Integer.valueOf(3));
  50. Item item = container.getItem(itemId);
  51. assertEquals("Hello", item.getItemProperty("myColumn").getValue());
  52. assertEquals(Integer.valueOf(3),
  53. item.getItemProperty("myOther").getValue());
  54. }
  55. @Test(expected = IllegalArgumentException.class)
  56. public void testInvalidPropertyAmount() {
  57. grid.addRow("Hello", Integer.valueOf(3));
  58. }
  59. @Test
  60. public void testRemovedColumn() {
  61. grid.addColumn("myOther", Integer.class);
  62. grid.removeColumn("myColumn");
  63. grid.addRow(Integer.valueOf(3));
  64. Item item = container.getItem(Integer.valueOf(1));
  65. assertEquals("Default value should be used for removed column", "",
  66. item.getItemProperty("myColumn").getValue());
  67. assertEquals(Integer.valueOf(3),
  68. item.getItemProperty("myOther").getValue());
  69. }
  70. @Test
  71. public void testMultiplePropertiesAfterReorder() {
  72. grid.addColumn("myOther", Integer.class);
  73. grid.setColumnOrder("myOther", "myColumn");
  74. grid.addRow(Integer.valueOf(3), "Hello");
  75. Item item = container.getItem(Integer.valueOf(1));
  76. assertEquals("Hello", item.getItemProperty("myColumn").getValue());
  77. assertEquals(Integer.valueOf(3),
  78. item.getItemProperty("myOther").getValue());
  79. }
  80. @Test
  81. public void testInvalidType_NothingAdded() {
  82. try {
  83. grid.addRow(Integer.valueOf(5));
  84. // Can't use @Test(expect = Foo.class) since we also want to verify
  85. // state after exception was thrown
  86. fail("Adding wrong type should throw ClassCastException");
  87. } catch (IllegalArgumentException e) {
  88. assertEquals("No row should have been added", 0, container.size());
  89. }
  90. }
  91. @Test
  92. public void testUnsupportingContainer() {
  93. setContainerRemoveColumns(new BeanItemContainer<Person>(Person.class));
  94. try {
  95. grid.addRow("name");
  96. // Can't use @Test(expect = Foo.class) since we also want to verify
  97. // state after exception was thrown
  98. fail("Adding to BeanItemContainer container should throw UnsupportedOperationException");
  99. } catch (UnsupportedOperationException e) {
  100. assertEquals("No row should have been added", 0, container.size());
  101. }
  102. }
  103. @Test
  104. public void testCustomContainer() {
  105. BeanItemContainer<Person> container = new BeanItemContainer<Person>(
  106. Person.class) {
  107. @Override
  108. public Object addItem() {
  109. BeanItem<Person> item = addBean(new Person());
  110. return getBeanIdResolver().getIdForBean(item.getBean());
  111. }
  112. };
  113. setContainerRemoveColumns(container);
  114. grid.addRow("name");
  115. assertEquals(1, container.size());
  116. assertEquals("name", container.getIdByIndex(0).getFirstName());
  117. }
  118. @Test
  119. public void testSetterThrowing() {
  120. BeanItemContainer<Person> container = new BeanItemContainer<Person>(
  121. Person.class) {
  122. @Override
  123. public Object addItem() {
  124. BeanItem<Person> item = addBean(new Person() {
  125. @Override
  126. public void setFirstName(String firstName) {
  127. if ("name".equals(firstName)) {
  128. throw new RuntimeException(firstName);
  129. } else {
  130. super.setFirstName(firstName);
  131. }
  132. }
  133. });
  134. return getBeanIdResolver().getIdForBean(item.getBean());
  135. }
  136. };
  137. setContainerRemoveColumns(container);
  138. try {
  139. grid.addRow("name");
  140. // Can't use @Test(expect = Foo.class) since we also want to verify
  141. // state after exception was thrown
  142. fail("Adding row should throw MethodException");
  143. } catch (MethodException e) {
  144. assertEquals("Got the wrong exception", "name",
  145. e.getCause().getMessage());
  146. assertEquals("There should be no rows in the container", 0,
  147. container.size());
  148. }
  149. }
  150. private void setContainerRemoveColumns(
  151. BeanItemContainer<Person> container) {
  152. // Remove predefined column so we can change container
  153. grid.removeAllColumns();
  154. grid.setContainerDataSource(container);
  155. grid.removeAllColumns();
  156. grid.addColumn("firstName");
  157. }
  158. }