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.

GridContainerTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.vaadin.v7.tests.server.component.grid;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import static org.junit.Assert.fail;
  5. import java.io.IOException;
  6. import java.io.ObjectOutputStream;
  7. import java.io.OutputStream;
  8. import org.junit.Test;
  9. import com.vaadin.ui.Component;
  10. import com.vaadin.ui.Label;
  11. import com.vaadin.v7.data.util.IndexedContainer;
  12. import com.vaadin.v7.ui.Grid;
  13. import com.vaadin.v7.ui.Grid.DetailsGenerator;
  14. import com.vaadin.v7.ui.Grid.RowReference;
  15. public class GridContainerTest {
  16. /**
  17. * Null Stream used with serialization tests
  18. */
  19. protected static OutputStream NULLSTREAM = new OutputStream() {
  20. @Override
  21. public void write(int b) {
  22. }
  23. };
  24. @Test
  25. public void testDetailsGeneratorDoesNotResetOnContainerChange() {
  26. Grid grid = new Grid();
  27. DetailsGenerator detGen = new DetailsGenerator() {
  28. @Override
  29. public Component getDetails(RowReference rowReference) {
  30. return new Label("Empty details");
  31. }
  32. };
  33. grid.setDetailsGenerator(detGen);
  34. grid.setContainerDataSource(createContainer());
  35. assertEquals("DetailsGenerator changed", detGen,
  36. grid.getDetailsGenerator());
  37. }
  38. @Test
  39. public void testSetContainerTwice() throws Exception {
  40. TestGrid grid = new TestGrid();
  41. grid.setContainerDataSource(createContainer());
  42. // Simulate initial response to ensure "lazy" state changes are done
  43. // before resetting the datasource
  44. grid.beforeClientResponse(true);
  45. grid.getDataProvider().beforeClientResponse(true);
  46. grid.setContainerDataSource(createContainer());
  47. }
  48. @SuppressWarnings("unchecked")
  49. private IndexedContainer createContainer() {
  50. IndexedContainer container = new IndexedContainer();
  51. container.addContainerProperty("x", String.class, null);
  52. container.addItem(0).getItemProperty("x").setValue("y");
  53. return container;
  54. }
  55. @Test
  56. public void setColumnsOrder() {
  57. Grid grid = new Grid();
  58. IndexedContainer ic = new IndexedContainer();
  59. ic.addContainerProperty("foo", String.class, "");
  60. ic.addContainerProperty("baz", String.class, "");
  61. ic.addContainerProperty("bar", String.class, "");
  62. grid.setContainerDataSource(ic);
  63. grid.setColumns("foo", "baz", "bar");
  64. assertEquals("foo", grid.getColumns().get(0).getPropertyId());
  65. assertEquals("baz", grid.getColumns().get(1).getPropertyId());
  66. assertEquals("bar", grid.getColumns().get(2).getPropertyId());
  67. }
  68. @Test
  69. public void addColumnNotInContainer() {
  70. Grid grid = new Grid();
  71. grid.setContainerDataSource(new IndexedContainer());
  72. try {
  73. grid.addColumn("notInContainer");
  74. fail("Adding a property id not in the container should throw an exception");
  75. } catch (IllegalStateException e) {
  76. assertTrue(e.getMessage().contains("notInContainer"));
  77. assertTrue(
  78. e.getMessage().contains("does not exist in the container"));
  79. }
  80. }
  81. @Test
  82. public void setColumnsForPropertyIdNotInContainer() {
  83. Grid grid = new Grid();
  84. grid.setContainerDataSource(new IndexedContainer());
  85. try {
  86. grid.setColumns("notInContainer", "notThereEither");
  87. fail("Setting columns for property ids not in the container should throw an exception");
  88. } catch (IllegalStateException e) {
  89. // addColumn is run in random order..
  90. assertTrue(e.getMessage().contains("notInContainer")
  91. || e.getMessage().contains("notThereEither"));
  92. assertTrue(
  93. e.getMessage().contains("does not exist in the container"));
  94. }
  95. }
  96. @Test(expected = IllegalStateException.class)
  97. public void multipleAddColumnsForDefaultContainer() {
  98. Grid grid = new Grid();
  99. grid.addColumn("foo");
  100. grid.addColumn("foo");
  101. }
  102. @Test
  103. public void testSerializeRpcDataProviderWithRowChanges()
  104. throws IOException {
  105. Grid grid = new Grid();
  106. IndexedContainer container = new IndexedContainer();
  107. grid.setContainerDataSource(container);
  108. container.addItem();
  109. serializeComponent(grid);
  110. }
  111. protected void serializeComponent(Component component) throws IOException {
  112. ObjectOutputStream stream = null;
  113. try {
  114. stream = new ObjectOutputStream(NULLSTREAM);
  115. stream.writeObject(component);
  116. } finally {
  117. if (stream != null) {
  118. stream.close();
  119. }
  120. }
  121. }
  122. }