aboutsummaryrefslogtreecommitdiffstats
path: root/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java
blob: fcea5805863a0e778364f20059ef8e20aad4ea17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package org.apache.poi.stress;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.OldExcelFormatException;
import org.apache.poi.hssf.dev.BiffViewer;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.RecordFormatException;
import org.junit.Test;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertFalse;

public class HSSFFileHandler extends SpreadsheetHandler {
	private final POIFSFileHandler delegate = new POIFSFileHandler();
	@Override
    public void handleFile(InputStream stream) throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook(stream);
		handleWorkbook(wb);
		
		// TODO: some documents fail currently...
        //HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
        //evaluator.evaluateAll();
        
		delegate.handlePOIDocument(wb);

		// also try to see if some of the Records behave incorrectly
		// TODO: still fails on some records... RecordsStresser.handleWorkbook(wb);
	}

	private static final Set<String> EXPECTED_ADDITIONAL_FAILURES = new HashSet<String>();
	static {
		// encrypted
		EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/35897-type4.xls");
		EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/xor-encryption-abc.xls");
		EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/password.xls");
		// broken files
		EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/43493.xls");
		// TODO: ok to ignore?
		EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/50833.xls");
		EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/51832.xls");
		EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/XRefCalc.xls");
	}

	@Override
	public void handleAdditional(File file) throws Exception {
		// redirect stdout as the examples often write lots of text
		PrintStream oldOut = System.out;
		try {
			System.setOut(new PrintStream(new OutputStream() {
				@Override
				public void write(int b) throws IOException {
				}
			}));

			BiffViewer.main(new String[]{file.getAbsolutePath()});

			assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!",
					EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName()));
		} catch (OldExcelFormatException e) {
			// old excel formats are not supported here
		} catch (EncryptedDocumentException e) {
			if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
				throw e;
			}
		} catch (RecordFormatException e) {
			if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
				throw e;
			}
		} catch (RuntimeException e) {
			if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
				throw e;
			}
		} finally {
			System.setOut(oldOut);
		}
	}

	// a test-case to test this locally without executing the full TestAllFiles
	@Test
	public void test() throws Exception {
		InputStream stream = new FileInputStream("test-data/spreadsheet/49219.xls");
		try {
			handleFile(stream);
		} finally {
			stream.close();
		}
	}

	// a test-case to test this locally without executing the full TestAllFiles
    @Test
    public void testExtractor() throws Exception {
        handleExtracting(new File("test-data/spreadsheet/BOOK_in_capitals.xls"));
    }
}