1 16 package org.springframework.web.servlet.view.document; 17 18 import java.io.ByteArrayInputStream ; 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import junit.framework.TestCase; 27 import org.apache.poi.hssf.usermodel.HSSFCell; 28 import org.apache.poi.hssf.usermodel.HSSFRow; 29 import org.apache.poi.hssf.usermodel.HSSFSheet; 30 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 31 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 32 33 import org.springframework.mock.web.MockHttpServletRequest; 34 import org.springframework.mock.web.MockHttpServletResponse; 35 import org.springframework.mock.web.MockServletContext; 36 import org.springframework.web.context.support.StaticWebApplicationContext; 37 import org.springframework.web.servlet.DispatcherServlet; 38 import org.springframework.web.servlet.LocaleResolver; 39 40 43 public class ExcelViewTests extends TestCase { 44 45 private MockServletContext servletCtx; 46 47 private MockHttpServletRequest request; 48 49 private MockHttpServletResponse response; 50 51 private StaticWebApplicationContext webAppCtx; 52 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 61 public void testExcel() throws Exception { 62 AbstractExcelView excelView = new AbstractExcelView() { 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 69 HSSFCell cell = getCell(sheet, 2, 4); 71 cell.setCellValue("Test Value"); 72 cell = getCell(sheet, 2, 3); 73 setText(cell, "Test Value"); 74 cell = getCell(sheet, 3, 4); 75 setText(cell, "Test Value"); 76 cell = getCell(sheet, 2, 4); 77 setText(cell, "Test Value"); 78 } 79 }; 80 81 excelView.render(new HashMap (), request, response); 82 83 POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream (response.getContentAsByteArray())); 84 HSSFWorkbook wb = new HSSFWorkbook(poiFs); 85 assertEquals("Test Sheet", wb.getSheetName(0)); 86 HSSFSheet sheet = wb.getSheet("Test Sheet"); 87 HSSFRow row = sheet.getRow(2); 88 HSSFCell cell = row.getCell((short)4); 89 assertEquals("Test Value", cell.getStringCellValue()); 90 } 91 92 public void testExcelWithTemplateNoLoc() throws Exception { 93 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, 94 newDummyLocaleResolver("nl","nl")); 95 96 AbstractExcelView excelView = new AbstractExcelView() { 97 protected void buildExcelDocument(Map model, HSSFWorkbook wb, 98 HttpServletRequest request, HttpServletResponse response) 99 throws Exception { 100 HSSFSheet sheet = wb.getSheet("Sheet1"); 101 102 HSSFCell cell = getCell(sheet, 2, 4); 104 cell.setCellValue("Test Value"); 105 cell = getCell(sheet, 2, 3); 106 setText(cell, "Test Value"); 107 cell = getCell(sheet, 3, 4); 108 setText(cell, "Test Value"); 109 cell = getCell(sheet, 2, 4); 110 setText(cell, "Test Value"); 111 } 112 }; 113 114 excelView.setApplicationContext(webAppCtx); 115 excelView.setUrl("template"); 116 excelView.render(new HashMap (), request, response); 117 118 POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream (response.getContentAsByteArray())); 119 HSSFWorkbook wb = new HSSFWorkbook(poiFs); 120 HSSFSheet sheet = wb.getSheet("Sheet1"); 121 HSSFRow row = sheet.getRow(0); 122 HSSFCell cell = row.getCell((short)0); 123 assertEquals("Test Template", cell.getStringCellValue()); 124 } 125 126 public void testExcelWithTemplateAndCountryAndLanguage() throws Exception { 127 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, 128 newDummyLocaleResolver("en", "US")); 129 130 AbstractExcelView excelView = new AbstractExcelView() { 131 protected void buildExcelDocument(Map model, HSSFWorkbook wb, 132 HttpServletRequest request, HttpServletResponse response) 133 throws Exception { 134 HSSFSheet sheet = wb.getSheet("Sheet1"); 135 136 HSSFCell cell = getCell(sheet, 2, 4); 138 cell.setCellValue("Test Value"); 139 cell = getCell(sheet, 2, 3); 140 setText(cell, "Test Value"); 141 cell = getCell(sheet, 3, 4); 142 setText(cell, "Test Value"); 143 cell = getCell(sheet, 2, 4); 144 setText(cell, "Test Value"); 145 } 146 }; 147 148 excelView.setApplicationContext(webAppCtx); 149 excelView.setUrl("template"); 150 excelView.render(new HashMap (), request, response); 151 152 POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream (response.getContentAsByteArray())); 153 HSSFWorkbook wb = new HSSFWorkbook(poiFs); 154 HSSFSheet sheet = wb.getSheet("Sheet1"); 155 HSSFRow row = sheet.getRow(0); 156 HSSFCell cell = row.getCell((short)0); 157 assertEquals("Test Template American English", cell.getStringCellValue()); 158 } 159 160 public void testExcelWithTemplateAndLanguage() throws Exception { 161 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, 162 newDummyLocaleResolver("de", "")); 163 164 AbstractExcelView excelView = new AbstractExcelView() { 165 protected void buildExcelDocument(Map model, HSSFWorkbook wb, 166 HttpServletRequest request, HttpServletResponse response) 167 throws Exception { 168 HSSFSheet sheet = wb.getSheet("Sheet1"); 169 170 HSSFCell cell = getCell(sheet, 2, 4); 172 cell.setCellValue("Test Value"); 173 cell = getCell(sheet, 2, 3); 174 setText(cell, "Test Value"); 175 cell = getCell(sheet, 3, 4); 176 setText(cell, "Test Value"); 177 cell = getCell(sheet, 2, 4); 178 setText(cell, "Test Value"); 179 } 180 }; 181 182 excelView.setApplicationContext(webAppCtx); 183 excelView.setUrl("template"); 184 excelView.render(new HashMap (), request, response); 185 186 POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream (response.getContentAsByteArray())); 187 HSSFWorkbook wb = new HSSFWorkbook(poiFs); 188 HSSFSheet sheet = wb.getSheet("Sheet1"); 189 HSSFRow row = sheet.getRow(0); 190 HSSFCell cell = row.getCell((short)0); 191 assertEquals("Test Template auf Deutsch", cell.getStringCellValue()); 192 } 193 194 private LocaleResolver newDummyLocaleResolver(final String lang, final String country) { 195 return new LocaleResolver() { 196 public Locale resolveLocale(HttpServletRequest request) { 197 return new Locale (lang, country); 198 } 199 public void setLocale(HttpServletRequest request, 200 HttpServletResponse response, Locale locale) { 201 203 } 204 }; 205 } 206 207 } 208 | Popular Tags |