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.

GridColumnAddingAndRemovingTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.tests.server.component.grid;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import com.vaadin.data.Container;
  23. import com.vaadin.data.Property;
  24. import com.vaadin.data.util.IndexedContainer;
  25. import com.vaadin.ui.LegacyGrid;
  26. public class GridColumnAddingAndRemovingTest {
  27. LegacyGrid grid = new LegacyGrid();
  28. Container.Indexed container;
  29. @Before
  30. public void setUp() {
  31. container = grid.getContainerDataSource();
  32. container.addItem();
  33. }
  34. @Test
  35. public void testAddColumn() {
  36. grid.addColumn("foo");
  37. Property<?> property = container
  38. .getContainerProperty(container.firstItemId(), "foo");
  39. assertEquals(property.getType(), String.class);
  40. }
  41. @Test(expected = IllegalStateException.class)
  42. public void testAddColumnTwice() {
  43. grid.addColumn("foo");
  44. grid.addColumn("foo");
  45. }
  46. @Test
  47. public void testAddRemoveAndAddAgainColumn() {
  48. grid.addColumn("foo");
  49. grid.removeColumn("foo");
  50. // Removing a column, doesn't remove the property
  51. Property<?> property = container
  52. .getContainerProperty(container.firstItemId(), "foo");
  53. assertEquals(property.getType(), String.class);
  54. grid.addColumn("foo");
  55. }
  56. @Test
  57. public void testAddNumberColumns() {
  58. grid.addColumn("bar", Integer.class);
  59. grid.addColumn("baz", Double.class);
  60. Property<?> property = container
  61. .getContainerProperty(container.firstItemId(), "bar");
  62. assertEquals(property.getType(), Integer.class);
  63. assertEquals(null, property.getValue());
  64. property = container.getContainerProperty(container.firstItemId(),
  65. "baz");
  66. assertEquals(property.getType(), Double.class);
  67. assertEquals(null, property.getValue());
  68. }
  69. @Test(expected = IllegalStateException.class)
  70. public void testAddDifferentTypeColumn() {
  71. grid.addColumn("foo");
  72. grid.removeColumn("foo");
  73. grid.addColumn("foo", Integer.class);
  74. }
  75. @Test(expected = IllegalStateException.class)
  76. public void testAddColumnToNonDefaultContainer() {
  77. grid.setContainerDataSource(new IndexedContainer());
  78. grid.addColumn("foo");
  79. }
  80. @Test
  81. public void testAddColumnForExistingProperty() {
  82. grid.addColumn("bar");
  83. IndexedContainer container2 = new IndexedContainer();
  84. container2.addContainerProperty("foo", Integer.class, 0);
  85. container2.addContainerProperty("bar", String.class, "");
  86. grid.setContainerDataSource(container2);
  87. assertNull("Grid should not have a column for property foo",
  88. grid.getColumn("foo"));
  89. assertNotNull("Grid did should have a column for property bar",
  90. grid.getColumn("bar"));
  91. for (LegacyGrid.Column column : grid.getColumns()) {
  92. assertNotNull("Grid getColumns returned a null value", column);
  93. }
  94. grid.removeAllColumns();
  95. grid.addColumn("foo");
  96. assertNotNull("Grid should now have a column for property foo",
  97. grid.getColumn("foo"));
  98. assertNull("Grid should not have a column for property bar anymore",
  99. grid.getColumn("bar"));
  100. }
  101. @Test(expected = IllegalStateException.class)
  102. public void testAddIncompatibleColumnProperty() {
  103. grid.addColumn("bar");
  104. grid.removeAllColumns();
  105. grid.addColumn("bar", Integer.class);
  106. }
  107. @Test
  108. public void testAddBooleanColumnProperty() {
  109. grid.addColumn("foo", Boolean.class);
  110. Property<?> property = container
  111. .getContainerProperty(container.firstItemId(), "foo");
  112. assertEquals(property.getType(), Boolean.class);
  113. assertEquals(property.getValue(), null);
  114. }
  115. }