PageRenderTime 36ms CodeModel.GetById 2ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/report/JaxbParseTest.java

http://sigma-h.googlecode.com/
Java | 111 lines | 82 code | 20 blank | 9 comment | 0 complexity | 82886452c8e0491a3e7368c4b565f4af 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;
  7
  8import org.junit.Assert;
  9import org.junit.Test;
 10import org.sigmah.shared.report.model.*;
 11
 12import javax.xml.bind.JAXBContext;
 13import javax.xml.bind.JAXBException;
 14import javax.xml.bind.Marshaller;
 15import javax.xml.bind.Unmarshaller;
 16import java.io.InputStreamReader;
 17import java.util.Calendar;
 18
 19/**
 20 * @author Alex Bertram
 21 */
 22public class JaxbParseTest {
 23
 24    public Report parseXml(String filename) throws JAXBException {
 25        JAXBContext jc = JAXBContext.newInstance(Report.class.getPackage().getName());
 26        Unmarshaller um = jc.createUnmarshaller();
 27        um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
 28        return (Report) um.unmarshal(new InputStreamReader(
 29                getClass().getResourceAsStream("/report-def/parse-test/" + filename)));
 30    }
 31
 32    public static void dumpXml(Report report) throws JAXBException {
 33        JAXBContext jc = JAXBContext.newInstance(Report.class.getPackage().getName());
 34        Marshaller m = jc.createMarshaller();
 35        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
 36        m.marshal(report, System.out);
 37    }
 38
 39    @Test
 40    public void testReport() throws JAXBException {
 41        Report report = parseXml("report.xml");
 42        Assert.assertEquals("My Title", report.getTitle());
 43        Assert.assertEquals(ReportFrequency.Monthly, report.getFrequency());
 44    }                        
 45
 46    @Test
 47    public void testElements() throws JAXBException {
 48        Report report = parseXml("report-elements.xml");
 49        dumpXml(report);
 50        Assert.assertEquals("element count", 7, report.getElements().size());
 51        Assert.assertTrue("pivotTable", report.getElements().get(0) instanceof PivotTableElement);
 52        Assert.assertTrue("pivotChart", report.getElements().get(1) instanceof PivotChartElement);
 53        Assert.assertTrue("table", report.getElements().get(2) instanceof TableElement);
 54        Assert.assertTrue("map", report.getElements().get(3) instanceof MapElement);
 55        Assert.assertTrue("static", report.getElements().get(4) instanceof StaticElement);
 56        Assert.assertTrue("static", report.getElements().get(5) instanceof StaticElement);
 57        Assert.assertTrue("static", report.getElements().get(6) instanceof StaticElement);
 58    }
 59
 60    @Test
 61    public void testDimensions() throws Throwable {
 62        Report report = parseXml("dimensions.xml");
 63        dumpXml(report);
 64
 65        Assert.assertEquals("element count", 1, report.getElements().size());
 66        Assert.assertTrue("is pivotTable", report.getElements().get(0) instanceof PivotTableElement);
 67
 68        PivotTableElement table = report.getElement(0);
 69        Assert.assertEquals("dimension count", 2, table.getRowDimensions().size());
 70        Assert.assertEquals("indicator type", DimensionType.Indicator, table.getRowDimensions().get(0).getType());
 71        Assert.assertTrue("admin subclass", table.getRowDimensions().get(1) instanceof AdminDimension);
 72        Assert.assertEquals("level is 91", 91, ((AdminDimension) table.getRowDimensions().get(1)).getLevelId() );
 73    }
 74
 75    @Test
 76    public void testMarshallElements() throws Throwable {
 77        Report report = new Report();
 78        report.addElement(new PivotChartElement());
 79        report.addElement(new PivotTableElement());
 80
 81        dumpXml(report);
 82    }
 83
 84    @Test
 85    public void testTable() throws Throwable {
 86        Report report = parseXml("table.xml");
 87        dumpXml(report);
 88
 89        TableElement table = report.getElement(0);
 90
 91        Assert.assertEquals("column count", 8, table.getRootColumn().getChildren().size());
 92//        Assert.assertEquals(TableColumn.SortOrder.Descending, table.getSortBy().get(0).getOrder());
 93//        Assert.assertEquals(TableColumn.SortOrder.Ascending, table.getSortBy().get(1).getOrder());
 94    }
 95
 96    @Test
 97    public void testFilter() throws Throwable {
 98        Report report = parseXml("filter.xml");
 99        dumpXml(report);
100
101        Calendar minDate = Calendar.getInstance();
102        minDate.setTime(report.getFilter().getDateRange().getMinDate());
103        Assert.assertEquals(2008, minDate.get(Calendar.YEAR));
104        Assert.assertEquals(11, minDate.get(Calendar.MONTH));
105        Assert.assertEquals(1, minDate.get(Calendar.DATE));
106
107        Assert.assertTrue(report.getFilter().isRestricted(DimensionType.Indicator));
108        Assert.assertTrue(report.getFilter().getRestrictions(DimensionType.Indicator).contains(21));
109
110    }
111}