Reinstate and modernize Jasper Reports tests

This commit is contained in:
Sam Brannen
2015-09-28 19:22:23 +02:00
parent 9c66dfa7b5
commit 58c2990794
8 changed files with 147 additions and 199 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,24 +20,17 @@ import org.junit.Test;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import static org.junit.Assert.*;
/** /**
* @author Rob Harrop * @author Rob Harrop
*/ */
public abstract class AbstractConfigurableJasperReportsViewTests extends AbstractJasperReportsViewTests { public abstract class AbstractConfigurableJasperReportsViewTests extends AbstractJasperReportsViewTests {
@Test @Test(expected = IllegalArgumentException.class)
public void testNoConfiguredExporter() throws Exception { public void noConfiguredExporter() throws Exception {
ConfigurableJasperReportsView view = new ConfigurableJasperReportsView(); ConfigurableJasperReportsView view = new ConfigurableJasperReportsView();
view.setUrl(COMPILED_REPORT); view.setUrl(COMPILED_REPORT);
try { // Should not be able to set up view class without an exporter class.
view.setApplicationContext(new StaticApplicationContext()); view.setApplicationContext(new StaticApplicationContext());
fail("Should not be able to setup view class without an exporter class.");
}
catch (IllegalArgumentException e) {
// success
}
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
@@ -52,9 +52,13 @@ public abstract class AbstractJasperReportsTests {
"org.eclipse.jdt.internal.compiler.Compiler", AbstractJasperReportsTests.class.getClassLoader()); "org.eclipse.jdt.internal.compiler.Compiler", AbstractJasperReportsTests.class.getClassLoader());
protected MockHttpServletRequest request; protected final MockHttpServletResponse response = new MockHttpServletResponse();
protected MockHttpServletResponse response; protected final MockHttpServletRequest request = new MockHttpServletRequest();
{
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.addPreferredLocale(Locale.GERMAN);
}
@BeforeClass @BeforeClass
@@ -62,14 +66,6 @@ public abstract class AbstractJasperReportsTests {
Assume.canLoadNativeDirFonts(); Assume.canLoadNativeDirFonts();
} }
@Before
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() { protected Map<String, Object> getModel() {
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -22,27 +22,29 @@ import java.util.LinkedList;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource; import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider; import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.mock.web.test.MockServletContext; import org.springframework.mock.web.test.MockServletContext;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.ui.jasperreports.PersonBean; import org.springframework.ui.jasperreports.PersonBean;
import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.mockito.BDDMockito.*; import static org.mockito.BDDMockito.*;
/** /**
@@ -50,67 +52,59 @@ import static org.mockito.BDDMockito.*;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
*/ */
@SuppressWarnings("deprecation")
public abstract class AbstractJasperReportsViewTests extends AbstractJasperReportsTests { public abstract class AbstractJasperReportsViewTests extends AbstractJasperReportsTests {
protected AbstractJasperReportsView getView(String url) throws Exception { @Rule
AbstractJasperReportsView view = getViewImplementation(); public final ExpectedException exception = ExpectedException.none();
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. * Simple test to see if compiled report succeeds.
*/ */
@Test @Test
public void testCompiledReport() throws Exception { public void compiledReport() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT); AbstractJasperReportsView view = getView(COMPILED_REPORT);
view.render(getModel(), request, response); view.render(getModel(), request, response);
assertTrue(response.getContentAsByteArray().length > 0); assertTrue(response.getContentAsByteArray().length > 0);
if (view instanceof AbstractJasperReportsSingleFormatView &&
((AbstractJasperReportsSingleFormatView) view).useWriter()) { assumeTrue(view instanceof AbstractJasperReportsSingleFormatView
String output = response.getContentAsString(); && ((AbstractJasperReportsSingleFormatView) view).useWriter());
assertTrue("Output should contain 'MeineSeite'", output.contains("MeineSeite"));
} String output = response.getContentAsString();
assertTrue("Output should contain 'MeineSeite'", output.contains("MeineSeite"));
} }
@Test @Test
public void testUncompiledReport() throws Exception { public void uncompiledReport() throws Exception {
Assume.group(TestGroup.CUSTOM_COMPILATION); assumeTrue(canCompileReport);
if (!canCompileReport) {
return;
}
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
view.render(getModel(), request, response); view.render(getModel(), request, response);
assertTrue(response.getContentAsByteArray().length > 0); assertTrue(response.getContentAsByteArray().length > 0);
} }
@Test(expected = ApplicationContextException.class) @Test
public void testWithInvalidPath() throws Exception { public void withInvalidPath() throws Exception {
exception.expect(ApplicationContextException.class);
getView("foo.jasper"); getView("foo.jasper");
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testInvalidExtension() throws Exception { public void invalidExtension() throws Exception {
exception.expect(IllegalArgumentException.class);
getView("foo.bar"); getView("foo.bar");
} }
@Test @Test
public void testContentType() throws Exception { public void contentType() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT); AbstractJasperReportsView view = getView(COMPILED_REPORT);
view.render(getModel(), request, response); view.render(getModel(), request, response);
assertEquals("Response content type is incorrect", getDesiredContentType(), response.getContentType()); assertEquals("Response content type is incorrect", getDesiredContentType(), response.getContentType());
} }
@Test @Test
public void testWithoutDatasource() throws Exception { public void withoutDatasource() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
AbstractJasperReportsView view = getView(COMPILED_REPORT); AbstractJasperReportsView view = getView(COMPILED_REPORT);
@@ -119,7 +113,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithCollection() throws Exception { public void withCollection() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
model.put("reportData", getData()); model.put("reportData", getData());
@@ -130,7 +124,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
@Test @Test
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public void testWithMultipleCollections() throws Exception { public void withMultipleCollections() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
model.put("reportData", getData()); model.put("reportData", getData());
@@ -141,7 +135,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithJRDataSourceProvider() throws Exception { public void withJRDataSourceProvider() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
model.put("dataSource", new MockDataSourceProvider(PersonBean.class)); model.put("dataSource", new MockDataSourceProvider(PersonBean.class));
@@ -152,7 +146,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
@Test @Test
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public void testWithSpecificCollection() throws Exception { public void withSpecificCollection() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
model.put("reportData", getData()); model.put("reportData", getData());
@@ -164,7 +158,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithArray() throws Exception { public void withArray() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
model.put("reportData", getData().toArray()); model.put("reportData", getData().toArray());
@@ -174,7 +168,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithMultipleArrays() throws Exception { public void withMultipleArrays() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
model.put("reportData", getData().toArray()); model.put("reportData", getData().toArray());
@@ -185,7 +179,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithSpecificArray() throws Exception { public void withSpecificArray() throws Exception {
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
model.put("reportData", getData().toArray()); model.put("reportData", getData().toArray());
@@ -197,10 +191,8 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithSubReport() throws Exception { public void withSubReport() throws Exception {
if (!canCompileReport) { assumeTrue(canCompileReport);
return;
}
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.put("SubReportData", getProductData()); model.put("SubReportData", getProductData());
@@ -219,10 +211,8 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithNonExistentSubReport() throws Exception { public void withNonExistentSubReport() throws Exception {
if (!canCompileReport) { assumeTrue(canCompileReport);
return;
}
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.put("SubReportData", getProductData()); model.put("SubReportData", getProductData());
@@ -235,34 +225,32 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
view.setSubReportUrls(subReports); view.setSubReportUrls(subReports);
view.setSubReportDataKeys(new String[]{"SubReportData"}); view.setSubReportDataKeys(new String[]{"SubReportData"});
try { // Invalid report URL should throw ApplicationContextException
view.initApplicationContext(); exception.expect(ApplicationContextException.class);
fail("Invalid report URL should throw ApplicationContextException"); view.initApplicationContext();
}
catch (ApplicationContextException ex) {
// success
}
} }
@Ignore // TODO Determine why encoding does not get overridden.
public void ignoreTestOverrideExporterParameters() throws Exception { @Ignore("Disabled since encoding does not get overridden")
@Test
public void overrideExporterParameters() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT); AbstractJasperReportsView view = getView(COMPILED_REPORT);
if (!(view instanceof AbstractJasperReportsSingleFormatView) || !((AbstractJasperReportsSingleFormatView) view).useWriter()) { assumeTrue(view instanceof AbstractJasperReportsSingleFormatView
return; && ((AbstractJasperReportsSingleFormatView) view).useWriter());
}
String characterEncoding = "UTF-8"; String characterEncoding = "UTF-8";
String overiddenCharacterEncoding = "ASCII"; String overiddenCharacterEncoding = "ASCII";
Map<Object, Object> parameters = new HashMap<Object, Object>(); Map<Object, Object> parameters = new HashMap<Object, Object>();
parameters.put(JRExporterParameter.CHARACTER_ENCODING, characterEncoding); parameters.put(net.sf.jasperreports.engine.JRExporterParameter.CHARACTER_ENCODING, characterEncoding);
view.setExporterParameters(parameters); view.setExporterParameters(parameters);
view.convertExporterParameters(); view.convertExporterParameters();
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.put(JRExporterParameter.CHARACTER_ENCODING.toString(), overiddenCharacterEncoding); model.put(net.sf.jasperreports.engine.JRExporterParameter.CHARACTER_ENCODING.toString(),
overiddenCharacterEncoding);
view.render(model, this.request, this.response); view.render(model, this.request, this.response);
@@ -270,10 +258,8 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testSubReportWithUnspecifiedParentDataSource() throws Exception { public void subReportWithUnspecifiedParentDataSource() throws Exception {
if (!canCompileReport) { assumeTrue(canCompileReport);
return;
}
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.put("SubReportData", getProductData()); model.put("SubReportData", getProductData());
@@ -285,25 +271,20 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
view.setSubReportUrls(subReports); view.setSubReportUrls(subReports);
view.setSubReportDataKeys(new String[]{"SubReportData"}); view.setSubReportDataKeys(new String[]{"SubReportData"});
try { // Unspecified reportDataKey should throw exception when subReportDataSources is specified
view.initApplicationContext(); exception.expect(ApplicationContextException.class);
fail("Unspecified reportDataKey should throw exception when subReportDataSources is specified"); view.initApplicationContext();
}
catch (ApplicationContextException ex) {
// success
}
} }
@Test @Test
public void testContentDisposition() throws Exception { public void contentDisposition() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT); AbstractJasperReportsView view = getView(COMPILED_REPORT);
view.render(getModel(), request, response); view.render(getModel(), request, response);
assertEquals("Invalid content type", "inline", response.getHeader("Content-Disposition")); assertEquals("Invalid content type", "inline", response.getHeader("Content-Disposition"));
} }
@Test @Test
public void testOverrideContentDisposition() throws Exception { public void overrideContentDisposition() throws Exception {
Properties headers = new Properties(); Properties headers = new Properties();
String cd = "attachment"; String cd = "attachment";
headers.setProperty("Content-Disposition", cd); headers.setProperty("Content-Disposition", cd);
@@ -315,7 +296,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testSetCustomHeaders() throws Exception { public void setCustomHeaders() throws Exception {
Properties headers = new Properties(); Properties headers = new Properties();
String key = "foo"; String key = "foo";
@@ -332,12 +313,8 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
} }
@Test @Test
public void testWithJdbcDataSource() throws Exception { public void withJdbcDataSource() throws Exception {
Assume.group(TestGroup.CUSTOM_COMPILATION); assumeTrue(canCompileReport);
if (!canCompileReport) {
return;
}
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
view.setJdbcDataSource(getMockJdbcDataSource()); view.setJdbcDataSource(getMockJdbcDataSource());
@@ -345,22 +322,14 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
Map<String, Object> model = getModel(); Map<String, Object> model = getModel();
model.remove("dataSource"); model.remove("dataSource");
try { // DataSource was not used as report DataSource
view.render(model, request, response); exception.expect(SQLException.class);
fail("DataSource was not used as report DataSource"); view.render(model, request, response);
}
catch (SQLException ex) {
// expected
}
} }
@Test @Test
public void testWithJdbcDataSourceInModel() throws Exception { public void withJdbcDataSourceInModel() throws Exception {
Assume.group(TestGroup.CUSTOM_COMPILATION); assumeTrue(canCompileReport);
if (!canCompileReport) {
return;
}
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
@@ -368,52 +337,32 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
model.remove("dataSource"); model.remove("dataSource");
model.put("someKey", getMockJdbcDataSource()); model.put("someKey", getMockJdbcDataSource());
try { // DataSource was not used as report DataSource
view.render(model, request, response); exception.expect(SQLException.class);
fail("DataSource was not used as report DataSource"); view.render(model, request, response);
}
catch (SQLException ex) {
// expected
}
} }
@Test @Test
public void testJRDataSourceOverridesJdbcDataSource() throws Exception { public void jrDataSourceOverridesJdbcDataSource() throws Exception {
Assume.group(TestGroup.CUSTOM_COMPILATION); assumeTrue(canCompileReport);
if (!canCompileReport) {
return;
}
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
view.setJdbcDataSource(getMockJdbcDataSource()); view.setJdbcDataSource(getMockJdbcDataSource());
try { view.render(getModel(), request, response);
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;
} }
@Test @Test
public void testWithCharacterEncoding() throws Exception { public void withCharacterEncoding() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT); AbstractJasperReportsView view = getView(COMPILED_REPORT);
if (!(view instanceof AbstractJasperReportsSingleFormatView) || !((AbstractJasperReportsSingleFormatView) view).useWriter()) { assumeTrue(view instanceof AbstractJasperReportsSingleFormatView
return; && ((AbstractJasperReportsSingleFormatView) view).useWriter());
}
String characterEncoding = "UTF-8"; String characterEncoding = "UTF-8";
Map<Object, Object> parameters = new HashMap<Object, Object>(); Map<Object, Object> parameters = new HashMap<Object, Object>();
parameters.put(JRExporterParameter.CHARACTER_ENCODING, characterEncoding); parameters.put(net.sf.jasperreports.engine.JRExporterParameter.CHARACTER_ENCODING, characterEncoding);
view.setExporterParameters(parameters); view.setExporterParameters(parameters);
view.convertExporterParameters(); view.convertExporterParameters();
@@ -422,11 +371,28 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertEquals(characterEncoding, this.response.getCharacterEncoding()); assertEquals(characterEncoding, this.response.getCharacterEncoding());
} }
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;
}
protected abstract AbstractJasperReportsView getViewImplementation(); protected abstract AbstractJasperReportsView getViewImplementation();
protected abstract String getDesiredContentType(); protected abstract String getDesiredContentType();
private DataSource getMockJdbcDataSource() throws SQLException {
DataSource ds = mock(DataSource.class);
given(ds.getConnection()).willThrow(new SQLException());
return ds;
}
private class MockDataSourceProvider extends JRAbstractBeanDataSourceProvider { private class MockDataSourceProvider extends JRAbstractBeanDataSourceProvider {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,17 +16,16 @@
package org.springframework.web.servlet.view.jasperreports; package org.springframework.web.servlet.view.jasperreports;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
/** /**
* @author Rob Harrop * @author Rob Harrop
*/ */
public class ConfigurableJasperReportsViewWithStreamTests extends AbstractConfigurableJasperReportsViewTests { public class ConfigurableJasperReportsViewWithStreamTests extends AbstractConfigurableJasperReportsViewTests {
@Override @Override
@SuppressWarnings("deprecation")
protected AbstractJasperReportsView getViewImplementation() { protected AbstractJasperReportsView getViewImplementation() {
ConfigurableJasperReportsView view = new ConfigurableJasperReportsView(); ConfigurableJasperReportsView view = new ConfigurableJasperReportsView();
view.setExporterClass(JRHtmlExporter.class); view.setExporterClass(net.sf.jasperreports.engine.export.JRHtmlExporter.class);
view.setUseWriter(true); view.setUseWriter(true);
view.setContentType("application/pdf"); view.setContentType("application/pdf");
return view; return view;
@@ -36,4 +35,5 @@ public class ConfigurableJasperReportsViewWithStreamTests extends AbstractConfig
protected String getDesiredContentType() { protected String getDesiredContentType() {
return "application/pdf"; return "application/pdf";
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,9 +21,8 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.web.test.MockServletContext; import org.springframework.mock.web.test.MockServletContext;
@@ -36,6 +35,7 @@ import static org.junit.Assert.*;
* @author Rob Harrop * @author Rob Harrop
* @author Sam Brannen * @author Sam Brannen
*/ */
@SuppressWarnings("deprecation")
public class ExporterParameterTests extends AbstractJasperReportsTests { public class ExporterParameterTests extends AbstractJasperReportsTests {
@Test @Test
@@ -46,13 +46,12 @@ public class ExporterParameterTests extends AbstractJasperReportsTests {
AbstractJasperReportsView view = new AbstractJasperReportsView() { AbstractJasperReportsView view = new AbstractJasperReportsView() {
@Override @Override
@SuppressWarnings("deprecation")
protected void renderReport(JasperPrint filledReport, Map<String, Object> model, HttpServletResponse response) protected void renderReport(JasperPrint filledReport, Map<String, Object> model, HttpServletResponse response)
throws Exception { throws Exception {
assertEquals("Invalid number of exporter parameters", 1, getConvertedExporterParameters().size()); assertEquals("Invalid number of exporter parameters", 1, getConvertedExporterParameters().size());
JRExporterParameter key = JRHtmlExporterParameter.IMAGES_URI; net.sf.jasperreports.engine.JRExporterParameter key = net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI;
Object value = getConvertedExporterParameters().get(key); Object value = getConvertedExporterParameters().get(key);
assertNotNull("Value not mapped to correct key", value); assertNotNull("Value not mapped to correct key", value);
@@ -112,7 +111,8 @@ public class ExporterParameterTests extends AbstractJasperReportsTests {
view.setExporterParameters(params); view.setExporterParameters(params);
view.convertExporterParameters(); view.convertExporterParameters();
Object value = view.getConvertedExporterParameters().get(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN); Object value = view.getConvertedExporterParameters().get(
net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN);
assertEquals(Boolean.TRUE, value); assertEquals(Boolean.TRUE, value);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2005 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -44,40 +44,36 @@ public class JasperReportViewResolverTests {
viewResolver.setSuffix(suffix); viewResolver.setSuffix(suffix);
viewResolver.setApplicationContext(ctx); viewResolver.setApplicationContext(ctx);
AbstractJasperReportsView view = AbstractJasperReportsView view = (AbstractJasperReportsView) viewResolver.resolveViewName(viewName,
(AbstractJasperReportsView) viewResolver.resolveViewName(viewName, Locale.ENGLISH); Locale.ENGLISH);
assertNotNull("View should not be null", view); assertNotNull("View should not be null", view);
assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl()); assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl());
} }
@Test @Test(expected = IllegalArgumentException.class)
public void setIncorrectViewClass() { public void setIncorrectViewClass() {
try { // Should not be able to set view class to a class that does not extend
new JasperReportsViewResolver().setViewClass(VelocityView.class); // AbstractJasperReportsView.
fail("Should not be able to set view class to a class that does not extend AbstractJasperReportsView"); new JasperReportsViewResolver().setViewClass(VelocityView.class);
}
catch (IllegalArgumentException ex) {
// success
}
} }
@Test @Test
public void withViewNamesAndEndsWithPattern() throws Exception { public void withViewNamesAndEndsWithPattern() throws Exception {
doViewNamesTest(new String[]{"DataSource*"}); doViewNamesTest("DataSource*");
} }
@Test @Test
public void withViewNamesAndStartsWithPattern() throws Exception { public void withViewNamesAndStartsWithPattern() throws Exception {
doViewNamesTest(new String[]{"*Report"}); doViewNamesTest("*Report");
} }
@Test @Test
public void withViewNamesAndStatic() throws Exception { public void withViewNamesAndStatic() throws Exception {
doViewNamesTest(new String[]{"DataSourceReport"}); doViewNamesTest("DataSourceReport");
} }
private void doViewNamesTest(String[] viewNames) throws Exception { private void doViewNamesTest(String... viewNames) throws Exception {
StaticApplicationContext ctx = new StaticApplicationContext(); StaticApplicationContext ctx = new StaticApplicationContext();
String prefix = "org/springframework/ui/jasperreports/"; String prefix = "org/springframework/ui/jasperreports/";
String suffix = ".jasper"; String suffix = ".jasper";
@@ -90,10 +86,11 @@ public class JasperReportViewResolverTests {
viewResolver.setViewNames(viewNames); viewResolver.setViewNames(viewNames);
viewResolver.setApplicationContext(ctx); viewResolver.setApplicationContext(ctx);
AbstractJasperReportsView view = AbstractJasperReportsView view = (AbstractJasperReportsView) viewResolver.resolveViewName(viewName,
(AbstractJasperReportsView) viewResolver.resolveViewName(viewName, Locale.ENGLISH); Locale.ENGLISH);
assertNotNull("View should not be null", view); assertNotNull("View should not be null", view);
assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl()); assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl());
assertNull(viewResolver.resolveViewName("foo", Locale.ENGLISH)); assertNull(viewResolver.resolveViewName("foo", Locale.ENGLISH));
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
package org.springframework.web.servlet.view.jasperreports; package org.springframework.web.servlet.view.jasperreports;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.BeanDefinitionReader;
@@ -44,7 +43,8 @@ public class JasperReportsHtmlViewTests extends AbstractJasperReportsViewTests {
} }
@Test @Test
public void testConfigureExporterParametersWithEncodingFromPropertiesFile() throws Exception { @SuppressWarnings("deprecation")
public void configureExporterParametersWithEncodingFromPropertiesFile() throws Exception {
GenericWebApplicationContext ac = new GenericWebApplicationContext(); GenericWebApplicationContext ac = new GenericWebApplicationContext();
ac.setServletContext(new MockServletContext()); ac.setServletContext(new MockServletContext());
BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(ac); BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(ac);
@@ -52,7 +52,8 @@ public class JasperReportsHtmlViewTests extends AbstractJasperReportsViewTests {
ac.refresh(); ac.refresh();
AbstractJasperReportsView view = (AbstractJasperReportsView) ac.getBean("report"); AbstractJasperReportsView view = (AbstractJasperReportsView) ac.getBean("report");
String encoding = (String) view.getConvertedExporterParameters().get(JRHtmlExporterParameter.CHARACTER_ENCODING); String encoding = (String) view.getConvertedExporterParameters().get(
net.sf.jasperreports.engine.export.JRHtmlExporterParameter.CHARACTER_ENCODING);
assertEquals("UTF-8", encoding); assertEquals("UTF-8", encoding);
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,19 +19,23 @@ package org.springframework.web.servlet.view.jasperreports;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperPrint;
import org.junit.Test; import org.junit.Test;
import org.springframework.tests.Assume; import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup; import org.springframework.tests.TestGroup;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.junit.Assume.*;
/** /**
* @author Rob Harrop * @author Rob Harrop
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
*/ */
public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsViewTests { public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsViewTests {
@@ -41,12 +45,9 @@ public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsView
} }
@Test @Test
public void testSimpleHtmlRender() throws Exception { public void simpleHtmlRender() throws Exception {
Assume.group(TestGroup.CUSTOM_COMPILATION); Assume.group(TestGroup.CUSTOM_COMPILATION);
assumeTrue(canCompileReport);
if (!canCompileReport) {
return;
}
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
@@ -58,14 +59,11 @@ public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsView
assertEquals("Invalid content type", "text/html", response.getContentType()); assertEquals("Invalid content type", "text/html", response.getContentType());
} }
@Override
@Test @Test
public void testOverrideContentDisposition() throws Exception { @Override
public void overrideContentDisposition() throws Exception {
Assume.group(TestGroup.CUSTOM_COMPILATION); Assume.group(TestGroup.CUSTOM_COMPILATION);
assumeTrue(canCompileReport);
if (!canCompileReport) {
return;
}
AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); AbstractJasperReportsView view = getView(UNCOMPILED_REPORT);
@@ -86,12 +84,9 @@ public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsView
} }
@Test @Test
public void testExporterParametersAreCarriedAcross() throws Exception { public void exporterParametersAreCarriedAcross() throws Exception {
Assume.group(TestGroup.CUSTOM_COMPILATION); Assume.group(TestGroup.CUSTOM_COMPILATION);
assumeTrue(canCompileReport);
if (!canCompileReport) {
return;
}
JasperReportsMultiFormatView view = (JasperReportsMultiFormatView) getView(UNCOMPILED_REPORT); JasperReportsMultiFormatView view = (JasperReportsMultiFormatView) getView(UNCOMPILED_REPORT);