You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Nick Chalko <ni...@chalko.com> on 2003/01/07 21:24:10 UTC

HSSFAssert class.

I find this class usefull at work for testing my application that 
generates spreadsheets.
------------------------------------------------------------------------


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 nchalko
 *
 */
public class HSSFAssert  {
    /**
     * Constructor for HSSFTestUtils.
     */
    private HSSFAssert() {
        super();
    }
    /**
     * Method assertHSSFValuesEquals.
     * @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);

    }

    /**
     * Method assertHSSFValuesEquals.
     * @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);
        }

    }

    /**
     * Method assertHSSFValuesEquals.
     * @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);
        }
    }

    /**
     * Method assertHSSFValuesEquals.
     * @param string
     * @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);
        }
    }

    /**
     * Method assertHSSFValuesEquals.
     * @param string
     * @param expectedCell
     * @param expectedCell1
     */
    public static void assertHSSFCellValuesEquals(String message, 
HSSFCell expectedCell, HSSFCell resultCell) {
        if (expectedCell==null && resultCell==null) {return ;}
        
        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;
        }

    }

    public static HSSFWorkbook openWorkBook(File file) throws IOException {

        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
        return new HSSFWorkbook(fs);

    }

    /**
     * Method assertHSSFSheetPrintSetupEquals.
     * @param string
     * @param expectedSheet
     * @param resultSheet
     */
    public static void assertHSSFPrintSetupEquals(String message,  
HSSFPrintSetup expectedPrintSetup, HSSFPrintSetup resultPrintSetup) {
        
        Assert.assertEquals(message +"Paper 
Size",expectedPrintSetup.getPaperSize(),resultPrintSetup.getPaperSize());
        
    }

}