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.

TestXSSFEventBasedExcelExtractor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.extractor;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18. import junit.framework.TestCase;
  19. import org.apache.poi.POITextExtractor;
  20. import org.apache.poi.POIXMLTextExtractor;
  21. import org.apache.poi.hssf.HSSFTestDataSamples;
  22. import org.apache.poi.hssf.extractor.ExcelExtractor;
  23. import org.apache.poi.xssf.XSSFTestDataSamples;
  24. /**
  25. * Tests for {@link XSSFEventBasedExcelExtractor}
  26. */
  27. public class TestXSSFEventBasedExcelExtractor extends TestCase {
  28. protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception {
  29. return new XSSFEventBasedExcelExtractor(XSSFTestDataSamples.
  30. openSamplePackage(sampleName));
  31. }
  32. /**
  33. * Get text out of the simple file
  34. */
  35. public void testGetSimpleText() throws Exception {
  36. // a very simple file
  37. XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsx");
  38. extractor.getText();
  39. String text = extractor.getText();
  40. assertTrue(text.length() > 0);
  41. // Check sheet names
  42. assertTrue(text.startsWith("Sheet1"));
  43. assertTrue(text.endsWith("Sheet3\n"));
  44. // Now without, will have text
  45. extractor.setIncludeSheetNames(false);
  46. text = extractor.getText();
  47. String CHUNK1 =
  48. "Lorem\t111\n" +
  49. "ipsum\t222\n" +
  50. "dolor\t333\n" +
  51. "sit\t444\n" +
  52. "amet\t555\n" +
  53. "consectetuer\t666\n" +
  54. "adipiscing\t777\n" +
  55. "elit\t888\n" +
  56. "Nunc\t999\n";
  57. String CHUNK2 =
  58. "The quick brown fox jumps over the lazy dog\n" +
  59. "hello, xssf hello, xssf\n" +
  60. "hello, xssf hello, xssf\n" +
  61. "hello, xssf hello, xssf\n" +
  62. "hello, xssf hello, xssf\n";
  63. assertEquals(
  64. CHUNK1 +
  65. "at\t4995\n" +
  66. CHUNK2
  67. , text);
  68. // Now get formulas not their values
  69. extractor.setFormulasNotResults(true);
  70. text = extractor.getText();
  71. assertEquals(
  72. CHUNK1 +
  73. "at\tSUM(B1:B9)\n" +
  74. CHUNK2, text);
  75. // With sheet names too
  76. extractor.setIncludeSheetNames(true);
  77. text = extractor.getText();
  78. assertEquals(
  79. "Sheet1\n" +
  80. CHUNK1 +
  81. "at\tSUM(B1:B9)\n" +
  82. "rich test\n" +
  83. CHUNK2 +
  84. "Sheet3\n"
  85. , text);
  86. extractor.close();
  87. }
  88. public void testGetComplexText() throws Exception {
  89. // A fairly complex file
  90. XSSFEventBasedExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
  91. extractor.getText();
  92. String text = extractor.getText();
  93. assertTrue(text.length() > 0);
  94. // Might not have all formatting it should do!
  95. assertTrue(text.startsWith(
  96. "Avgtxfull\n" +
  97. "(iii) AVERAGE TAX RATES ON ANNUAL"
  98. ));
  99. extractor.close();
  100. }
  101. public void testInlineStrings() throws Exception {
  102. XSSFEventBasedExcelExtractor extractor = getExtractor("InlineStrings.xlsx");
  103. extractor.setFormulasNotResults(true);
  104. String text = extractor.getText();
  105. // Numbers
  106. assertTrue("Unable to find expected word in text\n" + text, text.contains("43"));
  107. assertTrue("Unable to find expected word in text\n" + text, text.contains("22"));
  108. // Strings
  109. assertTrue("Unable to find expected word in text\n" + text, text.contains("ABCDE"));
  110. assertTrue("Unable to find expected word in text\n" + text, text.contains("Long Text"));
  111. // Inline Strings
  112. assertTrue("Unable to find expected word in text\n" + text, text.contains("1st Inline String"));
  113. assertTrue("Unable to find expected word in text\n" + text, text.contains("And More"));
  114. // Formulas
  115. assertTrue("Unable to find expected word in text\n" + text, text.contains("A2"));
  116. assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2"));
  117. extractor.close();
  118. }
  119. /**
  120. * Test that we return pretty much the same as
  121. * ExcelExtractor does, when we're both passed
  122. * the same file, just saved as xls and xlsx
  123. */
  124. public void testComparedToOLE2() throws Exception {
  125. // A fairly simple file - ooxml
  126. XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx");
  127. ExcelExtractor ole2Extractor =
  128. new ExcelExtractor(HSSFTestDataSamples.openSampleWorkbook("SampleSS.xls"));
  129. POITextExtractor[] extractors =
  130. new POITextExtractor[] { ooxmlExtractor, ole2Extractor };
  131. for (int i = 0; i < extractors.length; i++) {
  132. POITextExtractor extractor = extractors[i];
  133. String text = extractor.getText().replaceAll("[\r\t]", "");
  134. assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n"));
  135. Pattern pattern = Pattern.compile(".*13(\\.0+)?\\s+Sheet3.*", Pattern.DOTALL);
  136. Matcher m = pattern.matcher(text);
  137. assertTrue(m.matches());
  138. }
  139. ole2Extractor.close();
  140. ooxmlExtractor.close();
  141. }
  142. /**
  143. * Test text extraction from text box using getShapes()
  144. * @throws Exception
  145. */
  146. public void testShapes() throws Exception{
  147. XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsx");
  148. try {
  149. String text = ooxmlExtractor.getText();
  150. assertTrue(text.indexOf("Line 1") > -1);
  151. assertTrue(text.indexOf("Line 2") > -1);
  152. assertTrue(text.indexOf("Line 3") > -1);
  153. } finally {
  154. ooxmlExtractor.close();
  155. }
  156. }
  157. /**
  158. * Test that we return the same output for unstyled numbers as the
  159. * non-event-based XSSFExcelExtractor.
  160. */
  161. public void testUnstyledNumbersComparedToNonEventBasedExtractor()
  162. throws Exception {
  163. String expectedOutput = "Sheet1\n99.99\n";
  164. XSSFExcelExtractor extractor = new XSSFExcelExtractor(
  165. XSSFTestDataSamples.openSampleWorkbook("56011.xlsx"));
  166. try {
  167. assertEquals(expectedOutput, extractor.getText().replace(",", "."));
  168. } finally {
  169. extractor.close();
  170. }
  171. XSSFEventBasedExcelExtractor fixture =
  172. new XSSFEventBasedExcelExtractor(
  173. XSSFTestDataSamples.openSamplePackage("56011.xlsx"));
  174. try {
  175. assertEquals(expectedOutput, fixture.getText().replace(",", "."));
  176. } finally {
  177. fixture.close();
  178. }
  179. }
  180. /**
  181. * Test that we return the same output headers and footers as the
  182. * non-event-based XSSFExcelExtractor.
  183. */
  184. public void testHeadersAndFootersComparedToNonEventBasedExtractor()
  185. throws Exception {
  186. String expectedOutputWithHeadersAndFooters =
  187. "Sheet1\n" +
  188. "&\"Calibri,Regular\"&K000000top left\t&\"Calibri,Regular\"&K000000top center\t&\"Calibri,Regular\"&K000000top right\n" +
  189. "abc\t123\n" +
  190. "&\"Calibri,Regular\"&K000000bottom left\t&\"Calibri,Regular\"&K000000bottom center\t&\"Calibri,Regular\"&K000000bottom right\n";
  191. String expectedOutputWithoutHeadersAndFooters =
  192. "Sheet1\n" +
  193. "abc\t123\n";
  194. XSSFExcelExtractor extractor = new XSSFExcelExtractor(
  195. XSSFTestDataSamples.openSampleWorkbook("headerFooterTest.xlsx"));
  196. try {
  197. assertEquals(expectedOutputWithHeadersAndFooters, extractor.getText());
  198. extractor.setIncludeHeadersFooters(false);
  199. assertEquals(expectedOutputWithoutHeadersAndFooters, extractor.getText());
  200. } finally {
  201. extractor.close();
  202. }
  203. XSSFEventBasedExcelExtractor fixture =
  204. new XSSFEventBasedExcelExtractor(
  205. XSSFTestDataSamples.openSamplePackage("headerFooterTest.xlsx"));
  206. try {
  207. assertEquals(expectedOutputWithHeadersAndFooters, fixture.getText());
  208. fixture.setIncludeHeadersFooters(false);
  209. assertEquals(expectedOutputWithoutHeadersAndFooters, fixture.getText());
  210. } finally {
  211. fixture.close();
  212. }
  213. }
  214. /**
  215. * Test that XSSFEventBasedExcelExtractor outputs comments when specified.
  216. * The output will contain two improvements over the output from
  217. * XSSFExcelExtractor in that (1) comments from empty cells will be
  218. * outputted, and (2) the author will not be outputted twice.
  219. * <p>
  220. * This test will need to be modified if these improvements are ported to
  221. * XSSFExcelExtractor.
  222. */
  223. public void testCommentsComparedToNonEventBasedExtractor()
  224. throws Exception {
  225. String expectedOutputWithoutComments =
  226. "Sheet1\n" +
  227. "\n" +
  228. "abc\n" +
  229. "\n" +
  230. "123\n" +
  231. "\n" +
  232. "\n" +
  233. "\n";
  234. String nonEventBasedExtractorOutputWithComments =
  235. "Sheet1\n" +
  236. "\n" +
  237. "abc Comment by Shaun Kalley: Shaun Kalley: Comment A2\n" +
  238. "\n" +
  239. "123 Comment by Shaun Kalley: Shaun Kalley: Comment B4\n" +
  240. "\n" +
  241. "\n" +
  242. "\n";
  243. String eventBasedExtractorOutputWithComments =
  244. "Sheet1\n" +
  245. "Comment by Shaun Kalley: Comment A1\tComment by Shaun Kalley: Comment B1\n" +
  246. "abc Comment by Shaun Kalley: Comment A2\tComment by Shaun Kalley: Comment B2\n" +
  247. "Comment by Shaun Kalley: Comment A3\tComment by Shaun Kalley: Comment B3\n" +
  248. "Comment by Shaun Kalley: Comment A4\t123 Comment by Shaun Kalley: Comment B4\n" +
  249. "Comment by Shaun Kalley: Comment A5\tComment by Shaun Kalley: Comment B5\n" +
  250. "Comment by Shaun Kalley: Comment A7\tComment by Shaun Kalley: Comment B7\n" +
  251. "Comment by Shaun Kalley: Comment A8\tComment by Shaun Kalley: Comment B8\n";
  252. XSSFExcelExtractor extractor = new XSSFExcelExtractor(
  253. XSSFTestDataSamples.openSampleWorkbook("commentTest.xlsx"));
  254. try {
  255. assertEquals(expectedOutputWithoutComments, extractor.getText());
  256. extractor.setIncludeCellComments(true);
  257. assertEquals(nonEventBasedExtractorOutputWithComments, extractor.getText());
  258. } finally {
  259. extractor.close();
  260. }
  261. XSSFEventBasedExcelExtractor fixture =
  262. new XSSFEventBasedExcelExtractor(
  263. XSSFTestDataSamples.openSamplePackage("commentTest.xlsx"));
  264. try {
  265. assertEquals(expectedOutputWithoutComments, fixture.getText());
  266. fixture.setIncludeCellComments(true);
  267. assertEquals(eventBasedExtractorOutputWithComments, fixture.getText());
  268. } finally {
  269. fixture.close();
  270. }
  271. }
  272. public void testFile56278_normal() throws Exception {
  273. // first with normal Text Extractor
  274. POIXMLTextExtractor extractor = new XSSFExcelExtractor(
  275. XSSFTestDataSamples.openSampleWorkbook("56278.xlsx"));
  276. try {
  277. assertNotNull(extractor.getText());
  278. } finally {
  279. extractor.close();
  280. }
  281. }
  282. public void testFile56278_event() throws Exception {
  283. // then with event based one
  284. POIXMLTextExtractor extractor = getExtractor("56278.xlsx");
  285. try {
  286. assertNotNull(extractor.getText());
  287. } finally {
  288. extractor.close();
  289. }
  290. }
  291. public void test59021() throws Exception {
  292. XSSFEventBasedExcelExtractor ex =
  293. new XSSFEventBasedExcelExtractor(
  294. XSSFTestDataSamples.openSamplePackage("59021.xlsx"));
  295. String text = ex.getText();
  296. assertTrue("can't find Abhkazia", text.contains("Abkhazia - Fixed"));
  297. assertTrue("can't find 10/02/2016", text.contains("10/02/2016"));
  298. }
  299. }