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.

TestAllFiles.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 static org.junit.Assert.assertFalse;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.io.BufferedInputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.InputStream;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Locale;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import org.apache.poi.stress.AbstractFileHandler;
  32. import org.apache.poi.stress.FileHandler;
  33. import org.apache.poi.stress.HDGFFileHandler;
  34. import org.apache.poi.stress.HMEFFileHandler;
  35. import org.apache.poi.stress.HPBFFileHandler;
  36. import org.apache.poi.stress.HPSFFileHandler;
  37. import org.apache.poi.stress.HSLFFileHandler;
  38. import org.apache.poi.stress.HSMFFileHandler;
  39. import org.apache.poi.stress.HSSFFileHandler;
  40. import org.apache.poi.stress.HWPFFileHandler;
  41. import org.apache.poi.stress.OPCFileHandler;
  42. import org.apache.poi.stress.POIFSFileHandler;
  43. import org.apache.poi.stress.XDGFFileHandler;
  44. import org.apache.poi.stress.XSLFFileHandler;
  45. import org.apache.poi.stress.XSSFBFileHandler;
  46. import org.apache.poi.stress.XSSFFileHandler;
  47. import org.apache.poi.stress.XWPFFileHandler;
  48. import org.apache.tools.ant.DirectoryScanner;
  49. import org.junit.Test;
  50. import org.junit.runner.RunWith;
  51. import org.junit.runners.Parameterized;
  52. import org.junit.runners.Parameterized.Parameter;
  53. import org.junit.runners.Parameterized.Parameters;
  54. /**
  55. * This is an integration test which performs various actions on all stored test-files and tries
  56. * to reveal problems which are introduced, but not covered (yet) by unit tests.
  57. *
  58. * This test looks for any file under the test-data directory and tries to do some useful
  59. * processing with it based on it's type.
  60. *
  61. * The test is implemented as a junit {@link Parameterized} test, which leads
  62. * to one test-method call for each file (currently around 950 files are handled).
  63. *
  64. * There is a a mapping of extension to implementations of the interface
  65. * {@link FileHandler} which defines how the file is loaded and which actions are
  66. * tried with the file.
  67. *
  68. * The test can be expanded by adding more actions to the FileHandlers, this automatically
  69. * applies the action to any such file in our test-data repository.
  70. *
  71. * There is also a list of files that should actually fail.
  72. *
  73. * Note: It is also a test-failure if a file that is expected to fail now actually works,
  74. * i.e. if a bug was fixed in POI itself, the file should be removed from the expected-failures
  75. * here as well! This is to ensure that files that should not work really do not work, e.g.
  76. * that we do not remove expected sanity checks.
  77. */
  78. @RunWith(Parameterized.class)
  79. public class TestAllFiles {
  80. private static final File ROOT_DIR = new File("test-data");
  81. static final String[] SCAN_EXCLUDES = new String[] { "**/.svn/**", "lost+found" };
  82. // map file extensions to the actual mappers
  83. static final Map<String, FileHandler> HANDLERS = new HashMap<String, FileHandler>();
  84. static {
  85. // Excel
  86. HANDLERS.put(".xls", new HSSFFileHandler());
  87. HANDLERS.put(".xlsx", new XSSFFileHandler());
  88. HANDLERS.put(".xlsm", new XSSFFileHandler());
  89. HANDLERS.put(".xltx", new XSSFFileHandler());
  90. HANDLERS.put(".xlsb", new XSSFBFileHandler());
  91. // Word
  92. HANDLERS.put(".doc", new HWPFFileHandler());
  93. HANDLERS.put(".docx", new XWPFFileHandler());
  94. HANDLERS.put(".dotx", new XWPFFileHandler());
  95. HANDLERS.put(".docm", new XWPFFileHandler());
  96. // OpenXML4J files
  97. HANDLERS.put(".ooxml", new OPCFileHandler()); // OPCPackage
  98. HANDLERS.put(".zip", new OPCFileHandler()); // OPCPackage
  99. // Powerpoint
  100. HANDLERS.put(".ppt", new HSLFFileHandler());
  101. HANDLERS.put(".pptx", new XSLFFileHandler());
  102. HANDLERS.put(".pptm", new XSLFFileHandler());
  103. HANDLERS.put(".ppsm", new XSLFFileHandler());
  104. HANDLERS.put(".ppsx", new XSLFFileHandler());
  105. HANDLERS.put(".thmx", new XSLFFileHandler());
  106. HANDLERS.put(".potx", new XSLFFileHandler());
  107. // Outlook
  108. HANDLERS.put(".msg", new HSMFFileHandler());
  109. // Publisher
  110. HANDLERS.put(".pub", new HPBFFileHandler());
  111. // Visio - binary
  112. HANDLERS.put(".vsd", new HDGFFileHandler());
  113. // Visio - ooxml
  114. HANDLERS.put(".vsdm", new XDGFFileHandler());
  115. HANDLERS.put(".vsdx", new XDGFFileHandler());
  116. HANDLERS.put(".vssm", new XDGFFileHandler());
  117. HANDLERS.put(".vssx", new XDGFFileHandler());
  118. HANDLERS.put(".vstm", new XDGFFileHandler());
  119. HANDLERS.put(".vstx", new XDGFFileHandler());
  120. // Visio - not handled yet
  121. HANDLERS.put(".vst", new NullFileHandler());
  122. HANDLERS.put(".vss", new NullFileHandler());
  123. // POIFS
  124. HANDLERS.put(".ole2", new POIFSFileHandler());
  125. // Microsoft Admin Template?
  126. HANDLERS.put(".adm", new HPSFFileHandler());
  127. // Microsoft TNEF
  128. HANDLERS.put(".dat", new HMEFFileHandler());
  129. // TODO: are these readable by some of the formats?
  130. HANDLERS.put(".shw", new NullFileHandler());
  131. HANDLERS.put(".zvi", new NullFileHandler());
  132. HANDLERS.put(".mpp", new NullFileHandler());
  133. HANDLERS.put(".qwp", new NullFileHandler());
  134. HANDLERS.put(".wps", new NullFileHandler());
  135. HANDLERS.put(".bin", new NullFileHandler());
  136. HANDLERS.put(".xps", new NullFileHandler());
  137. HANDLERS.put(".sldprt", new NullFileHandler());
  138. HANDLERS.put(".mdb", new NullFileHandler());
  139. HANDLERS.put(".vml", new NullFileHandler());
  140. // ignore some file types, images, other formats, ...
  141. HANDLERS.put(".txt", new NullFileHandler());
  142. HANDLERS.put(".pdf", new NullFileHandler());
  143. HANDLERS.put(".rtf", new NullFileHandler());
  144. HANDLERS.put(".gif", new NullFileHandler());
  145. HANDLERS.put(".html", new NullFileHandler());
  146. HANDLERS.put(".png", new NullFileHandler());
  147. HANDLERS.put(".wmf", new NullFileHandler());
  148. HANDLERS.put(".emf", new NullFileHandler());
  149. HANDLERS.put(".dib", new NullFileHandler());
  150. HANDLERS.put(".svg", new NullFileHandler());
  151. HANDLERS.put(".pict", new NullFileHandler());
  152. HANDLERS.put(".jpg", new NullFileHandler());
  153. HANDLERS.put(".jpeg", new NullFileHandler());
  154. HANDLERS.put(".tif", new NullFileHandler());
  155. HANDLERS.put(".tiff", new NullFileHandler());
  156. HANDLERS.put(".wav", new NullFileHandler());
  157. HANDLERS.put(".pfx", new NullFileHandler());
  158. HANDLERS.put(".xml", new NullFileHandler());
  159. HANDLERS.put(".csv", new NullFileHandler());
  160. HANDLERS.put(".ods", new NullFileHandler());
  161. // VBA source files
  162. HANDLERS.put(".vba", new NullFileHandler());
  163. HANDLERS.put(".bas", new NullFileHandler());
  164. HANDLERS.put(".frm", new NullFileHandler());
  165. HANDLERS.put(".frx", new NullFileHandler()); //binary
  166. HANDLERS.put(".cls", new NullFileHandler());
  167. // map some files without extension
  168. HANDLERS.put("spreadsheet/BigSSTRecord", new NullFileHandler());
  169. HANDLERS.put("spreadsheet/BigSSTRecord2", new NullFileHandler());
  170. HANDLERS.put("spreadsheet/BigSSTRecord2CR1", new NullFileHandler());
  171. HANDLERS.put("spreadsheet/BigSSTRecord2CR2", new NullFileHandler());
  172. HANDLERS.put("spreadsheet/BigSSTRecord2CR3", new NullFileHandler());
  173. HANDLERS.put("spreadsheet/BigSSTRecord2CR4", new NullFileHandler());
  174. HANDLERS.put("spreadsheet/BigSSTRecord2CR5", new NullFileHandler());
  175. HANDLERS.put("spreadsheet/BigSSTRecord2CR6", new NullFileHandler());
  176. HANDLERS.put("spreadsheet/BigSSTRecord2CR7", new NullFileHandler());
  177. HANDLERS.put("spreadsheet/BigSSTRecordCR", new NullFileHandler());
  178. HANDLERS.put("spreadsheet/test_properties1", new NullFileHandler());
  179. }
  180. private static Set<String> unmodifiableHashSet(String... a) {
  181. return Collections.unmodifiableSet(hashSet(a));
  182. }
  183. private static Set<String> hashSet(String... a) {
  184. return new HashSet<String>(Arrays.asList(a));
  185. }
  186. // Old Word Documents where we can at least extract some text
  187. private static final Set<String> OLD_FILES = unmodifiableHashSet(
  188. "document/Bug49933.doc",
  189. "document/Bug51944.doc",
  190. "document/Word6.doc",
  191. "document/Word6_sections.doc",
  192. "document/Word6_sections2.doc",
  193. "document/Word95.doc",
  194. "document/word95err.doc",
  195. "document/Bug60936.doc",
  196. "document/Bug60942.doc",
  197. "document/Bug60942b.doc",
  198. "hpsf/TestMickey.doc",
  199. "document/52117.doc"
  200. );
  201. private static final Set<String> EXPECTED_FAILURES = unmodifiableHashSet(
  202. // password protected files
  203. "spreadsheet/password.xls",
  204. "spreadsheet/protected_passtika.xlsx",
  205. "spreadsheet/51832.xls",
  206. "document/PasswordProtected.doc",
  207. "slideshow/Password_Protected-hello.ppt",
  208. "slideshow/Password_Protected-56-hello.ppt",
  209. "slideshow/Password_Protected-np-hello.ppt",
  210. "slideshow/cryptoapi-proc2356.ppt",
  211. //"document/bug53475-password-is-pass.docx",
  212. //"document/bug53475-password-is-solrcell.docx",
  213. "spreadsheet/xor-encryption-abc.xls",
  214. "spreadsheet/35897-type4.xls",
  215. //"poifs/protect.xlsx",
  216. //"poifs/protected_sha512.xlsx",
  217. //"poifs/extenxls_pwd123.xlsx",
  218. //"poifs/protected_agile.docx",
  219. "spreadsheet/58616.xlsx",
  220. "poifs/60320-protected.xlsx",
  221. // TODO: fails XMLExportTest, is this ok?
  222. "spreadsheet/CustomXMLMapping-singleattributenamespace.xlsx",
  223. "spreadsheet/55864.xlsx",
  224. "spreadsheet/57890.xlsx",
  225. // TODO: these fail now with some NPE/file read error because we now try to compute every value via Cell.toString()!
  226. "spreadsheet/44958.xls",
  227. "spreadsheet/44958_1.xls",
  228. "spreadsheet/testArraysAndTables.xls",
  229. // TODO: good to ignore?
  230. "spreadsheet/sample-beta.xlsx",
  231. "document/cpansearch.perl.org_src_tobyink_acme-rundoc-0.001_word-lib_hello_world.docm",
  232. // This is actually a spreadsheet!
  233. "hpsf/TestRobert_Flaherty.doc",
  234. // some files that are broken, eg Word 95, ...
  235. "spreadsheet/43493.xls",
  236. "spreadsheet/46904.xls",
  237. "document/Bug50955.doc",
  238. "document/57843.doc",
  239. "slideshow/PPT95.ppt",
  240. "openxml4j/OPCCompliance_CoreProperties_DCTermsNamespaceLimitedUseFAIL.docx",
  241. "openxml4j/OPCCompliance_CoreProperties_DoNotUseCompatibilityMarkupFAIL.docx",
  242. "openxml4j/OPCCompliance_CoreProperties_LimitedXSITypeAttribute_NotPresentFAIL.docx",
  243. "openxml4j/OPCCompliance_CoreProperties_LimitedXSITypeAttribute_PresentWithUnauthorizedValueFAIL.docx",
  244. "openxml4j/OPCCompliance_CoreProperties_OnlyOneCorePropertiesPartFAIL.docx",
  245. "openxml4j/OPCCompliance_CoreProperties_UnauthorizedXMLLangAttributeFAIL.docx",
  246. "openxml4j/OPCCompliance_DerivedPartNameFAIL.docx",
  247. "openxml4j/invalid.xlsx",
  248. "spreadsheet/54764-2.xlsx", // see TestXSSFBugs.bug54764()
  249. "spreadsheet/54764.xlsx", // see TestXSSFBugs.bug54764()
  250. "poifs/unknown_properties.msg", // POIFS properties corrupted
  251. "poifs/only-zero-byte-streams.ole2", // No actual contents
  252. "spreadsheet/poc-xmlbomb.xlsx", // contains xml-entity-expansion
  253. "spreadsheet/poc-shared-strings.xlsx", // contains shared-string-entity-expansion
  254. "spreadsheet/60255_extra_drawingparts.xlsx", // Non-drawing drawing
  255. // old Excel files, which we only support simple text extraction of
  256. "spreadsheet/testEXCEL_2.xls",
  257. "spreadsheet/testEXCEL_3.xls",
  258. "spreadsheet/testEXCEL_4.xls",
  259. "spreadsheet/testEXCEL_5.xls",
  260. "spreadsheet/testEXCEL_95.xls",
  261. "spreadsheet/59074.xls",
  262. "spreadsheet/60284.xls",
  263. // OOXML Strict is not yet supported, see bug #57699
  264. "spreadsheet/SampleSS.strict.xlsx",
  265. "spreadsheet/SimpleStrict.xlsx",
  266. "spreadsheet/sample.strict.xlsx",
  267. "spreadsheet/57914.xlsx",
  268. // non-TNEF files
  269. "ddf/Container.dat",
  270. "ddf/47143.dat",
  271. // sheet cloning errors
  272. "spreadsheet/47813.xlsx",
  273. "spreadsheet/56450.xls",
  274. "spreadsheet/OddStyleRecord.xls",
  275. "spreadsheet/WithChartSheet.xlsx",
  276. "spreadsheet/chart_sheet.xlsx",
  277. "spreadsheet/SimpleScatterChart.xlsx"
  278. );
  279. private static final Set<String> IGNORED = unmodifiableHashSet(
  280. // need JDK8+ - https://bugs.openjdk.java.net/browse/JDK-8038081
  281. "slideshow/42474-2.ppt",
  282. // OPC handler works / XSSF handler fails
  283. "spreadsheet/57181.xlsm"
  284. );
  285. @Parameters(name="{index}: {0} using {1}")
  286. public static Iterable<Object[]> files() {
  287. DirectoryScanner scanner = new DirectoryScanner();
  288. scanner.setBasedir(ROOT_DIR);
  289. scanner.setExcludes(SCAN_EXCLUDES);
  290. scanner.scan();
  291. System.out.println("Handling " + scanner.getIncludedFiles().length + " files");
  292. List<Object[]> files = new ArrayList<Object[]>();
  293. for(String file : scanner.getIncludedFiles()) {
  294. file = file.replace('\\', '/'); // ... failures/handlers lookup doesn't work on windows otherwise
  295. if (IGNORED.contains(file)) {
  296. System.out.println("Ignoring " + file);
  297. continue;
  298. }
  299. FileHandler handler = HANDLERS.get(getExtension(file));
  300. files.add(new Object[] { file, handler });
  301. // for some file-types also run OPCFileHandler
  302. if(handler instanceof XSSFFileHandler ||
  303. handler instanceof XWPFFileHandler ||
  304. handler instanceof XSLFFileHandler ||
  305. handler instanceof XDGFFileHandler) {
  306. files.add(new Object[] { file, HANDLERS.get(".ooxml") });
  307. }
  308. }
  309. return files;
  310. }
  311. @SuppressWarnings("DefaultAnnotationParam")
  312. @Parameter(value=0)
  313. public String file;
  314. @Parameter(value=1)
  315. public FileHandler handler;
  316. @Test
  317. public void testAllFiles() throws Exception {
  318. System.out.println("Reading " + file + " with " + handler.getClass());
  319. assertNotNull("Unknown file extension for file: " + file + ": " + getExtension(file), handler);
  320. File inputFile = new File(ROOT_DIR, file);
  321. try {
  322. InputStream stream = new BufferedInputStream(new FileInputStream(inputFile), 64*1024);
  323. try {
  324. handler.handleFile(stream);
  325. assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
  326. OLD_FILES.contains(file));
  327. } finally {
  328. stream.close();
  329. }
  330. handler.handleExtracting(inputFile);
  331. // special cases where docx-handling breaks, but OPCPackage handling works
  332. boolean ignoredOPC = (file.endsWith(".docx") || file.endsWith(".xlsx") ||
  333. file.endsWith(".xlsb") || file.endsWith(".pptx")) &&
  334. handler instanceof OPCFileHandler;
  335. assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
  336. EXPECTED_FAILURES.contains(file) && !ignoredOPC);
  337. } catch (OldFileFormatException e) {
  338. // for old word files we should still support extracting text
  339. if(OLD_FILES.contains(file)) {
  340. handler.handleExtracting(inputFile);
  341. } else {
  342. // check if we expect failure for this file
  343. if(!EXPECTED_FAILURES.contains(file) && !AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.contains(file)) {
  344. System.out.println("Failed: " + file);
  345. throw new Exception("While handling " + file, e);
  346. }
  347. }
  348. } catch (Exception e) {
  349. // check if we expect failure for this file
  350. if(!EXPECTED_FAILURES.contains(file) && !AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.contains(file)) {
  351. System.out.println("Failed: " + file);
  352. throw new Exception("While handling " + file, e);
  353. }
  354. }
  355. // let some file handlers do additional stuff
  356. handler.handleAdditional(inputFile);
  357. }
  358. static String getExtension(String file) {
  359. int pos = file.lastIndexOf('.');
  360. if(pos == -1 || pos == file.length()-1) {
  361. return file;
  362. }
  363. return file.substring(pos).toLowerCase(Locale.ROOT);
  364. }
  365. private static class NullFileHandler implements FileHandler {
  366. @Override
  367. public void handleFile(InputStream stream) throws Exception {
  368. }
  369. @Override
  370. public void handleExtracting(File file) throws Exception {
  371. }
  372. @Override
  373. public void handleAdditional(File file) throws Exception {
  374. }
  375. }
  376. }