This project has retired. For details please refer to its Attic page.
XSSFWorkbookTest xref
View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.plugin.officescraper;
19  
20  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21  import org.apache.poi.ss.usermodel.Cell;
22  import org.apache.poi.ss.usermodel.Row;
23  import org.apache.poi.ss.usermodel.Sheet;
24  import org.apache.poi.ss.usermodel.Workbook;
25  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  /**
35   * General test to verify usability of the {@link XSSFWorkbook} class.
36   *
37   * @author Michele Mostarda (mostarda@fbk.eu)
38   */
39  public class XSSFWorkbookTest {
40  
41      private static final Logger logger = LoggerFactory.getLogger(XSSFWorkbookTest.class);
42  
43      @Test
44      public void testXLSXFormatAccess() throws IOException {
45          verifyResource("test1-workbook.xlsx");
46      }
47  
48      @Test
49      public void testXLSFormatAccess() throws IOException {
50          verifyResource("test2-workbook.xls");
51      }
52  
53      private void verifyResource(String resource) throws IOException {
54          final InputStream document = this.getClass().getResourceAsStream(resource);
55          final Workbook wb;
56          if(resource.endsWith(".xlsx")) {
57              wb = new XSSFWorkbook(document);
58          } else if(resource.endsWith("xls")) {
59              wb = new HSSFWorkbook(document);
60          } else {
61              throw new IllegalArgumentException("Unsupported extension for resource " + resource);
62          }
63          Assert.assertEquals(2, wb.getNumberOfSheets());
64          Sheet sheet;
65          for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
66              sheet = wb.getSheetAt(sheetIndex);
67              int rowcount = 0;
68              for (Row row : sheet) {
69                  rowcount++;
70                  int cellcount = 0;
71                  for (Cell cell : row) {
72                      cellcount++;
73                      logger.debug(
74                              String.format(
75                                      "cell [%d, %d]: %s",
76                                      cell.getRowIndex(),
77                                      cell.getColumnIndex(),
78                                      cell.getStringCellValue()
79                              )
80                      );
81                      verifyContent(sheetIndex, cell.getRowIndex(), cell.getColumnIndex(), cell.getStringCellValue());
82                  }
83                  Assert.assertEquals(3, cellcount);
84              }
85              Assert.assertEquals(3, rowcount);
86          }
87      }
88  
89      private void verifyContent(int sheet, int row, int col, String content) {
90          Assert.assertEquals(
91                  String.format("%s %d.%d", sheet == 0 ? "a" : "b", row + 1, col + 1),
92                  content
93          );
94      }
95  
96  }