From 2184c36b2171cbf65b459c34e43f04932ba059a7 Mon Sep 17 00:00:00 2001 From: Glen Stampoultzis Date: Mon, 29 Apr 2002 11:10:29 +0000 Subject: [PATCH] Some testing related stuff. The first shot at a sanity tester for XLS record structure. Needs heaps more checks but it's a start. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352558 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/model/Workbook.java | 5 + .../org/apache/poi/hssf/util/HSSFColor.java | 8 +- .../poi/hssf/usermodel/SanityChecker.java | 198 ++++++++++++++++++ .../poi/hssf/usermodel/TestCellStyle.java | 12 +- .../poi/hssf/usermodel/TestWorkbook.java | 36 +--- 5 files changed, 227 insertions(+), 32 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/usermodel/SanityChecker.java diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index a6ab7af382..10fb57f430 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -1772,4 +1772,9 @@ public class Workbook { } return null; } + + public List getRecords() + { + return records; + } } diff --git a/src/java/org/apache/poi/hssf/util/HSSFColor.java b/src/java/org/apache/poi/hssf/util/HSSFColor.java index c3d58f4be8..6e9757ef8d 100644 --- a/src/java/org/apache/poi/hssf/util/HSSFColor.java +++ b/src/java/org/apache/poi/hssf/util/HSSFColor.java @@ -87,7 +87,7 @@ public class HSSFColor * it takes to create it once per request but you will not hold onto it * if you have none of those requests. * - * @returns a hashtable containing all colors mapped to their gnumeric-like + * @return a hashtable containing all colors mapped to their gnumeric-like * triplet string */ @@ -148,7 +148,7 @@ public class HSSFColor } /** - * @returns index to the standard palet + * @return index to the standard palet */ public short getIndex() @@ -157,7 +157,7 @@ public class HSSFColor } /** - * @returns short[] triplet representation like that in Excel + * @return triplet representation like that in Excel */ public short [] getTriplet() @@ -168,7 +168,7 @@ public class HSSFColor // its a hack but its a good hack /** - * @returns a hex string exactly like a gnumeric triplet + * @return a hex string exactly like a gnumeric triplet */ public String getHexString() diff --git a/src/testcases/org/apache/poi/hssf/usermodel/SanityChecker.java b/src/testcases/org/apache/poi/hssf/usermodel/SanityChecker.java new file mode 100644 index 0000000000..b8974c85a0 --- /dev/null +++ b/src/testcases/org/apache/poi/hssf/usermodel/SanityChecker.java @@ -0,0 +1,198 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.poi.hssf.usermodel; + +import junit.framework.Assert; +import org.apache.poi.hssf.model.Sheet; +import org.apache.poi.hssf.model.Workbook; +import org.apache.poi.hssf.record.*; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import java.util.List; + +/** + * Designed to check wither the records written actually make sense. + */ +public class SanityChecker + extends Assert +{ + private class CheckRecord + { + Class record; + char occurance; // 1 = one time, M = many times + + public CheckRecord(Class record, char occurance) + { + this.record = record; + this.occurance = occurance; + } + + public Class getRecord() + { + return record; + } + + public char getOccurance() + { + return occurance; + } + } + + CheckRecord[] workbookRecords = new CheckRecord[] { + new CheckRecord(BOFRecord.class, '1'), + new CheckRecord(InterfaceHdrRecord.class, '1'), + new CheckRecord(MMSRecord.class, '1'), + new CheckRecord(InterfaceEndRecord.class, '1'), + new CheckRecord(WriteAccessRecord.class, '1'), + new CheckRecord(CodepageRecord.class, '1'), + new CheckRecord(DSFRecord.class, '1'), + new CheckRecord(TabIdRecord.class, '1'), + new CheckRecord(FnGroupCountRecord.class, '1'), + new CheckRecord(WindowProtectRecord.class, '1'), + new CheckRecord(ProtectRecord.class, '1'), + new CheckRecord(PasswordRev4Record.class, '1'), + new CheckRecord(WindowOneRecord.class, '1'), + new CheckRecord(BackupRecord.class, '1'), + new CheckRecord(HideObjRecord.class, '1'), + new CheckRecord(DateWindow1904Record.class, '1'), + new CheckRecord(PrecisionRecord.class, '1'), + new CheckRecord(RefreshAllRecord.class, '1'), + new CheckRecord(BookBoolRecord.class, '1'), + new CheckRecord(FontRecord.class, 'M'), + new CheckRecord(FormatRecord.class, 'M'), + new CheckRecord(ExtendedFormatRecord.class, 'M'), + new CheckRecord(StyleRecord.class, 'M'), + new CheckRecord(UseSelFSRecord.class, '1'), + new CheckRecord(BoundSheetRecord.class, '1'), // Is this right? + new CheckRecord(CountryRecord.class, '1'), + new CheckRecord(SSTRecord.class, '1'), + new CheckRecord(ExtSSTRecord.class, '1'), + new CheckRecord(EOFRecord.class, '1'), + }; + + CheckRecord[] sheetRecords = new CheckRecord[] { + new CheckRecord(BOFRecord.class, '1'), + new CheckRecord(CalcModeRecord.class, '1'), + new CheckRecord(RefModeRecord.class, '1'), + new CheckRecord(IterationRecord.class, '1'), + new CheckRecord(DeltaRecord.class, '1'), + new CheckRecord(SaveRecalcRecord.class, '1'), + new CheckRecord(PrintHeadersRecord.class, '1'), + new CheckRecord(PrintGridlinesRecord.class, '1'), + new CheckRecord(GridsetRecord.class, '1'), + new CheckRecord(GutsRecord.class, '1'), + new CheckRecord(DefaultRowHeightRecord.class, '1'), + new CheckRecord(WSBoolRecord.class, '1'), + new CheckRecord(HeaderRecord.class, '1'), + new CheckRecord(FooterRecord.class, '1'), + new CheckRecord(HCenterRecord.class, '1'), + new CheckRecord(VCenterRecord.class, '1'), + new CheckRecord(PrintSetupRecord.class, '1'), + new CheckRecord(DefaultColWidthRecord.class, '1'), + new CheckRecord(DimensionsRecord.class, '1'), + new CheckRecord(WindowTwoRecord.class, '1'), + new CheckRecord(SelectionRecord.class, '1'), + new CheckRecord(EOFRecord.class, '1') + }; + + public void checkWorkbookRecords(Workbook workbook) + { + List records = workbook.getRecords(); + assertTrue(records.get(0) instanceof BOFRecord); + assertTrue(records.get(records.size() - 1) instanceof EOFRecord); + + checkRecordOrder(records, workbookRecords); + } + + public void checkSheetRecords(Sheet sheet) + { + List records = sheet.getRecords(); + assertTrue(records.get(0) instanceof BOFRecord); + assertTrue(records.get(records.size() - 1) instanceof EOFRecord); + + checkRecordOrder(records, sheetRecords); + } + + public void checkHSSFWorkbook(HSSFWorkbook wb) + { + checkWorkbookRecords(wb.getWorkbook()); + for (int i = 0; i < wb.getNumberOfSheets(); i++) + checkSheetRecords(wb.getSheetAt(i).getSheet()); + + } + + private void checkRecordOrder(List records, CheckRecord[] check) + { + int checkIndex = 0; + for (int recordIndex = 0; recordIndex < records.size(); recordIndex++) + { + Record record = (Record) records.get(recordIndex); + if (check[checkIndex].getRecord().isInstance(record)) + { + if (check[checkIndex].getOccurance() == 'M') + { + // skip over duplicate records if multiples are allowed + while (recordIndex+1 < records.size() && check[checkIndex].getRecord().isInstance(records.get(recordIndex+1))) + recordIndex++; + } + checkIndex++; + } + if (checkIndex >= check.length) + return; + } + fail("Could not find required record: " + check[checkIndex]); + } + +} diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java index 04fa4c6584..4b55b820cb 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java @@ -127,7 +127,9 @@ public class TestCellStyle } wb.write(out); out.close(); - assertEquals("FILE LENGTH == 87040", file.length(), 87040); + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); + assertEquals("FILE LENGTH == 87040", file.length(), 87040); // remove me assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); @@ -164,8 +166,10 @@ public class TestCellStyle wb.write(out); out.close(); + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); - assertEquals("FILE LENGTH ", 5632, file.length()); + assertEquals("FILE LENGTH ", 5632, file.length()); // remove me assertEquals("LAST ROW ", 0, s.getLastRowNum()); assertEquals("FIRST ROW ", 0, s.getFirstRowNum()); @@ -226,7 +230,9 @@ public class TestCellStyle } wb.write(out); out.close(); - assertEquals("FILE LENGTH == 87040", file.length(), 87040); + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); + assertEquals("FILE LENGTH == 87040", file.length(), 87040); // remove me assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java index 85a7129c9c..5660ae0273 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java @@ -73,6 +73,8 @@ import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate; * Class to test Workbook functionality * * @author Andrew C. Oliver + * @author Greg Merrill + * @author Siggi Cherem */ public class TestWorkbook @@ -88,6 +90,7 @@ public class TestWorkbook private static final String LAST_NAME_VALUE = "Bush"; private static final String FIRST_NAME_VALUE = "George"; private static final String SSN_VALUE = "555555555"; + private SanityChecker sanityChecker = new SanityChecker(); /** * Constructor TestWorkbook @@ -108,7 +111,6 @@ public class TestWorkbook * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. * HSSFSheet last row or first row is incorrect.

* - * @author Andrew C. Oliver */ public void testWriteSheetSimple() @@ -139,8 +141,9 @@ public class TestWorkbook } wb.write(out); out.close(); + sanityChecker.checkHSSFWorkbook(wb); assertEquals("FILE LENGTH == 87040", 87040, - file.length()); // changed because of new sheet behavior + file.length()); // remove me assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); @@ -156,7 +159,6 @@ public class TestWorkbook * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. * HSSFSheet last row or first row is incorrect.

* - * @author Andrew C. Oliver */ public void testWriteModifySheetSimple() @@ -198,11 +200,9 @@ public class TestWorkbook wb.write(out); out.close(); - // System.out.println(file.length()); - // assertEquals("FILE LENGTH == 87552",file.length(), 87552); - // System.out.println(s.getLastRowNum()); + sanityChecker.checkHSSFWorkbook(wb); assertEquals("FILE LENGTH == 45568", 45568, - file.length()); // changed due to new sheet behavior (<3) + file.length()); // remove assertEquals("LAST ROW == 74", 74, s.getLastRowNum()); assertEquals("FIRST ROW == 25", 25, s.getFirstRowNum()); } @@ -214,8 +214,6 @@ public class TestWorkbook * FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values * in the sheet in their known positions.

* - * @author Greg Merrill - * @author Andrew C. Oliver */ public void testReadSimple() @@ -242,8 +240,6 @@ public class TestWorkbook * FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values * in the sheet in their known positions.

* - * @author Greg Merrill - * @author Andrew C. Oliver */ public void testReadEmployeeSimple() @@ -283,8 +279,6 @@ public class TestWorkbook * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced.

* - * @author Andrew C. Oliver - * @author Greg Merrill */ public void testModifySimple() @@ -328,8 +322,6 @@ public class TestWorkbook * is incorrect or has not been replaced or the incorrect cell has its value replaced * or is incorrect.

* - * @author Andrew C. Oliver - * @author Greg Merrill */ public void testModifySimpleWithSkip() @@ -381,8 +373,6 @@ public class TestWorkbook * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced.

* - * @author Andrew C. Oliver - * @author Greg Merrill */ public void testModifySimpleWithStyling() @@ -433,8 +423,6 @@ public class TestWorkbook * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced.

* - * @author Andrew C. Oliver - * @author Greg Merrill */ public void testModifyEmployee() @@ -487,8 +475,6 @@ public class TestWorkbook * SUCCESS: HSSF reads a sheet. HSSF returns that the cell is a numeric type cell.

* FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly indentifies the cell

* - * @author Siggi Cherem - * @author Andrew C. Oliver (acoliver at apache dot org) */ public void testReadSheetWithRK() @@ -519,7 +505,6 @@ public class TestWorkbook * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. * HSSFSheet last row or first row is incorrect.

* - * @author Andrew C. Oliver */ public void testWriteModifySheetMerged() @@ -555,6 +540,7 @@ public class TestWorkbook ( short ) 15)); wb.write(out); out.close(); + sanityChecker.checkHSSFWorkbook(wb); in = new FileInputStream(file); wb = new HSSFWorkbook(new POIFSFileSystem(in)); s = wb.getSheetAt(0); @@ -582,7 +568,7 @@ public class TestWorkbook throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); + wb.createSheet(); Workbook workbook = wb.getWorkbook(); BackupRecord record = workbook.getBackupRecord(); @@ -629,12 +615,12 @@ public class TestWorkbook { String filename = System.getProperty("HSSF.testdata.path"); - // assume andy is running this in the debugger + // assume this is relative to basedir if (filename == null) { System.setProperty( "HSSF.testdata.path", - "/home/andy/npoi3/poi/production/testcases/net/sourceforge/poi/hssf/data"); + "src/testcases/org/apache/poi/hssf/data"); } System.out .println("Testing org.apache.poi.hssf.usermodel.HSSFWorkbook"); -- 2.39.5