1 16 17 package org.springframework.web.servlet.view.jasperreports; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import javax.servlet.http.HttpServletResponse ; 24 25 import net.sf.jasperreports.engine.JasperPrint; 26 27 30 public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsViewTests { 31 32 protected void extendModel(Map model) { 33 model.put(getDiscriminatorKey(), "csv"); 34 } 35 36 public void testSimpleHtmlRender() throws Exception { 37 if (!canCompileReport) { 38 return; 39 } 40 41 AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); 42 43 Map 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 { 52 if (!canCompileReport) { 53 return; 54 } 55 56 AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); 57 58 Map model = getBaseModel(); 59 model.put(getDiscriminatorKey(), "csv"); 60 61 String headerValue = "inline; filename=foo.txt"; 62 63 Properties mappings = new Properties (); 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 { 75 if (!canCompileReport) { 76 return; 77 } 78 79 JasperReportsMultiFormatView view = (JasperReportsMultiFormatView) getView(UNCOMPILED_REPORT); 80 81 Properties mappings = new Properties (); 82 mappings.put("test", ExporterParameterTestView.class.getName()); 83 84 Map exporterParameters = new HashMap (); 85 86 exporterParameters.put(ExporterParameterTestView.TEST_PARAM, "foo"); 88 89 view.setExporterParameters(exporterParameters); 90 view.setFormatMappings(mappings); 91 view.initApplicationContext(); 92 93 Map model = getBaseModel(); 94 model.put(getDiscriminatorKey(), "test"); 95 96 view.render(model, request, response); 97 } 98 99 protected String getDiscriminatorKey() { 100 return "format"; 101 } 102 103 protected AbstractJasperReportsView getViewImplementation() { 104 return new JasperReportsMultiFormatView(); 105 } 106 107 protected String getDesiredContentType() { 108 return "text/csv"; 109 } 110 111 private Map getBaseModel() { 112 Map model = new HashMap (); 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 TEST_PARAM = "net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI"; 122 123 protected void renderReport(JasperPrint filledReport, Map parameters, HttpServletResponse 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 |