You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

POIDataSamples.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  22. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  23. import org.apache.poi.util.IOUtils;
  24. /**
  25. * Centralises logic for finding/opening sample files
  26. */
  27. public final class POIDataSamples {
  28. /**
  29. * Name of the system property that defined path to the test data.
  30. */
  31. public static final String TEST_PROPERTY = "POI.testdata.path";
  32. private static POIDataSamples _instSlideshow;
  33. private static POIDataSamples _instSpreadsheet;
  34. private static POIDataSamples _instDocument;
  35. private static POIDataSamples _instDiagram;
  36. private static POIDataSamples _instOpenxml4j;
  37. private static POIDataSamples _instPOIFS;
  38. private static POIDataSamples _instDDF;
  39. private static POIDataSamples _instHMEF;
  40. private static POIDataSamples _instHPSF;
  41. private static POIDataSamples _instHPBF;
  42. private static POIDataSamples _instHSMF;
  43. private static POIDataSamples _instXmlDSign;
  44. private File _resolvedDataDir;
  45. /** {@code true} if standard system property is not set,
  46. * but the data is available on the test runtime classpath */
  47. private boolean _sampleDataIsAvaliableOnClassPath;
  48. private final String _moduleDir;
  49. /**
  50. *
  51. * @param moduleDir the name of the directory containing the test files
  52. */
  53. private POIDataSamples(String moduleDir){
  54. _moduleDir = moduleDir;
  55. initialise();
  56. }
  57. public static POIDataSamples getSpreadSheetInstance(){
  58. if(_instSpreadsheet == null) _instSpreadsheet = new POIDataSamples("spreadsheet");
  59. return _instSpreadsheet;
  60. }
  61. public static POIDataSamples getDocumentInstance(){
  62. if(_instDocument == null) _instDocument = new POIDataSamples("document");
  63. return _instDocument;
  64. }
  65. public static POIDataSamples getSlideShowInstance(){
  66. if(_instSlideshow == null) _instSlideshow = new POIDataSamples("slideshow");
  67. return _instSlideshow;
  68. }
  69. public static POIDataSamples getDiagramInstance(){
  70. if(_instOpenxml4j == null) _instOpenxml4j = new POIDataSamples("diagram");
  71. return _instOpenxml4j;
  72. }
  73. public static POIDataSamples getOpenXML4JInstance(){
  74. if(_instDiagram == null) _instDiagram = new POIDataSamples("openxml4j");
  75. return _instDiagram;
  76. }
  77. public static POIDataSamples getPOIFSInstance(){
  78. if(_instPOIFS == null) _instPOIFS = new POIDataSamples("poifs");
  79. return _instPOIFS;
  80. }
  81. public static POIDataSamples getDDFInstance(){
  82. if(_instDDF == null) _instDDF = new POIDataSamples("ddf");
  83. return _instDDF;
  84. }
  85. public static POIDataSamples getHPSFInstance(){
  86. if(_instHPSF == null) _instHPSF = new POIDataSamples("hpsf");
  87. return _instHPSF;
  88. }
  89. public static POIDataSamples getPublisherInstance(){
  90. if(_instHPBF == null) _instHPBF = new POIDataSamples("publisher");
  91. return _instHPBF;
  92. }
  93. public static POIDataSamples getHMEFInstance(){
  94. if(_instHMEF == null) _instHMEF = new POIDataSamples("hmef");
  95. return _instHMEF;
  96. }
  97. public static POIDataSamples getHSMFInstance(){
  98. if(_instHSMF == null) _instHSMF = new POIDataSamples("hsmf");
  99. return _instHSMF;
  100. }
  101. public static POIDataSamples getXmlDSignInstance() {
  102. if(_instXmlDSign == null) _instXmlDSign = new POIDataSamples("xmldsign");
  103. return _instXmlDSign;
  104. }
  105. /**
  106. * Opens a sample file from the test data directory
  107. *
  108. * @param sampleFileName the file to open
  109. * @return an open {@code InputStream} for the specified sample file
  110. */
  111. public InputStream openResourceAsStream(String sampleFileName) {
  112. if (_sampleDataIsAvaliableOnClassPath) {
  113. InputStream result = sampleFileName == null ? null :
  114. openClasspathResource(sampleFileName);
  115. if(result == null) {
  116. throw new RuntimeException("specified test sample file '" + sampleFileName
  117. + "' not found on the classpath");
  118. }
  119. // wrap to avoid temp warning method about auto-closing input stream
  120. return new NonSeekableInputStream(result);
  121. }
  122. if (_resolvedDataDir == null) {
  123. throw new RuntimeException("Must set system property '"
  124. + TEST_PROPERTY
  125. + "' properly before running tests");
  126. }
  127. File f = getFile(sampleFileName);
  128. try {
  129. return new FileInputStream(f);
  130. } catch (FileNotFoundException e) {
  131. throw new RuntimeException(e);
  132. }
  133. }
  134. /**
  135. *
  136. * @param sampleFileName the name of the test file
  137. * @return Verifies that the file with the given name exists in the test-data directory
  138. * @throws RuntimeException if the file was not found
  139. */
  140. public File getFile(String sampleFileName) {
  141. File f = new File(_resolvedDataDir, sampleFileName);
  142. if (!f.exists()) {
  143. throw new RuntimeException("Sample file '" + sampleFileName
  144. + "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'");
  145. }
  146. try {
  147. if(sampleFileName.length() > 0) {
  148. String fn = sampleFileName;
  149. if(fn.indexOf('/') > 0) {
  150. fn = fn.substring(fn.indexOf('/')+1);
  151. }
  152. if(!fn.equals(f.getCanonicalFile().getName())){
  153. throw new RuntimeException("File name is case-sensitive: requested '" + fn
  154. + "' but actual file is '" + f.getCanonicalFile().getName() + "'");
  155. }
  156. }
  157. } catch (IOException e){
  158. throw new RuntimeException(e);
  159. }
  160. return f;
  161. }
  162. private void initialise() {
  163. String dataDirName = System.getProperty(TEST_PROPERTY);
  164. if (dataDirName == null) {
  165. // check to see if we can just get the resources from the classpath
  166. InputStream is = openClasspathResource("");
  167. if (is != null) {
  168. try {
  169. is.close(); // be nice
  170. } catch (IOException e) {
  171. throw new RuntimeException(e);
  172. }
  173. _sampleDataIsAvaliableOnClassPath = true;
  174. return;
  175. }
  176. if (new File("test-data").exists()) {
  177. dataDirName = "test-data";
  178. } else if (new File("../test-data").exists()) {
  179. dataDirName = "../test-data";
  180. } else {
  181. throw new RuntimeException("Must set system property '" +
  182. TEST_PROPERTY + "' before running tests");
  183. }
  184. }
  185. File dataDir = new File(dataDirName, _moduleDir);
  186. if (!dataDir.exists()) {
  187. throw new RuntimeException("Data dir '" + dataDir + "' does not exist");
  188. }
  189. // convert to canonical file, to make any subsequent error messages
  190. // clearer.
  191. try {
  192. _resolvedDataDir = dataDir.getCanonicalFile();
  193. } catch (IOException e) {
  194. throw new RuntimeException(e);
  195. }
  196. }
  197. /**
  198. * Opens a test sample file from the 'data' sub-package of this class's package.
  199. *
  200. * @param sampleFileName the file to open
  201. * @return {@code null} if the sample file is not deployed on the classpath.
  202. */
  203. private InputStream openClasspathResource(String sampleFileName) {
  204. return getClass().getResourceAsStream("/" + _moduleDir + "/" + sampleFileName);
  205. }
  206. private static final class NonSeekableInputStream extends InputStream {
  207. private final InputStream _is;
  208. public NonSeekableInputStream(InputStream is) {
  209. _is = is;
  210. }
  211. @Override
  212. public int read() throws IOException {
  213. return _is.read();
  214. }
  215. @Override
  216. public int read(byte[] b, int off, int len) throws IOException {
  217. return _is.read(b, off, len);
  218. }
  219. @Override
  220. public boolean markSupported() {
  221. return false;
  222. }
  223. @Override
  224. public void close() throws IOException {
  225. _is.close();
  226. }
  227. }
  228. /**
  229. * @param fileName the file to open
  230. * @return byte array of sample file content from file found in standard test-data directory
  231. */
  232. public byte[] readFile(String fileName) {
  233. try (InputStream fis = openResourceAsStream(fileName);
  234. UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  235. IOUtils.copy(fis, bos);
  236. return bos.toByteArray();
  237. } catch (IOException e) {
  238. throw new RuntimeException(e);
  239. }
  240. }
  241. public static POIFSFileSystem writeOutAndReadBack(POIFSFileSystem original) throws IOException {
  242. try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
  243. original.writeFilesystem(baos);
  244. return new POIFSFileSystem(baos.toInputStream());
  245. }
  246. }
  247. }