KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import net.sf.jasperreports.engine.JRExporterParameter;
26 import net.sf.jasperreports.engine.JasperPrint;
27 import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
28
29 import org.springframework.context.support.StaticApplicationContext;
30
31 /**
32  * @author Rob Harrop
33  */

34 public class ExporterParameterTests extends AbstractJasperReportsTests {
35
36     public void testParameterParsing() throws Exception JavaDoc {
37         Map JavaDoc params = new HashMap JavaDoc();
38         params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI", "/foo/bar");
39
40         AbstractJasperReportsView view = new AbstractJasperReportsView() {
41             protected void renderReport(
42                     JasperPrint filledReport, Map JavaDoc model, HttpServletResponse JavaDoc response)
43                     throws Exception JavaDoc {
44
45                 assertEquals("Invalid number of exporter parameters", 1, getConvertedExporterParameters().size());
46
47                 JRExporterParameter key = JRHtmlExporterParameter.IMAGES_URI;
48                 Object JavaDoc value = getConvertedExporterParameters().get(key);
49
50                 assertNotNull("Value not mapped to correct key", value);
51                 assertEquals("Incorrect value for parameter", "/foo/bar", value);
52             }
53         };
54
55         view.setExporterParameters(params);
56         setViewProperties(view);
57         view.render(getModel(), request, response);
58     }
59
60     public void testInvalidClass() throws Exception JavaDoc {
61         Map JavaDoc params = new HashMap JavaDoc();
62         params.put("foo.net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI", "/foo");
63
64         AbstractJasperReportsView view = new JasperReportsHtmlView();
65         setViewProperties(view);
66
67         try {
68             view.setExporterParameters(params);
69             view.convertExporterParameters();
70             fail("Should have thrown IllegalArgumentException");
71         }
72         catch (IllegalArgumentException JavaDoc ex) {
73             // expected
74
}
75     }
76
77     public void testInvalidField() {
78         Map JavaDoc params = new HashMap JavaDoc();
79         params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI_FOO", "/foo");
80
81         AbstractJasperReportsView view = new JasperReportsHtmlView();
82         setViewProperties(view);
83
84         try {
85             view.setExporterParameters(params);
86             view.convertExporterParameters();
87             fail("Should have thrown IllegalArgumentException");
88         }
89         catch (IllegalArgumentException JavaDoc ex) {
90             // expected
91
}
92     }
93
94     public void testInvalidType() {
95         Map JavaDoc params = new HashMap JavaDoc();
96         params.put("java.lang.Boolean.TRUE", "/foo");
97
98         AbstractJasperReportsView view = new JasperReportsHtmlView();
99         setViewProperties(view);
100
101         try {
102             view.setExporterParameters(params);
103             view.convertExporterParameters();
104             fail("Should have thrown IllegalArgumentException");
105         }
106         catch (IllegalArgumentException JavaDoc ex) {
107             // expected
108
}
109     }
110
111     private void setViewProperties(AbstractJasperReportsView view) {
112         view.setUrl("org/springframework/ui/jasperreports/DataSourceReport.jasper");
113         StaticApplicationContext ac = new StaticApplicationContext();
114         ac.addMessage("page", Locale.GERMAN, "MeineSeite");
115         ac.refresh();
116         view.setApplicationContext(ac);
117     }
118
119 }
120
Popular Tags