Source Code Cross Referenced for TestWorkbook.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » hssf » usermodel » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » poi 3.0.2 beta2 » org.apache.poi.hssf.usermodel 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ====================================================================
002:         Licensed to the Apache Software Foundation (ASF) under one or more
003:         contributor license agreements.  See the NOTICE file distributed with
004:         this work for additional information regarding copyright ownership.
005:         The ASF licenses this file to You under the Apache License, Version 2.0
006:         (the "License"); you may not use this file except in compliance with
007:         the License.  You may obtain a copy of the License at
008:
009:         http://www.apache.org/licenses/LICENSE-2.0
010:
011:         Unless required by applicable law or agreed to in writing, software
012:         distributed under the License is distributed on an "AS IS" BASIS,
013:         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         See the License for the specific language governing permissions and
015:         limitations under the License.
016:         ==================================================================== */
017:
018:        package org.apache.poi.hssf.usermodel;
019:
020:        import junit.framework.TestCase;
021:        import org.apache.poi.hssf.model.Workbook;
022:        import org.apache.poi.hssf.record.BackupRecord;
023:        import org.apache.poi.hssf.record.LabelSSTRecord;
024:        import org.apache.poi.hssf.record.Record;
025:        import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate;
026:        import org.apache.poi.hssf.util.Region;
027:        import org.apache.poi.poifs.filesystem.POIFSFileSystem;
028:        import org.apache.poi.util.TempFile;
029:
030:        import java.io.File;
031:        import java.io.FileInputStream;
032:        import java.io.FileOutputStream;
033:        import java.io.IOException;
034:        import java.util.Iterator;
035:
036:        /**
037:         * Class to test Workbook functionality
038:         *
039:         * @author Andrew C. Oliver
040:         * @author Greg Merrill
041:         * @author Siggi Cherem
042:         */
043:
044:        public class TestWorkbook extends TestCase {
045:            private static final String LAST_NAME_KEY = "lastName";
046:            private static final String FIRST_NAME_KEY = "firstName";
047:            private static final String SSN_KEY = "ssn";
048:            private static final String REPLACE_ME = "replaceMe";
049:            private static final String REPLACED = "replaced";
050:            private static final String DO_NOT_REPLACE = "doNotReplace";
051:            private static final String EMPLOYEE_INFORMATION = "Employee Info";
052:            private static final String LAST_NAME_VALUE = "Bush";
053:            private static final String FIRST_NAME_VALUE = "George";
054:            private static final String SSN_VALUE = "555555555";
055:            private SanityChecker sanityChecker = new SanityChecker();
056:
057:            /**
058:             * Constructor TestWorkbook
059:             *
060:             * @param name
061:             */
062:
063:            public TestWorkbook(String name) {
064:                super (name);
065:            }
066:
067:            /**
068:             * TEST NAME:  Test Write Sheet Simple <P>
069:             * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values.<P>
070:             * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects
071:             *             Last row, first row is tested against the correct values (99,0).<P>
072:             * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.
073:             *             HSSFSheet last row or first row is incorrect.             <P>
074:             *
075:             */
076:
077:            public void testWriteSheetSimple() throws IOException {
078:                File file = TempFile.createTempFile("testWriteSheetSimple",
079:                        ".xls");
080:                FileOutputStream out = new FileOutputStream(file);
081:                HSSFWorkbook wb = new HSSFWorkbook();
082:                HSSFSheet s = wb.createSheet();
083:                HSSFRow r = null;
084:                HSSFCell c = null;
085:
086:                for (short rownum = (short) 0; rownum < 100; rownum++) {
087:                    r = s.createRow(rownum);
088:
089:                    // r.setRowNum(( short ) rownum);
090:                    for (short cellnum = (short) 0; cellnum < 50; cellnum += 2) {
091:                        c = r.createCell(cellnum);
092:                        c
093:                                .setCellValue(rownum
094:                                        * 10000
095:                                        + cellnum
096:                                        + (((double) rownum / 1000) + ((double) cellnum / 10000)));
097:                        c = r.createCell((short) (cellnum + 1));
098:                        c.setCellValue("TEST");
099:                    }
100:                }
101:                wb.write(out);
102:                out.close();
103:                sanityChecker.checkHSSFWorkbook(wb);
104:                assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
105:                assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
106:
107:                // assert((s.getLastRowNum() == 99));
108:            }
109:
110:            /**
111:             * TEST NAME:  Test Write/Modify Sheet Simple <P>
112:             * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values,
113:             *             remove some rows, yet still have a valid file/data.<P>
114:             * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects
115:             *             Last row, first row is tested against the correct values (74,25).<P>
116:             * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.
117:             *             HSSFSheet last row or first row is incorrect.             <P>
118:             *
119:             */
120:
121:            public void testWriteModifySheetSimple() throws IOException {
122:                File file = TempFile.createTempFile("testWriteSheetSimple",
123:                        ".xls");
124:                FileOutputStream out = new FileOutputStream(file);
125:                HSSFWorkbook wb = new HSSFWorkbook();
126:                HSSFSheet s = wb.createSheet();
127:                HSSFRow r = null;
128:                HSSFCell c = null;
129:
130:                for (short rownum = (short) 0; rownum < 100; rownum++) {
131:                    r = s.createRow(rownum);
132:
133:                    // r.setRowNum(( short ) rownum);
134:                    for (short cellnum = (short) 0; cellnum < 50; cellnum += 2) {
135:                        c = r.createCell(cellnum);
136:                        c
137:                                .setCellValue(rownum
138:                                        * 10000
139:                                        + cellnum
140:                                        + (((double) rownum / 1000) + ((double) cellnum / 10000)));
141:                        c = r.createCell((short) (cellnum + 1));
142:                        c.setCellValue("TEST");
143:                    }
144:                }
145:                for (short rownum = (short) 0; rownum < 25; rownum++) {
146:                    r = s.getRow(rownum);
147:                    s.removeRow(r);
148:                }
149:                for (short rownum = (short) 75; rownum < 100; rownum++) {
150:                    r = s.getRow(rownum);
151:                    s.removeRow(r);
152:                }
153:                wb.write(out);
154:                out.close();
155:
156:                sanityChecker.checkHSSFWorkbook(wb);
157:                assertEquals("LAST ROW == 74", 74, s.getLastRowNum());
158:                assertEquals("FIRST ROW == 25", 25, s.getFirstRowNum());
159:            }
160:
161:            /**
162:             * TEST NAME:  Test Read Simple <P>
163:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet (Simple.xls).<P>
164:             * SUCCESS:    HSSF reads the sheet.  Matches values in their particular positions.<P>
165:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF cannot identify values
166:             *             in the sheet in their known positions.<P>
167:             *
168:             */
169:
170:            public void testReadSimple() throws IOException {
171:                String filename = System.getProperty("HSSF.testdata.path");
172:
173:                filename = filename + "/Simple.xls";
174:                FileInputStream stream = new FileInputStream(filename);
175:                POIFSFileSystem fs = new POIFSFileSystem(stream);
176:                HSSFWorkbook workbook = new HSSFWorkbook(fs);
177:                HSSFSheet sheet = workbook.getSheetAt(0);
178:
179:                assertEquals(REPLACE_ME, sheet.getRow((short) 0).getCell(
180:                        (short) 0).getStringCellValue());
181:                stream.close();
182:            }
183:
184:            /**
185:             * TEST NAME:  Test Read Simple w/ Data Format<P>
186:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet (SimpleWithDataFormat.xls).<P>
187:             * SUCCESS:    HSSF reads the sheet.  Matches values in their particular positions and format is correct<P>
188:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF cannot identify values
189:             *             in the sheet in their known positions.<P>
190:             *
191:             */
192:
193:            public void testReadSimpleWithDataFormat() throws IOException {
194:                String filename = System.getProperty("HSSF.testdata.path");
195:
196:                filename = filename + "/SimpleWithDataFormat.xls";
197:                FileInputStream stream = new FileInputStream(filename);
198:                POIFSFileSystem fs = new POIFSFileSystem(stream);
199:                HSSFWorkbook workbook = new HSSFWorkbook(fs);
200:                HSSFSheet sheet = workbook.getSheetAt(0);
201:                HSSFDataFormat format = workbook.createDataFormat();
202:                HSSFCell cell = sheet.getRow((short) 0).getCell((short) 0);
203:
204:                assertEquals(1.25, cell.getNumericCellValue(), 1e-10);
205:
206:                assertEquals(format.getFormat(cell.getCellStyle()
207:                        .getDataFormat()), "0.0");
208:                stream.close();
209:            }
210:
211:            /**
212:             * TEST NAME:  Test Read/Write Simple w/ Data Format<P>
213:             * OBJECTIVE:  Test that HSSF can write a sheet with custom data formats and then read it and get the proper formats.<P>
214:             * SUCCESS:    HSSF reads the sheet.  Matches values in their particular positions and format is correct<P>
215:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF cannot identify values
216:             *             in the sheet in their known positions.<P>
217:             *
218:             */
219:
220:            public void testWriteDataFormat() throws IOException {
221:                File file = TempFile.createTempFile("testWriteDataFormat",
222:                        ".xls");
223:                FileOutputStream out = new FileOutputStream(file);
224:                HSSFWorkbook wb = new HSSFWorkbook();
225:                HSSFSheet s = wb.createSheet();
226:                HSSFRow r = null;
227:                HSSFCell c = null;
228:                HSSFDataFormat format = wb.createDataFormat();
229:                HSSFCellStyle cs = wb.createCellStyle();
230:
231:                short df = format.getFormat("0.0");
232:                cs.setDataFormat(df);
233:
234:                r = s.createRow((short) 0);
235:                c = r.createCell((short) 0);
236:                c.setCellStyle(cs);
237:                c.setCellValue(1.25);
238:
239:                wb.write(out);
240:                out.close();
241:
242:                FileInputStream stream = new FileInputStream(file);
243:                POIFSFileSystem fs = new POIFSFileSystem(stream);
244:                HSSFWorkbook workbook = new HSSFWorkbook(fs);
245:                HSSFSheet sheet = workbook.getSheetAt(0);
246:                HSSFCell cell = sheet.getRow((short) 0).getCell((short) 0);
247:                format = workbook.createDataFormat();
248:
249:                assertEquals(1.25, cell.getNumericCellValue(), 1e-10);
250:
251:                assertEquals(format.getFormat(df), "0.0");
252:
253:                assertEquals(format, workbook.createDataFormat());
254:
255:                stream.close();
256:            }
257:
258:            /**
259:             * TEST NAME:  Test Read Employee Simple <P>
260:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet (Employee.xls).<P>
261:             * SUCCESS:    HSSF reads the sheet.  Matches values in their particular positions.<P>
262:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF cannot identify values
263:             *             in the sheet in their known positions.<P>
264:             *
265:             */
266:
267:            public void testReadEmployeeSimple() throws IOException {
268:                String filename = System.getProperty("HSSF.testdata.path");
269:
270:                filename = filename + "/Employee.xls";
271:                FileInputStream stream = new FileInputStream(filename);
272:                POIFSFileSystem fs = new POIFSFileSystem(stream);
273:                HSSFWorkbook workbook = new HSSFWorkbook(fs);
274:                HSSFSheet sheet = workbook.getSheetAt(0);
275:
276:                assertEquals(EMPLOYEE_INFORMATION, sheet.getRow(1).getCell(
277:                        (short) 1).getStringCellValue());
278:                assertEquals(LAST_NAME_KEY, sheet.getRow(3).getCell((short) 2)
279:                        .getStringCellValue());
280:                assertEquals(FIRST_NAME_KEY, sheet.getRow(4).getCell((short) 2)
281:                        .getStringCellValue());
282:                assertEquals(SSN_KEY, sheet.getRow(5).getCell((short) 2)
283:                        .getStringCellValue());
284:                stream.close();
285:            }
286:
287:            /**
288:             * TEST NAME:  Test Modify Sheet Simple <P>
289:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet with a string value and replace
290:             *             it with another string value.<P>
291:             * SUCCESS:    HSSF reads a sheet.  HSSF replaces the cell value with another cell value. HSSF
292:             *             writes the sheet out to another file.  HSSF reads the result and ensures the value
293:             *             has been properly replaced.    <P>
294:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF does not write the sheet or excepts.
295:             *             HSSF does not re-read the sheet or excepts.  Upon re-reading the sheet the value
296:             *             is incorrect or has not been replaced. <P>
297:             *
298:             */
299:
300:            public void testModifySimple() throws IOException {
301:                String filename = System.getProperty("HSSF.testdata.path");
302:
303:                filename = filename + "/Simple.xls";
304:                FileInputStream instream = new FileInputStream(filename);
305:                POIFSFileSystem fsin = new POIFSFileSystem(instream);
306:                HSSFWorkbook workbook = new HSSFWorkbook(fsin);
307:                HSSFSheet sheet = workbook.getSheetAt(0);
308:                HSSFCell cell = sheet.getRow((short) 0).getCell((short) 0);
309:
310:                cell.setCellValue(REPLACED);
311:                File destination = TempFile.createTempFile("SimpleResult",
312:                        ".xls");
313:                FileOutputStream outstream = new FileOutputStream(destination);
314:
315:                workbook.write(outstream);
316:                instream.close();
317:                outstream.close();
318:                instream = new FileInputStream(destination);
319:                workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
320:                sheet = workbook.getSheetAt(0);
321:                cell = sheet.getRow((short) 0).getCell((short) 0);
322:                assertEquals(REPLACED, cell.getStringCellValue());
323:                instream.close();
324:            }
325:
326:            /**
327:             * TEST NAME:  Test Modify Sheet Simple With Skipped cells<P>
328:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet with string values and replace
329:             *             them with other string values while not replacing other cells.<P>
330:             * SUCCESS:    HSSF reads a sheet.  HSSF replaces the cell value with another cell value. HSSF
331:             *             writes the sheet out to another file.  HSSF reads the result and ensures the value
332:             *             has been properly replaced and unreplaced values are still unreplaced.    <P>
333:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF does not write the sheet or excepts.
334:             *             HSSF does not re-read the sheet or excepts.  Upon re-reading the sheet the value
335:             *             is incorrect or has not been replaced or the incorrect cell has its value replaced
336:             *             or is incorrect. <P>
337:             *
338:             */
339:
340:            public void testModifySimpleWithSkip() throws IOException {
341:                String filename = System.getProperty("HSSF.testdata.path");
342:
343:                filename = filename + "/SimpleWithSkip.xls";
344:                FileInputStream instream = new FileInputStream(filename);
345:                POIFSFileSystem fsin = new POIFSFileSystem(instream);
346:                HSSFWorkbook workbook = new HSSFWorkbook(fsin);
347:                HSSFSheet sheet = workbook.getSheetAt(0);
348:                HSSFCell cell = sheet.getRow((short) 0).getCell((short) 1);
349:
350:                cell.setCellValue(REPLACED);
351:                cell = sheet.getRow((short) 1).getCell((short) 0);
352:                cell.setCellValue(REPLACED);
353:                File destination = TempFile.createTempFile(
354:                        "SimpleWithSkipResult", ".xls");
355:                FileOutputStream outstream = new FileOutputStream(destination);
356:
357:                workbook.write(outstream);
358:                instream.close();
359:                outstream.close();
360:                instream = new FileInputStream(destination);
361:                workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
362:                sheet = workbook.getSheetAt(0);
363:                cell = sheet.getRow((short) 0).getCell((short) 1);
364:                assertEquals(REPLACED, cell.getStringCellValue());
365:                cell = sheet.getRow((short) 0).getCell((short) 0);
366:                assertEquals(DO_NOT_REPLACE, cell.getStringCellValue());
367:                cell = sheet.getRow((short) 1).getCell((short) 0);
368:                assertEquals(REPLACED, cell.getStringCellValue());
369:                cell = sheet.getRow((short) 1).getCell((short) 1);
370:                assertEquals(DO_NOT_REPLACE, cell.getStringCellValue());
371:                instream.close();
372:            }
373:
374:            /**
375:             * TEST NAME:  Test Modify Sheet With Styling<P>
376:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet with string values and replace
377:             *             them with other string values despite any styling.  In this release of HSSF styling will
378:             *             probably be lost and is NOT tested.<P>
379:             * SUCCESS:    HSSF reads a sheet.  HSSF replaces the cell values with other cell values. HSSF
380:             *             writes the sheet out to another file.  HSSF reads the result and ensures the value
381:             *             has been properly replaced.    <P>
382:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF does not write the sheet or excepts.
383:             *             HSSF does not re-read the sheet or excepts.  Upon re-reading the sheet the value
384:             *             is incorrect or has not been replaced. <P>
385:             *
386:             */
387:
388:            public void testModifySimpleWithStyling() throws IOException {
389:                String filename = System.getProperty("HSSF.testdata.path");
390:
391:                filename = filename + "/SimpleWithStyling.xls";
392:                FileInputStream instream = new FileInputStream(filename);
393:                POIFSFileSystem fsin = new POIFSFileSystem(instream);
394:                HSSFWorkbook workbook = new HSSFWorkbook(fsin);
395:                HSSFSheet sheet = workbook.getSheetAt(0);
396:
397:                for (int k = 0; k < 4; k++) {
398:                    HSSFCell cell = sheet.getRow((short) k).getCell((short) 0);
399:
400:                    cell.setCellValue(REPLACED);
401:                }
402:                File destination = TempFile.createTempFile(
403:                        "SimpleWithStylingResult", ".xls");
404:                FileOutputStream outstream = new FileOutputStream(destination);
405:
406:                workbook.write(outstream);
407:                instream.close();
408:                outstream.close();
409:                instream = new FileInputStream(destination);
410:                workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
411:                sheet = workbook.getSheetAt(0);
412:                for (int k = 0; k < 4; k++) {
413:                    HSSFCell cell = sheet.getRow((short) k).getCell((short) 0);
414:
415:                    assertEquals(REPLACED, cell.getStringCellValue());
416:                }
417:                instream.close();
418:            }
419:
420:            /**
421:             * TEST NAME:  Test Modify Employee Sheet<P>
422:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet with string values and replace
423:             *             them with other string values despite any styling.  In this release of HSSF styling will
424:             *             probably be lost and is NOT tested.<P>
425:             * SUCCESS:    HSSF reads a sheet.  HSSF replaces the cell values with other cell values. HSSF
426:             *             writes the sheet out to another file.  HSSF reads the result and ensures the value
427:             *             has been properly replaced.    <P>
428:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF does not write the sheet or excepts.
429:             *             HSSF does not re-read the sheet or excepts.  Upon re-reading the sheet the value
430:             *             is incorrect or has not been replaced. <P>
431:             *
432:             */
433:
434:            public void testModifyEmployee() throws IOException {
435:                String filename = System.getProperty("HSSF.testdata.path");
436:
437:                filename = filename + "/Employee.xls";
438:                FileInputStream instream = new FileInputStream(filename);
439:                POIFSFileSystem fsin = new POIFSFileSystem(instream);
440:                HSSFWorkbook workbook = new HSSFWorkbook(fsin);
441:                HSSFSheet sheet = workbook.getSheetAt(0);
442:                HSSFCell cell = sheet.getRow((short) 3).getCell((short) 2);
443:
444:                cell.setCellValue(LAST_NAME_VALUE);
445:                cell = sheet.getRow((short) 4).getCell((short) 2);
446:                cell.setCellValue(FIRST_NAME_VALUE);
447:                cell = sheet.getRow((short) 5).getCell((short) 2);
448:                cell.setCellValue(SSN_VALUE);
449:                File destination = TempFile.createTempFile("EmployeeResult",
450:                        ".xls");
451:                FileOutputStream outstream = new FileOutputStream(destination);
452:
453:                workbook.write(outstream);
454:                instream.close();
455:                outstream.close();
456:                instream = new FileInputStream(destination);
457:                workbook = new HSSFWorkbook(new POIFSFileSystem(instream));
458:                sheet = workbook.getSheetAt(0);
459:                assertEquals(EMPLOYEE_INFORMATION, sheet.getRow(1).getCell(
460:                        (short) 1).getStringCellValue());
461:                assertEquals(LAST_NAME_VALUE, sheet.getRow(3)
462:                        .getCell((short) 2).getStringCellValue());
463:                assertEquals(FIRST_NAME_VALUE, sheet.getRow(4).getCell(
464:                        (short) 2).getStringCellValue());
465:                assertEquals(SSN_VALUE, sheet.getRow(5).getCell((short) 2)
466:                        .getStringCellValue());
467:                instream.close();
468:            }
469:
470:            /**
471:             * TEST NAME:  Test Read Sheet with an RK number<P>
472:             * OBJECTIVE:  Test that HSSF can read a simple spreadsheet with and RKRecord and correctly
473:             *             identify the cell as numeric and convert it to a NumberRecord.  <P>
474:             * SUCCESS:    HSSF reads a sheet.  HSSF returns that the cell is a numeric type cell.    <P>
475:             * FAILURE:    HSSF does not read a sheet or excepts.  HSSF incorrectly indentifies the cell<P>
476:             *
477:             */
478:
479:            public void testReadSheetWithRK() throws IOException {
480:                String filename = System.getProperty("HSSF.testdata.path");
481:
482:                filename = filename + "/rk.xls";
483:
484:                // a.xls has a value on position (0,0)
485:                FileInputStream in = new FileInputStream(filename);
486:                POIFSFileSystem fs = new POIFSFileSystem(in);
487:                HSSFWorkbook h = new HSSFWorkbook(fs);
488:                HSSFSheet s = h.getSheetAt(0);
489:                HSSFRow r = s.getRow(0);
490:                HSSFCell c = r.getCell((short) 0);
491:                int a = c.getCellType();
492:
493:                assertEquals(a, c.CELL_TYPE_NUMERIC);
494:            }
495:
496:            /**
497:             * TEST NAME:  Test Write/Modify Sheet Simple <P>
498:             * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values,
499:             *             remove some rows, yet still have a valid file/data.<P>
500:             * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects
501:             *             Last row, first row is tested against the correct values (74,25).<P>
502:             * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.
503:             *             HSSFSheet last row or first row is incorrect.             <P>
504:             *
505:             */
506:
507:            public void testWriteModifySheetMerged() throws IOException {
508:                File file = TempFile.createTempFile("testWriteSheetMerged",
509:                        ".xls");
510:                FileOutputStream out = new FileOutputStream(file);
511:                FileInputStream in = null;
512:                HSSFWorkbook wb = new HSSFWorkbook();
513:                HSSFSheet s = wb.createSheet();
514:                HSSFRow r = null;
515:                HSSFCell c = null;
516:
517:                for (short rownum = (short) 0; rownum < 100; rownum++) {
518:                    r = s.createRow(rownum);
519:
520:                    // r.setRowNum(( short ) rownum);
521:                    for (short cellnum = (short) 0; cellnum < 50; cellnum += 2) {
522:                        c = r.createCell(cellnum);
523:                        c
524:                                .setCellValue(rownum
525:                                        * 10000
526:                                        + cellnum
527:                                        + (((double) rownum / 1000) + ((double) cellnum / 10000)));
528:                        c = r.createCell((short) (cellnum + 1));
529:                        c.setCellValue("TEST");
530:                    }
531:                }
532:                s.addMergedRegion(new Region((short) 0, (short) 0, (short) 10,
533:                        (short) 10));
534:                s.addMergedRegion(new Region((short) 30, (short) 5, (short) 40,
535:                        (short) 15));
536:                wb.write(out);
537:                out.close();
538:                sanityChecker.checkHSSFWorkbook(wb);
539:                in = new FileInputStream(file);
540:                wb = new HSSFWorkbook(new POIFSFileSystem(in));
541:                s = wb.getSheetAt(0);
542:                Region r1 = s.getMergedRegionAt(0);
543:                Region r2 = s.getMergedRegionAt(1);
544:
545:                in.close();
546:
547:                // System.out.println(file.length());
548:                // assertEquals("FILE LENGTH == 87552",file.length(), 87552);
549:                // System.out.println(s.getLastRowNum());
550:                assertEquals("REGION1 = 0,0,10,10", 0, new Region((short) 0,
551:                        (short) 0, (short) 10, (short) 10).compareTo(r1));
552:                assertEquals("REGION2 == 30,5,40,15", 0, new Region((short) 30,
553:                        (short) 5, (short) 40, (short) 15).compareTo(r2));
554:            }
555:
556:            /**
557:             * Test the backup field gets set as expected.
558:             */
559:
560:            public void testBackupRecord() throws Exception {
561:                HSSFWorkbook wb = new HSSFWorkbook();
562:                wb.createSheet();
563:                Workbook workbook = wb.getWorkbook();
564:                BackupRecord record = workbook.getBackupRecord();
565:
566:                assertEquals(0, record.getBackup());
567:                wb.setBackupFlag(true);
568:                assertEquals(1, record.getBackup());
569:            }
570:
571:            /**
572:             * This tests is for bug [ #506658 ] Repeating output.
573:             *
574:             * We need to make sure only one LabelSSTRecord is produced.
575:             */
576:
577:            public void testRepeatingBug() throws Exception {
578:                HSSFWorkbook workbook = new HSSFWorkbook();
579:                HSSFSheet sheet = workbook.createSheet("Design Variants");
580:                HSSFRow row = sheet.createRow((short) 2);
581:                HSSFCell cell = row.createCell((short) 1);
582:
583:                cell.setCellValue("Class");
584:                cell = row.createCell((short) 2);
585:
586:                // workbook.write(new FileOutputStream("/a2.xls"));
587:                ValueRecordsAggregate valueAggregate = (ValueRecordsAggregate) sheet
588:                        .getSheet().findFirstRecordBySid(
589:                                ValueRecordsAggregate.sid);
590:                int sstRecords = 0;
591:                Iterator iterator = valueAggregate.getIterator();
592:
593:                while (iterator.hasNext()) {
594:                    if (((Record) iterator.next()).getSid() == LabelSSTRecord.sid) {
595:                        sstRecords++;
596:                    }
597:                }
598:                assertEquals(1, sstRecords);
599:            }
600:
601:            public void testManyRows() throws Exception {
602:                String testName = "TestManyRows";
603:                File file = TempFile.createTempFile(testName, ".xls");
604:                FileOutputStream out = new FileOutputStream(file);
605:                HSSFWorkbook workbook = new HSSFWorkbook();
606:                HSSFSheet sheet = workbook.createSheet();
607:                HSSFRow row = null;
608:                HSSFCell cell = null;
609:                int i, j;
610:                for (i = 0, j = 32771; j > 0; i++, j--) {
611:                    row = sheet.createRow(i);
612:                    cell = row.createCell((short) 0);
613:                    cell.setCellValue(i);
614:                }
615:                workbook.write(out);
616:                out.close();
617:                sanityChecker.checkHSSFWorkbook(workbook);
618:                assertEquals("LAST ROW == 32770", 32770, sheet.getLastRowNum());
619:                double lastVal = cell.getNumericCellValue();
620:
621:                FileInputStream in = new FileInputStream(file);
622:                POIFSFileSystem fs = new POIFSFileSystem(in);
623:                HSSFWorkbook wb = new HSSFWorkbook(fs);
624:                HSSFSheet s = wb.getSheetAt(0);
625:                row = s.getRow(32770);
626:                cell = row.getCell((short) 0);
627:                assertEquals("Value from last row == 32770", lastVal, cell
628:                        .getNumericCellValue(), 0);
629:                assertEquals("LAST ROW == 32770", 32770, s.getLastRowNum());
630:                in.close();
631:                file.deleteOnExit();
632:            }
633:
634:            /**
635:             * Generate a file to visually/programmatically verify repeating rows and cols made it
636:             */
637:            public void testRepeatingColsRows() throws IOException {
638:                HSSFWorkbook workbook = new HSSFWorkbook();
639:                HSSFSheet sheet = workbook.createSheet("Test Print Titles");
640:                String sheetName = workbook.getSheetName(0);
641:
642:                HSSFRow row = sheet.createRow(0);
643:
644:                HSSFCell cell = row.createCell((short) 1);
645:                cell.setCellValue("hi");
646:
647:                workbook.setRepeatingRowsAndColumns(0, 0, 1, 0, 0);
648:
649:                File file = TempFile.createTempFile("testPrintTitles", ".xls");
650:
651:                FileOutputStream fileOut = new FileOutputStream(file);
652:                workbook.write(fileOut);
653:                fileOut.close();
654:
655:                assertTrue("file exists", file.exists());
656:
657:            }
658:
659:            public static void main(String[] ignored_args) {
660:                String filename = System.getProperty("HSSF.testdata.path");
661:
662:                // assume this is relative to basedir
663:                if (filename == null) {
664:                    System.setProperty("HSSF.testdata.path",
665:                            "src/testcases/org/apache/poi/hssf/data");
666:                }
667:                System.out
668:                        .println("Testing org.apache.poi.hssf.usermodel.HSSFWorkbook");
669:                junit.textui.TestRunner.run(TestWorkbook.class);
670:            }
671:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.