View | Details | Raw Unified | Return to bug 17925
Collapse All | Expand All

(-)HSSFAssert.java (+221 lines)
Added Link Here
1
2
/* ====================================================================
3
 * The Apache Software License, Version 1.1
4
 *
5
 * Copyright (c) 2003 The Apache Software Foundation.  All rights
6
 * reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. The end-user documentation included with the redistribution,
21
 *    if any, must include the following acknowledgment:
22
 *       "This product includes software developed by the
23
 *        Apache Software Foundation (http://www.apache.org/)."
24
 *    Alternately, this acknowledgment may appear in the software itself,
25
 *    if and wherever such third-party acknowledgments normally appear.
26
 *
27
 * 4. The names "Apache" and "Apache Software Foundation" and
28
 *    "Apache POI" must not be used to endorse or promote products
29
 *    derived from this software without prior written permission. For
30
 *    written permission, please contact apache@apache.org.
31
 *
32
 * 5. Products derived from this software may not be called "Apache",
33
 *    "Apache POI", nor may "Apache" appear in their name, without
34
 *    prior written permission of the Apache Software Foundation.
35
 *
36
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
 * SUCH DAMAGE.
48
 * ====================================================================
49
 *
50
 * This software consists of voluntary contributions made by many
51
 * individuals on behalf of the Apache Software Foundation.  For more
52
 * information on the Apache Software Foundation, please see
53
 * <http://www.apache.org/>.
54
 */
55
56
package org.apache.poi.hssf.test;
57
import java.io.File;
58
import java.io.FileInputStream;
59
import java.io.IOException;
60
import junit.framework.Assert;
61
import org.apache.poi.hssf.usermodel.HSSFCell;
62
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
63
import org.apache.poi.hssf.usermodel.HSSFRow;
64
import org.apache.poi.hssf.usermodel.HSSFSheet;
65
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
66
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
67
/**
68
 * JUnit asserts for HSSF.
69
 * @author Nick Chalko (nickchalko@apache.org)
70
 */
71
public class HSSFAssert {
72
    /**
73
     * Constructor for HSSFTestUtils.
74
     */
75
    private HSSFAssert() {
76
        super();
77
    }
78
    /**
79
     * Assert two spreadsheet files have equal values..
80
     * @param message
81
     * @param expected The "good" xsl file to compare against
82
     * @param result The file to test.
83
     * @throws AssertionFailedError if the assertion fails.
84
     */
85
    public static void assertHSSFValuesEquals(String message, File expected, File result) throws IOException {
86
        Assert.assertTrue(message + " file " + expected + " does not exists", expected.exists());
87
        Assert.assertTrue(message + " file " + result + " does not exists", result.exists());
88
        HSSFWorkbook expectedWB = openWorkBook(expected);
89
        HSSFWorkbook resultWB = openWorkBook(result);
90
        assertHSSFWorkbookValuesEquals(message + " " + expected.getName(), expectedWB, resultWB);
91
    }
92
    /**
93
     * Assert two workbooks have equal values..
94
     * @param message
95
     * @param expectedWB
96
     * @param resultWB
97
     */
98
    public static void assertHSSFWorkbookValuesEquals(String message, HSSFWorkbook expectedWB, HSSFWorkbook resultWB) {
99
        Assert.assertEquals(
100
            message + " number of sheets",
101
            expectedWB.getNumberOfSheets(),
102
            resultWB.getNumberOfSheets());
103
        int count = expectedWB.getNumberOfSheets();
104
        for (int i = 0; i < count; i++) {
105
            HSSFSheet expectedSheet = expectedWB.getSheetAt(i);
106
            HSSFSheet resultSheet = resultWB.getSheetAt(i);
107
            assertHSSFSheetValuesEquals(message + " sheet " + i + " ", expectedSheet, resultSheet);
108
        }
109
    }
110
    /**
111
     * Assert two sheets have have equal values..
112
     * @param message
113
     * @param expectedSheet
114
     * @param resultSheet
115
     */
116
    public static void assertHSSFSheetValuesEquals(String message, HSSFSheet expectedSheet, HSSFSheet resultSheet) {
117
        Assert.assertEquals(
118
            message + " first row number ",
119
            expectedSheet.getFirstRowNum(),
120
            resultSheet.getFirstRowNum());
121
        Assert.assertEquals(message + " last row number ", expectedSheet.getLastRowNum(), resultSheet.getLastRowNum());
122
        int firstRow = expectedSheet.getFirstRowNum();
123
        int lastRow = expectedSheet.getLastRowNum();
124
        for (int i = firstRow; i <= lastRow; i++) {
125
            HSSFRow expectedRow = expectedSheet.getRow(i);
126
            HSSFRow resultRow = resultSheet.getRow(i);
127
            assertHSSFRowValuesEquals(message + " row " + i + " ", expectedRow, resultRow);
128
        }
129
    }
130
    /**
131
     * Assert to rows have equal value.
132
     * @param message
133
     * @param expectedRow
134
     * @param resultRow
135
     */
136
    public static void assertHSSFRowValuesEquals(String message, HSSFRow expectedRow, HSSFRow resultRow) {
137
        //Assert.assertEquals(message + "  number of cells", expectedRow.getPhysicalNumberOfCells(), resultRow.getPhysicalNumberOfCells());
138
        short firstCell = (short) Math.min(expectedRow.getFirstCellNum(), resultRow.getFirstCellNum());
139
        short lastCell = (short) Math.max(expectedRow.getLastCellNum(), resultRow.getLastCellNum());
140
        for (short i = firstCell; i <= lastCell; i++) {
141
            HSSFCell expectedCell = expectedRow.getCell(i);
142
            HSSFCell resultCell = resultRow.getCell(i);
143
            assertHSSFCellValuesEquals(message + " cell " + i + " ", expectedCell, resultCell);
144
        }
145
    }
146
    /**
147
     * Asserts two cell values are equal.
148
     * @param message
149
     * @param expectedCell
150
     * @param expectedCell1
151
     */
152
    public static void assertHSSFCellValuesEquals(String message, HSSFCell expectedCell, HSSFCell resultCell) {
153
        if (expectedCell == null && resultCell == null) {
154
            return;
155
        }
156
        if (expectedCell == null || resultCell == null) {
157
            Assert.assertEquals(message + " value", expectedCell, resultCell);
158
        }
159
        Assert.assertEquals(message + " type ", expectedCell.getCellType(), resultCell.getCellType());
160
        switch (expectedCell.getCellType()) {
161
            case HSSFCell.CELL_TYPE_BLANK :
162
                break;
163
            case HSSFCell.CELL_TYPE_BOOLEAN :
164
                Assert.assertEquals(
165
                    message + " value",
166
                    expectedCell.getBooleanCellValue(),
167
                    resultCell.getBooleanCellValue());
168
                break;
169
            case HSSFCell.CELL_TYPE_ERROR :
170
                Assert.assertEquals(
171
                    message + " value",
172
                    expectedCell.getErrorCellValue(),
173
                    resultCell.getErrorCellValue());
174
                break;
175
            case HSSFCell.CELL_TYPE_FORMULA :
176
                Assert.assertEquals(
177
                    message + " value",
178
                    expectedCell.getStringCellValue(),
179
                    resultCell.getStringCellValue());
180
                break;
181
            case HSSFCell.CELL_TYPE_NUMERIC :
182
                Assert.assertEquals(
183
                    message + " value",
184
                    expectedCell.getNumericCellValue(),
185
                    resultCell.getNumericCellValue(),
186
                    0);
187
                break;
188
            case HSSFCell.CELL_TYPE_STRING :
189
                Assert.assertEquals(
190
                    message + " value",
191
                    expectedCell.getStringCellValue(),
192
                    resultCell.getStringCellValue());
193
                break;
194
            default :
195
                Assert.fail(message + " unexpected type " + expectedCell.getCellType());
196
                break;
197
        }
198
    }
199
    /**
200
     * Convinencse method to open a workbook.
201
     * @param file
202
     * @return HSSFWorkbook
203
     * @throws IOException
204
     */
205
    public static HSSFWorkbook openWorkBook(File file) throws IOException {
206
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
207
        return new HSSFWorkbook(fs);
208
    }
209
    /**
210
     * Asserts that the print setup of two sheets are equal
211
     * @param message
212
     * @param expectedSheet
213
     * @param resultSheet
214
     */
215
    public static void assertHSSFPrintSetupEquals(
216
        String message,
217
        HSSFPrintSetup expectedPrintSetup,
218
        HSSFPrintSetup resultPrintSetup) {
219
        Assert.assertEquals(message + "Paper Size", expectedPrintSetup.getPaperSize(), resultPrintSetup.getPaperSize());
220
    }
221
}

Return to bug 17925