1 16 17 package org.apache.poi.hssf.eventmodel; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.OutputStream ; 22 import java.io.InputStream ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 43 public class TestModelFactory extends TestCase 44 { 45 private ModelFactory factory; 46 private HSSFWorkbook book; 47 private InputStream in; 48 private List models; 49 50 54 public TestModelFactory(String 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 ); 60 } 61 62 public static void main(String [] args) 63 { 64 junit.textui.TestRunner.run(TestModelFactory.class); 65 } 66 67 protected void setUp() throws Exception 68 { 69 super.setUp(); 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()) 76 ); 77 in = fs.createDocumentInputStream("Workbook"); 78 } 79 80 protected void tearDown() throws Exception 81 { 82 super.tearDown(); 83 factory = null; 84 book = null; 85 in = null; 86 } 87 88 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 108 public void testRun() 109 { 110 Model temp = null; 111 Iterator 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 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); 144 book.write(stream); 145 return stream; 146 } 147 148 } 149 150 153 class MFListener implements ModelFactoryListener { 154 private List mlist; 155 public MFListener(List 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 models() { 166 return mlist.iterator(); 167 } 168 169 } | Popular Tags |