KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > eventmodel > TestModelFactory


1 /* ====================================================================
2    Copyright 2003-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.eventmodel;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
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;
35
36 import junit.framework.TestCase;
37
38 /**
39  * Tests the ModelFactory.
40  *
41  * @author Andrew C. Oliver acoliver@apache.org
42  */

43 public class TestModelFactory extends TestCase
44 {
45     private ModelFactory factory;
46     private HSSFWorkbook book;
47     private InputStream JavaDoc in;
48     private List JavaDoc models;
49
50     /**
51      * Tests that the listeners collection is created
52      * @param arg0
53      */

54     public TestModelFactory(String JavaDoc arg0)
55     {
56         super(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 JavaDoc);
60     }
61
62     public static void main(String JavaDoc[] args)
63     {
64         junit.textui.TestRunner.run(TestModelFactory.class);
65     }
66
67     protected void setUp() throws Exception JavaDoc
68     {
69         super.setUp();
70         models = new ArrayList JavaDoc(3);
71         factory = new ModelFactory();
72         book = new HSSFWorkbook();
73         ByteArrayOutputStream JavaDoc stream = (ByteArrayOutputStream JavaDoc)setupRunFile(book);
74         POIFSFileSystem fs = new POIFSFileSystem(
75                                    new ByteArrayInputStream JavaDoc(stream.toByteArray())
76                                    );
77         in = fs.createDocumentInputStream("Workbook");
78     }
79
80     protected void tearDown() throws Exception JavaDoc
81     {
82         super.tearDown();
83         factory = null;
84         book = null;
85         in = null;
86     }
87
88     /**
89      * tests that listeners can be registered
90      */

91     public void testRegisterListener()
92     {
93         if (factory.listeners.size() != 0) {
94          factory = new ModelFactory();
95         }
96         
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);
102     }
103
104     /**
105      * tests that given a simple input stream with one workbook and sheet
106      * that those models are processed and returned.
107      */

108     public void testRun()
109     {
110         Model temp = null;
111         Iterator JavaDoc mi = null;
112         
113         if (factory.listeners.size() != 0) {
114          factory = new ModelFactory();
115         }
116         
117         factory.registerListener(new MFListener(models));
118         factory.run(in);
119         
120         assertTrue("Models size must be 2 was = "+models.size(),
121                                              models.size() == 2);
122         mi = models.iterator();
123         temp = (Model)mi.next();
124         
125         assertTrue("First model is Workbook was " + temp.getClass().getName(),
126                     temp instanceof Workbook);
127                     
128         temp = (Model)mi.next();
129         
130         assertTrue("Second model is Sheet was " + temp.getClass().getName(),
131                     temp instanceof Sheet);
132         
133     }
134     
135     /**
136      * Sets up a test file
137      */

138     private ByteArrayOutputStream JavaDoc setupRunFile(HSSFWorkbook book) throws Exception JavaDoc {
139         ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
140         HSSFSheet sheet = book.createSheet("Test");
141         HSSFRow row = sheet.createRow(0);
142         HSSFCell cell = row.createCell((short)0);
143         cell.setCellValue(10.5);
144         book.write(stream);
145         return stream;
146     }
147
148 }
149
150 /**
151  * listener for use in the test
152  */

153 class MFListener implements ModelFactoryListener {
154     private List JavaDoc mlist;
155     public MFListener(List JavaDoc mlist) {
156       this.mlist = mlist;
157     }
158     
159     public boolean process(Model model)
160     {
161         mlist.add(model);
162         return true;
163     }
164     
165     public Iterator JavaDoc models() {
166         return mlist.iterator();
167     }
168
169 }
Popular Tags