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.

TicketTests.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.vaadin.data.util.sqlcontainer;
  2. import java.math.BigDecimal;
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import org.easymock.EasyMock;
  8. import org.easymock.IAnswer;
  9. import org.junit.Assert;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import com.vaadin.data.Container.Filter;
  13. import com.vaadin.data.Item;
  14. import com.vaadin.data.util.filter.Compare.Equal;
  15. import com.vaadin.data.util.sqlcontainer.SQLTestsConstants.DB;
  16. import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
  17. import com.vaadin.data.util.sqlcontainer.query.FreeformQuery;
  18. import com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate;
  19. import com.vaadin.data.util.sqlcontainer.query.TableQuery;
  20. import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper;
  21. import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder;
  22. import com.vaadin.ui.Table;
  23. import com.vaadin.ui.Window;
  24. public class TicketTests {
  25. private SimpleJDBCConnectionPool connectionPool;
  26. @Before
  27. public void setUp() throws SQLException {
  28. connectionPool = new SimpleJDBCConnectionPool(
  29. SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL,
  30. SQLTestsConstants.dbUser, SQLTestsConstants.dbPwd, 2, 2);
  31. DataGenerator.addPeopleToDatabase(connectionPool);
  32. }
  33. @Test
  34. public void ticket5867_throwsIllegalState_transactionAlreadyActive()
  35. throws SQLException {
  36. SQLContainer container = new SQLContainer(new FreeformQuery(
  37. "SELECT * FROM people", Arrays.asList("ID"), connectionPool));
  38. Table table = new Table();
  39. Window w = new Window();
  40. w.setContent(table);
  41. table.setContainerDataSource(container);
  42. }
  43. @SuppressWarnings("unchecked")
  44. @Test
  45. public void ticket6136_freeform_ageIs18() throws SQLException {
  46. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  47. Arrays.asList("ID"), connectionPool);
  48. FreeformStatementDelegate delegate = EasyMock
  49. .createMock(FreeformStatementDelegate.class);
  50. final ArrayList<Filter> filters = new ArrayList<Filter>();
  51. delegate.setFilters(null);
  52. EasyMock.expectLastCall().anyTimes();
  53. delegate.setOrderBy(EasyMock.isA(List.class));
  54. EasyMock.expectLastCall().anyTimes();
  55. delegate.setOrderBy(null);
  56. EasyMock.expectLastCall().anyTimes();
  57. delegate.setFilters(EasyMock.isA(List.class));
  58. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  59. @Override
  60. public Object answer() throws Throwable {
  61. List<Filter> orders = (List<Filter>) EasyMock
  62. .getCurrentArguments()[0];
  63. filters.clear();
  64. filters.addAll(orders);
  65. return null;
  66. }
  67. }).anyTimes();
  68. EasyMock.expect(
  69. delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt()))
  70. .andAnswer(new IAnswer<StatementHelper>() {
  71. @Override
  72. public StatementHelper answer() throws Throwable {
  73. Object[] args = EasyMock.getCurrentArguments();
  74. int offset = (Integer) (args[0]);
  75. int limit = (Integer) (args[1]);
  76. return FreeformQueryUtil.getQueryWithFilters(filters,
  77. offset, limit);
  78. }
  79. }).anyTimes();
  80. EasyMock.expect(delegate.getCountStatement())
  81. .andAnswer(new IAnswer<StatementHelper>() {
  82. @Override
  83. public StatementHelper answer() throws Throwable {
  84. StatementHelper sh = new StatementHelper();
  85. StringBuffer query = new StringBuffer(
  86. "SELECT COUNT(*) FROM people");
  87. if (!filters.isEmpty()) {
  88. query.append(QueryBuilder.getWhereStringForFilters(
  89. filters, sh));
  90. }
  91. sh.setQueryString(query.toString());
  92. return sh;
  93. }
  94. }).anyTimes();
  95. EasyMock.replay(delegate);
  96. query.setDelegate(delegate);
  97. SQLContainer container = new SQLContainer(query);
  98. // Ville, Kalle, Pelle, Börje
  99. Assert.assertEquals(4, container.size());
  100. Assert.assertEquals("Börje",
  101. container.getContainerProperty(container.lastItemId(), "NAME")
  102. .getValue());
  103. container.addContainerFilter(new Equal("AGE", 18));
  104. // Pelle
  105. Assert.assertEquals(1, container.size());
  106. Assert.assertEquals("Pelle",
  107. container.getContainerProperty(container.firstItemId(), "NAME")
  108. .getValue());
  109. if (SQLTestsConstants.db == DB.ORACLE) {
  110. Assert.assertEquals(new BigDecimal(18), container
  111. .getContainerProperty(container.firstItemId(), "AGE")
  112. .getValue());
  113. } else {
  114. Assert.assertEquals(
  115. 18,
  116. container.getContainerProperty(container.firstItemId(),
  117. "AGE").getValue());
  118. }
  119. EasyMock.verify(delegate);
  120. }
  121. @Test
  122. public void ticket6136_table_ageIs18() throws SQLException {
  123. TableQuery query = new TableQuery("people", connectionPool,
  124. SQLTestsConstants.sqlGen);
  125. SQLContainer container = new SQLContainer(query);
  126. // Ville, Kalle, Pelle, Börje
  127. Assert.assertEquals(4, container.size());
  128. container.addContainerFilter(new Equal("AGE", 18));
  129. // Pelle
  130. Assert.assertEquals(1, container.size());
  131. Assert.assertEquals("Pelle",
  132. container.getContainerProperty(container.firstItemId(), "NAME")
  133. .getValue());
  134. if (SQLTestsConstants.db == DB.ORACLE) {
  135. Assert.assertEquals(new BigDecimal(18), container
  136. .getContainerProperty(container.firstItemId(), "AGE")
  137. .getValue());
  138. } else {
  139. Assert.assertEquals(
  140. 18,
  141. container.getContainerProperty(container.firstItemId(),
  142. "AGE").getValue());
  143. }
  144. }
  145. @Test
  146. public void ticket7434_getItem_Modified_Changed_Unchanged()
  147. throws SQLException {
  148. SQLContainer container = new SQLContainer(new TableQuery("people",
  149. connectionPool, SQLTestsConstants.sqlGen));
  150. Object id = container.firstItemId();
  151. Item item = container.getItem(id);
  152. String name = (String) item.getItemProperty("NAME").getValue();
  153. // set a different name
  154. item.getItemProperty("NAME").setValue("otherName");
  155. Assert.assertEquals("otherName", item.getItemProperty("NAME")
  156. .getValue());
  157. // access the item and reset the name to its old value
  158. Item item2 = container.getItem(id);
  159. item2.getItemProperty("NAME").setValue(name);
  160. Assert.assertEquals(name, item2.getItemProperty("NAME").getValue());
  161. Item item3 = container.getItem(id);
  162. String name3 = (String) item3.getItemProperty("NAME").getValue();
  163. Assert.assertEquals(name, name3);
  164. }
  165. @Test
  166. public void ticket10032_empty_set_metadata_correctly_handled()
  167. throws SQLException {
  168. // If problem exists will break when method getPropertyIds()
  169. // is called in constructor SQLContainer(QueryDelegate delegate).
  170. SQLContainer container = new SQLContainer(new FreeformQuery(
  171. "SELECT * FROM people WHERE name='does_not_exist'",
  172. Arrays.asList("ID"), connectionPool));
  173. Assert.assertTrue("Got items while expected empty set",
  174. container.size() == 0);
  175. }
  176. }