Here is a sample usage of the class.
// Already existing "GOOD" file String expected = "testStatic1.xls"; // Where the orgram will write it String result = "output/text.xls"; myClass.createQAReportFile(result); assertSheet1Values(expected, result); assertSheet1PrintSetup(expected, result);
Index: HSSFAssert.java =================================================================== RCS file: HSSFAssert.java diff -N HSSFAssert.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ HSSFAssert.java 10 Mar 2003 23:30:34 -0000 @@ -0,0 +1,221 @@ + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 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 [EMAIL PROTECTED] + * + * 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 + * <http://www.apache.org/>. + */ + +package org.apache.poi.hssf.test; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import junit.framework.Assert; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFPrintSetup; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; +/** + * JUnit asserts for HSSF. + * @author Nick Chalko ([EMAIL PROTECTED]) + */ +public class HSSFAssert { + /** + * Constructor for HSSFTestUtils. + */ + private HSSFAssert() { + super(); + } + /** + * Assert two spreadsheet files have equal values.. + * @param message + * @param expected The "good" xsl file to compare against + * @param result The file to test. + * @throws AssertionFailedError if the assertion fails. + */ + public static void assertHSSFValuesEquals(String message, File expected, File result) throws IOException { + Assert.assertTrue(message + " file " + expected + " does not exists", expected.exists()); + Assert.assertTrue(message + " file " + result + " does not exists", result.exists()); + HSSFWorkbook expectedWB = openWorkBook(expected); + HSSFWorkbook resultWB = openWorkBook(result); + assertHSSFWorkbookValuesEquals(message + " " + expected.getName(), expectedWB, resultWB); + } + /** + * Assert two workbooks have equal values.. + * @param message + * @param expectedWB + * @param resultWB + */ + public static void assertHSSFWorkbookValuesEquals(String message, HSSFWorkbook expectedWB, HSSFWorkbook resultWB) { + Assert.assertEquals( + message + " number of sheets", + expectedWB.getNumberOfSheets(), + resultWB.getNumberOfSheets()); + int count = expectedWB.getNumberOfSheets(); + for (int i = 0; i < count; i++) { + HSSFSheet expectedSheet = expectedWB.getSheetAt(i); + HSSFSheet resultSheet = resultWB.getSheetAt(i); + assertHSSFSheetValuesEquals(message + " sheet " + i + " ", expectedSheet, resultSheet); + } + } + /** + * Assert two sheets have have equal values.. + * @param message + * @param expectedSheet + * @param resultSheet + */ + public static void assertHSSFSheetValuesEquals(String message, HSSFSheet expectedSheet, HSSFSheet resultSheet) { + Assert.assertEquals( + message + " first row number ", + expectedSheet.getFirstRowNum(), + resultSheet.getFirstRowNum()); + Assert.assertEquals(message + " last row number ", expectedSheet.getLastRowNum(), resultSheet.getLastRowNum()); + int firstRow = expectedSheet.getFirstRowNum(); + int lastRow = expectedSheet.getLastRowNum(); + for (int i = firstRow; i <= lastRow; i++) { + HSSFRow expectedRow = expectedSheet.getRow(i); + HSSFRow resultRow = resultSheet.getRow(i); + assertHSSFRowValuesEquals(message + " row " + i + " ", expectedRow, resultRow); + } + } + /** + * Assert to rows have equal value. + * @param message + * @param expectedRow + * @param resultRow + */ + public static void assertHSSFRowValuesEquals(String message, HSSFRow expectedRow, HSSFRow resultRow) { + //Assert.assertEquals(message + " number of cells", expectedRow.getPhysicalNumberOfCells(), resultRow.getPhysicalNumberOfCells()); + short firstCell = (short) Math.min(expectedRow.getFirstCellNum(), resultRow.getFirstCellNum()); + short lastCell = (short) Math.max(expectedRow.getLastCellNum(), resultRow.getLastCellNum()); + for (short i = firstCell; i <= lastCell; i++) { + HSSFCell expectedCell = expectedRow.getCell(i); + HSSFCell resultCell = resultRow.getCell(i); + assertHSSFCellValuesEquals(message + " cell " + i + " ", expectedCell, resultCell); + } + } + /** + * Asserts two cell values are equal. + * @param message + * @param expectedCell + * @param expectedCell1 + */ + public static void assertHSSFCellValuesEquals(String message, HSSFCell expectedCell, HSSFCell resultCell) { + if (expectedCell == null && resultCell == null) { + return; + } + if (expectedCell == null || resultCell == null) { + Assert.assertEquals(message + " value", expectedCell, resultCell); + } + Assert.assertEquals(message + " type ", expectedCell.getCellType(), resultCell.getCellType()); + switch (expectedCell.getCellType()) { + case HSSFCell.CELL_TYPE_BLANK : + break; + case HSSFCell.CELL_TYPE_BOOLEAN : + Assert.assertEquals( + message + " value", + expectedCell.getBooleanCellValue(), + resultCell.getBooleanCellValue()); + break; + case HSSFCell.CELL_TYPE_ERROR : + Assert.assertEquals( + message + " value", + expectedCell.getErrorCellValue(), + resultCell.getErrorCellValue()); + break; + case HSSFCell.CELL_TYPE_FORMULA : + Assert.assertEquals( + message + " value", + expectedCell.getStringCellValue(), + resultCell.getStringCellValue()); + break; + case HSSFCell.CELL_TYPE_NUMERIC : + Assert.assertEquals( + message + " value", + expectedCell.getNumericCellValue(), + resultCell.getNumericCellValue(), + 0); + break; + case HSSFCell.CELL_TYPE_STRING : + Assert.assertEquals( + message + " value", + expectedCell.getStringCellValue(), + resultCell.getStringCellValue()); + break; + default : + Assert.fail(message + " unexpected type " + expectedCell.getCellType()); + break; + } + } + /** + * Convinencse method to open a workbook. + * @param file + * @return HSSFWorkbook + * @throws IOException + */ + public static HSSFWorkbook openWorkBook(File file) throws IOException { + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file)); + return new HSSFWorkbook(fs); + } + /** + * Asserts that the print setup of two sheets are equal + * @param message + * @param expectedSheet + * @param resultSheet + */ + public static void assertHSSFPrintSetupEquals( + String message, + HSSFPrintSetup expectedPrintSetup, + HSSFPrintSetup resultPrintSetup) { + Assert.assertEquals(message + "Paper Size", expectedPrintSetup.getPaperSize(), resultPrintSetup.getPaperSize()); + } +}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
