1 16 17 package org.springframework.web.servlet.view.jasperreports; 18 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 23 import javax.servlet.http.HttpServletResponse ; 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 34 public class ExporterParameterTests extends AbstractJasperReportsTests { 35 36 public void testParameterParsing() throws Exception { 37 Map params = new HashMap (); 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 model, HttpServletResponse response) 43 throws Exception { 44 45 assertEquals("Invalid number of exporter parameters", 1, getConvertedExporterParameters().size()); 46 47 JRExporterParameter key = JRHtmlExporterParameter.IMAGES_URI; 48 Object 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 { 61 Map params = new HashMap (); 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 ex) { 73 } 75 } 76 77 public void testInvalidField() { 78 Map params = new HashMap (); 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 ex) { 90 } 92 } 93 94 public void testInvalidType() { 95 Map params = new HashMap (); 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 ex) { 107 } 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 |