Skip to content

Commit b92e429

Browse files
author
Phillip Webb
committed
Upgrade to jexcelapi 2.6.12 and fix test failures
Upgrade to the latest release of jexcelapi and work-around the ArrayIndexOutOfBounds test exception on *nix machines. Issue: SPR-11334
1 parent 709ac28 commit b92e429

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ project("spring-webmvc") {
668668
}
669669
optional("rome:rome:1.0")
670670
optional("com.lowagie:itext:2.1.7")
671-
optional("net.sourceforge.jexcelapi:jxl:2.6.3")
671+
optional("net.sourceforge.jexcelapi:jxl:2.6.12")
672672
optional("org.apache.poi:poi:3.9")
673673
optional("org.apache.velocity:velocity:1.7")
674674
optional("velocity-tools:velocity-tools-view:1.4")

spring-webmvc/src/test/java/org/springframework/web/servlet/view/document/ExcelViewTests.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.web.servlet.view.document;
1818

1919
import java.io.ByteArrayInputStream;
20+
import java.lang.reflect.Field;
2021
import java.util.HashMap;
2122
import java.util.Locale;
2223
import java.util.Map;
@@ -27,18 +28,21 @@
2728
import jxl.Cell;
2829
import jxl.Sheet;
2930
import jxl.Workbook;
31+
import jxl.WorkbookSettings;
32+
import jxl.read.biff.WorkbookParser;
3033
import jxl.write.Label;
3134
import jxl.write.WritableSheet;
3235
import jxl.write.WritableWorkbook;
36+
3337
import org.apache.poi.hssf.usermodel.HSSFCell;
3438
import org.apache.poi.hssf.usermodel.HSSFRow;
3539
import org.apache.poi.hssf.usermodel.HSSFSheet;
3640
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
3741
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
38-
3942
import org.springframework.mock.web.test.MockHttpServletRequest;
4043
import org.springframework.mock.web.test.MockHttpServletResponse;
4144
import org.springframework.mock.web.test.MockServletContext;
45+
import org.springframework.util.ReflectionUtils;
4246
import org.springframework.web.context.support.StaticWebApplicationContext;
4347
import org.springframework.web.servlet.DispatcherServlet;
4448
import org.springframework.web.servlet.LocaleResolver;
@@ -194,7 +198,7 @@ protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
194198
}
195199

196200
public void testJExcel() throws Exception {
197-
AbstractJExcelView excelView = new AbstractJExcelView() {
201+
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
198202
@Override
199203
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
200204
HttpServletRequest request, HttpServletResponse response) throws Exception {
@@ -220,7 +224,7 @@ public void testJExcelWithTemplateNoLoc() throws Exception {
220224
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
221225
newDummyLocaleResolver("nl", "nl"));
222226

223-
AbstractJExcelView excelView = new AbstractJExcelView() {
227+
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
224228
@Override
225229
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
226230
HttpServletRequest request, HttpServletResponse response) throws Exception {
@@ -247,7 +251,7 @@ public void testJExcelWithTemplateAndCountryAndLanguage() throws Exception {
247251
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
248252
newDummyLocaleResolver("en", "US"));
249253

250-
AbstractJExcelView excelView = new AbstractJExcelView() {
254+
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
251255
@Override
252256
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
253257
HttpServletRequest request, HttpServletResponse response) throws Exception {
@@ -274,7 +278,7 @@ public void testJExcelWithTemplateAndLanguage() throws Exception {
274278
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
275279
newDummyLocaleResolver("de", ""));
276280

277-
AbstractJExcelView excelView = new AbstractJExcelView() {
281+
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
278282
@Override
279283
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
280284
HttpServletRequest request, HttpServletResponse response) throws Exception {
@@ -310,4 +314,27 @@ public void setLocale(HttpServletRequest request, HttpServletResponse response,
310314
};
311315
}
312316

317+
318+
/**
319+
* Workaround JXL bug that causes ArrayIndexOutOfBounds exceptions when running in
320+
* *nix machines. Same bug as reported at http://jira.pentaho.com/browse/PDI-5031.
321+
*
322+
* We want to use the latest JXL code because it doesn't include log4j config files
323+
* inside the jar. Since the project appears to be abandoned AbstractJExcelView
324+
* will be deprecated.
325+
*/
326+
private static abstract class UnixSafeAbstractJExcelView extends AbstractJExcelView {
327+
328+
@Override
329+
protected Workbook getTemplateSource(String url, HttpServletRequest request)
330+
throws Exception {
331+
Workbook workbook = super.getTemplateSource(url, request);
332+
Field field = WorkbookParser.class.getDeclaredField("settings");
333+
field.setAccessible(true);
334+
WorkbookSettings settings = (WorkbookSettings) ReflectionUtils.getField(field, workbook);
335+
settings.setWriteAccess(null);
336+
return workbook;
337+
}
338+
339+
}
313340
}

0 commit comments

Comments
 (0)