KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > document > ExcelViewTests


1 /*
2  * Copyright 2002-2005 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
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
41  * @author Alef Arendsen
42  */

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 JavaDoc {
62         AbstractExcelView excelView = new AbstractExcelView() {
63             protected void buildExcelDocument(Map JavaDoc model, HSSFWorkbook wb,
64                                                                                 HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
65                     throws Exception JavaDoc {
66                 HSSFSheet sheet = wb.createSheet();
67                 wb.setSheetName(0, "Test Sheet");
68                 
69                 // test all possible permutation of row or column not existing
70
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 JavaDoc(), request, response);
82         
83         POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream JavaDoc(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 JavaDoc {
93         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
94                 newDummyLocaleResolver("nl","nl"));
95
96         AbstractExcelView excelView = new AbstractExcelView() {
97             protected void buildExcelDocument(Map JavaDoc model, HSSFWorkbook wb,
98                                                                                 HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
99                     throws Exception JavaDoc {
100                 HSSFSheet sheet = wb.getSheet("Sheet1");
101                 
102                 // test all possible permutation of row or column not existing
103
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 JavaDoc(), request, response);
117         
118         POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream JavaDoc(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 JavaDoc {
127         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
128                 newDummyLocaleResolver("en", "US"));
129
130         AbstractExcelView excelView = new AbstractExcelView() {
131             protected void buildExcelDocument(Map JavaDoc model, HSSFWorkbook wb,
132                                                                                 HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
133                     throws Exception JavaDoc {
134                 HSSFSheet sheet = wb.getSheet("Sheet1");
135                 
136                 // test all possible permutation of row or column not existing
137
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 JavaDoc(), request, response);
151         
152         POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream JavaDoc(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 JavaDoc {
161         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
162                 newDummyLocaleResolver("de", ""));
163
164         AbstractExcelView excelView = new AbstractExcelView() {
165             protected void buildExcelDocument(Map JavaDoc model, HSSFWorkbook wb,
166                 HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
167             throws Exception JavaDoc {
168                 HSSFSheet sheet = wb.getSheet("Sheet1");
169                 
170                 // test all possible permutation of row or column not existing
171
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 JavaDoc(), request, response);
185         
186         POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream JavaDoc(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 JavaDoc lang, final String JavaDoc country) {
195         return new LocaleResolver() {
196             public Locale JavaDoc resolveLocale(HttpServletRequest JavaDoc request) {
197                 return new Locale JavaDoc(lang, country);
198             }
199             public void setLocale(HttpServletRequest JavaDoc request,
200                     HttpServletResponse JavaDoc response, Locale JavaDoc locale) {
201                 // not supported!
202

203             }
204         };
205     }
206
207 }
208
Popular Tags