001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hssf.extractor;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021:
022: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
023:
024: import junit.framework.TestCase;
025:
026: public class TestExcelExtractor extends TestCase {
027: public void testSimple() throws Exception {
028: String path = System.getProperty("HSSF.testdata.path");
029: FileInputStream fin = new FileInputStream(path + File.separator
030: + "Simple.xls");
031:
032: ExcelExtractor extractor = new ExcelExtractor(
033: new POIFSFileSystem(fin));
034:
035: assertEquals("Sheet1\nreplaceMe\nSheet2\nSheet3\n", extractor
036: .getText());
037:
038: // Now turn off sheet names
039: extractor.setIncludeSheetNames(false);
040: assertEquals("replaceMe\n", extractor.getText());
041: }
042:
043: public void testNumericFormula() throws Exception {
044: String path = System.getProperty("HSSF.testdata.path");
045: FileInputStream fin = new FileInputStream(path + File.separator
046: + "sumifformula.xls");
047:
048: ExcelExtractor extractor = new ExcelExtractor(
049: new POIFSFileSystem(fin));
050:
051: assertEquals("Sheet1\n" + "1000.0\t1.0\t5.0\n"
052: + "2000.0\t2.0\t\n" + "3000.0\t3.0\t\n"
053: + "4000.0\t4.0\t\n" + "5000.0\t5.0\t\n"
054: + "Sheet2\nSheet3\n", extractor.getText());
055:
056: extractor.setFormulasNotResults(true);
057:
058: assertEquals("Sheet1\n"
059: + "1000.0\t1.0\tSUMIF(A1:A5,\">4000\",B1:B5)\n"
060: + "2000.0\t2.0\t\n" + "3000.0\t3.0\t\n"
061: + "4000.0\t4.0\t\n" + "5000.0\t5.0\t\n"
062: + "Sheet2\nSheet3\n", extractor.getText());
063: }
064:
065: public void testwithContinueRecords() throws Exception {
066: String path = System.getProperty("HSSF.testdata.path");
067: FileInputStream fin = new FileInputStream(path + File.separator
068: + "StringContinueRecords.xls");
069:
070: ExcelExtractor extractor = new ExcelExtractor(
071: new POIFSFileSystem(fin));
072:
073: extractor.getText();
074:
075: // Has masses of text
076: // Until we fixed bug #41064, this would've
077: // failed by now
078: assertTrue(extractor.getText().length() > 40960);
079: }
080:
081: public void testStringConcat() throws Exception {
082: String path = System.getProperty("HSSF.testdata.path");
083: FileInputStream fin = new FileInputStream(path + File.separator
084: + "SimpleWithFormula.xls");
085:
086: ExcelExtractor extractor = new ExcelExtractor(
087: new POIFSFileSystem(fin));
088:
089: // Comes out as NaN if treated as a number
090: // And as XYZ if treated as a string
091: assertEquals(
092: "Sheet1\nreplaceme\nreplaceme\nreplacemereplaceme\nSheet2\nSheet3\n",
093: extractor.getText());
094:
095: extractor.setFormulasNotResults(true);
096:
097: assertEquals(
098: "Sheet1\nreplaceme\nreplaceme\nCONCATENATE(A1,A2)\nSheet2\nSheet3\n",
099: extractor.getText());
100: }
101:
102: public void testStringFormula() throws Exception {
103: String path = System.getProperty("HSSF.testdata.path");
104: FileInputStream fin = new FileInputStream(path + File.separator
105: + "StringFormulas.xls");
106:
107: ExcelExtractor extractor = new ExcelExtractor(
108: new POIFSFileSystem(fin));
109:
110: // Comes out as NaN if treated as a number
111: // And as XYZ if treated as a string
112: assertEquals("Sheet1\nXYZ\nSheet2\nSheet3\n", extractor
113: .getText());
114:
115: extractor.setFormulasNotResults(true);
116:
117: assertEquals("Sheet1\nUPPER(\"xyz\")\nSheet2\nSheet3\n",
118: extractor.getText());
119: }
120: }
|