PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/sync/SiteUpdateBuilderTest.java

http://sigma-h.googlecode.com/
Java | 76 lines | 55 code | 15 blank | 6 comment | 0 complexity | 6bb5c84a9c45fa5346873ad9ed5d488b 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.sync;
 7
 8import com.google.inject.Inject;
 9import com.google.inject.Provider;
10import org.json.JSONException;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13import org.sigmah.server.dao.OnDataSet;
14import org.sigmah.server.util.logging.LoggingModule;
15import org.sigmah.shared.command.GetSyncRegionUpdates;
16import org.sigmah.shared.command.result.SyncRegionUpdate;
17import org.sigmah.shared.domain.Site;
18import org.sigmah.shared.domain.User;
19import org.sigmah.test.InjectionSupport;
20import org.sigmah.test.MockHibernateModule;
21import org.sigmah.test.Modules;
22
23import javax.persistence.EntityManager;
24import javax.persistence.EntityManagerFactory;
25import java.util.Date;
26
27import static org.hamcrest.CoreMatchers.*;
28import static org.junit.Assert.assertThat;
29import static org.junit.matchers.JUnitMatchers.containsString;
30
31@RunWith(InjectionSupport.class)
32@Modules({
33        MockHibernateModule.class,
34        LoggingModule.class
35})
36public class SiteUpdateBuilderTest {
37
38    @Inject
39    private Provider<SiteUpdateBuilder> builder;
40
41    @Inject
42    private EntityManagerFactory emf;
43
44    @Test
45    @OnDataSet("/dbunit/sites-simple1.db.xml")
46    public void subsequentCallsAreUpToDate() throws JSONException {
47
48        User user = new User();
49        user.setId(1);
50
51        // update one of the sites so we have a realistic nano value type stamp
52        EntityManager em = emf.createEntityManager();
53        em.getTransaction().begin();
54        Site site = em.find(Site.class, 1);
55        site.setComments("I'm slightly new");
56        site.setDateEdited(new Date());
57        em.getTransaction().commit();
58        em.close();
59
60        SyncRegionUpdate initialUpdate = builder.get().build(user, new GetSyncRegionUpdates("sites/1/1", null));
61        assertThat(initialUpdate.isComplete(), equalTo(true));
62        assertThat(initialUpdate.getSql(), not(nullValue()));
63        assertThat(initialUpdate.getSql(), containsString("slightly new"));
64        System.out.println(initialUpdate.getSql());
65
66        // nothing has changed!
67
68        SyncRegionUpdate subsequentUpdate = builder.get().build(user,
69                new GetSyncRegionUpdates("sites/1/1", initialUpdate.getVersion()));
70
71        assertThat(subsequentUpdate.isComplete(), equalTo(true));
72        assertThat(subsequentUpdate.getSql(), nullValue());
73        assertThat(subsequentUpdate.getVersion(), equalTo(initialUpdate.getVersion()));
74    }
75
76}