KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hpsf > basic > TestBasic


1 /* ====================================================================
2    Copyright 2002-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
18 package org.apache.poi.hpsf.basic;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileFilter JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.util.List JavaDoc;
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 /**
45  * <p>Tests the basic HPSF functionality.</p>
46  *
47  * @author Rainer Klute (klute@rainer-klute.de)
48  * @since 2002-07-20
49  * @version $Id: TestBasic.java,v 1.12 2004/06/22 16:15:10 klute Exp $
50  */

51 public class TestBasic extends TestCase
52 {
53
54     static final String JavaDoc POI_FS = "TestGermanWord90.doc";
55     static final String JavaDoc[] POI_FILES = new String JavaDoc[]
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     /**
85      * <p>Test case constructor.</p>
86      *
87      * @param name The test case's name.
88      */

89     public TestBasic(final String JavaDoc name)
90     {
91         super(name);
92     }
93
94
95
96     /**
97      * <p>Read a the test file from the "data" directory.</p>
98      *
99      * @exception FileNotFoundException if the file to be read does not exist.
100      * @exception IOException if any other I/O exception occurs.
101      */

102     public void setUp() throws FileNotFoundException JavaDoc, IOException JavaDoc
103     {
104         final File JavaDoc dataDir =
105             new File JavaDoc(System.getProperty("HPSF.testdata.path"));
106         final File JavaDoc data = new File JavaDoc(dataDir, POI_FS);
107         poiFiles = Util.readPOIFiles(data);
108     }
109
110
111
112     /**
113      * <p>Checks the names of the files in the POI filesystem. They
114      * are expected to be in a certain order.</p>
115      *
116      * @exception IOException if an I/O exception occurs
117      */

118     public void testReadFiles() throws IOException JavaDoc
119     {
120         String JavaDoc[] 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     /**
128      * <p>Tests whether property sets can be created from the POI
129      * files in the POI file system. This test case expects the first
130      * file to be a {@link SummaryInformation}, the second file to be
131      * a {@link DocumentSummaryInformation} and the rest to be no
132      * property sets. In the latter cases a {@link
133      * NoPropertySetStreamException} will be thrown when trying to
134      * create a {@link PropertySet}.</p>
135      *
136      * @exception IOException if an I/O exception occurs.
137      *
138      * @exception UnsupportedEncodingException if a character encoding is not
139      * supported.
140      */

141     public void testCreatePropertySets()
142     throws UnsupportedEncodingException JavaDoc, IOException JavaDoc
143     {
144         Class JavaDoc[] expected = new Class JavaDoc[]
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 JavaDoc in = new ByteArrayInputStream JavaDoc(poiFiles[i].getBytes());
155             Object JavaDoc 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     /**
176      * <p>Tests the {@link PropertySet} methods. The test file has two
177      * property sets: the first one is a {@link SummaryInformation},
178      * the second one is a {@link DocumentSummaryInformation}.</p>
179      *
180      * @exception IOException if an I/O exception occurs
181      * @exception HPSFException if any HPSF exception occurs
182      */

183     public void testPropertySetMethods() throws IOException JavaDoc, HPSFException
184     {
185         /* Loop over the two property sets. */
186         for (int i = 0; i < 2; i++)
187         {
188             byte[] b = poiFiles[i].getBytes();
189             PropertySet ps =
190                 PropertySetFactory.create(new ByteArrayInputStream JavaDoc(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 JavaDoc(ps.getClassID().getBytes()),
195                                 new String JavaDoc(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     /**
207      * <p>Tests the {@link Section} methods. The test file has two
208      * property sets: the first one is a {@link SummaryInformation},
209      * the second one is a {@link DocumentSummaryInformation}.</p>
210      *
211      * @exception IOException if an I/O exception occurs
212      * @exception HPSFException if any HPSF exception occurs
213      */

214     public void testSectionMethods() throws IOException JavaDoc, HPSFException
215     {
216         final SummaryInformation si = (SummaryInformation)
217             PropertySetFactory.create(new ByteArrayInputStream JavaDoc
218                 (poiFiles[0].getBytes()));
219         final List JavaDoc 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     /**
232      * <p>This test methods reads all property set streams from all POI
233      * filesystems in the "data" directory.</p>
234      */

235     public void testReadAllFiles()
236     {
237         final File JavaDoc dataDir =
238             new File JavaDoc(System.getProperty("HPSF.testdata.path"));
239         final File JavaDoc[] fileList = dataDir.listFiles(new FileFilter JavaDoc()
240             {
241                 public boolean accept(final File JavaDoc f)
242                 {
243                     return f.isFile();
244                 }
245             });
246         try
247         {
248             for (int i = 0; i < fileList.length; i++)
249             {
250                 File JavaDoc f = fileList[i];
251                 /* Read the POI filesystem's property set streams: */
252                 final POIFile[] psf1 = Util.readPropertySets(f);
253
254                 for (int j = 0; j < psf1.length; j++)
255                 {
256                     final InputStream JavaDoc in =
257                         new ByteArrayInputStream JavaDoc(psf1[j].getBytes());
258                     PropertySetFactory.create(in);
259                 }
260             }
261         }
262         catch (Throwable JavaDoc t)
263         {
264             final String JavaDoc s = org.apache.poi.hpsf.Util.toString(t);
265             fail(s);
266         }
267     }
268
269
270
271     /**
272      * <p>Runs the test cases stand-alone.</p>
273      *
274      * @param args Command-line arguments (ignored)
275      *
276      * @exception Throwable if any sort of exception or error occurs
277      */

278     public static void main(final String JavaDoc[] args) throws Throwable JavaDoc
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