KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > jasperreports > AbstractJasperReportsViewTests


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.web.servlet.view.jasperreports;
18
19 import java.sql.SQLException JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import javax.sql.DataSource JavaDoc;
26
27 import net.sf.jasperreports.engine.JRDataSource;
28 import net.sf.jasperreports.engine.JRException;
29 import net.sf.jasperreports.engine.JasperReport;
30 import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;
31 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
32 import org.easymock.MockControl;
33
34 import org.springframework.context.ApplicationContextException;
35 import org.springframework.context.support.StaticApplicationContext;
36 import org.springframework.ui.jasperreports.PersonBean;
37
38 /**
39  * @author Rob Harrop
40  * @author Juergen Hoeller
41  */

42 public abstract class AbstractJasperReportsViewTests extends AbstractJasperReportsTests {
43
44     protected abstract AbstractJasperReportsView getViewImplementation();
45
46     protected abstract String JavaDoc getDesiredContentType();
47
48
49     protected AbstractJasperReportsView getView(String JavaDoc url) throws Exception JavaDoc {
50         AbstractJasperReportsView view = getViewImplementation();
51         view.setUrl(url);
52         StaticApplicationContext ac = new StaticApplicationContext();
53         ac.addMessage("page", Locale.GERMAN, "MeineSeite");
54         ac.refresh();
55         view.setApplicationContext(ac);
56         return view;
57     }
58
59     /**
60      * Simple test to see if compiled report succeeds.
61      */

62     public void testCompiledReport() throws Exception JavaDoc {
63         AbstractJasperReportsView view = getView(COMPILED_REPORT);
64         view.render(getModel(), request, response);
65         assertTrue(response.getContentAsByteArray().length > 0);
66         if (view instanceof AbstractJasperReportsSingleFormatView &&
67                 ((AbstractJasperReportsSingleFormatView) view).useWriter()) {
68             String JavaDoc output = response.getContentAsString();
69             assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1);
70         }
71     }
72
73     public void testUncompiledReport() throws Exception JavaDoc {
74         if (!canCompileReport) {
75             return;
76         }
77
78         AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
79         view.render(getModel(), request, response);
80         assertTrue(response.getContentAsByteArray().length > 0);
81     }
82
83     public void testWithInvalidPath() throws Exception JavaDoc {
84         try {
85             getView("foo.jasper");
86             fail("Invalid path should throw ApplicationContextException");
87         }
88         catch (ApplicationContextException ex) {
89             // good!
90
}
91     }
92
93     public void testInvalidExtension() throws Exception JavaDoc {
94         try {
95             getView("foo.bar");
96             fail("Invalid extension should throw IllegalArgumentException");
97         }
98         catch (IllegalArgumentException JavaDoc ex) {
99             // expected
100
}
101     }
102
103     public void testContentType() throws Exception JavaDoc {
104         AbstractJasperReportsView view = getView(COMPILED_REPORT);
105
106         // removed assert because not all views no in advance what the content type will be,
107
// plus the important test is the finished response.
108
//assertEquals("View content type is incorrect", getDesiredContentType(), view.getContentType());
109
view.render(getModel(), request, response);
110         assertEquals("Response content type is incorrect", getDesiredContentType(), response.getContentType());
111     }
112
113     public void testWithoutDatasource() throws Exception JavaDoc {
114         Map JavaDoc model = getModel();
115         model.remove("dataSource");
116         try {
117             AbstractJasperReportsView view = getView(COMPILED_REPORT);
118             view.render(model, request, response);
119             fail("No data source should result in NoDataSourceException");
120         }
121         catch (IllegalArgumentException JavaDoc ex) {
122             // expected
123
}
124     }
125
126     public void testWithCollection() throws Exception JavaDoc {
127         Map JavaDoc model = getModel();
128         model.remove("dataSource");
129         model.put("reportData", getData());
130         AbstractJasperReportsView view = getView(COMPILED_REPORT);
131         view.render(model, request, response);
132         assertTrue(response.getContentAsByteArray().length > 0);
133     }
134
135     public void testWithMultipleCollections() throws Exception JavaDoc {
136         Map JavaDoc model = getModel();
137         model.remove("dataSource");
138         model.put("reportData", getData());
139         model.put("otherData", new LinkedList JavaDoc());
140         try {
141             AbstractJasperReportsView view = getView(COMPILED_REPORT);
142             view.render(model, request, response);
143             fail("No data source should result in NoDataSourceException");
144         }
145         catch (IllegalArgumentException JavaDoc ex) {
146             // expected
147
}
148     }
149
150     public void testWithJRDataSourceProvider() throws Exception JavaDoc {
151         Map JavaDoc model = getModel();
152         model.remove("dataSource");
153         model.put("dataSource", new MockDataSourceProvider(PersonBean.class));
154         AbstractJasperReportsView view = getView(COMPILED_REPORT);
155         view.render(model, request, response);
156         assertTrue(response.getContentAsByteArray().length > 0);
157     }
158
159     public void testWithSpecificCollection() throws Exception JavaDoc {
160         Map JavaDoc model = getModel();
161         model.remove("dataSource");
162         model.put("reportData", getData());
163         model.put("otherData", new LinkedList JavaDoc());
164         AbstractJasperReportsView view = getView(COMPILED_REPORT);
165         view.setReportDataKey("reportData");
166         view.render(model, request, response);
167         assertTrue(response.getContentAsByteArray().length > 0);
168     }
169
170     public void testWithArray() throws Exception JavaDoc {
171         Map JavaDoc model = getModel();
172         model.remove("dataSource");
173         model.put("reportData", getData().toArray());
174         AbstractJasperReportsView view = getView(COMPILED_REPORT);
175         view.render(model, request, response);
176         assertTrue(response.getContentAsByteArray().length > 0);
177     }
178
179     public void testWithMultipleArrays() throws Exception JavaDoc {
180         Map JavaDoc model = getModel();
181         model.remove("dataSource");
182         model.put("reportData", getData().toArray());
183         model.put("otherData", new String JavaDoc[0]);
184         try {
185             AbstractJasperReportsView view = getView(COMPILED_REPORT);
186             view.render(model, request, response);
187             fail("No data source should result in NoDataSourceException");
188         }
189         catch (IllegalArgumentException JavaDoc ex) {
190             // expected
191
}
192     }
193
194     public void testWithSpecificArray() throws Exception JavaDoc {
195         Map JavaDoc model = getModel();
196         model.remove("dataSource");
197         model.put("reportData", getData().toArray());
198         model.put("otherData", new String JavaDoc[0]);
199         AbstractJasperReportsView view = getView(COMPILED_REPORT);
200         view.setReportDataKey("reportData");
201         view.render(model, request, response);
202         assertTrue(response.getContentAsByteArray().length > 0);
203     }
204
205     public void testWithSubReport() throws Exception JavaDoc {
206         if (!canCompileReport) {
207             return;
208         }
209
210         Map JavaDoc model = getModel();
211         model.put("SubReportData", getProductData());
212
213         Properties JavaDoc subReports = new Properties JavaDoc();
214         subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChild.jrxml");
215
216         AbstractJasperReportsView view = getView(SUB_REPORT_PARENT);
217         view.setReportDataKey("dataSource");
218         view.setSubReportUrls(subReports);
219         view.setSubReportDataKeys(new String JavaDoc[]{"SubReportData"});
220         view.initApplicationContext();
221         view.render(model, request, response);
222
223         assertTrue(response.getContentAsByteArray().length > 0);
224     }
225
226     public void testWithNonExistentSubReport() throws Exception JavaDoc {
227         if (!canCompileReport) {
228             return;
229         }
230
231         Map JavaDoc model = getModel();
232         model.put("SubReportData", getProductData());
233
234         Properties JavaDoc subReports = new Properties JavaDoc();
235         subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChildFalse.jrxml");
236
237         AbstractJasperReportsView view = getView(SUB_REPORT_PARENT);
238         view.setReportDataKey("dataSource");
239         view.setSubReportUrls(subReports);
240         view.setSubReportDataKeys(new String JavaDoc[]{"SubReportData"});
241
242         try {
243             view.initApplicationContext();
244             fail("Invalid report URL should throw ApplicationContext Exception");
245         }
246         catch (ApplicationContextException ex) {
247             // success
248
}
249     }
250
251     public void testSubReportWithUnspecifiedParentDataSource() throws Exception JavaDoc {
252         if (!canCompileReport) {
253             return;
254         }
255
256         Map JavaDoc model = getModel();
257         model.put("SubReportData", getProductData());
258
259         Properties JavaDoc subReports = new Properties JavaDoc();
260         subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChildFalse.jrxml");
261
262         AbstractJasperReportsView view = getView(SUB_REPORT_PARENT);
263         view.setSubReportUrls(subReports);
264         view.setSubReportDataKeys(new String JavaDoc[]{"SubReportData"});
265
266         try {
267             view.initApplicationContext();
268             fail("Unspecified reportDataKey should throw exception when subReportDataSources is specified");
269         }
270         catch (ApplicationContextException ex) {
271             // success
272
}
273     }
274
275     public void testContentDisposition() throws Exception JavaDoc {
276         AbstractJasperReportsView view = getView(COMPILED_REPORT);
277         view.render(getModel(), request, response);
278         assertEquals("Invalid content type", "inline", response.getHeader("Content-Disposition"));
279
280     }
281
282     public void testOverrideContentDisposition() throws Exception JavaDoc {
283         Properties JavaDoc headers = new Properties JavaDoc();
284         String JavaDoc cd = "attachment";
285         headers.setProperty("Content-Disposition", cd);
286
287         AbstractJasperReportsView view = getView(COMPILED_REPORT);
288         view.setHeaders(headers);
289         view.render(getModel(), request, response);
290         assertEquals("Invalid content type", cd, response.getHeader("Content-Disposition"));
291     }
292
293     public void testSetCustomHeaders() throws Exception JavaDoc {
294         Properties JavaDoc headers = new Properties JavaDoc();
295
296         String JavaDoc key = "foo";
297         String JavaDoc value = "bar";
298
299         headers.setProperty(key, value);
300
301         AbstractJasperReportsView view = getView(COMPILED_REPORT);
302         view.setHeaders(headers);
303         view.render(getModel(), request, response);
304
305         assertNotNull("Header not present", response.getHeader(key));
306         assertEquals("Invalid header value", value, response.getHeader(key));
307
308     }
309
310     public void testWithSqlDataSource() throws Exception JavaDoc {
311         if (!canCompileReport) {
312             return;
313         }
314
315         AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
316         view.setJdbcDataSource(getMockSqlDataSource());
317
318         Map JavaDoc model = getModel();
319         model.remove("dataSource");
320
321         try {
322             view.render(model, request, response);
323             fail("DataSource was not used as report DataSource");
324         }
325         catch (SQLException JavaDoc ex) {
326             // expected
327
}
328     }
329
330     public void testJRDataSourceOverridesDataSource() throws Exception JavaDoc {
331         if (!canCompileReport) {
332             return;
333         }
334
335         AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
336         view.setJdbcDataSource(getMockSqlDataSource());
337
338         try {
339             view.render(getModel(), request, response);
340         }
341         catch (SQLException JavaDoc ex) {
342             fail("javax.sql.DataSource was used when JRDataSource should have overriden it");
343         }
344     }
345
346     private DataSource JavaDoc getMockSqlDataSource() throws SQLException JavaDoc {
347         MockControl ctl = MockControl.createControl(DataSource JavaDoc.class);
348         DataSource JavaDoc ds = (DataSource JavaDoc) ctl.getMock();
349         ds.getConnection();
350         ctl.setThrowable(new SQLException JavaDoc());
351         ctl.replay();
352         return ds;
353     }
354
355
356     private class MockDataSourceProvider extends JRAbstractBeanDataSourceProvider {
357
358         public MockDataSourceProvider(Class JavaDoc clazz) {
359             super(clazz);
360         }
361
362         public JRDataSource create(JasperReport jasperReport) throws JRException {
363             return new JRBeanCollectionDataSource(getData());
364         }
365
366         public void dispose(JRDataSource jrDataSource) throws JRException {
367
368         }
369     }
370 }
371
Popular Tags