PageRenderTime 32ms CodeModel.GetById 15ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/sigmah/src/test/java/org/sigmah/client/page/entry/editor/AdminFieldSetTest.java

http://sigma-h.googlecode.com/
Java | 266 lines | 127 code | 89 blank | 50 comment | 0 complexity | 99f2d3e9b9e97cb22cfcc9cebd6f0de1 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.client.page.entry.editor;
  7
  8import com.extjs.gxt.ui.client.store.ListStore;
  9import org.junit.Assert;
 10import org.junit.Test;
 11import org.sigmah.client.mock.DispatcherStub;
 12import org.sigmah.client.mock.DummyData;
 13import org.sigmah.client.page.entry.editor.mock.MockAdminFieldSet;
 14import org.sigmah.shared.command.GetAdminEntities;
 15import org.sigmah.shared.dto.ActivityDTO;
 16import org.sigmah.shared.dto.AdminEntityDTO;
 17import org.sigmah.shared.dto.SchemaDTO;
 18import org.sigmah.shared.dto.SiteDTO;
 19
 20import static org.easymock.EasyMock.*;
 21
 22/**
 23 * @author Alex Bertram (akbertram@gmail.com)
 24 */
 25public class AdminFieldSetTest {
 26
 27
 28
 29    @Test
 30    public void testSetSite() throws Exception {
 31
 32        // Test data
 33        SchemaDTO schema = DummyData.PEAR();
 34        ActivityDTO nfi = schema.getActivityById(91);
 35
 36        // Collaborator: command service
 37        DispatcherStub service = new DispatcherStub();
 38        service.setResult(new GetAdminEntities(1), DummyData.getProvinces());
 39        service.setResult(new GetAdminEntities(2, 100), DummyData.getTerritoires(100));
 40
 41        // Collaborator: view
 42        MockAdminFieldSet fieldSet = new MockAdminFieldSet();
 43
 44        // CLASS Under Test
 45        AdminFieldSetPresenter presenter = new AdminFieldSetPresenter(service, nfi, fieldSet);
 46
 47
 48        // Verify that setting a sites results in the correct values being
 49        // sent to the view, and that the correct combos are enabled
 50        presenter.setSite(DummyData.PEAR_Sites().get(4));
 51
 52        Assert.assertEquals(101, fieldSet.getValue(2).getId());
 53
 54        Assert.assertTrue("province combo is enabled", fieldSet.getEnabled(1));
 55        Assert.assertTrue("territory combo is enabled", fieldSet.getEnabled(2));
 56
 57
 58        // VERIFY that the correct command has been set for combos
 59        ListStore<AdminEntityDTO> store = fieldSet.getStore(2);
 60
 61        store.getLoader().load();
 62
 63        Assert.assertEquals("territory store count", 2, store.getCount());
 64
 65    }
 66
 67    @Test
 68    public void testInitBlank() throws Exception {
 69
 70        // TEST data
 71        SchemaDTO schema = DummyData.PEAR();
 72        ActivityDTO nfi = schema.getActivityById(91);
 73
 74        // Collaborator: Command Service
 75        DispatcherStub service = new DispatcherStub();
 76
 77        // Collaborator: View
 78        MockAdminFieldSet fieldSet = new MockAdminFieldSet();
 79
 80        // CLASS under TEST
 81        AdminFieldSetPresenter presenter = new AdminFieldSetPresenter(service, nfi.getDatabase().getCountry(), fieldSet);
 82        presenter.setSite(new SiteDTO());
 83
 84        // verify that the combos are properly enabled
 85        Assert.assertTrue("province combo is enabled", fieldSet.getEnabled(1));
 86        Assert.assertFalse("territory combo is disabled", fieldSet.getEnabled(2));
 87        Assert.assertFalse("secteur combo is disabled", fieldSet.getEnabled(3));
 88    }
 89
 90    @Test
 91    public void testCascade() throws Exception {
 92
 93
 94        // Test Data
 95        SchemaDTO schema = DummyData.PEAR();
 96        ActivityDTO nfi = schema.getActivityById(91);
 97
 98        // Collaborator: Command Service
 99        DispatcherStub service = new DispatcherStub();
100        service.setResult(new GetAdminEntities(1), DummyData.getProvinces());
101        service.setResult(new GetAdminEntities(2, 100), DummyData.getTerritoires(100));
102
103        // Collaborator: view
104        MockAdminFieldSet fieldSet = new MockAdminFieldSet();
105
106        // CLASS under TEST
107        AdminFieldSetPresenter presenter = new AdminFieldSetPresenter(service, nfi, fieldSet);
108
109
110
111        // VERIFY that root level loader is properly configured
112        presenter.setSite(DummyData.PEAR_Sites().get(5));
113
114        ListStore<AdminEntityDTO> store = fieldSet.getStore(1);
115        store.getLoader().load();
116
117        Assert.assertEquals("province store count", 2, store.getCount());
118
119
120
121        // VERIFY that a change to the province reconfigures the territory loader
122        presenter.onSelectionChanged(1, DummyData.getProvinces().getData().get(0));
123
124        store = fieldSet.getStore(2);
125        store.getLoader().load();
126
127        Assert.assertEquals("territory store count", 2, store.getCount());
128        Assert.assertEquals("Beni", store.getAt(0).getName());
129    }
130
131
132    @Test
133    public void testCascadeReplace() throws Exception {
134
135        // Test data
136        SchemaDTO schema = DummyData.PEAR();
137        ActivityDTO nfi = schema.getActivityById(91);
138
139        // Collaborator: command service
140        DispatcherStub service = new DispatcherStub();
141        service.setResult(new GetAdminEntities(1), DummyData.getProvinces());
142        service.setResult(new GetAdminEntities(2, 200), DummyData.getTerritoires(200));
143
144        // Collaborator: view
145        MockAdminFieldSet fieldSet = new MockAdminFieldSet();
146
147        // CLASS Under Test
148        AdminFieldSetPresenter presenter = new AdminFieldSetPresenter(service, nfi, fieldSet);
149
150        // SETUP selection
151        presenter.setSite(DummyData.PEAR_Sites().get(4));
152
153        // VERIFY that change to province correctly cascades
154        presenter.onSelectionChanged(1, DummyData.getProvinces().getData().get(1));
155
156        Assert.assertNull("territory is null", fieldSet.getValue(2));
157        Assert.assertNull("sector is null", fieldSet.getValue(3));
158        Assert.assertFalse("sector is disabled", fieldSet.getEnabled(3));
159
160        ListStore<AdminEntityDTO> store = fieldSet.getStore(2);
161        store.getLoader().load();
162
163        Assert.assertEquals("territory store count", 2, store.getCount());
164        Assert.assertEquals("correct territory list", "Shabunda", store.getAt(0).getName());
165    }
166
167    @Test
168    public void testBounds() {
169
170        // Test data
171        SchemaDTO schema = DummyData.PEAR();
172        ActivityDTO nfi = schema.getActivityById(91);
173
174        // Collaborator: command service      
175        DispatcherStub service = new DispatcherStub();
176        service.setResult(new GetAdminEntities(1), DummyData.getProvinces());
177        service.setResult(new GetAdminEntities(2, 200), DummyData.getTerritoires(200));
178
179        // Collaborator: view
180        MockAdminFieldSet fieldSet = new MockAdminFieldSet();
181
182        // CLASS Under Test
183        AdminFieldSetPresenter presenter = new AdminFieldSetPresenter(service, nfi, fieldSet);
184
185        // SETUP selection
186        presenter.setSite(DummyData.PEAR_Sites().get(4));
187
188        // VERIFY bounds are correct
189        Assert.assertEquals("bounds", DummyData.Beni.getBounds(), presenter.getBounds());
190        Assert.assertEquals("bounds name", DummyData.Beni.getName(), presenter.getBoundsName());
191    }
192
193
194
195    @Test
196    public void testBoundsChange() {
197
198        // Test data
199        SchemaDTO schema = DummyData.PEAR();
200        ActivityDTO nfi = schema.getActivityById(91);
201
202        // Collaborator: command service
203        DispatcherStub service = new DispatcherStub();
204        service.setResult(new GetAdminEntities(1), DummyData.getProvinces());
205        service.setResult(new GetAdminEntities(2, 200), DummyData.getTerritoires(200));
206
207        // Collaborator: view
208        MockAdminFieldSet fieldSet = new MockAdminFieldSet();
209
210        // CLASS Under Test
211        AdminFieldSetPresenter presenter = new AdminFieldSetPresenter(service, nfi, fieldSet);
212
213        // SETUP selection
214        presenter.setSite(DummyData.PEAR_Sites().get(4));
215
216        // VERIFY that a change to admin level triggers a bounds changed call
217
218        AdminFieldSetPresenter.Listener listener = createMock(AdminFieldSetPresenter.Listener.class);
219        listener.onBoundsChanged(eq(DummyData.Masisi.getName()), eq(DummyData.Masisi.getBounds()));
220        listener.onModified();
221        replay(listener);
222
223        presenter.setListener(listener);
224        fieldSet.setValueAndFire(2, DummyData.Masisi);
225
226        verify(listener);
227    }
228
229
230
231    @Test
232    public void testDirtyChecking() throws Exception {
233
234        // Test data
235        SchemaDTO schema = DummyData.PEAR();
236        ActivityDTO nfi = schema.getActivityById(91);
237
238        // Collaborator: command service
239        DispatcherStub service = new DispatcherStub();
240
241        // Collaborator: view
242        MockAdminFieldSet fieldSet = new MockAdminFieldSet();
243
244        // CLASS Under Test
245        AdminFieldSetPresenter presenter = new AdminFieldSetPresenter(service, nfi, fieldSet);
246
247        // VERIFY isDirty returns false
248        presenter.setSite(DummyData.PEAR_Sites().get(4));
249
250        Assert.assertFalse(presenter.isDirty());
251
252        // VERIFY isDirty returns true after a change
253        fieldSet.setValueAndFire(3, new AdminEntityDTO(3, 999, DummyData.Beni.getId(), "Ruizi"));
254
255        Assert.assertTrue(presenter.isDirty());
256
257        // VERIFY isDirty returns false after we change back
258        fieldSet.setValueAndFire(3, DummyData.Watalina);
259
260        Assert.assertFalse(presenter.isDirty());
261
262    }
263
264
265
266}