PageRenderTime 47ms CodeModel.GetById 27ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

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