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.

TestOldExcelExtractor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.hssf.extractor;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.PrintStream;
  28. import org.apache.poi.EmptyFileException;
  29. import org.apache.poi.EncryptedDocumentException;
  30. import org.apache.poi.POIDataSamples;
  31. import org.apache.poi.hssf.HSSFTestDataSamples;
  32. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  33. import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
  34. import org.apache.poi.util.RecordFormatException;
  35. import org.junit.Ignore;
  36. import org.junit.Test;
  37. /**
  38. * Unit tests for the Excel 5/95 and Excel 4 (and older) text
  39. * extractor
  40. */
  41. public final class TestOldExcelExtractor {
  42. private static OldExcelExtractor createExtractor(String sampleFileName) throws IOException {
  43. File file = HSSFTestDataSamples.getSampleFile(sampleFileName);
  44. return new OldExcelExtractor(file);
  45. }
  46. @Test
  47. public void testSimpleExcel3() throws Exception {
  48. OldExcelExtractor extractor = createExtractor("testEXCEL_3.xls");
  49. // Check we can call getText without error
  50. String text = extractor.getText();
  51. // Check we find a few words we expect in there
  52. assertContains(text, "Season beginning August");
  53. assertContains(text, "USDA");
  54. // Check we find a few numbers we expect in there
  55. assertContains(text, "347");
  56. assertContains(text, "228");
  57. // Check we find a few string-literal dates in there
  58. assertContains(text, "1981/82");
  59. // Check the type
  60. assertEquals(3, extractor.getBiffVersion());
  61. assertEquals(0x10, extractor.getFileType());
  62. extractor.close();
  63. }
  64. @Test
  65. public void testSimpleExcel3NoReading() throws Exception {
  66. OldExcelExtractor extractor = createExtractor("testEXCEL_3.xls");
  67. assertNotNull(extractor);
  68. extractor.close();
  69. }
  70. @Test
  71. public void testSimpleExcel4() throws Exception {
  72. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  73. // Check we can call getText without error
  74. String text = extractor.getText();
  75. // Check we find a few words we expect in there
  76. assertContains(text, "Size");
  77. assertContains(text, "Returns");
  78. // Check we find a few numbers we expect in there
  79. assertContains(text, "11");
  80. assertContains(text, "784");
  81. // Check the type
  82. assertEquals(4, extractor.getBiffVersion());
  83. assertEquals(0x10, extractor.getFileType());
  84. extractor.close();
  85. }
  86. @Test
  87. public void testSimpleExcel5() throws Exception {
  88. for (String ver : new String[] {"5", "95"}) {
  89. OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls");
  90. // Check we can call getText without error
  91. String text = extractor.getText();
  92. // Check we find a few words we expect in there
  93. assertContains(text, "Sample Excel");
  94. assertContains(text, "Written and saved");
  95. // Check we find a few numbers we expect in there
  96. assertContains(text, "15");
  97. assertContains(text, "169");
  98. // Check we got the sheet names (new formats only)
  99. assertContains(text, "Sheet: Feuil3");
  100. // Check the type
  101. assertEquals(5, extractor.getBiffVersion());
  102. assertEquals(0x05, extractor.getFileType());
  103. extractor.close();
  104. }
  105. }
  106. @Test
  107. public void testStrings() throws Exception {
  108. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  109. String text = extractor.getText();
  110. // Simple strings
  111. assertContains(text, "Table 10 -- Examination Coverage:");
  112. assertContains(text, "Recommended and Average Recommended Additional Tax After");
  113. assertContains(text, "Individual income tax returns, total");
  114. // More complicated strings
  115. assertContains(text, "$100,000 or more");
  116. assertContains(text, "S corporation returns, Form 1120S [10,15]");
  117. assertContains(text, "individual income tax return \u201Cshort forms.\u201D");
  118. // Formula based strings
  119. // TODO Find some then test
  120. extractor.close();
  121. }
  122. @Test
  123. public void testFormattedNumbersExcel4() throws Exception {
  124. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  125. String text = extractor.getText();
  126. // Simple numbers
  127. assertContains(text, "151");
  128. assertContains(text, "784");
  129. // Numbers which come from formulas
  130. assertContains(text, "0.398"); // TODO Rounding
  131. assertContains(text, "624");
  132. // Formatted numbers
  133. // TODO
  134. // assertContains(text, "55,624");
  135. // assertContains(text, "11,743,477");
  136. extractor.close();
  137. }
  138. @Test
  139. public void testFormattedNumbersExcel5() throws Exception {
  140. for (String ver : new String[] {"5", "95"}) {
  141. OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls");
  142. String text = extractor.getText();
  143. // Simple numbers
  144. assertContains(text, "1");
  145. // Numbers which come from formulas
  146. assertContains(text, "13");
  147. assertContains(text, "169");
  148. // Formatted numbers
  149. // TODO
  150. // assertContains(text, "100.00%");
  151. // assertContains(text, "155.00%");
  152. // assertContains(text, "1,125");
  153. // assertContains(text, "189,945");
  154. // assertContains(text, "1,234,500");
  155. // assertContains(text, "$169.00");
  156. // assertContains(text, "$1,253.82");
  157. extractor.close();
  158. }
  159. }
  160. @Test
  161. public void testFromFile() throws Exception {
  162. for (String ver : new String[] {"4", "5", "95"}) {
  163. String filename = "testEXCEL_"+ver+".xls";
  164. File f = HSSFTestDataSamples.getSampleFile(filename);
  165. OldExcelExtractor extractor = new OldExcelExtractor(f);
  166. String text = extractor.getText();
  167. assertNotNull(text);
  168. assertTrue(text.length() > 100);
  169. extractor.close();
  170. }
  171. }
  172. @Test
  173. public void testOpenInvalidFile() throws Exception {
  174. // a file that exists, but is a different format
  175. try {
  176. createExtractor("WithVariousData.xlsx");
  177. fail("Should catch Exception here");
  178. } catch (OfficeXmlFileException e) {
  179. // expected here
  180. }
  181. // a completely different type of file
  182. try {
  183. createExtractor("48936-strings.txt");
  184. fail("Should catch Exception here");
  185. } catch (RecordFormatException e) {
  186. // expected here
  187. }
  188. }
  189. @Test
  190. public void testOpenNonExistingFile() throws Exception {
  191. // a file that exists, but is a different format
  192. try {
  193. OldExcelExtractor extractor = new OldExcelExtractor(new File("notexistingfile.xls"));
  194. extractor.close();
  195. fail("Should catch Exception here");
  196. } catch (EmptyFileException e) {
  197. // expected here
  198. }
  199. }
  200. @Test
  201. public void testInputStream() throws Exception {
  202. File file = HSSFTestDataSamples.getSampleFile("testEXCEL_3.xls");
  203. InputStream stream = new FileInputStream(file);
  204. try {
  205. OldExcelExtractor extractor = new OldExcelExtractor(stream);
  206. String text = extractor.getText();
  207. assertNotNull(text);
  208. extractor.close();
  209. } finally {
  210. stream.close();
  211. }
  212. }
  213. @Test
  214. public void testInputStreamNPOIHeader() throws Exception {
  215. File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
  216. InputStream stream = new FileInputStream(file);
  217. try {
  218. OldExcelExtractor extractor = new OldExcelExtractor(stream);
  219. extractor.close();
  220. } finally {
  221. stream.close();
  222. }
  223. }
  224. @Test
  225. public void testNPOIFSFileSystem() throws Exception {
  226. File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
  227. NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
  228. try {
  229. OldExcelExtractor extractor = new OldExcelExtractor(fs);
  230. extractor.close();
  231. } finally {
  232. fs.close();
  233. }
  234. }
  235. @Test
  236. public void testDirectoryNode() throws Exception {
  237. File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
  238. NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
  239. try {
  240. OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot());
  241. extractor.close();
  242. } finally {
  243. fs.close();
  244. }
  245. }
  246. @Test
  247. public void testDirectoryNodeInvalidFile() throws Exception {
  248. File file = POIDataSamples.getDocumentInstance().getFile("test.doc");
  249. NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
  250. try {
  251. OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot());
  252. extractor.close();
  253. fail("Should catch exception here");
  254. } catch (FileNotFoundException e) {
  255. // expected here
  256. } finally {
  257. fs.close();
  258. }
  259. }
  260. @Ignore("Calls System.exit()")
  261. @Test
  262. public void testMainUsage() throws Exception {
  263. PrintStream save = System.err;
  264. try {
  265. ByteArrayOutputStream out = new ByteArrayOutputStream();
  266. try {
  267. PrintStream str = new PrintStream(out, false, "UTF-8");
  268. System.setErr(str);
  269. OldExcelExtractor.main(new String[] {});
  270. } finally {
  271. out.close();
  272. }
  273. } finally {
  274. System.setErr(save);
  275. }
  276. }
  277. @Test
  278. public void testMain() throws Exception {
  279. File file = HSSFTestDataSamples.getSampleFile("testEXCEL_3.xls");
  280. PrintStream save = System.out;
  281. try {
  282. ByteArrayOutputStream out = new ByteArrayOutputStream();
  283. try {
  284. PrintStream str = new PrintStream(out, false, "UTF-8");
  285. System.setOut(str);
  286. OldExcelExtractor.main(new String[] {file.getAbsolutePath()});
  287. } finally {
  288. out.close();
  289. }
  290. String string = new String(out.toByteArray(), "UTF-8");
  291. assertTrue("Had: " + string,
  292. string.contains("Table C-13--Lemons"));
  293. } finally {
  294. System.setOut(save);
  295. }
  296. }
  297. @Test
  298. public void testEncryptionException() throws Exception {
  299. //test file derives from Common Crawl
  300. File file = HSSFTestDataSamples.getSampleFile("60284.xls");
  301. OldExcelExtractor ex = new OldExcelExtractor(file);
  302. assertEquals(5, ex.getBiffVersion());
  303. assertEquals(5, ex.getFileType());
  304. try {
  305. ex.getText();
  306. fail();
  307. } catch (EncryptedDocumentException e) {
  308. assertTrue("correct exception thrown", true);
  309. }
  310. }
  311. }