Lines 44-49
Link Here
|
44 |
* paticular datatypes, etc. |
44 |
* paticular datatypes, etc. |
45 |
* @author Andrew C. Oliver (andy at superlinksoftware dot com) |
45 |
* @author Andrew C. Oliver (andy at superlinksoftware dot com) |
46 |
* @author Dan Sherman (dsherman at isisph.com) |
46 |
* @author Dan Sherman (dsherman at isisph.com) |
|
|
47 |
* @author Alex Jacoby (ajacoby at gmail.com) |
47 |
*/ |
48 |
*/ |
48 |
|
49 |
|
49 |
public class TestHSSFCell |
50 |
public class TestHSSFCell |
Lines 107-148
Link Here
|
107 |
} |
108 |
} |
108 |
|
109 |
|
109 |
/** |
110 |
/** |
110 |
* Checks that the recognition of files using 1904 date windowing |
111 |
* Checks that the recognition of files using 1904 date windowing |
111 |
* is working properly. Conversion of the date is also an issue, |
112 |
* is working properly. Conversion of the date is also an issue, |
112 |
* but there's a separate unit test for that. |
113 |
* but there's a separate unit test for that. |
113 |
*/ |
114 |
*/ |
114 |
public void testDateWindowing() throws Exception { |
115 |
public void testDateWindowingRead() throws Exception { |
115 |
GregorianCalendar cal = new GregorianCalendar(2000,0,1); // Jan. 1, 2000 |
116 |
GregorianCalendar cal = new GregorianCalendar(2000,0,1); // Jan. 1, 2000 |
116 |
Date date = cal.getTime(); |
117 |
Date date = cal.getTime(); |
117 |
String path = System.getProperty("HSSF.testdata.path"); |
118 |
String path = System.getProperty("HSSF.testdata.path"); |
118 |
|
119 |
|
119 |
// first check a file with 1900 Date Windowing |
120 |
// first check a file with 1900 Date Windowing |
120 |
String filename = path + "/1900DateWindowing.xls"; |
121 |
String filename = path + "/1900DateWindowing.xls"; |
121 |
FileInputStream stream = new FileInputStream(filename); |
122 |
FileInputStream stream = new FileInputStream(filename); |
122 |
POIFSFileSystem fs = new POIFSFileSystem(stream); |
123 |
POIFSFileSystem fs = new POIFSFileSystem(stream); |
123 |
HSSFWorkbook workbook = new HSSFWorkbook(fs); |
124 |
HSSFWorkbook workbook = new HSSFWorkbook(fs); |
124 |
HSSFSheet sheet = workbook.getSheetAt(0); |
125 |
HSSFSheet sheet = workbook.getSheetAt(0); |
125 |
|
126 |
|
126 |
assertEquals("Date from file using 1900 Date Windowing", |
127 |
assertEquals("Date from file using 1900 Date Windowing", |
127 |
date.getTime(), |
128 |
date.getTime(), |
128 |
sheet.getRow(0).getCell((short)0) |
129 |
sheet.getRow(0).getCell((short)0) |
129 |
.getDateCellValue().getTime()); |
130 |
.getDateCellValue().getTime()); |
130 |
stream.close(); |
131 |
stream.close(); |
131 |
|
132 |
|
132 |
// now check a file with 1904 Date Windowing |
133 |
// now check a file with 1904 Date Windowing |
133 |
filename = path + "/1904DateWindowing.xls"; |
134 |
filename = path + "/1904DateWindowing.xls"; |
134 |
stream = new FileInputStream(filename); |
135 |
stream = new FileInputStream(filename); |
135 |
fs = new POIFSFileSystem(stream); |
136 |
fs = new POIFSFileSystem(stream); |
136 |
workbook = new HSSFWorkbook(fs); |
137 |
workbook = new HSSFWorkbook(fs); |
137 |
sheet = workbook.getSheetAt(0); |
138 |
sheet = workbook.getSheetAt(0); |
138 |
|
139 |
|
139 |
assertEquals("Date from file using 1904 Date Windowing", |
140 |
assertEquals("Date from file using 1904 Date Windowing", |
140 |
date.getTime(), |
141 |
date.getTime(), |
141 |
sheet.getRow(0).getCell((short)0) |
142 |
sheet.getRow(0).getCell((short)0) |
142 |
.getDateCellValue().getTime()); |
143 |
.getDateCellValue().getTime()); |
143 |
stream.close(); |
144 |
stream.close(); |
144 |
} |
145 |
} |
145 |
|
146 |
|
|
|
147 |
/** |
148 |
* Checks that dates are properly written to both types of files: |
149 |
* those with 1900 and 1904 date windowing. Note that if the |
150 |
* previous test ({@link #testDateWindowingRead}) fails, the |
151 |
* results of this test are meaningless. |
152 |
*/ |
153 |
public void testDateWindowingWrite() throws Exception { |
154 |
GregorianCalendar cal = new GregorianCalendar(2000,0,1); // Jan. 1, 2000 |
155 |
Date date = cal.getTime(); |
156 |
String path = System.getProperty("HSSF.testdata.path"); |
157 |
|
158 |
// first check a file with 1900 Date Windowing |
159 |
String filename = path + "/1900DateWindowing.xls"; |
160 |
writeCell(filename, 0, (short) 1, date); |
161 |
assertEquals("Date from file using 1900 Date Windowing", |
162 |
date.getTime(), |
163 |
readCell(filename, 0, (short) 1).getTime()); |
164 |
|
165 |
// now check a file with 1904 Date Windowing |
166 |
filename = path + "/1904DateWindowing.xls"; |
167 |
writeCell(filename, 0, (short) 1, date); |
168 |
assertEquals("Date from file using 1900 Date Windowing", |
169 |
date.getTime(), |
170 |
readCell(filename, 0, (short) 1).getTime()); |
171 |
} |
172 |
|
173 |
/** |
174 |
* Sets cell value and writes file. |
175 |
*/ |
176 |
private void writeCell(String filename, |
177 |
int rowIdx, short colIdx, Date date) throws Exception { |
178 |
FileInputStream stream = new FileInputStream(filename); |
179 |
POIFSFileSystem fs = new POIFSFileSystem(stream); |
180 |
HSSFWorkbook workbook = new HSSFWorkbook(fs); |
181 |
HSSFSheet sheet = workbook.getSheetAt(0); |
182 |
HSSFRow row = sheet.getRow(rowIdx); |
183 |
HSSFCell cell = row.getCell(colIdx); |
184 |
|
185 |
if (cell == null) { |
186 |
cell = row.createCell(colIdx); |
187 |
} |
188 |
cell.setCellValue(date); |
189 |
|
190 |
// Write the file |
191 |
stream.close(); |
192 |
FileOutputStream oStream = new FileOutputStream(filename); |
193 |
workbook.write(oStream); |
194 |
oStream.close(); |
195 |
} |
196 |
|
197 |
/** |
198 |
* Reads cell value from file. |
199 |
*/ |
200 |
private Date readCell(String filename, |
201 |
int rowIdx, short colIdx) throws Exception { |
202 |
FileInputStream stream = new FileInputStream(filename); |
203 |
POIFSFileSystem fs = new POIFSFileSystem(stream); |
204 |
HSSFWorkbook workbook = new HSSFWorkbook(fs); |
205 |
HSSFSheet sheet = workbook.getSheetAt(0); |
206 |
HSSFRow row = sheet.getRow(rowIdx); |
207 |
HSSFCell cell = row.getCell(colIdx); |
208 |
return cell.getDateCellValue(); |
209 |
} |
210 |
|
146 |
/** |
211 |
/** |
147 |
* Tests that the active cell can be correctly read and set |
212 |
* Tests that the active cell can be correctly read and set |
148 |
*/ |
213 |
*/ |