Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TestHSSFEventFactory.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.eventusermodel;
  16. import java.io.InputStream;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import junit.framework.TestCase;
  20. import org.apache.poi.hssf.HSSFTestDataSamples;
  21. import org.apache.poi.hssf.record.ContinueRecord;
  22. import org.apache.poi.hssf.record.DVALRecord;
  23. import org.apache.poi.hssf.record.DVRecord;
  24. import org.apache.poi.hssf.record.EOFRecord;
  25. import org.apache.poi.hssf.record.FeatHdrRecord;
  26. import org.apache.poi.hssf.record.Record;
  27. import org.apache.poi.hssf.record.SelectionRecord;
  28. import org.apache.poi.hssf.record.WindowTwoRecord;
  29. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  30. /**
  31. *
  32. */
  33. public final class TestHSSFEventFactory extends TestCase {
  34. private static final InputStream openSample(String sampleFileName) {
  35. return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
  36. }
  37. public void testWithMissingRecords() throws Exception {
  38. HSSFRequest req = new HSSFRequest();
  39. MockHSSFListener mockListen = new MockHSSFListener();
  40. req.addListenerForAllRecords(mockListen);
  41. POIFSFileSystem fs = new POIFSFileSystem(openSample("SimpleWithSkip.xls"));
  42. HSSFEventFactory factory = new HSSFEventFactory();
  43. factory.processWorkbookEvents(req, fs);
  44. Record[] recs = mockListen.getRecords();
  45. // Check we got the records
  46. assertTrue( recs.length > 100 );
  47. // Check that the last few records are as we expect
  48. // (Makes sure we don't accidently skip the end ones)
  49. int numRec = recs.length;
  50. assertEquals(WindowTwoRecord.class, recs[numRec-3].getClass());
  51. assertEquals(SelectionRecord.class, recs[numRec-2].getClass());
  52. assertEquals(EOFRecord.class, recs[numRec-1].getClass());
  53. }
  54. public void testWithCrazyContinueRecords() throws Exception {
  55. // Some files have crazy ordering of their continue records
  56. // Check that we don't break on them (bug #42844)
  57. HSSFRequest req = new HSSFRequest();
  58. MockHSSFListener mockListen = new MockHSSFListener();
  59. req.addListenerForAllRecords(mockListen);
  60. POIFSFileSystem fs = new POIFSFileSystem(openSample("ContinueRecordProblem.xls"));
  61. HSSFEventFactory factory = new HSSFEventFactory();
  62. factory.processWorkbookEvents(req, fs);
  63. Record[] recs = mockListen.getRecords();
  64. // Check we got the records
  65. assertTrue( recs.length > 100 );
  66. // And none of them are continue ones
  67. for(int i=0; i<recs.length; i++) {
  68. assertFalse( recs[i] instanceof ContinueRecord );
  69. }
  70. // Check that the last few records are as we expect
  71. // (Makes sure we don't accidently skip the end ones)
  72. int numRec = recs.length;
  73. assertEquals(DVALRecord.class, recs[numRec-4].getClass());
  74. assertEquals(DVRecord.class, recs[numRec-3].getClass());
  75. assertEquals(FeatHdrRecord.class, recs[numRec-2].getClass());
  76. assertEquals(EOFRecord.class, recs[numRec-1].getClass());
  77. }
  78. /**
  79. * Unknown records can be continued.
  80. * Check that HSSFEventFactory doesn't break on them.
  81. * (the test file was provided in a reopen of bug #42844)
  82. */
  83. public void testUnknownContinueRecords() throws Exception {
  84. HSSFRequest req = new HSSFRequest();
  85. MockHSSFListener mockListen = new MockHSSFListener();
  86. req.addListenerForAllRecords(mockListen);
  87. POIFSFileSystem fs = new POIFSFileSystem(openSample("42844.xls"));
  88. HSSFEventFactory factory = new HSSFEventFactory();
  89. factory.processWorkbookEvents(req, fs);
  90. assertTrue("no errors while processing the file", true);
  91. }
  92. private static class MockHSSFListener implements HSSFListener {
  93. private final List<Record> records = new ArrayList<Record>();
  94. public MockHSSFListener() {}
  95. public Record[] getRecords() {
  96. Record[] result = new Record[records.size()];
  97. records.toArray(result);
  98. return result;
  99. }
  100. public void processRecord(Record record) {
  101. records.add(record);
  102. }
  103. }
  104. }