KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import net.sf.jasperreports.engine.JasperPrint;
26
27 /**
28  * @author Rob Harrop
29  */

30 public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsViewTests {
31
32     protected void extendModel(Map JavaDoc model) {
33         model.put(getDiscriminatorKey(), "csv");
34     }
35
36     public void testSimpleHtmlRender() throws Exception JavaDoc {
37         if (!canCompileReport) {
38             return;
39         }
40
41         AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
42
43         Map JavaDoc model = getBaseModel();
44         model.put(getDiscriminatorKey(), "html");
45
46         view.render(model, request, response);
47
48         assertEquals("Invalid content type", "text/html", response.getContentType());
49     }
50
51     public void testOverrideContentDisposition() throws Exception JavaDoc {
52         if (!canCompileReport) {
53             return;
54         }
55
56         AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
57
58         Map JavaDoc model = getBaseModel();
59         model.put(getDiscriminatorKey(), "csv");
60
61         String JavaDoc headerValue = "inline; filename=foo.txt";
62
63         Properties JavaDoc mappings = new Properties JavaDoc();
64         mappings.put("csv", headerValue);
65
66         ((JasperReportsMultiFormatView) view).setContentDispositionMappings(mappings);
67
68         view.render(model, request, response);
69
70         assertEquals("Invalid Content-Disposition header value", headerValue,
71                 response.getHeader("Content-Disposition"));
72     }
73
74     public void testExporterParametersAreCarriedAcross() throws Exception JavaDoc {
75         if (!canCompileReport) {
76             return;
77         }
78
79         JasperReportsMultiFormatView view = (JasperReportsMultiFormatView) getView(UNCOMPILED_REPORT);
80
81         Properties JavaDoc mappings = new Properties JavaDoc();
82         mappings.put("test", ExporterParameterTestView.class.getName());
83
84         Map JavaDoc exporterParameters = new HashMap JavaDoc();
85
86         // test view class performs the assertions - robh
87
exporterParameters.put(ExporterParameterTestView.TEST_PARAM, "foo");
88
89         view.setExporterParameters(exporterParameters);
90         view.setFormatMappings(mappings);
91         view.initApplicationContext();
92
93         Map JavaDoc model = getBaseModel();
94         model.put(getDiscriminatorKey(), "test");
95
96         view.render(model, request, response);
97     }
98
99     protected String JavaDoc getDiscriminatorKey() {
100         return "format";
101     }
102
103     protected AbstractJasperReportsView getViewImplementation() {
104         return new JasperReportsMultiFormatView();
105     }
106
107     protected String JavaDoc getDesiredContentType() {
108         return "text/csv";
109     }
110
111     private Map JavaDoc getBaseModel() {
112         Map JavaDoc model = new HashMap JavaDoc();
113         model.put("ReportTitle", "Foo");
114         model.put("dataSource", getData());
115         return model;
116     }
117
118
119     public static class ExporterParameterTestView extends AbstractJasperReportsView {
120
121         public static final String JavaDoc TEST_PARAM = "net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI";
122
123         protected void renderReport(JasperPrint filledReport, Map JavaDoc parameters, HttpServletResponse JavaDoc response) {
124             assertNotNull("Exporter parameters are null", getExporterParameters());
125             assertEquals("Incorrect number of exporter parameters", 1, getExporterParameters().size());
126         }
127     }
128
129 }
130
Popular Tags