001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.eventusermodel;
019:
020: import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
021: import org.apache.poi.hssf.eventusermodel.HSSFListener;
022:
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.util.ArrayList;
026:
027: import org.apache.poi.hssf.record.Record;
028: import org.apache.poi.hssf.record.ContinueRecord;
029: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
030:
031: import junit.framework.TestCase;
032:
033: public class TestHSSFEventFactory extends TestCase {
034: private String dirname;
035:
036: public TestHSSFEventFactory() {
037: dirname = System.getProperty("HSSF.testdata.path");
038: }
039:
040: public void testWithMissingRecords() throws Exception {
041: File f = new File(dirname + "/SimpleWithSkip.xls");
042:
043: HSSFRequest req = new HSSFRequest();
044: MockHSSFListener mockListen = new MockHSSFListener();
045: req.addListenerForAllRecords(mockListen);
046:
047: POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
048: HSSFEventFactory factory = new HSSFEventFactory();
049: factory.processWorkbookEvents(req, fs);
050:
051: // Check we got the records
052: assertTrue(mockListen.records.size() > 100);
053: }
054:
055: public void testWithCrazyContinueRecords() throws Exception {
056: // Some files have crazy ordering of their continue records
057: // Check that we don't break on them (bug #42844)
058:
059: File f = new File(dirname + "/ContinueRecordProblem.xls");
060:
061: HSSFRequest req = new HSSFRequest();
062: MockHSSFListener mockListen = new MockHSSFListener();
063: req.addListenerForAllRecords(mockListen);
064:
065: POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
066: HSSFEventFactory factory = new HSSFEventFactory();
067: factory.processWorkbookEvents(req, fs);
068:
069: // Check we got the records
070: assertTrue(mockListen.records.size() > 100);
071:
072: // And none of them are continue ones
073: Record[] r = (Record[]) mockListen.records
074: .toArray(new Record[mockListen.records.size()]);
075: for (int i = 0; i < r.length; i++) {
076: assertFalse(r[i] instanceof ContinueRecord);
077: }
078: }
079:
080: /**
081: * Unknown records can be continued.
082: * Check that HSSFEventFactory doesn't break on them.
083: * (the test file was provided in a reopen of bug #42844)
084: */
085: public void testUnknownContinueRecords() throws Exception {
086: File f = new File(dirname + "/42844.xls");
087:
088: HSSFRequest req = new HSSFRequest();
089: MockHSSFListener mockListen = new MockHSSFListener();
090: req.addListenerForAllRecords(mockListen);
091:
092: POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
093: HSSFEventFactory factory = new HSSFEventFactory();
094: factory.processWorkbookEvents(req, fs);
095:
096: assertTrue("no errors while processing the file", true);
097: }
098:
099: private static class MockHSSFListener implements HSSFListener {
100: private MockHSSFListener() {
101: }
102:
103: private ArrayList records = new ArrayList();
104:
105: public void processRecord(Record record) {
106: records.add(record);
107: }
108: }
109: }
|