Update HSLFSlideShow and HSSFWorkbook to take advantage of POIFS updates, and allow...
[poi.git] / src / testcases / org / apache / poi / hssf / HSSFTestDataSamples.java
blob703551f88e7e0e3ab2ab07d7960eabab640e5cbe
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
9 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================== */
18 package org.apache.poi.hssf;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
28 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
30 /**
31 * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder.
33 * @author Josh Micich
35 public final class HSSFTestDataSamples {
37 private static final String TEST_DATA_DIR_SYS_PROPERTY_NAME = "HSSF.testdata.path";
39 private static boolean _isInitialised;
40 private static File _resolvedDataDir;
41 /** <code>true</code> if standard system propery is not set,
42 * but the data is available on the test runtime classpath */
43 private static boolean _sampleDataIsAvaliableOnClassPath;
45 /**
46 * Opens a sample file from the standard HSSF test data directory
48 * @return an open <tt>InputStream</tt> for the specified sample file
50 public static InputStream openSampleFileStream(String sampleFileName) {
52 if(!_isInitialised) {
53 try {
54 initialise();
55 } finally {
56 _isInitialised = true;
59 if (_sampleDataIsAvaliableOnClassPath) {
60 InputStream result = openClasspathResource(sampleFileName);
61 if(result == null) {
62 throw new RuntimeException("specified test sample file '" + sampleFileName
63 + "' not found on the classpath");
65 // System.out.println("opening cp: " + sampleFileName);
66 // wrap to avoid temp warning method about auto-closing input stream
67 return new NonSeekableInputStream(result);
69 if (_resolvedDataDir == null) {
70 throw new RuntimeException("Must set system property '"
71 + TEST_DATA_DIR_SYS_PROPERTY_NAME
72 + "' properly before running tests");
75 File f = new File(_resolvedDataDir, sampleFileName);
76 if (!f.exists()) {
77 throw new RuntimeException("Sample file '" + sampleFileName
78 + "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'");
80 // System.out.println("opening " + f.getAbsolutePath());
81 try {
82 return new FileInputStream(f);
83 } catch (FileNotFoundException e) {
84 throw new RuntimeException(e);
88 private static void initialise() {
89 String dataDirName = System.getProperty(TEST_DATA_DIR_SYS_PROPERTY_NAME);
90 if (dataDirName == null) {
91 // check to see if we can just get the resources from the classpath
92 InputStream is = openClasspathResource("SampleSS.xls");
93 if(is != null) {
94 try {
95 is.close(); // be nice
96 } catch (IOException e) {
97 throw new RuntimeException(e);
99 _sampleDataIsAvaliableOnClassPath = true;
100 return;
104 throw new RuntimeException("Must set system property '"
105 + TEST_DATA_DIR_SYS_PROPERTY_NAME
106 + "' before running tests");
108 File dataDir = new File(dataDirName);
109 if (!dataDir.exists()) {
110 throw new RuntimeException("Data dir '" + dataDirName
111 + "' specified by system property '"
112 + TEST_DATA_DIR_SYS_PROPERTY_NAME + "' does not exist");
114 // convert to canonical file, to make any subsequent error messages clearer.
115 try {
116 _resolvedDataDir = dataDir.getCanonicalFile();
117 } catch (IOException e) {
118 throw new RuntimeException(e);
123 * Opens a test sample file from the 'data' sub-package of this class's package.
124 * @return <code>null</code> if the sample file is not deployed on the classpath.
126 private static InputStream openClasspathResource(String sampleFileName) {
127 return HSSFTestDataSamples.class.getResourceAsStream("data/" + sampleFileName);
130 private static final class NonSeekableInputStream extends InputStream {
132 private final InputStream _is;
134 public NonSeekableInputStream(InputStream is) {
135 _is = is;
138 public int read() throws IOException {
139 return _is.read();
141 public int read(byte[] b, int off, int len) throws IOException {
142 return _is.read(b, off, len);
144 public boolean markSupported() {
145 return false;
147 public void close() throws IOException {
148 _is.close();
152 public static HSSFWorkbook openSampleWorkbook(String sampleFileName) {
153 try {
154 return new HSSFWorkbook(openSampleFileStream(sampleFileName));
155 } catch (IOException e) {
156 throw new RuntimeException(e);
160 * Writes a spreadsheet to a <tt>ByteArrayOutputStream</tt> and reads it back
161 * from a <tt>ByteArrayInputStream</tt>.<p/>
162 * Useful for verifying that the serialisation round trip
164 public static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
166 try {
167 ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
168 original.write(baos);
169 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
170 return new HSSFWorkbook(bais);
171 } catch (IOException e) {
172 throw new RuntimeException(e);