Revised ExcelViewTests
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -17,16 +17,18 @@
|
||||
package org.springframework.web.servlet.view.document;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import jxl.Cell;
|
||||
import jxl.Sheet;
|
||||
import jxl.Workbook;
|
||||
import jxl.WorkbookSettings;
|
||||
import jxl.read.biff.WorkbookParser;
|
||||
import jxl.write.Label;
|
||||
import jxl.write.WritableSheet;
|
||||
import jxl.write.WritableWorkbook;
|
||||
@@ -35,28 +37,37 @@ import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for the AbstractExcelView and the AbstractJExcelView classes.
|
||||
*
|
||||
* @author Alef Arendsen
|
||||
* @author Bram Smeets
|
||||
*/
|
||||
public class ExcelViewTests extends TestCase {
|
||||
public class ExcelViewTests {
|
||||
|
||||
private MockServletContext servletCtx;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
private StaticWebApplicationContext webAppCtx;
|
||||
|
||||
@Override
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
servletCtx = new MockServletContext("org/springframework/web/servlet/view/document");
|
||||
request = new MockHttpServletRequest(servletCtx);
|
||||
@@ -65,6 +76,8 @@ public class ExcelViewTests extends TestCase {
|
||||
webAppCtx.setServletContext(servletCtx);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testExcel() throws Exception {
|
||||
AbstractExcelView excelView = new AbstractExcelView() {
|
||||
@Override
|
||||
@@ -94,6 +107,7 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Value", cell.getStringCellValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcelWithTemplateNoLoc() throws Exception {
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
|
||||
newDummyLocaleResolver("nl", "nl"));
|
||||
@@ -127,6 +141,7 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Template", cell.getStringCellValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcelWithTemplateAndCountryAndLanguage() throws Exception {
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
|
||||
newDummyLocaleResolver("en", "US"));
|
||||
@@ -160,6 +175,7 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Template American English", cell.getStringCellValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcelWithTemplateAndLanguage() throws Exception {
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
|
||||
newDummyLocaleResolver("de", ""));
|
||||
@@ -193,8 +209,9 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Template auf Deutsch", cell.getStringCellValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJExcel() throws Exception {
|
||||
AbstractJExcelView excelView = new AbstractJExcelView() {
|
||||
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
|
||||
@Override
|
||||
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
@@ -216,11 +233,12 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Value", cell.getContents());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJExcelWithTemplateNoLoc() throws Exception {
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
|
||||
newDummyLocaleResolver("nl", "nl"));
|
||||
|
||||
AbstractJExcelView excelView = new AbstractJExcelView() {
|
||||
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
|
||||
@Override
|
||||
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
@@ -243,11 +261,12 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Template", cell.getContents());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJExcelWithTemplateAndCountryAndLanguage() throws Exception {
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
|
||||
newDummyLocaleResolver("en", "US"));
|
||||
|
||||
AbstractJExcelView excelView = new AbstractJExcelView() {
|
||||
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
|
||||
@Override
|
||||
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
@@ -270,11 +289,12 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Template American English", cell.getContents());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJExcelWithTemplateAndLanguage() throws Exception {
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
|
||||
newDummyLocaleResolver("de", ""));
|
||||
|
||||
AbstractJExcelView excelView = new AbstractJExcelView() {
|
||||
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
|
||||
@Override
|
||||
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
@@ -297,6 +317,7 @@ public class ExcelViewTests extends TestCase {
|
||||
assertEquals("Test Template auf Deutsch", cell.getContents());
|
||||
}
|
||||
|
||||
|
||||
private LocaleResolver newDummyLocaleResolver(final String lang, final String country) {
|
||||
return new LocaleResolver() {
|
||||
@Override
|
||||
@@ -310,4 +331,25 @@ public class ExcelViewTests extends TestCase {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Workaround JXL bug that causes ArrayIndexOutOfBounds exceptions when running in
|
||||
* *nix machines. Same bug as reported at http://jira.pentaho.com/browse/PDI-5031.
|
||||
* <p>We want to use the latest JXL code because it doesn't include log4j config files
|
||||
* inside the jar. Since the project appears to be abandoned, AbstractJExcelView will
|
||||
* eventually get deprecated.
|
||||
*/
|
||||
private static abstract class UnixSafeAbstractJExcelView extends AbstractJExcelView {
|
||||
|
||||
@Override
|
||||
protected Workbook getTemplateSource(String url, HttpServletRequest request) throws Exception {
|
||||
Workbook workbook = super.getTemplateSource(url, request);
|
||||
Field field = WorkbookParser.class.getDeclaredField("settings");
|
||||
field.setAccessible(true);
|
||||
WorkbookSettings settings = (WorkbookSettings) ReflectionUtils.getField(field, workbook);
|
||||
settings.setWriteAccess(null);
|
||||
return workbook;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user