PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/report/generator/TableGeneratorTest.java

http://sigma-h.googlecode.com/
Java | 141 lines | 106 code | 28 blank | 7 comment | 0 complexity | dfa38231c938a96b14b8bff91019259f 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.report.generator;
  7
  8import junit.framework.Assert;
  9import org.junit.Before;
 10import org.junit.Test;
 11import org.sigmah.server.dao.IndicatorDAO;
 12import org.sigmah.server.dao.PivotDAO;
 13import org.sigmah.server.report.generator.map.MockBaseMapDAO;
 14import org.sigmah.shared.dao.*;
 15import org.sigmah.shared.domain.User;
 16import org.sigmah.shared.report.content.BubbleMapMarker;
 17import org.sigmah.shared.report.content.MapContent;
 18import org.sigmah.shared.report.content.TableData;
 19import org.sigmah.shared.report.model.BubbleMapLayer;
 20import org.sigmah.shared.report.model.MapElement;
 21import org.sigmah.shared.report.model.TableColumn;
 22import org.sigmah.shared.report.model.TableElement;
 23
 24import java.sql.ResultSet;
 25import java.sql.SQLException;
 26import java.util.Collections;
 27import java.util.List;
 28import java.util.Map;
 29
 30import static org.easymock.EasyMock.*;
 31
 32/**
 33 * @author Alex Bertram
 34 */
 35public class TableGeneratorTest {
 36    private User user;
 37
 38    @Before
 39    public void setUp() {
 40        user = new User();
 41        user.setName("Alex");
 42        user.setEmail("akbertra@mgail.com");
 43        user.setLocale("fr");
 44    }
 45
 46    @Test
 47    public void testTableGenerator() {
 48
 49        TableElement table = new TableElement();
 50        table.addColumn(new TableColumn("Location", "location.name"));
 51
 52        TableGenerator gtor = new TableGenerator(createPivotDAO(), new MockSiteTableDAO(), createIndicator(), null);
 53        gtor.generate(user, table, null, null);
 54
 55        Assert.assertNotNull("content is set", table.getContent());
 56
 57        TableData data = table.getContent().getData();
 58        List<TableData.Row> rows = data.getRows();
 59        Assert.assertEquals("row count", 1, rows.size());
 60
 61        TableData.Row row = rows.get(0);
 62        Assert.assertEquals("column data", "tampa bay", row.values[0]);
 63    }
 64
 65
 66    @Test
 67    public void testMap() {
 68
 69        TableElement table = new TableElement();
 70        table.addColumn(new TableColumn("Index", "map"));
 71        table.addColumn(new TableColumn("Site", "location.name"));
 72
 73        MapElement map = new MapElement();
 74        map.setBaseMapId("map1");
 75        BubbleMapLayer layer = new BubbleMapLayer();
 76        layer.setNumbering(BubbleMapLayer.NumberingType.ArabicNumerals);
 77        map.addLayer(layer);
 78        table.setMap(map);
 79
 80        TableGenerator gtor = new TableGenerator(createPivotDAO(), new MockSiteTableDAO(), createIndicator(),
 81                new MapGenerator(createPivotDAO(), new MockSiteTableDAO(), new MockBaseMapDAO()));
 82        gtor.generate(user, table, null, null);
 83
 84        MapContent mapContent = map.getContent();
 85        Assert.assertNotNull("map content", mapContent);
 86        Assert.assertEquals("marker count", 1, mapContent.getMarkers().size());
 87        Assert.assertEquals("label on marker", "1", ((BubbleMapMarker) mapContent.getMarkers().get(0)).getLabel());
 88
 89        Map<Integer, String> siteLabels = mapContent.siteLabelMap();
 90        Assert.assertEquals("site id in map", "1", siteLabels.get(1));
 91
 92        TableData.Row row = table.getContent().getData().getRows().get(0);
 93        Assert.assertEquals("label on row", "1", row.values[0]);
 94    }
 95
 96    private IndicatorDAO createIndicator() {
 97        IndicatorDAO indicatorDAO = createNiceMock(IndicatorDAO.class);
 98        replay(indicatorDAO);
 99        return indicatorDAO;
100    }
101
102    private PivotDAO createPivotDAO() {
103        PivotDAO pivotDAO = createNiceMock(PivotDAO.class);
104        replay(pivotDAO);
105        return pivotDAO;
106    }
107
108    private class MockSiteTableDAO implements SiteTableDAO {
109        @Override
110        public <RowT> List<RowT> query(User user, Filter filter, List<SiteOrder> orderings, SiteProjectionBinder<RowT> binder, int retrieve, int offset, int limit)  {
111            try {
112                final ResultSet rs = createNiceMock(ResultSet.class);
113                expect(rs.getInt(SiteTableColumn.id.index())).andReturn(1);
114                expect(rs.getObject(SiteTableColumn.id.index())).andReturn(1);
115                expect(rs.getObject(SiteTableColumn.location_name.index())).andReturn("tampa bay");
116                expect(rs.getDouble(SiteTableColumn.x.index())).andReturn(28.4);
117                expect(rs.getObject(SiteTableColumn.x.index())).andReturn(28.4);
118                expect(rs.getDouble(SiteTableColumn.y.index())).andReturn(1.2);
119                expect(rs.getObject(SiteTableColumn.y.index())).andReturn(1.2);
120                expect(rs.wasNull()).andReturn(false).anyTimes();
121                replay(rs);
122
123                return Collections.singletonList(binder.newInstance(new String[0], rs));
124            } catch (SQLException e) {
125                throw new AssertionError(e);
126            }
127        }
128
129        @Override
130        public int queryCount(User user, Filter filter) {
131            return 0;
132        }
133
134        @Override
135        public int queryPageNumber(User user, Filter filter, List<SiteOrder> orderings, int pageSize, int siteId) {
136            return 0;
137        }
138    }
139
140
141}