PageRenderTime 27ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/sidneyzhang/spring-framework
Java | 342 lines | 262 code | 50 blank | 30 comment | 0 complexity | 6f953e32079ca5e4e47a91250f382d85 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * Copyright 2002-2012 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.view.document;
  17. import java.io.ByteArrayInputStream;
  18. import java.util.HashMap;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import junit.framework.TestCase;
  24. import jxl.Cell;
  25. import jxl.Sheet;
  26. import jxl.Workbook;
  27. import jxl.write.Label;
  28. import jxl.write.WritableSheet;
  29. import jxl.write.WritableWorkbook;
  30. import org.apache.poi.hssf.usermodel.HSSFCell;
  31. import org.apache.poi.hssf.usermodel.HSSFRow;
  32. import org.apache.poi.hssf.usermodel.HSSFSheet;
  33. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  34. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  35. import org.springframework.mock.web.test.MockHttpServletRequest;
  36. import org.springframework.mock.web.test.MockHttpServletResponse;
  37. import org.springframework.mock.web.test.MockServletContext;
  38. import org.springframework.web.context.support.StaticWebApplicationContext;
  39. import org.springframework.web.servlet.DispatcherServlet;
  40. import org.springframework.web.servlet.LocaleResolver;
  41. /**
  42. * Tests for the AbstractExcelView and the AbstractJExcelView classes.
  43. *
  44. * @author Alef Arendsen
  45. * @author Bram Smeets
  46. */
  47. public class ExcelViewTests extends TestCase {
  48. private MockServletContext servletCtx;
  49. private MockHttpServletRequest request;
  50. private MockHttpServletResponse response;
  51. private StaticWebApplicationContext webAppCtx;
  52. @Override
  53. public void setUp() {
  54. servletCtx = new MockServletContext("org/springframework/web/servlet/view/document");
  55. request = new MockHttpServletRequest(servletCtx);
  56. response = new MockHttpServletResponse();
  57. webAppCtx = new StaticWebApplicationContext();
  58. webAppCtx.setServletContext(servletCtx);
  59. }
  60. public void testExcel() throws Exception {
  61. AbstractExcelView excelView = new AbstractExcelView() {
  62. @Override
  63. protected void buildExcelDocument(Map model, HSSFWorkbook wb,
  64. HttpServletRequest request, HttpServletResponse response)
  65. throws Exception {
  66. HSSFSheet sheet = wb.createSheet();
  67. wb.setSheetName(0, "Test Sheet");
  68. // test all possible permutation of row or column not existing
  69. HSSFCell cell = getCell(sheet, 2, 4);
  70. cell.setCellValue("Test Value");
  71. cell = getCell(sheet, 2, 3);
  72. setText(cell, "Test Value");
  73. cell = getCell(sheet, 3, 4);
  74. setText(cell, "Test Value");
  75. cell = getCell(sheet, 2, 4);
  76. setText(cell, "Test Value");
  77. }
  78. };
  79. excelView.render(new HashMap(), request, response);
  80. POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
  81. HSSFWorkbook wb = new HSSFWorkbook(poiFs);
  82. assertEquals("Test Sheet", wb.getSheetName(0));
  83. HSSFSheet sheet = wb.getSheet("Test Sheet");
  84. HSSFRow row = sheet.getRow(2);
  85. HSSFCell cell = row.getCell((short) 4);
  86. assertEquals("Test Value", cell.getStringCellValue());
  87. }
  88. public void testExcelWithTemplateNoLoc() throws Exception {
  89. request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
  90. newDummyLocaleResolver("nl", "nl"));
  91. AbstractExcelView excelView = new AbstractExcelView() {
  92. @Override
  93. protected void buildExcelDocument(Map model, HSSFWorkbook wb,
  94. HttpServletRequest request, HttpServletResponse response)
  95. throws Exception {
  96. HSSFSheet sheet = wb.getSheet("Sheet1");
  97. // test all possible permutation of row or column not existing
  98. HSSFCell cell = getCell(sheet, 2, 4);
  99. cell.setCellValue("Test Value");
  100. cell = getCell(sheet, 2, 3);
  101. setText(cell, "Test Value");
  102. cell = getCell(sheet, 3, 4);
  103. setText(cell, "Test Value");
  104. cell = getCell(sheet, 2, 4);
  105. setText(cell, "Test Value");
  106. }
  107. };
  108. excelView.setApplicationContext(webAppCtx);
  109. excelView.setUrl("template");
  110. excelView.render(new HashMap(), request, response);
  111. POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
  112. HSSFWorkbook wb = new HSSFWorkbook(poiFs);
  113. HSSFSheet sheet = wb.getSheet("Sheet1");
  114. HSSFRow row = sheet.getRow(0);
  115. HSSFCell cell = row.getCell((short) 0);
  116. assertEquals("Test Template", cell.getStringCellValue());
  117. }
  118. public void testExcelWithTemplateAndCountryAndLanguage() throws Exception {
  119. request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
  120. newDummyLocaleResolver("en", "US"));
  121. AbstractExcelView excelView = new AbstractExcelView() {
  122. @Override
  123. protected void buildExcelDocument(Map model, HSSFWorkbook wb,
  124. HttpServletRequest request, HttpServletResponse response)
  125. throws Exception {
  126. HSSFSheet sheet = wb.getSheet("Sheet1");
  127. // test all possible permutation of row or column not existing
  128. HSSFCell cell = getCell(sheet, 2, 4);
  129. cell.setCellValue("Test Value");
  130. cell = getCell(sheet, 2, 3);
  131. setText(cell, "Test Value");
  132. cell = getCell(sheet, 3, 4);
  133. setText(cell, "Test Value");
  134. cell = getCell(sheet, 2, 4);
  135. setText(cell, "Test Value");
  136. }
  137. };
  138. excelView.setApplicationContext(webAppCtx);
  139. excelView.setUrl("template");
  140. excelView.render(new HashMap(), request, response);
  141. POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
  142. HSSFWorkbook wb = new HSSFWorkbook(poiFs);
  143. HSSFSheet sheet = wb.getSheet("Sheet1");
  144. HSSFRow row = sheet.getRow(0);
  145. HSSFCell cell = row.getCell((short) 0);
  146. assertEquals("Test Template American English", cell.getStringCellValue());
  147. }
  148. public void testExcelWithTemplateAndLanguage() throws Exception {
  149. request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
  150. newDummyLocaleResolver("de", ""));
  151. AbstractExcelView excelView = new AbstractExcelView() {
  152. @Override
  153. protected void buildExcelDocument(Map model, HSSFWorkbook wb,
  154. HttpServletRequest request, HttpServletResponse response)
  155. throws Exception {
  156. HSSFSheet sheet = wb.getSheet("Sheet1");
  157. // test all possible permutation of row or column not existing
  158. HSSFCell cell = getCell(sheet, 2, 4);
  159. cell.setCellValue("Test Value");
  160. cell = getCell(sheet, 2, 3);
  161. setText(cell, "Test Value");
  162. cell = getCell(sheet, 3, 4);
  163. setText(cell, "Test Value");
  164. cell = getCell(sheet, 2, 4);
  165. setText(cell, "Test Value");
  166. }
  167. };
  168. excelView.setApplicationContext(webAppCtx);
  169. excelView.setUrl("template");
  170. excelView.render(new HashMap(), request, response);
  171. POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray()));
  172. HSSFWorkbook wb = new HSSFWorkbook(poiFs);
  173. HSSFSheet sheet = wb.getSheet("Sheet1");
  174. HSSFRow row = sheet.getRow(0);
  175. HSSFCell cell = row.getCell((short) 0);
  176. assertEquals("Test Template auf Deutsch", cell.getStringCellValue());
  177. }
  178. public void testJExcel() throws Exception {
  179. AbstractJExcelView excelView = new AbstractJExcelView() {
  180. @Override
  181. protected void buildExcelDocument(Map model,
  182. WritableWorkbook wb,
  183. HttpServletRequest request,
  184. HttpServletResponse response)
  185. throws Exception {
  186. WritableSheet sheet = wb.createSheet("Test Sheet", 0);
  187. // test all possible permutation of row or column not existing
  188. sheet.addCell(new Label(2, 4, "Test Value"));
  189. sheet.addCell(new Label(2, 3, "Test Value"));
  190. sheet.addCell(new Label(3, 4, "Test Value"));
  191. sheet.addCell(new Label(2, 4, "Test Value"));
  192. }
  193. };
  194. excelView.render(new HashMap(), request, response);
  195. Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  196. assertEquals("Test Sheet", wb.getSheet(0).getName());
  197. Sheet sheet = wb.getSheet("Test Sheet");
  198. Cell cell = sheet.getCell(2, 4);
  199. assertEquals("Test Value", cell.getContents());
  200. }
  201. public void testJExcelWithTemplateNoLoc() throws Exception {
  202. request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
  203. newDummyLocaleResolver("nl", "nl"));
  204. AbstractJExcelView excelView = new AbstractJExcelView() {
  205. @Override
  206. protected void buildExcelDocument(Map model,
  207. WritableWorkbook wb,
  208. HttpServletRequest request,
  209. HttpServletResponse response)
  210. throws Exception {
  211. WritableSheet sheet = wb.getSheet("Sheet1");
  212. // test all possible permutation of row or column not existing
  213. sheet.addCell(new Label(2, 4, "Test Value"));
  214. sheet.addCell(new Label(2, 3, "Test Value"));
  215. sheet.addCell(new Label(3, 4, "Test Value"));
  216. sheet.addCell(new Label(2, 4, "Test Value"));
  217. }
  218. };
  219. excelView.setApplicationContext(webAppCtx);
  220. excelView.setUrl("template");
  221. excelView.render(new HashMap(), request, response);
  222. Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  223. Sheet sheet = wb.getSheet("Sheet1");
  224. Cell cell = sheet.getCell(0, 0);
  225. assertEquals("Test Template", cell.getContents());
  226. }
  227. public void testJExcelWithTemplateAndCountryAndLanguage() throws Exception {
  228. request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
  229. newDummyLocaleResolver("en", "US"));
  230. AbstractJExcelView excelView = new AbstractJExcelView() {
  231. @Override
  232. protected void buildExcelDocument(Map model,
  233. WritableWorkbook wb,
  234. HttpServletRequest request,
  235. HttpServletResponse response)
  236. throws Exception {
  237. WritableSheet sheet = wb.getSheet("Sheet1");
  238. // test all possible permutation of row or column not existing
  239. sheet.addCell(new Label(2, 4, "Test Value"));
  240. sheet.addCell(new Label(2, 3, "Test Value"));
  241. sheet.addCell(new Label(3, 4, "Test Value"));
  242. sheet.addCell(new Label(2, 4, "Test Value"));
  243. }
  244. };
  245. excelView.setApplicationContext(webAppCtx);
  246. excelView.setUrl("template");
  247. excelView.render(new HashMap(), request, response);
  248. Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  249. Sheet sheet = wb.getSheet("Sheet1");
  250. Cell cell = sheet.getCell(0, 0);
  251. assertEquals("Test Template American English", cell.getContents());
  252. }
  253. public void testJExcelWithTemplateAndLanguage() throws Exception {
  254. request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
  255. newDummyLocaleResolver("de", ""));
  256. AbstractJExcelView excelView = new AbstractJExcelView() {
  257. @Override
  258. protected void buildExcelDocument(Map model,
  259. WritableWorkbook wb,
  260. HttpServletRequest request,
  261. HttpServletResponse response)
  262. throws Exception {
  263. WritableSheet sheet = wb.getSheet("Sheet1");
  264. // test all possible permutation of row or column not existing
  265. sheet.addCell(new Label(2, 4, "Test Value"));
  266. sheet.addCell(new Label(2, 3, "Test Value"));
  267. sheet.addCell(new Label(3, 4, "Test Value"));
  268. sheet.addCell(new Label(2, 4, "Test Value"));
  269. }
  270. };
  271. excelView.setApplicationContext(webAppCtx);
  272. excelView.setUrl("template");
  273. excelView.render(new HashMap(), request, response);
  274. Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  275. Sheet sheet = wb.getSheet("Sheet1");
  276. Cell cell = sheet.getCell(0, 0);
  277. assertEquals("Test Template auf Deutsch", cell.getContents());
  278. }
  279. private LocaleResolver newDummyLocaleResolver(final String lang, final String country) {
  280. return new LocaleResolver() {
  281. @Override
  282. public Locale resolveLocale(HttpServletRequest request) {
  283. return new Locale(lang, country);
  284. }
  285. @Override
  286. public void setLocale(HttpServletRequest request,
  287. HttpServletResponse response, Locale locale) {
  288. // not supported!
  289. }
  290. };
  291. }
  292. }