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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.tests.server.component.grid;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.fail;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import com.vaadin.tests.data.bean.Person;
  22. import com.vaadin.v7.data.Container;
  23. import com.vaadin.v7.data.Item;
  24. import com.vaadin.v7.data.util.BeanItem;
  25. import com.vaadin.v7.data.util.BeanItemContainer;
  26. import com.vaadin.v7.data.util.MethodProperty.MethodException;
  27. import com.vaadin.v7.ui.Grid;
  28. public class GridAddRowBuiltinContainerTest {
  29. Grid grid = new Grid();
  30. Container.Indexed container;
  31. @Before
  32. public void setUp() {
  33. container = grid.getContainerDataSource();
  34. grid.addColumn("myColumn");
  35. }
  36. @Test
  37. public void testSimpleCase() {
  38. Object itemId = grid.addRow("Hello");
  39. assertEquals(Integer.valueOf(1), itemId);
  40. assertEquals("There should be one item in the container", 1,
  41. container.size());
  42. assertEquals("Hello", container.getItem(itemId)
  43. .getItemProperty("myColumn").getValue());
  44. }
  45. @Test(expected = IllegalArgumentException.class)
  46. public void testNullParameter() {
  47. // cast to Object[] to distinguish from one null varargs value
  48. grid.addRow((Object[]) null);
  49. }
  50. @Test
  51. public void testNullValue() {
  52. // cast to Object to distinguish from a null varargs array
  53. Object itemId = grid.addRow((Object) null);
  54. assertEquals(null, container.getItem(itemId).getItemProperty("myColumn")
  55. .getValue());
  56. }
  57. @Test(expected = IllegalArgumentException.class)
  58. public void testAddInvalidType() {
  59. grid.addRow(Integer.valueOf(5));
  60. }
  61. @Test
  62. public void testMultipleProperties() {
  63. grid.addColumn("myOther", Integer.class);
  64. Object itemId = grid.addRow("Hello", Integer.valueOf(3));
  65. Item item = container.getItem(itemId);
  66. assertEquals("Hello", item.getItemProperty("myColumn").getValue());
  67. assertEquals(Integer.valueOf(3),
  68. item.getItemProperty("myOther").getValue());
  69. }
  70. @Test(expected = IllegalArgumentException.class)
  71. public void testInvalidPropertyAmount() {
  72. grid.addRow("Hello", Integer.valueOf(3));
  73. }
  74. @Test
  75. public void testRemovedColumn() {
  76. grid.addColumn("myOther", Integer.class);
  77. grid.removeColumn("myColumn");
  78. grid.addRow(Integer.valueOf(3));
  79. Item item = container.getItem(Integer.valueOf(1));
  80. assertEquals("Default value should be used for removed column", "",
  81. item.getItemProperty("myColumn").getValue());
  82. assertEquals(Integer.valueOf(3),
  83. item.getItemProperty("myOther").getValue());
  84. }
  85. @Test
  86. public void testMultiplePropertiesAfterReorder() {
  87. grid.addColumn("myOther", Integer.class);
  88. grid.setColumnOrder("myOther", "myColumn");
  89. grid.addRow(Integer.valueOf(3), "Hello");
  90. Item item = container.getItem(Integer.valueOf(1));
  91. assertEquals("Hello", item.getItemProperty("myColumn").getValue());
  92. assertEquals(Integer.valueOf(3),
  93. item.getItemProperty("myOther").getValue());
  94. }
  95. @Test
  96. public void testInvalidType_NothingAdded() {
  97. try {
  98. grid.addRow(Integer.valueOf(5));
  99. // Can't use @Test(expect = Foo.class) since we also want to verify
  100. // state after exception was thrown
  101. fail("Adding wrong type should throw ClassCastException");
  102. } catch (IllegalArgumentException e) {
  103. assertEquals("No row should have been added", 0, container.size());
  104. }
  105. }
  106. @Test
  107. public void testUnsupportingContainer() {
  108. setContainerRemoveColumns(new BeanItemContainer<Person>(Person.class));
  109. try {
  110. grid.addRow("name");
  111. // Can't use @Test(expect = Foo.class) since we also want to verify
  112. // state after exception was thrown
  113. fail("Adding to BeanItemContainer container should throw UnsupportedOperationException");
  114. } catch (UnsupportedOperationException e) {
  115. assertEquals("No row should have been added", 0, container.size());
  116. }
  117. }
  118. @Test
  119. public void testCustomContainer() {
  120. BeanItemContainer<Person> container = new BeanItemContainer<Person>(
  121. Person.class) {
  122. @Override
  123. public Object addItem() {
  124. BeanItem<Person> item = addBean(new Person());
  125. return getBeanIdResolver().getIdForBean(item.getBean());
  126. }
  127. };
  128. setContainerRemoveColumns(container);
  129. grid.addRow("name");
  130. assertEquals(1, container.size());
  131. assertEquals("name", container.getIdByIndex(0).getFirstName());
  132. }
  133. @Test
  134. public void testSetterThrowing() {
  135. BeanItemContainer<Person> container = new BeanItemContainer<Person>(
  136. Person.class) {
  137. @Override
  138. public Object addItem() {
  139. BeanItem<Person> item = addBean(new Person() {
  140. @Override
  141. public void setFirstName(String firstName) {
  142. if ("name".equals(firstName)) {
  143. throw new RuntimeException(firstName);
  144. } else {
  145. super.setFirstName(firstName);
  146. }
  147. }
  148. });
  149. return getBeanIdResolver().getIdForBean(item.getBean());
  150. }
  151. };
  152. setContainerRemoveColumns(container);
  153. try {
  154. grid.addRow("name");
  155. // Can't use @Test(expect = Foo.class) since we also want to verify
  156. // state after exception was thrown
  157. fail("Adding row should throw MethodException");
  158. } catch (MethodException e) {
  159. assertEquals("Got the wrong exception", "name",
  160. e.getCause().getMessage());
  161. assertEquals("There should be no rows in the container", 0,
  162. container.size());
  163. }
  164. }
  165. private void setContainerRemoveColumns(
  166. BeanItemContainer<Person> container) {
  167. // Remove predefined column so we can change container
  168. grid.removeAllColumns();
  169. grid.setContainerDataSource(container);
  170. grid.removeAllColumns();
  171. grid.addColumn("firstName");
  172. }
  173. }