aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
blob: f705b5b5b367e5cb9e38d581fb74cb088d684893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.xssf.usermodel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import junit.framework.TestCase;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.SharedStringSource;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;

/**
 * Tests for {@link XSSFCell}
 *
 */
public final class TestXSSFCell extends TestCase {
    
    private static final String TEST_C10_AUTHOR = "test C10 author";

	/**
     * Test setting and getting boolean values.
     */
    public void testSetGetBoolean() throws Exception {
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row);
        cell.setCellValue(true);
        assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
        assertTrue(cell.getBooleanCellValue());
        cell.setCellValue(false);
        assertFalse(cell.getBooleanCellValue());
        cell.setCellType(Cell.CELL_TYPE_NUMERIC);
        try {
            cell.getBooleanCellValue();
            fail("Exception expected");
        } catch (NumberFormatException e) {
            // success
        }
    }
    
    /**
     * Test setting and getting numeric values.
     */
    public void testSetGetNumeric() throws Exception {
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row);
        cell.setCellValue(10d);
        assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
        assertEquals(10d, cell.getNumericCellValue());
        cell.setCellValue(-23.76);
        assertEquals(-23.76, cell.getNumericCellValue());        
    }
    
    /**
     * Test setting and getting numeric values.
     */
    public void testSetGetDate() throws Exception {
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row);
        Date now = new Date();
        cell.setCellValue(now);
        assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
        assertEquals(now, cell.getDateCellValue());
        
        // Test case for 1904 hack
        Calendar cal = Calendar.getInstance();
        cal.set(1903, 1, 8);
        Date before1904 = cal.getTime();
        cell.setCellValue(before1904);
        assertEquals(before1904, cell.getDateCellValue());
        
        cell.setCellType(Cell.CELL_TYPE_BOOLEAN);        
        try {
            cell.getDateCellValue();
            fail("Exception expected");
        } catch (NumberFormatException e) {
            // success
        }
        
        cell.setCellValue(cal);
        assertEquals(before1904,cell.getDateCellValue());
        
    }
    
    public void testSetGetError() throws Exception {
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row);
        
        cell.setCellErrorValue((byte)0);
        assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
        assertEquals((byte)0, cell.getErrorCellValue());
        
        cell.setCellValue(2.2);
        assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
        
        cell.setCellErrorValue(Cell.ERROR_NAME);
        assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
        assertEquals(Cell.ERROR_NAME.getType(), cell.getErrorCellValue());
        assertEquals(Cell.ERROR_NAME.getStringRepr(), cell.getErrorCellString());
    }
    
    public void testSetGetFormula() throws Exception {
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row);
        String formula = "SQRT(C2^2+D2^2)";
        
        cell.setCellFormula(formula);
        assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
        assertEquals(formula, cell.getCellFormula());
        
        assertTrue( Double.isNaN( cell.getNumericCellValue() ));
    }
    
    public void testSetGetStringInline() throws Exception {
        CTCell rawCell = CTCell.Factory.newInstance();
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row, rawCell);
        
        // Default is shared string mode, so have to do this explicitly
        rawCell.setT(STCellType.INLINE_STR);
        
        assertEquals(STCellType.INT_INLINE_STR, rawCell.getT().intValue());
        assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
        assertEquals("", cell.getRichStringCellValue().getString());
        
        cell.setCellValue(new XSSFRichTextString("Foo"));
        assertEquals(STCellType.INT_INLINE_STR, rawCell.getT().intValue());
        assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
        assertEquals("Foo", cell.getRichStringCellValue().getString());
        
        // To number and back to string, stops being inline
        cell.setCellValue(1.4);
        cell.setCellValue(new XSSFRichTextString("Foo2"));
        assertEquals(STCellType.INT_S, rawCell.getT().intValue());
        assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
        assertEquals("Foo2", cell.getRichStringCellValue().getString());
    }
    
    public void testSetGetStringShared() {
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row);

        cell.setCellValue(new XSSFRichTextString(""));
        assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
        assertEquals("", cell.getRichStringCellValue().getString());

        cell.setCellValue(new XSSFRichTextString("Foo"));
        assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
        assertEquals("Foo", cell.getRichStringCellValue().getString());
    }
    
    /**
     * Test that empty cells (no v element) return default values.
     */
    public void testGetEmptyCellValue() {
        XSSFRow row = createParentObjects();
        XSSFCell cell = new XSSFCell(row);
        cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
        assertFalse(cell.getBooleanCellValue());
        cell.setCellType(Cell.CELL_TYPE_NUMERIC);
        assertTrue(Double.isNaN( cell.getNumericCellValue() ));
        assertNull(cell.getDateCellValue());
        cell.setCellType(Cell.CELL_TYPE_ERROR);
        assertEquals(0, cell.getErrorCellValue());
        cell.setCellType(Cell.CELL_TYPE_STRING);
        assertEquals("", cell.getRichStringCellValue().getString());
    }

    public void testParseCellNum() {
        assertEquals(0, XSSFCell.parseCellNum("A1"));
        assertEquals(1, XSSFCell.parseCellNum("B1"));
        assertEquals(1, XSSFCell.parseCellNum("B2"));
        assertEquals(26, XSSFCell.parseCellNum("AA1"));
        assertEquals(255, XSSFCell.parseCellNum("IV1"));
        assertEquals(255, XSSFCell.parseCellNum("IV32768"));
    }
    
    public void testFormatPosition() {
        XSSFRow row = createParentObjects();
        row.setRowNum(0);
        XSSFCell cell = new XSSFCell(row);
        cell.setCellNum((short) 0);
        assertEquals("A1", cell.formatPosition());
        cell.setCellNum((short) 25);
        assertEquals("Z1", cell.formatPosition());
        cell.setCellNum((short) 26);
        assertEquals("AA1", cell.formatPosition());
        cell.setCellNum((short) 255);
        assertEquals("IV1", cell.formatPosition());
        row.setRowNum(32767);
        assertEquals("IV32768", cell.formatPosition());
    }
    
    public static class DummySharedStringSource implements SharedStringSource {
        ArrayList<CTRst> strs = new ArrayList<CTRst>();
        public CTRst getEntryAt(int idx) {
            return strs.get(idx);
        }

        public synchronized int addEntry(CTRst s) {
            if(strs.contains(s)) {
                return strs.indexOf(s);
            }
            strs.add(s);
            return strs.size() - 1;
        }
    }
    
    public void testGetCellComment() {
        XSSFWorkbook workbook = new XSSFWorkbook();
        CTSheet ctSheet = CTSheet.Factory.newInstance();
        CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
        CTComments ctComments = CTComments.Factory.newInstance();
        CommentsTable sheetComments = new CommentsTable(ctComments);
        XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, sheetComments);
        assertNotNull(sheet);
        
        // Create C10 cell
        Row row = sheet.createRow(9);
        row.createCell(2);
        row.createCell(3);
        
        
        // Set a comment for C10 cell
        CTComment ctComment = ctComments.addNewCommentList().insertNewComment(0);
        ctComment.setRef("C10");
        ctComment.setAuthorId(sheetComments.findAuthor(TEST_C10_AUTHOR));
        
        assertNotNull(sheet.getRow(9).getCell((short)2));
        assertNotNull(sheet.getRow(9).getCell((short)2).getCellComment());
        assertEquals(TEST_C10_AUTHOR, sheet.getRow(9).getCell((short)2).getCellComment().getAuthor());
        assertNull(sheet.getRow(9).getCell((short)3).getCellComment());
    }
    
    public void testSetCellComment() {
        XSSFWorkbook workbook = new XSSFWorkbook();
        CTSheet ctSheet = CTSheet.Factory.newInstance();
        CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
        CTComments ctComments = CTComments.Factory.newInstance();
        CommentsTable comments = new CommentsTable(ctComments);
        XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, comments);
        assertNotNull(sheet);
        
        // Create C10 cell
        Row row = sheet.createRow(9);
        Cell cell = row.createCell(2);
        row.createCell(3);
        
        // Create a comment
        Comment comment = comments.addComment();
        comment.setAuthor(TEST_C10_AUTHOR);
        
        // Set a comment for C10 cell
        cell.setCellComment(comment);
        
        CTCell ctCell = ctWorksheet.getSheetData().getRowArray(0).getCArray(0);
		assertNotNull(ctCell);
		assertEquals("C10", ctCell.getR());
		long authorId = ctComments.getCommentList().getCommentArray(0).getAuthorId();
		assertEquals(TEST_C10_AUTHOR, comments.getAuthor(authorId));
    }
    
    public void testSetAsActiveCell() {
    	Workbook workbook = new XSSFWorkbook();
    	CTSheet ctSheet = CTSheet.Factory.newInstance();
    	CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
    	XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
    	Cell cell = sheet.createRow(0).createCell((short)0);
    	cell.setAsActiveCell();
    	
    	assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
    }
    
    
    /**
     * Tests that cell formatting stuff works as expected
     */
    public void testCellFormatting() {
    	Workbook workbook = new XSSFWorkbook();
    	Sheet sheet = workbook.createSheet();
    	CreationHelper creationHelper = workbook.getCreationHelper();
    	
    	CellStyle cs = workbook.createCellStyle();
    	assertNotNull(cs);
    	
    	assertNotNull(creationHelper);
    	assertNotNull(creationHelper.createDataFormat());
    	
    	cs.setDataFormat(
    			creationHelper.createDataFormat().getFormat("yyyy/mm/dd")
    	);
    	Cell cell = sheet.createRow(0).createCell((short)0);
    	cell.setCellValue(new Date(654321));
    	
    	assertNull(cell.getCellStyle());
    	cell.setCellStyle(cs);
    	
    	assertEquals(new Date(654321), cell.getDateCellValue());
    	assertNotNull(cell.getCellStyle());
    	assertEquals("yyyy/mm/dd", cell.getCellStyle().getDataFormatString());
    	
    	
    	// Save, re-load, and test again
    	Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    	Cell c2 = wb2.getSheetAt(0).getRow(0).getCell(0);
    	assertEquals(new Date(654321), c2.getDateCellValue());
    	assertEquals("yyyy/mm/dd", c2.getCellStyle().getDataFormatString());
    }

    private static XSSFRow createParentObjects() {
        XSSFWorkbook wb = new XSSFWorkbook();
        wb.setSharedStringSource(new SharedStringsTable());
        XSSFSheet sheet = new XSSFSheet(wb);
        XSSFRow row = new XSSFRow(sheet);
        return row;
    }

    /**
     * Test to ensure we can only assign cell styles that belong
     *  to our workbook, and not those from other workbooks.
     */
    public void testCellStyleWorkbookMatch() {
    	XSSFWorkbook wbA = new XSSFWorkbook();
    	XSSFWorkbook wbB = new XSSFWorkbook();
    	
    	XSSFCellStyle styA = wbA.createCellStyle();
    	XSSFCellStyle styB = wbB.createCellStyle();
    	
    	styA.verifyBelongsToStylesSource(wbA.getStylesSource());
    	styB.verifyBelongsToStylesSource(wbB.getStylesSource());
    	try {
    		styA.verifyBelongsToStylesSource(wbB.getStylesSource());
    		fail();
    	} catch(IllegalArgumentException e) {}
    	try {
    		styB.verifyBelongsToStylesSource(wbA.getStylesSource());
    		fail();
    	} catch(IllegalArgumentException e) {}
    	
    	Cell cellA = wbA.createSheet().createRow(0).createCell((short)0);
    	Cell cellB = wbB.createSheet().createRow(0).createCell((short)0);
    	
    	cellA.setCellStyle(styA);
    	cellB.setCellStyle(styB);
    	try {
        	cellA.setCellStyle(styB);
    		fail();
    	} catch(IllegalArgumentException e) {}
    	try {
        	cellB.setCellStyle(styA);
    		fail();
    	} catch(IllegalArgumentException e) {}
    }
    
    
    public void testHSSFXSSFToString(){
	Workbook xwb = new XSSFWorkbook();
    	Sheet xsheet = xwb.createSheet();
    	XSSFCell xcell = (XSSFCell) xsheet.createRow(0).createCell((short)0);
    	
    	Workbook hwb=new HSSFWorkbook();
    	Sheet hsheet=hwb.createSheet();
    	HSSFCell hcell = (HSSFCell) hsheet.createRow(0).createCell((short)0);

    	//BLANK
    	assertEquals(hcell.toString(),xcell.toString());
    	System.out.println("BLANK==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
    	//BOOLEAN
    	xcell.setCellValue(true);
    	xcell.setCellType(Cell.CELL_TYPE_BOOLEAN);
    	hcell.setCellValue(true);
    	hcell.setCellType(Cell.CELL_TYPE_BOOLEAN);
    	System.out.println("BOOLEAN==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
    	assertEquals(hcell.toString(),xcell.toString());
    	
	//NUMERIC

    	xcell.setCellValue(1234);
    	xcell.setCellType(Cell.CELL_TYPE_NUMERIC);
    	hcell.setCellValue(1234);
    	hcell.setCellType(Cell.CELL_TYPE_NUMERIC);
    	System.out.println("NUMERIC==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
    	assertEquals(hcell.toString(),xcell.toString());
    	
    	//DATE ********************
    	
    	Calendar cal = Calendar.getInstance();
        cal.set(1903, 1, 8);
    	xcell.setCellValue(cal.getTime());
    	CellStyle xstyle=xwb.createCellStyle();
        DataFormat format = xwb.createDataFormat();
    	xstyle.setDataFormat(format.getFormat("YYYY-MM-DD"));
    	xcell.setCellStyle(xstyle);

    	hcell.setCellValue(cal.getTime());
    	CellStyle hstyle=hwb.createCellStyle();
        DataFormat hformat = hwb.createDataFormat();
    	hstyle.setDataFormat(hformat.getFormat("YYYY-MM-DD"));
    	hcell.setCellStyle(hstyle);
    	
    	System.out.println("DATE==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
    	assertEquals(hcell.toString(),xcell.toString());
    	
    	
    	//STRING
    	xcell.setCellValue(new XSSFRichTextString("text string"));
    	xcell.setCellType(Cell.CELL_TYPE_STRING);
    	hcell.setCellValue(new HSSFRichTextString("text string"));
    	hcell.setCellType(Cell.CELL_TYPE_STRING);
    	System.out.println("STRING==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
    	assertEquals(hcell.toString(),xcell.toString());
    	
    	//ERROR
    	xcell.setCellErrorValue(Cell.ERROR_VALUE);
    	xcell.setCellType(Cell.CELL_TYPE_ERROR);

    	hcell.setCellErrorValue((byte)0);
    	hcell.setCellType(Cell.CELL_TYPE_ERROR);

    	System.out.println("ERROR==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
    	assertEquals(hcell.toString(),xcell.toString());
    	
    	//FORMULA
    	xcell.setCellFormula("A1+B2");
    	hcell.setCellValue("A1+B2");
    	System.out.println("FORMULA==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
    	assertEquals(hcell.toString(),xcell.toString());
    	
    }
    
    
}