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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 java.io.File;
  17. import java.io.InputStream;
  18. import org.apache.poi.POITestCase;
  19. import org.apache.poi.hssf.HSSFTestDataSamples;
  20. /**
  21. * Unit tests for the Excel 5/95 and Excel 4 (and older) text
  22. * extractor
  23. */
  24. public final class TestOldExcelExtractor extends POITestCase {
  25. private static OldExcelExtractor createExtractor(String sampleFileName) {
  26. InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
  27. try {
  28. return new OldExcelExtractor(is);
  29. } catch (Exception e) {
  30. throw new RuntimeException(e);
  31. }
  32. }
  33. public void testSimpleExcel4() {
  34. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  35. // Check we can call getText without error
  36. String text = extractor.getText();
  37. // Check we find a few words we expect in there
  38. assertContains(text, "Size");
  39. assertContains(text, "Returns");
  40. // Check we find a few numbers we expect in there
  41. assertContains(text, "11");
  42. assertContains(text, "784");
  43. }
  44. public void testSimpleExcel5() {
  45. for (String ver : new String[] {"5", "95"}) {
  46. OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls");
  47. // Check we can call getText without error
  48. String text = extractor.getText();
  49. // Check we find a few words we expect in there
  50. assertContains(text, "Sample Excel");
  51. assertContains(text, "Written and saved");
  52. // Check we find a few numbers we expect in there
  53. assertContains(text, "15");
  54. assertContains(text, "169");
  55. }
  56. }
  57. public void testStrings() {
  58. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  59. String text = extractor.getText();
  60. // Simple strings
  61. assertContains(text, "Table 10 -- Examination Coverage:");
  62. assertContains(text, "Recommended and Average Recommended Additional Tax After");
  63. assertContains(text, "Individual income tax returns, total");
  64. // More complicated strings
  65. assertContains(text, "$100,000 or more");
  66. assertContains(text, "S corporation returns, Form 1120S [10,15]");
  67. // TODO Get these quotes working correctly
  68. // assertContains(text, "individual income tax return \u201Cshort forms.\u201D");
  69. // Formula based strings
  70. // TODO Find some then test
  71. }
  72. public void testFormattedNumbersExcel4() {
  73. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  74. String text = extractor.getText();
  75. // Simple numbers
  76. assertContains(text, "151");
  77. assertContains(text, "784");
  78. // Numbers which come from formulas
  79. assertContains(text, "0.398"); // TODO Rounding
  80. assertContains(text, "624");
  81. // Formatted numbers
  82. // TODO
  83. // assertContains(text, "55,624");
  84. // assertContains(text, "11,743,477");
  85. }
  86. public void testFormattedNumbersExcel5() {
  87. for (String ver : new String[] {"5", "95"}) {
  88. OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls");
  89. String text = extractor.getText();
  90. // Simple numbers
  91. assertContains(text, "1");
  92. // Numbers which come from formulas
  93. assertContains(text, "13");
  94. assertContains(text, "169");
  95. // Formatted numbers
  96. // TODO
  97. // assertContains(text, "100.00%");
  98. // assertContains(text, "155.00%");
  99. // assertContains(text, "1,125");
  100. // assertContains(text, "189,945");
  101. // assertContains(text, "1,234,500");
  102. // assertContains(text, "$169.00");
  103. // assertContains(text, "$1,253.82");
  104. }
  105. }
  106. public void testFromFile() throws Exception {
  107. for (String ver : new String[] {"4", "5", "95"}) {
  108. String filename = "testEXCEL_"+ver+".xls";
  109. File f = HSSFTestDataSamples.getSampleFile(filename);
  110. OldExcelExtractor extractor = new OldExcelExtractor(f);
  111. String text = extractor.getText();
  112. assertNotNull(text);
  113. assertTrue(text.length() > 100);
  114. }
  115. }
  116. }