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