Workaround jasper report test fail on OSX

Add temporary Assume.canLoadNativeDirFonts() method allowing failing
jasper report tests to be bypassed on OSX.

This should be revisited when JDK 8 is released.

Issue: SPR-10537
This commit is contained in:
Phillip Webb
2013-05-07 08:21:17 -07:00
parent 043aafed86
commit e1c25ff1a3
8 changed files with 111 additions and 13 deletions

View File

@@ -26,7 +26,6 @@ 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;
@@ -42,20 +41,31 @@ 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.junit.BeforeClass;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.tests.Assume;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
* @author Juergen Hoeller
* @since 18.11.2004
*/
public class JasperReportsUtilsTests extends TestCase {
public class JasperReportsUtilsTests {
@BeforeClass
public static void assumptions() {
Assume.canLoadNativeDirFonts();
}
@Test
public void testRenderAsCsvWithDataSource() throws Exception {
StringWriter writer = new StringWriter();
JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getDataSource(), writer);
@@ -63,6 +73,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertCsvOutputCorrect(output);
}
@Test
public void testRenderAsCsvWithCollection() throws Exception {
StringWriter writer = new StringWriter();
JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getData(), writer);
@@ -70,6 +81,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertCsvOutputCorrect(output);
}
@Test
public void testRenderAsCsvWithExporterParameters() throws Exception {
StringWriter writer = new StringWriter();
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
@@ -80,6 +92,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertTrue("Delimiter is incorrect", output.contains("~"));
}
@Test
public void testRenderAsHtmlWithDataSource() throws Exception {
StringWriter writer = new StringWriter();
JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getDataSource(), writer);
@@ -87,6 +100,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertHtmlOutputCorrect(output);
}
@Test
public void testRenderAsHtmlWithCollection() throws Exception {
StringWriter writer = new StringWriter();
JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getData(), writer);
@@ -94,6 +108,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertHtmlOutputCorrect(output);
}
@Test
public void testRenderAsHtmlWithExporterParameters() throws Exception {
StringWriter writer = new StringWriter();
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
@@ -105,6 +120,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertTrue("URI not included", output.contains(uri));
}
@Test
public void testRenderAsPdfWithDataSource() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getDataSource(), os);
@@ -112,6 +128,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertPdfOutputCorrect(output);
}
@Test
public void testRenderAsPdfWithCollection() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getData(), os);
@@ -119,6 +136,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertPdfOutputCorrect(output);
}
@Test
public void testRenderAsPdfWithExporterParameters() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
@@ -129,6 +147,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertTrue(new String(output).contains("PDF-1.6"));
}
@Test
public void testRenderAsXlsWithDataSource() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JasperReportsUtils.renderAsXls(getReport(), getParameters(), getDataSource(), os);
@@ -136,6 +155,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertXlsOutputCorrect(output);
}
@Test
public void testRenderAsXlsWithCollection() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JasperReportsUtils.renderAsXls(getReport(), getParameters(), getData(), os);
@@ -143,6 +163,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertXlsOutputCorrect(output);
}
@Test
public void testRenderAsXlsWithExporterParameters() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>();
@@ -156,6 +177,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertTrue(monitor.isInvoked());
}
@Test
public void testRenderWithWriter() throws Exception {
StringWriter writer = new StringWriter();
JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource());
@@ -164,6 +186,7 @@ public class JasperReportsUtilsTests extends TestCase {
assertHtmlOutputCorrect(output);
}
@Test
public void testRenderWithOutputStream() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource());

View File

@@ -16,12 +16,15 @@
package org.springframework.tests;
import static org.junit.Assume.assumeFalse;
import java.awt.GraphicsEnvironment;
import java.lang.reflect.Method;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.junit.internal.AssumptionViolatedException;
import org.springframework.util.ClassUtils;
import static org.junit.Assume.*;
/**
* Provides utility methods that allow JUnit tests to {@link org.junit.Assume} certain
@@ -29,14 +32,14 @@ import org.junit.internal.AssumptionViolatedException;
* skipped.
*
* <p>For example, if a set of tests require at least JDK 1.7 it can use
* {@code Assume#atLeast(JdkVersion.JAVA_17)} as shown below:
* {@code Assume#atLeast(JavaVersion.JAVA_17)} as shown below:
*
* <pre class="code">
* public void MyTests {
*
* &#064;BeforeClass
* public static assumptions() {
* Assume.atLeast(JdkVersion.JAVA_17);
* public static void assumptions() {
* Assume.atLeast(JavaVersion.JAVA_17);
* }
*
* // ... all the test methods that require at least JDK 1.7
@@ -44,14 +47,14 @@ import org.junit.internal.AssumptionViolatedException;
* </pre>
*
* If only a single test requires at least JDK 1.7 it can use the
* {@code Assume#atLeast(JdkVersion.JAVA_17)} as shown below:
* {@code Assume#atLeast(JavaVersion.JAVA_17)} as shown below:
*
* <pre class="code">
* public void MyTests {
*
* &#064;Test
* public void requiresJdk17 {
* Assume.atLeast(JdkVersion.JAVA_17);
* Assume.atLeast(JavaVersion.JAVA_17);
* // ... perform the actual test
* }
* }
@@ -109,4 +112,20 @@ public abstract class Assume {
assumeFalse(log.isTraceEnabled());
assumeFalse(log.isDebugEnabled());
}
/**
* Assume that we can load fonts (https://java.net/jira/browse/MACOSX_PORT-355)
*/
public static void canLoadNativeDirFonts() {
try {
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
Class<?> parserClass = ClassUtils.forName("net.sf.jasperreports.engine.util.JRStyledTextParser", null);
Method method = parserClass.getMethod("getInstance");
method.setAccessible(true);
method.invoke(null);
} catch(Throwable ex) {
throw new AssumptionViolatedException(
"Requires GraphicsEnvironment that can load fonts.", ex);
}
}
}

View File

@@ -16,13 +16,17 @@
package org.springframework.web.servlet.view.jasperreports;
import org.junit.Test;
import org.springframework.context.support.StaticApplicationContext;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
*/
public abstract class AbstractConfigurableJasperReportsViewTests extends AbstractJasperReportsViewTests {
@Test
public void testNoConfiguredExporter() throws Exception {
ConfigurableJasperReportsView view = new ConfigurableJasperReportsView();
view.setUrl(COMPILED_REPORT);

View File

@@ -22,11 +22,13 @@ 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.junit.Before;
import org.junit.BeforeClass;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.tests.Assume;
import org.springframework.ui.jasperreports.PersonBean;
import org.springframework.ui.jasperreports.ProductBean;
import org.springframework.util.ClassUtils;
@@ -37,7 +39,7 @@ import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
* @author Rob Harrop
* @author Juergen Hoeller
*/
public abstract class AbstractJasperReportsTests extends TestCase {
public abstract class AbstractJasperReportsTests {
protected static final String COMPILED_REPORT = "/org/springframework/ui/jasperreports/DataSourceReport.jasper";
@@ -54,7 +56,12 @@ public abstract class AbstractJasperReportsTests extends TestCase {
protected MockHttpServletResponse response;
@Override
@BeforeClass
public static void assumptions() {
Assume.canLoadNativeDirFonts();
}
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();

View File

@@ -34,12 +34,14 @@ import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.junit.Ignore;
import org.junit.Test;
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.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
@@ -63,6 +65,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
/**
* Simple test to see if compiled report succeeds.
*/
@Test
public void testCompiledReport() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT);
view.render(getModel(), request, response);
@@ -74,6 +77,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
}
}
@Test
public void testUncompiledReport() throws Exception {
if (!canCompileReport) {
return;
@@ -84,6 +88,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getContentAsByteArray().length > 0);
}
@Test
public void testWithInvalidPath() throws Exception {
try {
getView("foo.jasper");
@@ -94,6 +99,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
}
}
@Test
public void testInvalidExtension() throws Exception {
try {
getView("foo.bar");
@@ -104,12 +110,14 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
}
}
@Test
public void testContentType() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT);
view.render(getModel(), request, response);
assertEquals("Response content type is incorrect", getDesiredContentType(), response.getContentType());
}
@Test
public void testWithoutDatasource() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -118,6 +126,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getStatus() == HttpServletResponse.SC_OK);
}
@Test
public void testWithCollection() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -127,6 +136,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getContentAsByteArray().length > 0);
}
@Test
public void testWithMultipleCollections() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -137,6 +147,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
// no clear data source found
}
@Test
public void testWithJRDataSourceProvider() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -146,6 +157,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getContentAsByteArray().length > 0);
}
@Test
public void testWithSpecificCollection() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -157,6 +169,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getContentAsByteArray().length > 0);
}
@Test
public void testWithArray() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -166,6 +179,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getContentAsByteArray().length > 0);
}
@Test
public void testWithMultipleArrays() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -176,6 +190,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
// no clear data source found
}
@Test
public void testWithSpecificArray() throws Exception {
Map model = getModel();
model.remove("dataSource");
@@ -187,6 +202,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getContentAsByteArray().length > 0);
}
@Test
public void testWithSubReport() throws Exception {
if (!canCompileReport) {
return;
@@ -208,6 +224,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertTrue(response.getContentAsByteArray().length > 0);
}
@Test
public void testWithNonExistentSubReport() throws Exception {
if (!canCompileReport) {
return;
@@ -258,6 +275,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertEquals(overiddenCharacterEncoding, this.response.getCharacterEncoding());
}
@Test
public void testSubReportWithUnspecifiedParentDataSource() throws Exception {
if (!canCompileReport) {
return;
@@ -282,6 +300,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
}
}
@Test
public void testContentDisposition() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT);
view.render(getModel(), request, response);
@@ -289,6 +308,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
}
@Test
public void testOverrideContentDisposition() throws Exception {
Properties headers = new Properties();
String cd = "attachment";
@@ -300,6 +320,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertEquals("Invalid content type", cd, response.getHeader("Content-Disposition"));
}
@Test
public void testSetCustomHeaders() throws Exception {
Properties headers = new Properties();
@@ -316,6 +337,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
assertEquals("Invalid header value", value, response.getHeader(key));
}
@Test
public void testWithJdbcDataSource() throws Exception {
if (!canCompileReport) {
return;
@@ -336,6 +358,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
}
}
@Test
public void testWithJdbcDataSourceInModel() throws Exception {
if (!canCompileReport) {
return;
@@ -356,6 +379,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
}
}
@Test
public void testJRDataSourceOverridesJdbcDataSource() throws Exception {
if (!canCompileReport) {
return;
@@ -378,6 +402,7 @@ public abstract class AbstractJasperReportsViewTests extends AbstractJasperRepor
return ds;
}
@Test
public void testWithCharacterEncoding() throws Exception {
AbstractJasperReportsView view = getView(COMPILED_REPORT);

View File

@@ -27,15 +27,19 @@ import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import org.junit.Test;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
*/
public class ExporterParameterTests extends AbstractJasperReportsTests {
@Test
public void testParameterParsing() throws Exception {
Map params = new HashMap();
params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI", "/foo/bar");
@@ -84,6 +88,7 @@ public class ExporterParameterTests extends AbstractJasperReportsTests {
view.render(getModel(), request, response);
}
@Test
public void testInvalidClass() throws Exception {
Map params = new HashMap();
params.put("foo.net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI", "/foo");
@@ -101,6 +106,7 @@ public class ExporterParameterTests extends AbstractJasperReportsTests {
}
}
@Test
public void testInvalidField() {
Map params = new HashMap();
params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI_FOO", "/foo");
@@ -118,6 +124,7 @@ public class ExporterParameterTests extends AbstractJasperReportsTests {
}
}
@Test
public void testInvalidType() {
Map params = new HashMap();
params.put("java.lang.Boolean.TRUE", "/foo");
@@ -136,6 +143,7 @@ public class ExporterParameterTests extends AbstractJasperReportsTests {
}
@Test
public void testTypeConversion() {
Map params = new HashMap();
params.put("net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN", "true");

View File

@@ -18,6 +18,7 @@ package org.springframework.web.servlet.view.jasperreports;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
@@ -25,6 +26,8 @@ import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
*/
@@ -40,6 +43,7 @@ public class JasperReportsHtmlViewTests extends AbstractJasperReportsViewTests {
return "text/html";
}
@Test
public void testConfigureExporterParametersWithEncodingFromPropertiesFile() throws Exception {
GenericWebApplicationContext ac = new GenericWebApplicationContext();
ac.setServletContext(new MockServletContext());

View File

@@ -19,10 +19,15 @@ 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;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
* @author Juergen Hoeller
@@ -34,6 +39,7 @@ public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsView
model.put(getDiscriminatorKey(), "csv");
}
@Test
public void testSimpleHtmlRender() throws Exception {
if (!canCompileReport) {
return;
@@ -50,6 +56,7 @@ public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsView
}
@Override
@Test
public void testOverrideContentDisposition() throws Exception {
if (!canCompileReport) {
return;
@@ -73,6 +80,7 @@ public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsView
response.getHeader("Content-Disposition"));
}
@Test
public void testExporterParametersAreCarriedAcross() throws Exception {
if (!canCompileReport) {
return;