diff options
author | Yegor Kozlov <yegor@apache.org> | 2009-08-19 18:51:44 +0000 |
---|---|---|
committer | Yegor Kozlov <yegor@apache.org> | 2009-08-19 18:51:44 +0000 |
commit | 8b283b14b10c01f6b7247555ac0098258d7f0164 (patch) | |
tree | c5186589a87b7e5c49b3394a6d750a8c09e1b686 /src/testcases/org/apache/poi/hssf | |
parent | bb7b42c1265efe711303c4f38b25bc0a1eb92247 (diff) | |
download | poi-8b283b14b10c01f6b7247555ac0098258d7f0164.tar.gz poi-8b283b14b10c01f6b7247555ac0098258d7f0164.zip |
Centralize logic for finding/opening sample files
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@805928 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf')
-rw-r--r-- | src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java | 161 |
1 files changed, 17 insertions, 144 deletions
diff --git a/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java b/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java index 113329265f..4d0f3a72c5 100644 --- a/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java +++ b/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java @@ -26,139 +26,35 @@ import java.io.IOException; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.POIDataSamples; /** * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder. * * @author Josh Micich */ -public final class HSSFTestDataSamples { +public final class HSSFTestDataSamples extends POIDataSamples { - private static final String TEST_DATA_DIR_SYS_PROPERTY_NAME = "HSSF.testdata.path"; + private static final HSSFTestDataSamples _inst = new HSSFTestDataSamples("HSSF.testdata.path", "SampleSS.xls"); - private static boolean _isInitialised; - private static File _resolvedDataDir; - /** <code>true</code> if standard system propery is not set, - * but the data is available on the test runtime classpath */ - private static boolean _sampleDataIsAvaliableOnClassPath; + private HSSFTestDataSamples(String dir, String classPathTestFile){ + super(dir, classPathTestFile); + } - /** - * Opens a sample file from the standard HSSF test data directory - * - * @return an open <tt>InputStream</tt> for the specified sample file - */ - public static InputStream openSampleFileStream(String sampleFileName) { - - if(!_isInitialised) { - try { - initialise(); - } finally { - _isInitialised = true; - } - } - if (_sampleDataIsAvaliableOnClassPath) { - InputStream result = openClasspathResource(sampleFileName); - if(result == null) { - throw new RuntimeException("specified test sample file '" + sampleFileName - + "' not found on the classpath"); - } -// System.out.println("opening cp: " + sampleFileName); - // wrap to avoid temp warning method about auto-closing input stream - return new NonSeekableInputStream(result); - } - if (_resolvedDataDir == null) { - throw new RuntimeException("Must set system property '" - + TEST_DATA_DIR_SYS_PROPERTY_NAME - + "' properly before running tests"); - } - - File f = new File(_resolvedDataDir, sampleFileName); - if (!f.exists()) { - throw new RuntimeException("Sample file '" + sampleFileName - + "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'"); - } - try { - if(!sampleFileName.equals(f.getCanonicalFile().getName())){ - throw new RuntimeException("File name is case-sensitive: requested '" + sampleFileName - + "' but actual file is '" + f.getCanonicalFile().getName() + "'"); - } - } catch (IOException e){ - throw new RuntimeException(e); - } - - try { - return new FileInputStream(f); - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } - } - - private static void initialise() { - String dataDirName = System.getProperty(TEST_DATA_DIR_SYS_PROPERTY_NAME); - if (dataDirName == null) { - // check to see if we can just get the resources from the classpath - InputStream is = openClasspathResource("SampleSS.xls"); - if (is != null) { - try { - is.close(); // be nice - } catch (IOException e) { - throw new RuntimeException(e); - } - _sampleDataIsAvaliableOnClassPath = true; - return; - } - - throw new RuntimeException("Must set system property '" - + TEST_DATA_DIR_SYS_PROPERTY_NAME + "' before running tests"); - } - File dataDir = new File(dataDirName); - if (!dataDir.exists()) { - throw new RuntimeException("Data dir '" + dataDirName - + "' specified by system property '" + TEST_DATA_DIR_SYS_PROPERTY_NAME - + "' does not exist"); - } - // convert to canonical file, to make any subsequent error messages - // clearer. - try { - _resolvedDataDir = dataDir.getCanonicalFile(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - /** - * Opens a test sample file from the 'data' sub-package of this class's package. - * @return <code>null</code> if the sample file is not deployed on the classpath. - */ - private static InputStream openClasspathResource(String sampleFileName) { - return HSSFTestDataSamples.class.getResourceAsStream("data/" + sampleFileName); - } + public static POIDataSamples getInstance(){ + return _inst; + } - private static final class NonSeekableInputStream extends InputStream { - - private final InputStream _is; - - public NonSeekableInputStream(InputStream is) { - _is = is; - } + public static InputStream openSampleFileStream(String sampleFileName) { + return _inst.openResourceAsStream(sampleFileName); + } + public static byte[] getTestDataFileContent(String fileName) { + return _inst.readFile(fileName); + } - public int read() throws IOException { - return _is.read(); - } - public int read(byte[] b, int off, int len) throws IOException { - return _is.read(b, off, len); - } - public boolean markSupported() { - return false; - } - public void close() throws IOException { - _is.close(); - } - } - - public static HSSFWorkbook openSampleWorkbook(String sampleFileName) { + public static HSSFWorkbook openSampleWorkbook(String sampleFileName) { try { - return new HSSFWorkbook(openSampleFileStream(sampleFileName)); + return new HSSFWorkbook(_inst.openResourceAsStream(sampleFileName)); } catch (IOException e) { throw new RuntimeException(e); } @@ -180,27 +76,4 @@ public final class HSSFTestDataSamples { } } - /** - * @return byte array of sample file content from file found in standard hssf test data dir - */ - public static byte[] getTestDataFileContent(String fileName) { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - - try { - InputStream fis = HSSFTestDataSamples.openSampleFileStream(fileName); - - byte[] buf = new byte[512]; - while (true) { - int bytesRead = fis.read(buf); - if (bytesRead < 1) { - break; - } - bos.write(buf, 0, bytesRead); - } - fis.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return bos.toByteArray(); - } } |