/sigmah/src/test/java/org/sigmah/server/endpoint/gwtrpc/SyncIntegrationTest.java

http://sigma-h.googlecode.com/ · Java · 187 lines · 144 code · 37 blank · 6 comment · 7 complexity · c009adfd1030a0fa9312208636dccb8f MD5 · raw file

  1. /*
  2. * All Sigmah code is released under the GNU General Public License v3
  3. * See COPYRIGHT.txt and LICENSE.txt.
  4. */
  5. package org.sigmah.server.endpoint.gwtrpc;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.sigmah.server.dao.OnDataSet;
  9. import org.sigmah.server.sync.TimestampHelper;
  10. import org.sigmah.server.util.BeanMappingModule;
  11. import org.sigmah.server.util.logging.LoggingModule;
  12. import org.sigmah.shared.command.handler.LocalHandlerTestCase;
  13. import org.sigmah.shared.domain.AdminEntity;
  14. import org.sigmah.shared.domain.Location;
  15. import org.sigmah.shared.domain.LocationType;
  16. import org.sigmah.test.InjectionSupport;
  17. import org.sigmah.test.MockHibernateModule;
  18. import org.sigmah.test.Modules;
  19. import javax.persistence.EntityManager;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import java.sql.Timestamp;
  24. import java.util.Date;
  25. import static org.hamcrest.CoreMatchers.equalTo;
  26. import static org.junit.Assert.*;
  27. @RunWith(InjectionSupport.class)
  28. @Modules({
  29. MockHibernateModule.class,
  30. BeanMappingModule.class,
  31. GwtRpcModule.class,
  32. LoggingModule.class
  33. })
  34. public class SyncIntegrationTest extends LocalHandlerTestCase {
  35. @Test
  36. @OnDataSet("/dbunit/sites-simple1.db.xml")
  37. public void run() throws SQLException {
  38. synchronize();
  39. assertThat(queryString("select Name from Indicator where IndicatorId=103"),
  40. equalTo("Nb. of distributions"));
  41. assertThat(queryString("select Name from AdminLevel where AdminLevelId=1"),
  42. equalTo("Province"));
  43. assertThat(queryString("select Name from AdminEntity where AdminEntityId=21"),
  44. equalTo("Irumu"));
  45. assertThat(queryString("select Name from Location where LocationId=7"), equalTo("Shabunda"));
  46. assertThat(queryInt("select value from IndicatorValue where ReportingPeriodId=601 and IndicatorId=6"),
  47. equalTo(35));
  48. assertThat(queryInt("select PartnerId from partnerInDatabase where databaseid=2"), equalTo(1));
  49. assertThat(queryInt("select AttributeGroupId from AttributeGroupInActivity where ActivityId=2"),
  50. equalTo(1));
  51. }
  52. @Test
  53. @OnDataSet("/dbunit/locations.db.xml")
  54. public void locationsAreChunked() throws SQLException {
  55. addLocationsToServerDatabase(220);
  56. synchronize();
  57. assertThat(Integer.valueOf(queryString("select count(*) from Location")), equalTo(220));
  58. // update a location on the server
  59. serverEm.getTransaction().begin();
  60. Location location = (Location) serverEm.createQuery("select l from Location l where l.name = 'Penekusu 26'")
  61. .getSingleResult();
  62. location.setAxe("Motown");
  63. location.setDateEdited(new Date());
  64. serverEm.getTransaction().commit();
  65. newRequest();
  66. synchronize();
  67. assertThat(queryInt("select count(*) from Location where Name='Penekusu 26'"), equalTo(1));
  68. assertThat(queryString("select axe from Location where Name='Penekusu 26'"), equalTo("Motown"));
  69. // now create a new location
  70. Location newLocation = new Location();
  71. newLocation.setName("Bukavu");
  72. newLocation.setDateCreated(new Date());
  73. newLocation.setDateEdited(new Date());
  74. newLocation.setLocationType(serverEm.find(LocationType.class, 1));
  75. serverEm.getTransaction().begin();
  76. serverEm.persist(newLocation);
  77. serverEm.getTransaction().commit();
  78. newRequest();
  79. synchronize();
  80. assertThat(queryString("select name from Location where LocationId = " + newLocation.getId()),
  81. equalTo("Bukavu"));
  82. }
  83. @Test
  84. @OnDataSet("/dbunit/locations.db.xml")
  85. public void timeStampSurvivesRoundTrip() {
  86. EntityManager entityManager = serverEntityManagerFactory.createEntityManager();
  87. entityManager.getTransaction().begin();
  88. Date now = new Date();
  89. Location loc = new Location();
  90. loc.setDateCreated(now);
  91. loc.setDateEdited(now);
  92. loc.setName("Penekusu");
  93. loc.setLocationType(entityManager.find(LocationType.class, 1));
  94. entityManager.persist(loc);
  95. entityManager.getTransaction().commit();
  96. entityManager.clear();
  97. entityManager.getTransaction().begin();
  98. Location loc2 = entityManager.find(Location.class, loc.getId());
  99. String tsString = TimestampHelper.toString(loc2.getDateCreated());
  100. Timestamp ts = TimestampHelper.fromString(tsString);
  101. assertFalse(loc2 == loc);
  102. assertThat((Timestamp)loc2.getDateCreated(), equalTo(ts));
  103. entityManager.getTransaction().commit();
  104. entityManager.clear();
  105. entityManager.getTransaction().begin();
  106. Location loc3 = entityManager.find(Location.class, loc.getId());
  107. assertFalse(ts.after(loc3.getDateCreated()));
  108. assertFalse(ts.before(loc3.getDateCreated()));
  109. entityManager.close();
  110. }
  111. private void addLocationsToServerDatabase(int count) {
  112. EntityManager entityManager = serverEntityManagerFactory.createEntityManager();
  113. entityManager.getTransaction().begin();
  114. Timestamp now = new Timestamp(new Date().getTime());
  115. for(int i=1;i<= count;++i) {
  116. Location loc = new Location();
  117. if(i%3 == 0) {
  118. loc.setDateCreated(new Date( now.getTime() - 100000));
  119. } else {
  120. loc.setDateCreated(now);
  121. }
  122. loc.setDateEdited(now);
  123. loc.setName("Penekusu " + i);
  124. loc.getAdminEntities().add(entityManager.getReference(AdminEntity.class, 2));
  125. loc.getAdminEntities().add(entityManager.getReference(AdminEntity.class, 12));
  126. loc.setLocationType(entityManager.find(LocationType.class, 1));
  127. entityManager.persist(loc);
  128. entityManager.flush();
  129. assertTrue(loc.getId() != 0);
  130. }
  131. entityManager.getTransaction().commit();
  132. entityManager.close();
  133. }
  134. private String queryString(String sql) throws SQLException {
  135. ResultSet rs = querySingleResult(sql);
  136. return rs.getString(1);
  137. }
  138. private Integer queryInt(String sql) throws SQLException {
  139. ResultSet rs = querySingleResult(sql);
  140. return rs.getInt(1);
  141. }
  142. private ResultSet querySingleResult(String sql) throws SQLException {
  143. Statement stmt = localConnection.createStatement();
  144. ResultSet rs = stmt.executeQuery(sql);
  145. if(!rs.next()) {
  146. throw new AssertionError("No rows returned for '" + sql + "'");
  147. }
  148. return rs;
  149. }
  150. }