View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   *
27   *
28   */
29  
30  package ar.com.fdvs.dj.test;
31  
32  
33  import ar.com.fdvs.dj.core.DJServletHelper;
34  import ar.com.fdvs.dj.core.DynamicJasperHelper;
35  import ar.com.fdvs.dj.domain.DJCalculation;
36  import ar.com.fdvs.dj.domain.DJValueFormatter;
37  import ar.com.fdvs.dj.domain.DynamicReport;
38  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
39  import ar.com.fdvs.dj.output.ReportWriter;
40  import net.sf.jasperreports.engine.JRDataSource;
41  import net.sf.jasperreports.engine.JasperFillManager;
42  import net.sf.jasperreports.view.JasperDesignViewer;
43  import net.sf.jasperreports.view.JasperViewer;
44  import org.springframework.mock.web.MockHttpServletRequest;
45  import org.springframework.mock.web.MockHttpServletResponse;
46  
47  import javax.servlet.http.HttpServletRequest;
48  import java.io.ByteArrayOutputStream;
49  import java.io.InputStream;
50  import java.util.Date;
51  import java.util.HashMap;
52  import java.util.Map;
53  
54  public class HtmlExportReportTest extends FastReportTest {
55  
56      @Override
57      public void testReport() throws Exception {
58          dr = buildReport();
59  
60          /**
61           * Get a JRDataSource implementation
62           */
63          JRDataSource ds = getDataSource();
64  
65  
66          /**
67           * Creates the JasperReport object, we pass as a Parameter
68           * the DynamicReport, a new ClassicLayoutManager instance (this
69           * one does the magic) and the JRDataSource
70           */
71          jr = DynamicJasperHelper.generateJasperReport(dr, getLayoutManager(), params);
72  
73          /**
74           * Creates the JasperPrint object, we pass as a Parameter
75           * the JasperReport object, and the JRDataSource
76           */
77          log.debug("Filling the report");
78          if (ds != null)
79              jp = JasperFillManager.fillReport(jr, params, ds);
80          else
81              jp = JasperFillManager.fillReport(jr, params);
82  
83          log.debug("Filling done!");
84          log.debug("Exporting the report (pdf, xls, etc)");
85  
86          //Exporting directly to a Response
87          MockHttpServletRequest request = new MockHttpServletRequest();
88          MockHttpServletResponse response = new MockHttpServletResponse();
89          DJServletHelper.exportToHtml(request, response,"/images", jp, null);
90  
91          //Exporting and returning an InputStream
92          MockHttpServletRequest request2 = new MockHttpServletRequest();
93          DJServletHelper.setPageTreshold(0);
94          InputStream is = DJServletHelper.exportToHtml(request2, "/images", jp, null);
95          ByteArrayOutputStream out = new ByteArrayOutputStream();
96          ReportWriter.copyStreams(is,out);
97  
98          //Comparing both generated html. They should be the same
99          assertEquals(response.getContentAsString(),new String(out.toByteArray()));
100 
101 
102         log.debug("test finished");
103 
104     }
105 
106     public void testReport2() throws Exception {
107         dr = buildReport();
108 
109         /**
110          * Creates the JasperReport object, we pass as a Parameter
111          * the DynamicReport, a new ClassicLayoutManager instance (this
112          * one does the magic) and the JRDataSource
113          */
114         jr = DynamicJasperHelper.generateJasperReport(dr, getLayoutManager(), params);
115 
116         log.debug("Filling done!");
117         log.debug("Exporting the report (pdf, xls, etc)");
118 
119         //Exporting directly to a Response
120         MockHttpServletRequest request = new MockHttpServletRequest();
121         MockHttpServletResponse response = new MockHttpServletResponse();
122         DJServletHelper.exportToHtml(request, response,"/images", dr, getLayoutManager(),getDataSource(),new HashMap(),null);
123 
124         //Exporting and returning an InputStream
125         MockHttpServletRequest request2 = new MockHttpServletRequest();
126         InputStream is = DJServletHelper.exportToHtml(request2,"/images", dr, getLayoutManager(),getDataSource(),new HashMap(),null);
127         ByteArrayOutputStream out = new ByteArrayOutputStream();
128         ReportWriter.copyStreams(is,out);
129 
130         //Comparing both generated html. They should be the same
131         assertEquals(response.getContentAsString(),new String(out.toByteArray()));
132 
133 
134         log.debug("test finished");
135 
136     }
137 
138 
139 	public static void main(String[] args) throws Exception {
140 		HtmlExportReportTest test = new HtmlExportReportTest();
141 		test.testReport();
142 		test.testReport2();
143 	}
144 
145 }