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
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org
.apache
.poi
.hssf
.eventmodel
;
20 import java
.io
.ByteArrayInputStream
;
21 import java
.io
.ByteArrayOutputStream
;
22 import java
.io
.InputStream
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
27 import org
.apache
.poi
.hssf
.model
.Model
;
28 import org
.apache
.poi
.hssf
.model
.Sheet
;
29 import org
.apache
.poi
.hssf
.model
.Workbook
;
30 import org
.apache
.poi
.hssf
.usermodel
.HSSFCell
;
31 import org
.apache
.poi
.hssf
.usermodel
.HSSFRow
;
32 import org
.apache
.poi
.hssf
.usermodel
.HSSFSheet
;
33 import org
.apache
.poi
.hssf
.usermodel
.HSSFWorkbook
;
34 import org
.apache
.poi
.poifs
.filesystem
.POIFSFileSystem
;
36 import junit
.framework
.TestCase
;
39 * Tests the ModelFactory.
41 * @author Andrew C. Oliver acoliver@apache.org
43 public class TestModelFactory
extends TestCase
45 private ModelFactory factory
;
46 private HSSFWorkbook book
;
47 private InputStream in
;
51 * Tests that the listeners collection is created
54 public TestModelFactory(String arg0
)
57 ModelFactory mf
= new ModelFactory();
58 assertTrue("listeners member cannot be null", mf
.listeners
!= null);
59 assertTrue("listeners member must be a List", mf
.listeners
instanceof List
);
62 public static void main(String
[] args
)
64 junit
.textui
.TestRunner
.run(TestModelFactory
.class);
67 protected void setUp() throws Exception
70 models
= new ArrayList(3);
71 factory
= new ModelFactory();
72 book
= new HSSFWorkbook();
73 ByteArrayOutputStream stream
= (ByteArrayOutputStream
)setupRunFile(book
);
74 POIFSFileSystem fs
= new POIFSFileSystem(
75 new ByteArrayInputStream(stream
.toByteArray())
77 in
= fs
.createDocumentInputStream("Workbook");
80 protected void tearDown() throws Exception
89 * tests that listeners can be registered
91 public void testRegisterListener()
93 if (factory
.listeners
.size() != 0) {
94 factory
= new ModelFactory();
97 factory
.registerListener(new MFListener(null));
98 factory
.registerListener(new MFListener(null));
99 assertTrue("Factory listeners should be two, was="+
100 factory
.listeners
.size(),
101 factory
.listeners
.size() == 2);
105 * tests that given a simple input stream with one workbook and sheet
106 * that those models are processed and returned.
108 public void testRun()
113 if (factory
.listeners
.size() != 0) {
114 factory
= new ModelFactory();
117 factory
.registerListener(new MFListener(models
));
120 assertTrue("Models size must be 2 was = "+models
.size(),
122 mi
= models
.iterator();
123 temp
= (Model
)mi
.next();
125 assertTrue("First model is Workbook was " + temp
.getClass().getName(),
126 temp
instanceof Workbook
);
128 temp
= (Model
)mi
.next();
130 assertTrue("Second model is Sheet was " + temp
.getClass().getName(),
131 temp
instanceof Sheet
);
136 * Sets up a test file
138 private ByteArrayOutputStream
setupRunFile(HSSFWorkbook book
) throws Exception
{
139 ByteArrayOutputStream stream
= new ByteArrayOutputStream();
140 HSSFSheet sheet
= book
.createSheet("Test");
141 HSSFRow row
= sheet
.createRow(0);
142 HSSFCell cell
= row
.createCell((short)0);
143 cell
.setCellValue(10.5);
151 * listener for use in the test
153 class MFListener
implements ModelFactoryListener
{
155 public MFListener(List mlist
) {
159 public boolean process(Model model
)
165 public Iterator
models() {
166 return mlist
.iterator();