aboutsummaryrefslogtreecommitdiffstats
path: root/src/integrationtest/org/apache/poi/stress/SpreadsheetHandler.java
blob: 123bfa7450711e6d4d9c4246a9df227b63c72c12 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ====================================================================
   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 static org.junit.Assert.assertNotNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.util.RecordFormatException;

public abstract class SpreadsheetHandler extends AbstractFileHandler {
	public void handleWorkbook(Workbook wb) throws IOException {
		// try to access some of the content
		readContent(wb);
		
		// write out the file
		writeToArray(wb);
		
		// access some more content (we had cases where writing corrupts the data in memory)
		readContent(wb);

		// write once more
		ByteArrayOutputStream out = writeToArray(wb);

		// read in the written file
		Workbook read;
		try {
			read = WorkbookFactory.create(new ByteArrayInputStream(out.toByteArray()));
		} catch (InvalidFormatException e) {
			throw new IllegalStateException(e);
		}
		assertNotNull(read);
		
		readContent(read);
		
		modifyContent(read);

		read.close();
	}

	private ByteArrayOutputStream writeToArray(Workbook wb) throws IOException {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		try {
			wb.write(stream);
		} finally {
			stream.close();
		}
		
		return stream;
	}

	private void readContent(Workbook wb) {
	    for(int i = 0;i < wb.getNumberOfSheets();i++) {
			Sheet sheet = wb.getSheetAt(i);
			assertNotNull(wb.getSheet(sheet.getSheetName()));
			sheet.groupColumn((short) 4, (short) 5);
			sheet.setColumnGroupCollapsed(4, true);
			sheet.setColumnGroupCollapsed(4, false);

			// don't do this for very large sheets as it will take a long time
			if(sheet.getPhysicalNumberOfRows() > 1000) {
			    continue;
			}
			
			for(Row row : sheet) {
			    for(Cell cell : row) {
			        assertNotNull(cell.toString());
			    }
			}
		}
	}
	
	private void modifyContent(Workbook wb) {
		/* a number of file fail because of various things: udf, unimplemented functions, ...
		we would need quite a list of excludes and the large regression tests would probably
		take a lot longer to run...
		try {
			// try to re-compute all formulas to find cases where parsing fails
			wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
		} catch (RuntimeException e) {
			// only allow a specific exception which indicates that an external
			// reference was not found
			if(!e.getMessage().contains("Could not resolve external workbook name")) {
				throw e;
			}

		}*/

	    for (int i=wb.getNumberOfSheets()-1; i>=0; i--) {
	        try {
	            wb.cloneSheet(i);
	        } catch (RecordFormatException e) {
	            if (e.getCause() instanceof CloneNotSupportedException) {
	                // ignore me
	                continue;
	            }
	            throw e;
	        } catch (RuntimeException e) {
	            if ("Could not find 'internal references' EXTERNALBOOK".equals(e.getMessage()) ||
	                    "CountryRecord not found".equals(e.getMessage()) ||
	                    "Cannot add more than 65535 shapes".equals(e.getMessage()) ) {
	                // ignore these here for now
	                continue;
                }
	            throw e;
	        }
	    }
	}
}