@Test public void getBody() throws Exception { byte[] content = "Hello World".getBytes("UTF-8"); FileCopyUtils.copy(content, response.getBody()); assertArrayEquals("Invalid content written", content, mockResponse.getContentAsByteArray()); }
@Test public void testExcel() throws Exception { AbstractExcelView excelView = new AbstractExcelView() { @Override protected void buildExcelDocument( Map<String, Object> model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.createSheet("Test Sheet"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); } }; excelView.render(new HashMap<String, Object>(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); assertEquals("Test Sheet", wb.getSheetName(0)); HSSFSheet sheet = wb.getSheet("Test Sheet"); HSSFRow row = sheet.getRow(2); HSSFCell cell = row.getCell(4); assertEquals("Test Value", cell.getStringCellValue()); }
@Test public void testJExcel() throws Exception { AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() { @Override protected void buildExcelDocument( Map<String, Object> model, WritableWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { WritableSheet sheet = wb.createSheet("Test Sheet", 0); // test all possible permutation of row or column not existing sheet.addCell(new Label(2, 4, "Test Value")); sheet.addCell(new Label(2, 3, "Test Value")); sheet.addCell(new Label(3, 4, "Test Value")); sheet.addCell(new Label(2, 4, "Test Value")); } }; excelView.render(new HashMap<String, Object>(), request, response); Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray())); assertEquals("Test Sheet", wb.getSheet(0).getName()); Sheet sheet = wb.getSheet("Test Sheet"); Cell cell = sheet.getCell(2, 4); assertEquals("Test Value", cell.getContents()); }
@Test public void testJExcelWithTemplateAndLanguage() throws Exception { request.setAttribute( DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("de", "")); AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() { @Override protected void buildExcelDocument( Map<String, Object> model, WritableWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { WritableSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing sheet.addCell(new Label(2, 4, "Test Value")); sheet.addCell(new Label(2, 3, "Test Value")); sheet.addCell(new Label(3, 4, "Test Value")); sheet.addCell(new Label(2, 4, "Test Value")); } }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap<String, Object>(), request, response); Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray())); Sheet sheet = wb.getSheet("Sheet1"); Cell cell = sheet.getCell(0, 0); assertEquals("Test Template auf Deutsch", cell.getContents()); }
@Test public void testExcelWithTemplateAndLanguage() throws Exception { request.setAttribute( DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("de", "")); AbstractExcelView excelView = new AbstractExcelView() { @Override protected void buildExcelDocument( Map<String, Object> model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); } }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap<String, Object>(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); HSSFSheet sheet = wb.getSheet("Sheet1"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell(0); assertEquals("Test Template auf Deutsch", cell.getStringCellValue()); }