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.

XSSFTestDataSamples.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.xssf;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  22. import org.apache.poi.hssf.HSSFTestDataSamples;
  23. import org.apache.poi.openxml4j.opc.OPCPackage;
  24. import org.apache.poi.ss.usermodel.Workbook;
  25. import org.apache.poi.util.TempFile;
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  27. /**
  28. * Centralises logic for finding/opening sample files in the test-data/spreadsheet folder.
  29. */
  30. public class XSSFTestDataSamples {
  31. /**
  32. * Used by {@link #writeOutAndReadBack(Workbook, String)}. If a
  33. * value is set for this in the System Properties, the xlsx file
  34. * will be written out to that directory.
  35. */
  36. public static final String TEST_OUTPUT_DIR = "poi.test.xssf.output.dir";
  37. public static File getSampleFile(String sampleFileName) {
  38. return HSSFTestDataSamples.getSampleFile(sampleFileName);
  39. }
  40. public static OPCPackage openSamplePackage(String sampleName) {
  41. try {
  42. return OPCPackage.open(HSSFTestDataSamples.openSampleFileStream(sampleName));
  43. } catch (Exception e) {
  44. throw new RuntimeException(e);
  45. }
  46. }
  47. public static XSSFWorkbook openSampleWorkbook(String sampleName) {
  48. InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName);
  49. try {
  50. return new XSSFWorkbook(is);
  51. } catch (IOException e) {
  52. throw new RuntimeException(e);
  53. }
  54. }
  55. /**
  56. * Write out workbook {@code wb} to {@link #TEST_OUTPUT_DIR}/testName.xlsx
  57. * (or create a temporary file if {@code TEST_OUTPUT_DIR} is not defined).
  58. *
  59. * @param wb the workbook to write
  60. * @param testName a fragment of the filename
  61. * @return the location where the workbook was saved
  62. * @throws IOException If writing the file fails
  63. */
  64. public static <R extends Workbook> File writeOut(R wb, String testName) throws IOException {
  65. final File file = getOutputFile(testName);
  66. writeOut(wb, file);
  67. return file;
  68. }
  69. private static <R extends Workbook> void writeOut(R wb, File file) throws IOException {
  70. try (FileOutputStream out = new FileOutputStream(file)) {
  71. wb.write(out);
  72. }
  73. }
  74. // Anticipates the location of where a workbook will be written to
  75. // Note that if TEST_OUTPUT_DIR is not set, this will create temporary files
  76. // with unique names. Subsequent calls with the same argument may return a different file.
  77. // Gets a test data sample file, deleting the file if it exists.
  78. // This is used in preparation for writing a workbook out to the returned output file.
  79. // testName is a filename fragment and should not include the extension
  80. private static File getOutputFile(String testName) throws IOException {
  81. final String testOutputDir = System.getProperty(TEST_OUTPUT_DIR);
  82. final File file;
  83. if (testOutputDir != null) {
  84. // In case user provided testName with a file extension, don't repeat the file extension a second time
  85. final String testNameWithExtension = (testName.endsWith(".xlsx") || testName.endsWith(".xlsxm")) ? testName : testName + ".xlsx";
  86. // FIXME: may want to defer to the TempFile with a persistent file creation strategy to the test output dir
  87. // This would add the random value in the middle of the filename so that test runs wouldn't overwrite files
  88. file = new File(testOutputDir, testNameWithExtension);
  89. }
  90. else {
  91. file = TempFile.createTempFile(testName, ".xlsx");
  92. }
  93. if (file.exists()) {
  94. if(!file.delete()) {
  95. throw new IOException("Could not delete file " + file);
  96. }
  97. }
  98. return file;
  99. }
  100. /**
  101. * Write out workbook {@code wb} to a memory buffer
  102. *
  103. * @param wb the workbook to write
  104. * @return the memory buffer
  105. * @throws IOException If writing the file fails
  106. */
  107. public static <R extends Workbook> UnsynchronizedByteArrayOutputStream writeOut(R wb) throws IOException {
  108. UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream(8192);
  109. wb.write(out);
  110. return out;
  111. }
  112. /**
  113. * Write out the workbook then closes the workbook.
  114. * This should be used when there is insufficient memory to have
  115. * both workbooks open.
  116. *
  117. * Make sure there are no references to any objects in the workbook
  118. * so that garbage collection may free the workbook.
  119. *
  120. * After calling this method, null the reference to {@code wb},
  121. * then call {@link #readBack(File)} or {@link #readBackAndDelete(File)} to re-read the file.
  122. *
  123. * Alternatively, use {@link #writeOutAndClose(Workbook)} to use a UnsynchronizedByteArrayOutputStream/ByteArrayInputStream
  124. * to avoid creating a temporary file. However, this may complicate the calling
  125. * code to avoid having the workbook, BAOS, and BAIS open at the same time.
  126. *
  127. * @param wb The workbook to write out, it is closed after the call.
  128. * @param testName file name to be used to write to a file. This file will be cleaned up by a call to readBack(String)
  129. * @return workbook location
  130. * @throws IllegalStateException if {@link #TEST_OUTPUT_DIR} System property is not set
  131. */
  132. public static <R extends Workbook> File writeOutAndClose(R wb, String testName) {
  133. try {
  134. File file = writeOut(wb, testName);
  135. // Do not close the workbook if there was a problem writing the workbook
  136. wb.close();
  137. return file;
  138. }
  139. catch (final IOException e) {
  140. throw new RuntimeException(e);
  141. }
  142. }
  143. /**
  144. * Write out workbook {@code wb} to a memory buffer,
  145. * then close the workbook
  146. *
  147. * @param wb the workbook to write
  148. * @return the memory buffer
  149. * @throws IllegalStateException If writing the file fails
  150. */
  151. public static <R extends Workbook> UnsynchronizedByteArrayOutputStream writeOutAndClose(R wb) throws IOException {
  152. UnsynchronizedByteArrayOutputStream out = writeOut(wb);
  153. // Do not close the workbook if there was a problem writing the workbook
  154. wb.close();
  155. return out;
  156. }
  157. /**
  158. * Read back a workbook that was written out to a file with
  159. * {@link #writeOut(Workbook, String))} or {@link #writeOutAndClose(Workbook, String)}.
  160. * Deletes the file after reading back the file.
  161. * Does not delete the file if an exception is raised.
  162. *
  163. * @param file the workbook file to read and delete
  164. * @return the read back workbook
  165. * @throws IOException If reading or deleting the file fails
  166. */
  167. public static XSSFWorkbook readBackAndDelete(File file) throws IOException {
  168. XSSFWorkbook wb = readBack(file);
  169. // do not delete the file if there's an error--might be helpful for debugging
  170. if(!file.delete()) {
  171. throw new IOException("Could not delete file " + file + " after reading");
  172. }
  173. return wb;
  174. }
  175. /**
  176. * Read back a workbook that was written out to a file with
  177. * {@link #writeOut(Workbook, String)} or {@link #writeOutAndClose(Workbook, String)}.
  178. *
  179. * @param file the workbook file to read
  180. * @return the read back workbook
  181. * @throws IOException If reading the file fails
  182. */
  183. public static XSSFWorkbook readBack(File file) throws IOException {
  184. try (InputStream in = new FileInputStream(file)) {
  185. return new XSSFWorkbook(in);
  186. }
  187. }
  188. /**
  189. * Read back a workbook that was written out to a memory buffer with
  190. * {@link #writeOut(Workbook)} or {@link #writeOutAndClose(Workbook)}.
  191. *
  192. * @param out the output stream to read back from
  193. * @return the read back workbook
  194. * @throws IOException If reading the file fails
  195. */
  196. public static XSSFWorkbook readBack(UnsynchronizedByteArrayOutputStream out) throws IOException {
  197. try (InputStream is = out.toInputStream()) {
  198. out.close();
  199. return new XSSFWorkbook(is);
  200. }
  201. }
  202. /**
  203. * Write out and read back using a memory buffer to avoid disk I/O.
  204. * If there is not enough memory to have two workbooks open at the same time,
  205. * consider using:
  206. *
  207. * Workbook wb = new XSSFWorkbook();
  208. * String testName = "example";
  209. *
  210. * {@code
  211. * File file = writeOutAndClose(wb, testName);
  212. * // clear all references that would prevent the workbook from getting garbage collected
  213. * wb = null;
  214. * Workbook wbBack = readBackAndDelete(file);
  215. * }
  216. *
  217. * @param wb the workbook to write out
  218. * @return the read back workbook
  219. */
  220. public static <R extends Workbook> R writeOutAndReadBack(R wb) {
  221. Workbook result;
  222. try {
  223. result = readBack(writeOut(wb));
  224. } catch (IOException e) {
  225. throw new RuntimeException(e);
  226. }
  227. @SuppressWarnings("unchecked")
  228. R r = (R) result;
  229. return r;
  230. }
  231. /**
  232. * Write out, close, and read back the workbook using a memory buffer to avoid disk I/O.
  233. *
  234. * @param wb the workbook to write out and close
  235. * @return the read back workbook
  236. */
  237. public static <R extends Workbook> R writeOutCloseAndReadBack(R wb) {
  238. Workbook result;
  239. try {
  240. result = readBack(writeOutAndClose(wb));
  241. } catch (IOException e) {
  242. throw new RuntimeException(e);
  243. }
  244. @SuppressWarnings("unchecked")
  245. R r = (R) result;
  246. return r;
  247. }
  248. /**
  249. * Writes the Workbook either into a file or into a byte array, depending on presence of
  250. * the system property {@value #TEST_OUTPUT_DIR}, and reads it in a new instance of the Workbook back.
  251. * If TEST_OUTPUT_DIR is set, the file will NOT be deleted at the end of this function.
  252. * @param wb workbook to write
  253. * @param testName file name to be used if writing into a file. The old file with the same name will be overridden.
  254. * @return new instance read from the stream written by the wb parameter.
  255. */
  256. public static <R extends Workbook> R writeOutAndReadBack(R wb, String testName) {
  257. if (System.getProperty(TEST_OUTPUT_DIR) == null) {
  258. return writeOutAndReadBack(wb);
  259. } else {
  260. try {
  261. Workbook result = readBack(writeOut(wb, testName));
  262. @SuppressWarnings("unchecked")
  263. R r = (R) result;
  264. return r;
  265. } catch (IOException e) {
  266. throw new RuntimeException(e);
  267. }
  268. }
  269. }
  270. }