17 changed files with 0 additions and 1628 deletions
@ -1,249 +0,0 @@
@@ -1,249 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.ui.jasperreports; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.StringWriter; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
import java.util.ResourceBundle; |
||||
|
||||
import junit.framework.TestCase; |
||||
import net.sf.jasperreports.engine.JRDataSource; |
||||
import net.sf.jasperreports.engine.JRExporterParameter; |
||||
import net.sf.jasperreports.engine.JRParameter; |
||||
import net.sf.jasperreports.engine.JasperFillManager; |
||||
import net.sf.jasperreports.engine.JasperPrint; |
||||
import net.sf.jasperreports.engine.JasperReport; |
||||
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; |
||||
import net.sf.jasperreports.engine.export.JRCsvExporterParameter; |
||||
import net.sf.jasperreports.engine.export.JRExportProgressMonitor; |
||||
import net.sf.jasperreports.engine.export.JRHtmlExporter; |
||||
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; |
||||
import net.sf.jasperreports.engine.export.JRPdfExporter; |
||||
import net.sf.jasperreports.engine.export.JRPdfExporterParameter; |
||||
import net.sf.jasperreports.engine.export.JRXlsExporterParameter; |
||||
import net.sf.jasperreports.engine.util.JRLoader; |
||||
import org.apache.poi.hssf.usermodel.HSSFCell; |
||||
import org.apache.poi.hssf.usermodel.HSSFRow; |
||||
import org.apache.poi.hssf.usermodel.HSSFSheet; |
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
||||
|
||||
import org.springframework.core.io.ClassPathResource; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
* @author Juergen Hoeller |
||||
* @since 18.11.2004 |
||||
*/ |
||||
public class JasperReportsUtilsTests extends TestCase { |
||||
|
||||
public void testRenderAsCsvWithDataSource() throws Exception { |
||||
StringWriter writer = new StringWriter(); |
||||
JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getDataSource(), writer); |
||||
String output = writer.getBuffer().toString(); |
||||
assertCsvOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsCsvWithCollection() throws Exception { |
||||
StringWriter writer = new StringWriter(); |
||||
JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getData(), writer); |
||||
String output = writer.getBuffer().toString(); |
||||
assertCsvOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsCsvWithExporterParameters() throws Exception { |
||||
StringWriter writer = new StringWriter(); |
||||
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>(); |
||||
exporterParameters.put(JRCsvExporterParameter.FIELD_DELIMITER, "~"); |
||||
JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getData(), writer, exporterParameters); |
||||
String output = writer.getBuffer().toString(); |
||||
assertCsvOutputCorrect(output); |
||||
assertTrue("Delimiter is incorrect", output.contains("~")); |
||||
} |
||||
|
||||
public void testRenderAsHtmlWithDataSource() throws Exception { |
||||
StringWriter writer = new StringWriter(); |
||||
JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getDataSource(), writer); |
||||
String output = writer.getBuffer().toString(); |
||||
assertHtmlOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsHtmlWithCollection() throws Exception { |
||||
StringWriter writer = new StringWriter(); |
||||
JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getData(), writer); |
||||
String output = writer.getBuffer().toString(); |
||||
assertHtmlOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsHtmlWithExporterParameters() throws Exception { |
||||
StringWriter writer = new StringWriter(); |
||||
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>(); |
||||
String uri = "/my/uri"; |
||||
exporterParameters.put(JRHtmlExporterParameter.IMAGES_URI, uri); |
||||
JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getData(), writer, exporterParameters); |
||||
String output = writer.getBuffer().toString(); |
||||
assertHtmlOutputCorrect(output); |
||||
assertTrue("URI not included", output.contains(uri)); |
||||
} |
||||
|
||||
public void testRenderAsPdfWithDataSource() throws Exception { |
||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||||
JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getDataSource(), os); |
||||
byte[] output = os.toByteArray(); |
||||
assertPdfOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsPdfWithCollection() throws Exception { |
||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||||
JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getData(), os); |
||||
byte[] output = os.toByteArray(); |
||||
assertPdfOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsPdfWithExporterParameters() throws Exception { |
||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||||
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>(); |
||||
exporterParameters.put(JRPdfExporterParameter.PDF_VERSION, JRPdfExporterParameter.PDF_VERSION_1_6); |
||||
JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getData(), os, exporterParameters); |
||||
byte[] output = os.toByteArray(); |
||||
assertPdfOutputCorrect(output); |
||||
assertTrue(new String(output).contains("PDF-1.6")); |
||||
} |
||||
|
||||
public void testRenderAsXlsWithDataSource() throws Exception { |
||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||||
JasperReportsUtils.renderAsXls(getReport(), getParameters(), getDataSource(), os); |
||||
byte[] output = os.toByteArray(); |
||||
assertXlsOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsXlsWithCollection() throws Exception { |
||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||||
JasperReportsUtils.renderAsXls(getReport(), getParameters(), getData(), os); |
||||
byte[] output = os.toByteArray(); |
||||
assertXlsOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderAsXlsWithExporterParameters() throws Exception { |
||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||||
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>(); |
||||
|
||||
SimpleProgressMonitor monitor = new SimpleProgressMonitor(); |
||||
exporterParameters.put(JRXlsExporterParameter.PROGRESS_MONITOR, monitor); |
||||
|
||||
JasperReportsUtils.renderAsXls(getReport(), getParameters(), getData(), os, exporterParameters); |
||||
byte[] output = os.toByteArray(); |
||||
assertXlsOutputCorrect(output); |
||||
assertTrue(monitor.isInvoked()); |
||||
} |
||||
|
||||
public void testRenderWithWriter() throws Exception { |
||||
StringWriter writer = new StringWriter(); |
||||
JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource()); |
||||
JasperReportsUtils.render(new JRHtmlExporter(), print, writer); |
||||
String output = writer.getBuffer().toString(); |
||||
assertHtmlOutputCorrect(output); |
||||
} |
||||
|
||||
public void testRenderWithOutputStream() throws Exception { |
||||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||||
JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource()); |
||||
JasperReportsUtils.render(new JRPdfExporter(), print, os); |
||||
byte[] output = os.toByteArray(); |
||||
assertPdfOutputCorrect(output); |
||||
} |
||||
|
||||
private void assertCsvOutputCorrect(String output) { |
||||
assertTrue("Output length should be greater than 0", (output.length() > 0)); |
||||
assertTrue("Output should start with Dear Lord!", output.startsWith("Dear Lord!")); |
||||
assertTrue("Output should contain 'MeineSeite'", output.contains("MeineSeite")); |
||||
} |
||||
|
||||
private void assertHtmlOutputCorrect(String output) { |
||||
assertTrue("Output length should be greater than 0", (output.length() > 0)); |
||||
assertTrue("Output should contain <html>", output.contains("<html>")); |
||||
assertTrue("Output should contain 'MeineSeite'", output.contains("MeineSeite")); |
||||
} |
||||
|
||||
private void assertPdfOutputCorrect(byte[] output) throws Exception { |
||||
assertTrue("Output length should be greater than 0", (output.length > 0)); |
||||
|
||||
String translated = new String(output, "US-ASCII"); |
||||
assertTrue("Output should start with %PDF", translated.startsWith("%PDF")); |
||||
} |
||||
|
||||
private void assertXlsOutputCorrect(byte[] output) throws Exception { |
||||
HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream(output)); |
||||
HSSFSheet sheet = workbook.getSheetAt(0); |
||||
assertNotNull("Sheet should not be null", sheet); |
||||
HSSFRow row = sheet.getRow(3); |
||||
HSSFCell cell = row.getCell((short) 1); |
||||
assertNotNull("Cell should not be null", cell); |
||||
assertEquals("Cell content should be Dear Lord!", "Dear Lord!", cell.getRichStringCellValue().getString()); |
||||
} |
||||
|
||||
private JasperReport getReport() throws Exception { |
||||
ClassPathResource resource = new ClassPathResource("DataSourceReport.jasper", getClass()); |
||||
return (JasperReport) JRLoader.loadObject(resource.getInputStream()); |
||||
} |
||||
|
||||
private Map<String, Object> getParameters() { |
||||
Map<String, Object> model = new HashMap<String, Object>(); |
||||
model.put("ReportTitle", "Dear Lord!"); |
||||
model.put(JRParameter.REPORT_LOCALE, Locale.GERMAN); |
||||
model.put(JRParameter.REPORT_RESOURCE_BUNDLE, |
||||
ResourceBundle.getBundle("org/springframework/ui/jasperreports/messages", Locale.GERMAN)); |
||||
return model; |
||||
} |
||||
|
||||
private JRDataSource getDataSource() { |
||||
return new JRBeanCollectionDataSource(getData()); |
||||
} |
||||
|
||||
private List<PersonBean> getData() { |
||||
List<PersonBean> list = new ArrayList<PersonBean>(); |
||||
for (int x = 0; x < 10; x++) { |
||||
PersonBean bean = new PersonBean(); |
||||
bean.setId(x); |
||||
bean.setName("Rob Harrop"); |
||||
bean.setStreet("foo"); |
||||
list.add(bean); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
|
||||
private static class SimpleProgressMonitor implements JRExportProgressMonitor { |
||||
|
||||
private boolean invoked = false; |
||||
|
||||
@Override |
||||
public void afterPageExport() { |
||||
this.invoked = true; |
||||
} |
||||
|
||||
public boolean isInvoked() { |
||||
return invoked; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,64 +0,0 @@
@@ -1,64 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2005 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.ui.jasperreports; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class PersonBean { |
||||
|
||||
private int id; |
||||
|
||||
private String name; |
||||
|
||||
private String street; |
||||
|
||||
private String city; |
||||
|
||||
public String getCity() { |
||||
return city; |
||||
} |
||||
|
||||
public void setCity(String city) { |
||||
this.city = city; |
||||
} |
||||
|
||||
public int getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(int id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getStreet() { |
||||
return street; |
||||
} |
||||
|
||||
public void setStreet(String street) { |
||||
this.street = street; |
||||
} |
||||
|
||||
} |
||||
@ -1,64 +0,0 @@
@@ -1,64 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2005 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.ui.jasperreports; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class ProductBean { |
||||
|
||||
private int id; |
||||
|
||||
private String name; |
||||
|
||||
private float quantity; |
||||
|
||||
private float price; |
||||
|
||||
public int getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(int id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public float getQuantity() { |
||||
return quantity; |
||||
} |
||||
|
||||
public void setQuantity(float quantity) { |
||||
this.quantity = quantity; |
||||
} |
||||
|
||||
public float getPrice() { |
||||
return price; |
||||
} |
||||
|
||||
public void setPrice(float price) { |
||||
this.price = price; |
||||
} |
||||
|
||||
} |
||||
@ -1,38 +0,0 @@
@@ -1,38 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2010 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import org.springframework.context.support.StaticApplicationContext; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public abstract class AbstractConfigurableJasperReportsViewTests extends AbstractJasperReportsViewTests { |
||||
|
||||
public void testNoConfiguredExporter() throws Exception { |
||||
ConfigurableJasperReportsView view = new ConfigurableJasperReportsView(); |
||||
view.setUrl(COMPILED_REPORT); |
||||
try { |
||||
view.setApplicationContext(new StaticApplicationContext()); |
||||
fail("Should not be able to setup view class without an exporter class."); |
||||
} |
||||
catch (IllegalArgumentException e) { |
||||
// success
|
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,107 +0,0 @@
@@ -1,107 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
|
||||
import junit.framework.TestCase; |
||||
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; |
||||
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest; |
||||
import org.springframework.mock.web.test.MockHttpServletResponse; |
||||
import org.springframework.ui.jasperreports.PersonBean; |
||||
import org.springframework.ui.jasperreports.ProductBean; |
||||
import org.springframework.util.ClassUtils; |
||||
import org.springframework.web.servlet.DispatcherServlet; |
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public abstract class AbstractJasperReportsTests extends TestCase { |
||||
|
||||
protected static final String COMPILED_REPORT = "/org/springframework/ui/jasperreports/DataSourceReport.jasper"; |
||||
|
||||
protected static final String UNCOMPILED_REPORT = "/org/springframework/ui/jasperreports/DataSourceReport.jrxml"; |
||||
|
||||
protected static final String SUB_REPORT_PARENT = "/org/springframework/ui/jasperreports/subReportParent.jrxml"; |
||||
|
||||
protected static final boolean canCompileReport = ClassUtils.isPresent( |
||||
"org.eclipse.jdt.internal.compiler.Compiler", AbstractJasperReportsTests.class.getClassLoader()); |
||||
|
||||
|
||||
protected MockHttpServletRequest request; |
||||
|
||||
protected MockHttpServletResponse response; |
||||
|
||||
|
||||
@Override |
||||
public void setUp() { |
||||
request = new MockHttpServletRequest(); |
||||
response = new MockHttpServletResponse(); |
||||
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); |
||||
request.addPreferredLocale(Locale.GERMAN); |
||||
} |
||||
|
||||
|
||||
protected Map<String, Object> getModel() { |
||||
Map model = new HashMap(); |
||||
model.put("ReportTitle", "Dear Lord!"); |
||||
model.put("dataSource", new JRBeanCollectionDataSource(getData())); |
||||
extendModel(model); |
||||
return model; |
||||
} |
||||
|
||||
/** |
||||
* Subclasses can extend the model if they need to. |
||||
*/ |
||||
protected void extendModel(Map<String, Object> model) { |
||||
} |
||||
|
||||
protected List getData() { |
||||
List list = new ArrayList(); |
||||
for (int x = 0; x < 10; x++) { |
||||
PersonBean bean = new PersonBean(); |
||||
bean.setId(x); |
||||
bean.setName("Rob Harrop"); |
||||
bean.setStreet("foo"); |
||||
list.add(bean); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
protected List getProductData() { |
||||
List list = new ArrayList(); |
||||
for (int x = 0; x < 10; x++) { |
||||
ProductBean bean = new ProductBean(); |
||||
bean.setId(x); |
||||
bean.setName("Foo Bar"); |
||||
bean.setPrice(1.9f); |
||||
bean.setQuantity(1.0f); |
||||
|
||||
list.add(bean); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
} |
||||
@ -1,423 +0,0 @@
@@ -1,423 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import java.sql.SQLException; |
||||
import java.util.HashMap; |
||||
import java.util.LinkedList; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
import java.util.Properties; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
import javax.sql.DataSource; |
||||
|
||||
import net.sf.jasperreports.engine.JRDataSource; |
||||
import net.sf.jasperreports.engine.JRException; |
||||
import net.sf.jasperreports.engine.JRExporterParameter; |
||||
import net.sf.jasperreports.engine.JasperReport; |
||||
import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider; |
||||
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; |
||||
|
||||
import org.junit.Ignore; |
||||
import org.springframework.context.ApplicationContextException; |
||||
import org.springframework.mock.web.test.MockServletContext; |
||||
import org.springframework.ui.jasperreports.PersonBean; |
||||
import org.springframework.web.context.support.StaticWebApplicationContext; |
||||
import org.springframework.web.servlet.DispatcherServlet; |
||||
|
||||
import static org.mockito.BDDMockito.*; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public abstract class AbstractJasperReportsViewTests extends AbstractJasperReportsTests { |
||||
|
||||
protected AbstractJasperReportsView getView(String url) throws Exception { |
||||
AbstractJasperReportsView view = getViewImplementation(); |
||||
view.setUrl(url); |
||||
StaticWebApplicationContext ac = new StaticWebApplicationContext(); |
||||
ac.setServletContext(new MockServletContext()); |
||||
ac.addMessage("page", Locale.GERMAN, "MeineSeite"); |
||||
ac.refresh(); |
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac); |
||||
view.setApplicationContext(ac); |
||||
return view; |
||||
} |
||||
|
||||
/** |
||||
* Simple test to see if compiled report succeeds. |
||||
*/ |
||||
public void testCompiledReport() throws Exception { |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(getModel(), request, response); |
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
if (view instanceof AbstractJasperReportsSingleFormatView && |
||||
((AbstractJasperReportsSingleFormatView) view).useWriter()) { |
||||
String output = response.getContentAsString(); |
||||
assertTrue("Output should contain 'MeineSeite'", output.contains("MeineSeite")); |
||||
} |
||||
} |
||||
|
||||
public void testUncompiledReport() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); |
||||
view.render(getModel(), request, response); |
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
} |
||||
|
||||
public void testWithInvalidPath() throws Exception { |
||||
try { |
||||
getView("foo.jasper"); |
||||
fail("Invalid path should throw ApplicationContextException"); |
||||
} |
||||
catch (ApplicationContextException ex) { |
||||
// good!
|
||||
} |
||||
} |
||||
|
||||
public void testInvalidExtension() throws Exception { |
||||
try { |
||||
getView("foo.bar"); |
||||
fail("Invalid extension should throw IllegalArgumentException"); |
||||
} |
||||
catch (IllegalArgumentException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
public void testContentType() throws Exception { |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(getModel(), request, response); |
||||
assertEquals("Response content type is incorrect", getDesiredContentType(), response.getContentType()); |
||||
} |
||||
|
||||
public void testWithoutDatasource() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(model, request, response); |
||||
assertTrue(response.getStatus() == HttpServletResponse.SC_OK); |
||||
} |
||||
|
||||
public void testWithCollection() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("reportData", getData()); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(model, request, response); |
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
} |
||||
|
||||
public void testWithMultipleCollections() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("reportData", getData()); |
||||
model.put("otherData", new LinkedList()); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(model, request, response); |
||||
// no clear data source found
|
||||
} |
||||
|
||||
public void testWithJRDataSourceProvider() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("dataSource", new MockDataSourceProvider(PersonBean.class)); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(model, request, response); |
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
} |
||||
|
||||
public void testWithSpecificCollection() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("reportData", getData()); |
||||
model.put("otherData", new LinkedList()); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.setReportDataKey("reportData"); |
||||
view.render(model, request, response); |
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
} |
||||
|
||||
public void testWithArray() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("reportData", getData().toArray()); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(model, request, response); |
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
} |
||||
|
||||
public void testWithMultipleArrays() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("reportData", getData().toArray()); |
||||
model.put("otherData", new String[0]); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(model, request, response); |
||||
// no clear data source found
|
||||
} |
||||
|
||||
public void testWithSpecificArray() throws Exception { |
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("reportData", getData().toArray()); |
||||
model.put("otherData", new String[0]); |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.setReportDataKey("reportData"); |
||||
view.render(model, request, response); |
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
} |
||||
|
||||
public void testWithSubReport() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
Map model = getModel(); |
||||
model.put("SubReportData", getProductData()); |
||||
|
||||
Properties subReports = new Properties(); |
||||
subReports.put("ProductsSubReport", "/org/springframework/ui/jasperreports/subReportChild.jrxml"); |
||||
|
||||
AbstractJasperReportsView view = getView(SUB_REPORT_PARENT); |
||||
view.setReportDataKey("dataSource"); |
||||
view.setSubReportUrls(subReports); |
||||
view.setSubReportDataKeys(new String[]{"SubReportData"}); |
||||
view.initApplicationContext(); |
||||
view.render(model, request, response); |
||||
|
||||
assertTrue(response.getContentAsByteArray().length > 0); |
||||
} |
||||
|
||||
public void testWithNonExistentSubReport() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
Map model = getModel(); |
||||
model.put("SubReportData", getProductData()); |
||||
|
||||
Properties subReports = new Properties(); |
||||
subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChildFalse.jrxml"); |
||||
|
||||
AbstractJasperReportsView view = getView(SUB_REPORT_PARENT); |
||||
view.setReportDataKey("dataSource"); |
||||
view.setSubReportUrls(subReports); |
||||
view.setSubReportDataKeys(new String[]{"SubReportData"}); |
||||
|
||||
try { |
||||
view.initApplicationContext(); |
||||
fail("Invalid report URL should throw ApplicationContext Exception"); |
||||
} |
||||
catch (ApplicationContextException ex) { |
||||
// success
|
||||
} |
||||
} |
||||
|
||||
@Ignore |
||||
public void ignoreTestOverrideExporterParameters() throws Exception { |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
|
||||
if (!(view instanceof AbstractJasperReportsSingleFormatView) || !((AbstractJasperReportsSingleFormatView) view).useWriter()) { |
||||
return; |
||||
} |
||||
|
||||
String characterEncoding = "UTF-8"; |
||||
String overiddenCharacterEncoding = "ASCII"; |
||||
|
||||
Map parameters = new HashMap(); |
||||
parameters.put(JRExporterParameter.CHARACTER_ENCODING, characterEncoding); |
||||
|
||||
view.setExporterParameters(parameters); |
||||
view.convertExporterParameters(); |
||||
|
||||
Map model = getModel(); |
||||
model.put(JRExporterParameter.CHARACTER_ENCODING, overiddenCharacterEncoding); |
||||
|
||||
view.render(model, this.request, this.response); |
||||
|
||||
assertEquals(overiddenCharacterEncoding, this.response.getCharacterEncoding()); |
||||
} |
||||
|
||||
public void testSubReportWithUnspecifiedParentDataSource() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
Map model = getModel(); |
||||
model.put("SubReportData", getProductData()); |
||||
|
||||
Properties subReports = new Properties(); |
||||
subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChildFalse.jrxml"); |
||||
|
||||
AbstractJasperReportsView view = getView(SUB_REPORT_PARENT); |
||||
view.setSubReportUrls(subReports); |
||||
view.setSubReportDataKeys(new String[]{"SubReportData"}); |
||||
|
||||
try { |
||||
view.initApplicationContext(); |
||||
fail("Unspecified reportDataKey should throw exception when subReportDataSources is specified"); |
||||
} |
||||
catch (ApplicationContextException ex) { |
||||
// success
|
||||
} |
||||
} |
||||
|
||||
public void testContentDisposition() throws Exception { |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.render(getModel(), request, response); |
||||
assertEquals("Invalid content type", "inline", response.getHeader("Content-Disposition")); |
||||
|
||||
} |
||||
|
||||
public void testOverrideContentDisposition() throws Exception { |
||||
Properties headers = new Properties(); |
||||
String cd = "attachment"; |
||||
headers.setProperty("Content-Disposition", cd); |
||||
|
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.setHeaders(headers); |
||||
view.render(getModel(), request, response); |
||||
assertEquals("Invalid content type", cd, response.getHeader("Content-Disposition")); |
||||
} |
||||
|
||||
public void testSetCustomHeaders() throws Exception { |
||||
Properties headers = new Properties(); |
||||
|
||||
String key = "foo"; |
||||
String value = "bar"; |
||||
|
||||
headers.setProperty(key, value); |
||||
|
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
view.setHeaders(headers); |
||||
view.render(getModel(), request, response); |
||||
|
||||
assertNotNull("Header not present", response.getHeader(key)); |
||||
assertEquals("Invalid header value", value, response.getHeader(key)); |
||||
} |
||||
|
||||
public void testWithJdbcDataSource() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); |
||||
view.setJdbcDataSource(getMockJdbcDataSource()); |
||||
|
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
|
||||
try { |
||||
view.render(model, request, response); |
||||
fail("DataSource was not used as report DataSource"); |
||||
} |
||||
catch (SQLException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
public void testWithJdbcDataSourceInModel() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); |
||||
|
||||
Map model = getModel(); |
||||
model.remove("dataSource"); |
||||
model.put("someKey", getMockJdbcDataSource()); |
||||
|
||||
try { |
||||
view.render(model, request, response); |
||||
fail("DataSource was not used as report DataSource"); |
||||
} |
||||
catch (SQLException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
public void testJRDataSourceOverridesJdbcDataSource() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); |
||||
view.setJdbcDataSource(getMockJdbcDataSource()); |
||||
|
||||
try { |
||||
view.render(getModel(), request, response); |
||||
} |
||||
catch (SQLException ex) { |
||||
fail("javax.sql.DataSource was used when JRDataSource should have overridden it"); |
||||
} |
||||
} |
||||
|
||||
private DataSource getMockJdbcDataSource() throws SQLException { |
||||
DataSource ds = mock(DataSource.class); |
||||
given(ds.getConnection()).willThrow(new SQLException()); |
||||
return ds; |
||||
} |
||||
|
||||
public void testWithCharacterEncoding() throws Exception { |
||||
AbstractJasperReportsView view = getView(COMPILED_REPORT); |
||||
|
||||
if (!(view instanceof AbstractJasperReportsSingleFormatView) || !((AbstractJasperReportsSingleFormatView) view).useWriter()) { |
||||
return; |
||||
} |
||||
|
||||
String characterEncoding = "UTF-8"; |
||||
|
||||
Map parameters = new HashMap(); |
||||
parameters.put(JRExporterParameter.CHARACTER_ENCODING, characterEncoding); |
||||
|
||||
view.setExporterParameters(parameters); |
||||
view.convertExporterParameters(); |
||||
|
||||
view.render(getModel(), this.request, this.response); |
||||
assertEquals(characterEncoding, this.response.getCharacterEncoding()); |
||||
} |
||||
|
||||
|
||||
protected abstract AbstractJasperReportsView getViewImplementation(); |
||||
|
||||
protected abstract String getDesiredContentType(); |
||||
|
||||
|
||||
private class MockDataSourceProvider extends JRAbstractBeanDataSourceProvider { |
||||
|
||||
public MockDataSourceProvider(Class clazz) { |
||||
super(clazz); |
||||
} |
||||
|
||||
@Override |
||||
public JRDataSource create(JasperReport jasperReport) throws JRException { |
||||
return new JRBeanCollectionDataSource(getData()); |
||||
} |
||||
|
||||
@Override |
||||
public void dispose(JRDataSource jrDataSource) throws JRException { |
||||
|
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,39 +0,0 @@
@@ -1,39 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import net.sf.jasperreports.engine.export.JRHtmlExporter; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class ConfigurableJasperReportsViewWithStreamTests extends AbstractConfigurableJasperReportsViewTests { |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
ConfigurableJasperReportsView view = new ConfigurableJasperReportsView(); |
||||
view.setExporterClass(JRHtmlExporter.class); |
||||
view.setUseWriter(true); |
||||
view.setContentType("application/pdf"); |
||||
return view; |
||||
} |
||||
|
||||
@Override |
||||
protected String getDesiredContentType() { |
||||
return "application/pdf"; |
||||
} |
||||
} |
||||
@ -1,39 +0,0 @@
@@ -1,39 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import net.sf.jasperreports.engine.export.JRPdfExporter; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class ConfigurableJasperReportsViewWithWriterTests extends AbstractConfigurableJasperReportsViewTests { |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
ConfigurableJasperReportsView view = new ConfigurableJasperReportsView(); |
||||
view.setExporterClass(JRPdfExporter.class); |
||||
view.setUseWriter(false); |
||||
view.setContentType("text/html"); |
||||
return view; |
||||
} |
||||
|
||||
@Override |
||||
protected String getDesiredContentType() { |
||||
return "text/html"; |
||||
} |
||||
} |
||||
@ -1,162 +0,0 @@
@@ -1,162 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Iterator; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import net.sf.jasperreports.engine.JRExporterParameter; |
||||
import net.sf.jasperreports.engine.JasperPrint; |
||||
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; |
||||
|
||||
import org.springframework.mock.web.test.MockServletContext; |
||||
import org.springframework.web.context.support.StaticWebApplicationContext; |
||||
import org.springframework.web.servlet.DispatcherServlet; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class ExporterParameterTests extends AbstractJasperReportsTests { |
||||
|
||||
public void testParameterParsing() throws Exception { |
||||
Map params = new HashMap(); |
||||
params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI", "/foo/bar"); |
||||
|
||||
AbstractJasperReportsView view = new AbstractJasperReportsView() { |
||||
@Override |
||||
protected void renderReport(JasperPrint filledReport, Map model, HttpServletResponse response) |
||||
throws Exception { |
||||
|
||||
assertEquals("Invalid number of exporter parameters", 1, getConvertedExporterParameters().size()); |
||||
|
||||
JRExporterParameter key = JRHtmlExporterParameter.IMAGES_URI; |
||||
Object value = getConvertedExporterParameters().get(key); |
||||
|
||||
assertNotNull("Value not mapped to correct key", value); |
||||
assertEquals("Incorrect value for parameter", "/foo/bar", value); |
||||
} |
||||
|
||||
/** |
||||
* Merges the configured {@link net.sf.jasperreports.engine.JRExporterParameter JRExporterParameters} with any specified |
||||
* in the supplied model data. {@link net.sf.jasperreports.engine.JRExporterParameter JRExporterParameters} in the model |
||||
* override those specified in the configuration. |
||||
* @see #setExporterParameters(java.util.Map) |
||||
*/ |
||||
protected Map mergeExporterParameters(Map model) { |
||||
Map mergedParameters = new HashMap(getConvertedExporterParameters()); |
||||
for (Iterator iterator = model.keySet().iterator(); iterator.hasNext();) { |
||||
Object key = iterator.next(); |
||||
|
||||
if (key instanceof JRExporterParameter) { |
||||
Object value = model.get(key); |
||||
if (value instanceof String) { |
||||
mergedParameters.put(key, value); |
||||
} |
||||
else { |
||||
logger.warn("Ignoring exporter parameter [" + key + "]. Value is not a String."); |
||||
} |
||||
} |
||||
} |
||||
return mergedParameters; |
||||
} |
||||
}; |
||||
|
||||
view.setExporterParameters(params); |
||||
setViewProperties(view); |
||||
view.render(getModel(), request, response); |
||||
} |
||||
|
||||
public void testInvalidClass() throws Exception { |
||||
Map params = new HashMap(); |
||||
params.put("foo.net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI", "/foo"); |
||||
|
||||
AbstractJasperReportsView view = new JasperReportsHtmlView(); |
||||
setViewProperties(view); |
||||
|
||||
try { |
||||
view.setExporterParameters(params); |
||||
view.convertExporterParameters(); |
||||
fail("Should have thrown IllegalArgumentException"); |
||||
} |
||||
catch (IllegalArgumentException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
public void testInvalidField() { |
||||
Map params = new HashMap(); |
||||
params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI_FOO", "/foo"); |
||||
|
||||
AbstractJasperReportsView view = new JasperReportsHtmlView(); |
||||
setViewProperties(view); |
||||
|
||||
try { |
||||
view.setExporterParameters(params); |
||||
view.convertExporterParameters(); |
||||
fail("Should have thrown IllegalArgumentException"); |
||||
} |
||||
catch (IllegalArgumentException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
public void testInvalidType() { |
||||
Map params = new HashMap(); |
||||
params.put("java.lang.Boolean.TRUE", "/foo"); |
||||
|
||||
AbstractJasperReportsView view = new JasperReportsHtmlView(); |
||||
setViewProperties(view); |
||||
|
||||
try { |
||||
view.setExporterParameters(params); |
||||
view.convertExporterParameters(); |
||||
fail("Should have thrown IllegalArgumentException"); |
||||
} |
||||
catch (IllegalArgumentException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
|
||||
public void testTypeConversion() { |
||||
Map params = new HashMap(); |
||||
params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN", "true"); |
||||
|
||||
AbstractJasperReportsView view = new JasperReportsHtmlView(); |
||||
setViewProperties(view); |
||||
|
||||
view.setExporterParameters(params); |
||||
view.convertExporterParameters(); |
||||
Object value = view.getConvertedExporterParameters().get(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN); |
||||
assertEquals(Boolean.TRUE, value); |
||||
} |
||||
|
||||
private void setViewProperties(AbstractJasperReportsView view) { |
||||
view.setUrl("/org/springframework/ui/jasperreports/DataSourceReport.jasper"); |
||||
StaticWebApplicationContext ac = new StaticWebApplicationContext(); |
||||
ac.setServletContext(new MockServletContext()); |
||||
ac.addMessage("page", Locale.GERMAN, "MeineSeite"); |
||||
ac.refresh(); |
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac); |
||||
view.setApplicationContext(ac); |
||||
} |
||||
|
||||
} |
||||
@ -1,92 +0,0 @@
@@ -1,92 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2005 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import java.util.Locale; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
import org.springframework.context.support.StaticApplicationContext; |
||||
import org.springframework.web.servlet.view.velocity.VelocityView; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class JasperReportViewResolverTests extends TestCase { |
||||
|
||||
public void testResolveView() throws Exception { |
||||
StaticApplicationContext ctx = new StaticApplicationContext(); |
||||
|
||||
String prefix = "org/springframework/ui/jasperreports/"; |
||||
String suffix = ".jasper"; |
||||
String viewName = "DataSourceReport"; |
||||
|
||||
JasperReportsViewResolver viewResolver = new JasperReportsViewResolver(); |
||||
viewResolver.setViewClass(JasperReportsHtmlView.class); |
||||
viewResolver.setPrefix(prefix); |
||||
viewResolver.setSuffix(suffix); |
||||
viewResolver.setApplicationContext(ctx); |
||||
|
||||
AbstractJasperReportsView view = |
||||
(AbstractJasperReportsView) viewResolver.resolveViewName(viewName, Locale.ENGLISH); |
||||
assertNotNull("View should not be null", view); |
||||
assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl()); |
||||
} |
||||
|
||||
public void testSetIncorrectViewClass() { |
||||
try { |
||||
new JasperReportsViewResolver().setViewClass(VelocityView.class); |
||||
fail("Should not be able to set view class to a class that does not extend AbstractJasperReportsView"); |
||||
} |
||||
catch (IllegalArgumentException ex) { |
||||
// success
|
||||
} |
||||
} |
||||
|
||||
public void testWithViewNamesAndEndsWithPattern() throws Exception { |
||||
doViewNamesTest(new String[]{"DataSource*"}); |
||||
} |
||||
|
||||
public void testWithViewNamesAndStartsWithPattern() throws Exception { |
||||
doViewNamesTest(new String[]{"*Report"}); |
||||
} |
||||
|
||||
public void testWithViewNamesAndStatic() throws Exception { |
||||
doViewNamesTest(new String[]{"DataSourceReport"}); |
||||
} |
||||
|
||||
private void doViewNamesTest(String[] viewNames) throws Exception { |
||||
StaticApplicationContext ctx = new StaticApplicationContext(); |
||||
|
||||
String prefix = "org/springframework/ui/jasperreports/"; |
||||
String suffix = ".jasper"; |
||||
String viewName = "DataSourceReport"; |
||||
|
||||
JasperReportsViewResolver viewResolver = new JasperReportsViewResolver(); |
||||
viewResolver.setViewClass(JasperReportsHtmlView.class); |
||||
viewResolver.setPrefix(prefix); |
||||
viewResolver.setSuffix(suffix); |
||||
viewResolver.setViewNames(viewNames); |
||||
viewResolver.setApplicationContext(ctx); |
||||
|
||||
AbstractJasperReportsView view = |
||||
(AbstractJasperReportsView) viewResolver.resolveViewName(viewName, Locale.ENGLISH); |
||||
assertNotNull("View should not be null", view); |
||||
assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl()); |
||||
assertNull(viewResolver.resolveViewName("foo", Locale.ENGLISH)); |
||||
} |
||||
} |
||||
@ -1,34 +0,0 @@
@@ -1,34 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class JasperReportsCsvViewTests extends AbstractJasperReportsViewTests { |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
return new JasperReportsCsvView(); |
||||
} |
||||
|
||||
@Override |
||||
protected String getDesiredContentType() { |
||||
return "text/csv"; |
||||
} |
||||
|
||||
} |
||||
@ -1,59 +0,0 @@
@@ -1,59 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; |
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader; |
||||
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; |
||||
import org.springframework.core.io.ClassPathResource; |
||||
import org.springframework.mock.web.test.MockServletContext; |
||||
import org.springframework.web.context.support.GenericWebApplicationContext; |
||||
import org.springframework.web.servlet.DispatcherServlet; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class JasperReportsHtmlViewTests extends AbstractJasperReportsViewTests { |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
return new JasperReportsHtmlView(); |
||||
} |
||||
|
||||
@Override |
||||
protected String getDesiredContentType() { |
||||
return "text/html"; |
||||
} |
||||
|
||||
public void testConfigureExporterParametersWithEncodingFromPropertiesFile() throws Exception { |
||||
GenericWebApplicationContext ac = new GenericWebApplicationContext(); |
||||
ac.setServletContext(new MockServletContext()); |
||||
BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(ac); |
||||
reader.loadBeanDefinitions(new ClassPathResource("view.properties", getClass())); |
||||
ac.refresh(); |
||||
|
||||
AbstractJasperReportsView view = (AbstractJasperReportsView) ac.getBean("report"); |
||||
String encoding = (String) view.getConvertedExporterParameters().get(JRHtmlExporterParameter.CHARACTER_ENCODING); |
||||
assertEquals("UTF-8", encoding); |
||||
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac); |
||||
view.render(getModel(), request, response); |
||||
assertEquals("Response content type is incorrect", "text/html;charset=UTF-8", response.getContentType()); |
||||
} |
||||
|
||||
} |
||||
@ -1,135 +0,0 @@
@@ -1,135 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.Properties; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import net.sf.jasperreports.engine.JasperPrint; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsViewTests { |
||||
|
||||
@Override |
||||
protected void extendModel(Map<String, Object> model) { |
||||
model.put(getDiscriminatorKey(), "csv"); |
||||
} |
||||
|
||||
public void testSimpleHtmlRender() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); |
||||
|
||||
Map<String, Object> model = getBaseModel(); |
||||
model.put(getDiscriminatorKey(), "html"); |
||||
|
||||
view.render(model, request, response); |
||||
|
||||
assertEquals("Invalid content type", "text/html", response.getContentType()); |
||||
} |
||||
|
||||
@Override |
||||
public void testOverrideContentDisposition() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); |
||||
|
||||
Map<String, Object> model = getBaseModel(); |
||||
model.put(getDiscriminatorKey(), "csv"); |
||||
|
||||
String headerValue = "inline; filename=foo.txt"; |
||||
|
||||
Properties mappings = new Properties(); |
||||
mappings.put("csv", headerValue); |
||||
|
||||
((JasperReportsMultiFormatView) view).setContentDispositionMappings(mappings); |
||||
|
||||
view.render(model, request, response); |
||||
|
||||
assertEquals("Invalid Content-Disposition header value", headerValue, |
||||
response.getHeader("Content-Disposition")); |
||||
} |
||||
|
||||
public void testExporterParametersAreCarriedAcross() throws Exception { |
||||
if (!canCompileReport) { |
||||
return; |
||||
} |
||||
|
||||
JasperReportsMultiFormatView view = (JasperReportsMultiFormatView) getView(UNCOMPILED_REPORT); |
||||
|
||||
Map<String, Class<? extends AbstractJasperReportsView>> mappings = |
||||
new HashMap<String, Class<? extends AbstractJasperReportsView>>(); |
||||
mappings.put("test", ExporterParameterTestView.class); |
||||
|
||||
Map<String, String> exporterParameters = new HashMap<String, String>(); |
||||
|
||||
// test view class performs the assertions - robh
|
||||
exporterParameters.put(ExporterParameterTestView.TEST_PARAM, "foo"); |
||||
|
||||
view.setExporterParameters(exporterParameters); |
||||
view.setFormatMappings(mappings); |
||||
view.initApplicationContext(); |
||||
|
||||
Map<String, Object> model = getBaseModel(); |
||||
model.put(getDiscriminatorKey(), "test"); |
||||
|
||||
view.render(model, request, response); |
||||
} |
||||
|
||||
protected String getDiscriminatorKey() { |
||||
return "format"; |
||||
} |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
return new JasperReportsMultiFormatView(); |
||||
} |
||||
|
||||
@Override |
||||
protected String getDesiredContentType() { |
||||
return "text/csv"; |
||||
} |
||||
|
||||
private Map<String, Object> getBaseModel() { |
||||
Map<String, Object> model = new HashMap<String, Object>(); |
||||
model.put("ReportTitle", "Foo"); |
||||
model.put("dataSource", getData()); |
||||
return model; |
||||
} |
||||
|
||||
|
||||
public static class ExporterParameterTestView extends AbstractJasperReportsView { |
||||
|
||||
public static final String TEST_PARAM = "net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI"; |
||||
|
||||
@Override |
||||
protected void renderReport(JasperPrint filledReport, Map parameters, HttpServletResponse response) { |
||||
assertNotNull("Exporter parameters are null", getExporterParameters()); |
||||
assertEquals("Incorrect number of exporter parameters", 1, getExporterParameters().size()); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,51 +0,0 @@
@@ -1,51 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public class JasperReportsMultiFormatViewWithCustomMappingsTests extends JasperReportsMultiFormatViewTests { |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
JasperReportsMultiFormatView view = new JasperReportsMultiFormatView(); |
||||
view.setFormatKey("fmt"); |
||||
Map<String, Class<? extends AbstractJasperReportsView>> mappings = |
||||
new HashMap<String, Class<? extends AbstractJasperReportsView>>(); |
||||
mappings.put("csv", JasperReportsCsvView.class); |
||||
mappings.put("comma-separated", JasperReportsCsvView.class); |
||||
mappings.put("html", JasperReportsHtmlView.class); |
||||
view.setFormatMappings(mappings); |
||||
return view; |
||||
} |
||||
|
||||
@Override |
||||
protected String getDiscriminatorKey() { |
||||
return "fmt"; |
||||
} |
||||
|
||||
@Override |
||||
protected void extendModel(Map<String, Object> model) { |
||||
model.put(getDiscriminatorKey(), "comma-separated"); |
||||
} |
||||
|
||||
} |
||||
@ -1,34 +0,0 @@
@@ -1,34 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class JasperReportsPdfViewTests extends AbstractJasperReportsViewTests { |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
return new JasperReportsPdfView(); |
||||
} |
||||
|
||||
@Override |
||||
protected String getDesiredContentType() { |
||||
return "application/pdf"; |
||||
} |
||||
|
||||
} |
||||
@ -1,34 +0,0 @@
@@ -1,34 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.view.jasperreports; |
||||
|
||||
/** |
||||
* @author Rob Harrop |
||||
*/ |
||||
public class JasperReportsXlsViewTests extends AbstractJasperReportsViewTests { |
||||
|
||||
@Override |
||||
protected AbstractJasperReportsView getViewImplementation() { |
||||
return new JasperReportsXlsView(); |
||||
} |
||||
|
||||
@Override |
||||
protected String getDesiredContentType() { |
||||
return "application/vnd.ms-excel"; |
||||
} |
||||
|
||||
} |
||||
@ -1,4 +0,0 @@
@@ -1,4 +0,0 @@
|
||||
report.(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView |
||||
report.url=/org/springframework/ui/jasperreports/DataSourceReport.jasper |
||||
report.exporterParameters[net.sf.jasperreports.engine.export.JRHtmlExporterParameter.CHARACTER_ENCODING]=UTF-8 |
||||
report.exporterParameters[net.sf.jasperreports.engine.JRExporterParameter.PAGE_INDEX]=0 |
||||
Loading…
Reference in new issue