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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 DISABLEDtestSimpleExcel3() {
  34. OldExcelExtractor extractor = createExtractor("testEXCEL_3.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, "Season beginning August");
  39. assertContains(text, "USDA");
  40. // Check we find a few numbers we expect in there
  41. assertContains(text, "347");
  42. assertContains(text, "228");
  43. // Check we find a few string-literal dates in there
  44. assertContains(text, "1981/82");
  45. // Check the type
  46. assertEquals(3, extractor.getBiffVersion());
  47. assertEquals(0x10, extractor.getFileType());
  48. }
  49. public void testSimpleExcel4() {
  50. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  51. // Check we can call getText without error
  52. String text = extractor.getText();
  53. // Check we find a few words we expect in there
  54. assertContains(text, "Size");
  55. assertContains(text, "Returns");
  56. // Check we find a few numbers we expect in there
  57. assertContains(text, "11");
  58. assertContains(text, "784");
  59. // Check the type
  60. assertEquals(4, extractor.getBiffVersion());
  61. assertEquals(0x10, extractor.getFileType());
  62. }
  63. public void testSimpleExcel5() {
  64. for (String ver : new String[] {"5", "95"}) {
  65. OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls");
  66. // Check we can call getText without error
  67. String text = extractor.getText();
  68. // Check we find a few words we expect in there
  69. assertContains(text, "Sample Excel");
  70. assertContains(text, "Written and saved");
  71. // Check we find a few numbers we expect in there
  72. assertContains(text, "15");
  73. assertContains(text, "169");
  74. // Check we got the sheet names (new formats only)
  75. assertContains(text, "Sheet: Feuil3");
  76. // Check the type
  77. assertEquals(5, extractor.getBiffVersion());
  78. assertEquals(0x05, extractor.getFileType());
  79. }
  80. }
  81. public void testStrings() {
  82. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  83. String text = extractor.getText();
  84. // Simple strings
  85. assertContains(text, "Table 10 -- Examination Coverage:");
  86. assertContains(text, "Recommended and Average Recommended Additional Tax After");
  87. assertContains(text, "Individual income tax returns, total");
  88. // More complicated strings
  89. assertContains(text, "$100,000 or more");
  90. assertContains(text, "S corporation returns, Form 1120S [10,15]");
  91. assertContains(text, "individual income tax return \u201Cshort forms.\u201D");
  92. // Formula based strings
  93. // TODO Find some then test
  94. }
  95. public void testFormattedNumbersExcel4() {
  96. OldExcelExtractor extractor = createExtractor("testEXCEL_4.xls");
  97. String text = extractor.getText();
  98. // Simple numbers
  99. assertContains(text, "151");
  100. assertContains(text, "784");
  101. // Numbers which come from formulas
  102. assertContains(text, "0.398"); // TODO Rounding
  103. assertContains(text, "624");
  104. // Formatted numbers
  105. // TODO
  106. // assertContains(text, "55,624");
  107. // assertContains(text, "11,743,477");
  108. }
  109. public void testFormattedNumbersExcel5() {
  110. for (String ver : new String[] {"5", "95"}) {
  111. OldExcelExtractor extractor = createExtractor("testEXCEL_"+ver+".xls");
  112. String text = extractor.getText();
  113. // Simple numbers
  114. assertContains(text, "1");
  115. // Numbers which come from formulas
  116. assertContains(text, "13");
  117. assertContains(text, "169");
  118. // Formatted numbers
  119. // TODO
  120. // assertContains(text, "100.00%");
  121. // assertContains(text, "155.00%");
  122. // assertContains(text, "1,125");
  123. // assertContains(text, "189,945");
  124. // assertContains(text, "1,234,500");
  125. // assertContains(text, "$169.00");
  126. // assertContains(text, "$1,253.82");
  127. }
  128. }
  129. public void testFromFile() throws Exception {
  130. for (String ver : new String[] {"4", "5", "95"}) {
  131. String filename = "testEXCEL_"+ver+".xls";
  132. File f = HSSFTestDataSamples.getSampleFile(filename);
  133. OldExcelExtractor extractor = new OldExcelExtractor(f);
  134. String text = extractor.getText();
  135. assertNotNull(text);
  136. assertTrue(text.length() > 100);
  137. }
  138. }
  139. }