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.eventmodel;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.poi.hssf.model.Model;
028: import org.apache.poi.hssf.model.Sheet;
029: import org.apache.poi.hssf.model.Workbook;
030: import org.apache.poi.hssf.usermodel.HSSFCell;
031: import org.apache.poi.hssf.usermodel.HSSFRow;
032: import org.apache.poi.hssf.usermodel.HSSFSheet;
033: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
034: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
035:
036: import junit.framework.TestCase;
037:
038: /**
039: * Tests the ModelFactory.
040: *
041: * @author Andrew C. Oliver acoliver@apache.org
042: */
043: public class TestModelFactory extends TestCase {
044: private ModelFactory factory;
045: private HSSFWorkbook book;
046: private InputStream in;
047: private List models;
048:
049: /**
050: * Tests that the listeners collection is created
051: * @param arg0
052: */
053: public TestModelFactory(String arg0) {
054: super (arg0);
055: ModelFactory mf = new ModelFactory();
056: assertTrue("listeners member cannot be null",
057: mf.listeners != null);
058: assertTrue("listeners member must be a List",
059: mf.listeners instanceof List);
060: }
061:
062: public static void main(String[] args) {
063: junit.textui.TestRunner.run(TestModelFactory.class);
064: }
065:
066: protected void setUp() throws Exception {
067: super .setUp();
068: models = new ArrayList(3);
069: factory = new ModelFactory();
070: book = new HSSFWorkbook();
071: ByteArrayOutputStream stream = (ByteArrayOutputStream) setupRunFile(book);
072: POIFSFileSystem fs = new POIFSFileSystem(
073: new ByteArrayInputStream(stream.toByteArray()));
074: in = fs.createDocumentInputStream("Workbook");
075: }
076:
077: protected void tearDown() throws Exception {
078: super .tearDown();
079: factory = null;
080: book = null;
081: in = null;
082: }
083:
084: /**
085: * tests that listeners can be registered
086: */
087: public void testRegisterListener() {
088: if (factory.listeners.size() != 0) {
089: factory = new ModelFactory();
090: }
091:
092: factory.registerListener(new MFListener(null));
093: factory.registerListener(new MFListener(null));
094: assertTrue("Factory listeners should be two, was="
095: + factory.listeners.size(),
096: factory.listeners.size() == 2);
097: }
098:
099: /**
100: * tests that given a simple input stream with one workbook and sheet
101: * that those models are processed and returned.
102: */
103: public void testRun() {
104: Model temp = null;
105: Iterator mi = null;
106:
107: if (factory.listeners.size() != 0) {
108: factory = new ModelFactory();
109: }
110:
111: factory.registerListener(new MFListener(models));
112: factory.run(in);
113:
114: assertTrue("Models size must be 2 was = " + models.size(),
115: models.size() == 2);
116: mi = models.iterator();
117: temp = (Model) mi.next();
118:
119: assertTrue("First model is Workbook was "
120: + temp.getClass().getName(), temp instanceof Workbook);
121:
122: temp = (Model) mi.next();
123:
124: assertTrue("Second model is Sheet was "
125: + temp.getClass().getName(), temp instanceof Sheet);
126:
127: }
128:
129: /**
130: * Sets up a test file
131: */
132: private ByteArrayOutputStream setupRunFile(HSSFWorkbook book)
133: throws Exception {
134: ByteArrayOutputStream stream = new ByteArrayOutputStream();
135: HSSFSheet sheet = book.createSheet("Test");
136: HSSFRow row = sheet.createRow(0);
137: HSSFCell cell = row.createCell((short) 0);
138: cell.setCellValue(10.5);
139: book.write(stream);
140: return stream;
141: }
142:
143: }
144:
145: /**
146: * listener for use in the test
147: */
148: class MFListener implements ModelFactoryListener {
149: private List mlist;
150:
151: public MFListener(List mlist) {
152: this .mlist = mlist;
153: }
154:
155: public boolean process(Model model) {
156: mlist.add(model);
157: return true;
158: }
159:
160: public Iterator models() {
161: return mlist.iterator();
162: }
163:
164: }
|