1 16 17 18 package org.apache.poi.hpsf.basic; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.File ; 22 import java.io.FileFilter ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.UnsupportedEncodingException ; 27 import java.util.List ; 28 29 import junit.framework.Assert; 30 import junit.framework.TestCase; 31 32 import org.apache.poi.hpsf.DocumentSummaryInformation; 33 import org.apache.poi.hpsf.HPSFException; 34 import org.apache.poi.hpsf.MarkUnsupportedException; 35 import org.apache.poi.hpsf.NoPropertySetStreamException; 36 import org.apache.poi.hpsf.PropertySet; 37 import org.apache.poi.hpsf.PropertySetFactory; 38 import org.apache.poi.hpsf.Section; 39 import org.apache.poi.hpsf.SummaryInformation; 40 import org.apache.poi.hpsf.wellknown.SectionIDMap; 41 42 43 44 51 public class TestBasic extends TestCase 52 { 53 54 static final String POI_FS = "TestGermanWord90.doc"; 55 static final String [] POI_FILES = new String [] 56 { 57 "\005SummaryInformation", 58 "\005DocumentSummaryInformation", 59 "WordDocument", 60 "\001CompObj", 61 "1Table" 62 }; 63 static final int BYTE_ORDER = 0xfffe; 64 static final int FORMAT = 0x0000; 65 static final int OS_VERSION = 0x00020A04; 66 static final byte[] CLASS_ID = 67 { 68 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 69 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 70 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 71 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 72 }; 73 static final int[] SECTION_COUNT = 74 {1, 2}; 75 static final boolean[] IS_SUMMARY_INFORMATION = 76 {true, false}; 77 static final boolean[] IS_DOCUMENT_SUMMARY_INFORMATION = 78 {false, true}; 79 80 POIFile[] poiFiles; 81 82 83 84 89 public TestBasic(final String name) 90 { 91 super(name); 92 } 93 94 95 96 102 public void setUp() throws FileNotFoundException , IOException 103 { 104 final File dataDir = 105 new File (System.getProperty("HPSF.testdata.path")); 106 final File data = new File (dataDir, POI_FS); 107 poiFiles = Util.readPOIFiles(data); 108 } 109 110 111 112 118 public void testReadFiles() throws IOException 119 { 120 String [] expected = POI_FILES; 121 for (int i = 0; i < expected.length; i++) 122 Assert.assertEquals(poiFiles[i].getName(), expected[i]); 123 } 124 125 126 127 141 public void testCreatePropertySets() 142 throws UnsupportedEncodingException , IOException 143 { 144 Class [] expected = new Class [] 145 { 146 SummaryInformation.class, 147 DocumentSummaryInformation.class, 148 NoPropertySetStreamException.class, 149 NoPropertySetStreamException.class, 150 NoPropertySetStreamException.class 151 }; 152 for (int i = 0; i < expected.length; i++) 153 { 154 InputStream in = new ByteArrayInputStream (poiFiles[i].getBytes()); 155 Object o; 156 try 157 { 158 o = PropertySetFactory.create(in); 159 } 160 catch (NoPropertySetStreamException ex) 161 { 162 o = ex; 163 } 164 catch (MarkUnsupportedException ex) 165 { 166 o = ex; 167 } 168 in.close(); 169 Assert.assertEquals(o.getClass(), expected[i]); 170 } 171 } 172 173 174 175 183 public void testPropertySetMethods() throws IOException , HPSFException 184 { 185 186 for (int i = 0; i < 2; i++) 187 { 188 byte[] b = poiFiles[i].getBytes(); 189 PropertySet ps = 190 PropertySetFactory.create(new ByteArrayInputStream (b)); 191 Assert.assertEquals(ps.getByteOrder(), BYTE_ORDER); 192 Assert.assertEquals(ps.getFormat(), FORMAT); 193 Assert.assertEquals(ps.getOSVersion(), OS_VERSION); 194 Assert.assertEquals(new String (ps.getClassID().getBytes()), 195 new String (CLASS_ID)); 196 Assert.assertEquals(ps.getSectionCount(), SECTION_COUNT[i]); 197 Assert.assertEquals(ps.isSummaryInformation(), 198 IS_SUMMARY_INFORMATION[i]); 199 Assert.assertEquals(ps.isDocumentSummaryInformation(), 200 IS_DOCUMENT_SUMMARY_INFORMATION[i]); 201 } 202 } 203 204 205 206 214 public void testSectionMethods() throws IOException , HPSFException 215 { 216 final SummaryInformation si = (SummaryInformation) 217 PropertySetFactory.create(new ByteArrayInputStream 218 (poiFiles[0].getBytes())); 219 final List sections = si.getSections(); 220 final Section s = (Section) sections.get(0); 221 Assert.assertTrue(org.apache.poi.hpsf.Util.equal 222 (s.getFormatID().getBytes(), SectionIDMap.SUMMARY_INFORMATION_ID)); 223 Assert.assertNotNull(s.getProperties()); 224 Assert.assertEquals(17, s.getPropertyCount()); 225 Assert.assertEquals("Titel", s.getProperty(2)); 226 Assert.assertEquals(1764, s.getSize()); 227 } 228 229 230 231 235 public void testReadAllFiles() 236 { 237 final File dataDir = 238 new File (System.getProperty("HPSF.testdata.path")); 239 final File [] fileList = dataDir.listFiles(new FileFilter () 240 { 241 public boolean accept(final File f) 242 { 243 return f.isFile(); 244 } 245 }); 246 try 247 { 248 for (int i = 0; i < fileList.length; i++) 249 { 250 File f = fileList[i]; 251 252 final POIFile[] psf1 = Util.readPropertySets(f); 253 254 for (int j = 0; j < psf1.length; j++) 255 { 256 final InputStream in = 257 new ByteArrayInputStream (psf1[j].getBytes()); 258 PropertySetFactory.create(in); 259 } 260 } 261 } 262 catch (Throwable t) 263 { 264 final String s = org.apache.poi.hpsf.Util.toString(t); 265 fail(s); 266 } 267 } 268 269 270 271 278 public static void main(final String [] args) throws Throwable 279 { 280 System.setProperty("HPSF.testdata.path", 281 "./src/testcases/org/apache/poi/hpsf/data"); 282 junit.textui.TestRunner.run(TestBasic.class); 283 } 284 285 } 286 | Popular Tags |