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
;
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
;
31 * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder.
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
;
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
) {
56 _isInitialised
= true;
59 if (_sampleDataIsAvaliableOnClassPath
) {
60 InputStream result
= openClasspathResource(sampleFileName
);
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
);
77 throw new RuntimeException("Sample file '" + sampleFileName
78 + "' not found in data dir '" + _resolvedDataDir
.getAbsolutePath() + "'");
80 // System.out.println("opening " + f.getAbsolutePath());
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");
95 is
.close(); // be nice
96 } catch (IOException e
) {
97 throw new RuntimeException(e
);
99 _sampleDataIsAvaliableOnClassPath
= true;
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.
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
) {
138 public int read() throws IOException
{
141 public int read(byte[] b
, int off
, int len
) throws IOException
{
142 return _is
.read(b
, off
, len
);
144 public boolean markSupported() {
147 public void close() throws IOException
{
152 public static HSSFWorkbook
openSampleWorkbook(String sampleFileName
) {
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
) {
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
);