KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ui > jasperreports > JasperReportsUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.ui.jasperreports;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28
29 import junit.framework.TestCase;
30 import net.sf.jasperreports.engine.JRDataSource;
31 import net.sf.jasperreports.engine.JRParameter;
32 import net.sf.jasperreports.engine.JasperFillManager;
33 import net.sf.jasperreports.engine.JasperPrint;
34 import net.sf.jasperreports.engine.JasperReport;
35 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
36 import net.sf.jasperreports.engine.export.JRHtmlExporter;
37 import net.sf.jasperreports.engine.export.JRPdfExporter;
38 import net.sf.jasperreports.engine.util.JRLoader;
39 import org.apache.poi.hssf.usermodel.HSSFCell;
40 import org.apache.poi.hssf.usermodel.HSSFRow;
41 import org.apache.poi.hssf.usermodel.HSSFSheet;
42 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
43
44 import org.springframework.core.io.ClassPathResource;
45
46 /**
47  * @author Rob Harrop
48  * @author Juergen Hoeller
49  * @since 18.11.2004
50  */

51 public class JasperReportsUtilsTests extends TestCase {
52
53     public void testRenderAsCsvWithDataSource() throws Exception JavaDoc {
54         StringWriter JavaDoc writer = new StringWriter JavaDoc();
55         JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getDataSource(), writer);
56         String JavaDoc output = writer.getBuffer().toString();
57         assertCsvOutputCorrect(output);
58     }
59     
60     public void testRenderAsCsvWithCollection() throws Exception JavaDoc {
61         StringWriter JavaDoc writer = new StringWriter JavaDoc();
62         JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getData(), writer);
63         String JavaDoc output = writer.getBuffer().toString();
64         assertCsvOutputCorrect(output);
65     }
66     
67     public void testRenderAsHtmlWithDataSource() throws Exception JavaDoc {
68         StringWriter JavaDoc writer = new StringWriter JavaDoc();
69         JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getDataSource(), writer);
70         String JavaDoc output = writer.getBuffer().toString();
71         assertHtmlOutputCorrect(output);
72     }
73     
74     public void testRenderAsHtmlWithCollection() throws Exception JavaDoc {
75         StringWriter JavaDoc writer = new StringWriter JavaDoc();
76         JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getData(), writer);
77         String JavaDoc output = writer.getBuffer().toString();
78         assertHtmlOutputCorrect(output);
79     }
80     
81     public void testRenderAsPdfWithDataSource() throws Exception JavaDoc {
82         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
83         JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getDataSource(), os);
84         byte[] output = os.toByteArray();
85         assertPdfOutputCorrect(output);
86     }
87     
88     public void testRenderAsPdfWithCollection() throws Exception JavaDoc {
89         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
90         JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getData(), os);
91         byte[] output = os.toByteArray();
92         assertPdfOutputCorrect(output);
93     }
94     
95     public void testRenderAsXlsWithDataSource() throws Exception JavaDoc {
96         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
97         JasperReportsUtils.renderAsXls(getReport(), getParameters(), getDataSource(), os);
98         byte[] output = os.toByteArray();
99         assertXlsOutputCorrect(output);
100     }
101     
102     public void testRenderAsXlsWithCollection() throws Exception JavaDoc {
103         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
104         JasperReportsUtils.renderAsXls(getReport(), getParameters(), getData(), os);
105         byte[] output = os.toByteArray();
106         assertXlsOutputCorrect(output);
107     }
108     
109     public void testRenderWithWriter() throws Exception JavaDoc {
110         StringWriter JavaDoc writer = new StringWriter JavaDoc();
111         JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource());
112         JasperReportsUtils.render(new JRHtmlExporter(), print, writer);
113         String JavaDoc output = writer.getBuffer().toString();
114         assertHtmlOutputCorrect(output);
115     }
116     
117     public void testRenderWithOutputStream() throws Exception JavaDoc {
118         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
119         JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource());
120         JasperReportsUtils.render(new JRPdfExporter(), print, os);
121         byte[] output = os.toByteArray();
122         assertPdfOutputCorrect(output);
123     }
124     
125     private void assertCsvOutputCorrect(String JavaDoc output) {
126         assertTrue("Output length should be greater than 0", (output.length() > 0));
127         assertTrue("Output should start with Dear Lord!", output.startsWith("Dear Lord!"));
128         assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1);
129     }
130     
131     private void assertHtmlOutputCorrect(String JavaDoc output) {
132         assertTrue("Output length should be greater than 0", (output.length() > 0));
133         assertTrue("Output should contain <html>", output.indexOf("<html>") > -1);
134         assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1);
135     }
136     
137     private void assertPdfOutputCorrect(byte[] output) throws Exception JavaDoc {
138         assertTrue("Output length should be greater than 0", (output.length > 0));
139         
140         String JavaDoc translated = new String JavaDoc(output, "US-ASCII");
141         assertTrue("Output should start with %PDF", translated.startsWith("%PDF"));
142     }
143     
144     private void assertXlsOutputCorrect(byte[] output) throws Exception JavaDoc {
145         HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream JavaDoc(output));
146         HSSFSheet sheet = workbook.getSheetAt(0);
147         assertNotNull("Sheet should not be null", sheet);
148         HSSFRow row = sheet.getRow(3);
149         HSSFCell cell = row.getCell((short)1);
150         assertNotNull("Cell should not be null", cell);
151         assertEquals("Cell content should be Dear Lord!", "Dear Lord!", cell.getStringCellValue());
152     }
153     
154     private JasperReport getReport() throws Exception JavaDoc {
155         ClassPathResource resource = new ClassPathResource("DataSourceReport.jasper", getClass());
156         return (JasperReport) JRLoader.loadObject(resource.getInputStream());
157     }
158     
159     private Map JavaDoc getParameters() {
160         Map JavaDoc model = new HashMap JavaDoc();
161         model.put("ReportTitle", "Dear Lord!");
162         model.put(JRParameter.REPORT_LOCALE, Locale.GERMAN);
163         model.put(JRParameter.REPORT_RESOURCE_BUNDLE,
164                 ResourceBundle.getBundle("org/springframework/ui/jasperreports/messages", Locale.GERMAN));
165         return model;
166     }
167
168     private JRDataSource getDataSource() {
169         return new JRBeanCollectionDataSource(getData());
170     }
171
172     private List JavaDoc getData() {
173         List JavaDoc list = new ArrayList JavaDoc();
174         for (int x = 0; x < 10; x++) {
175             PersonBean bean = new PersonBean();
176             bean.setId(x);
177             bean.setName("Rob Harrop");
178             bean.setStreet("foo");
179             list.add(bean);
180         }
181         return list;
182     }
183
184 }
185
Popular Tags