KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
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 ==================================================================== */

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.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26
27 import junit.framework.Assert;
28 import junit.framework.TestCase;
29
30 import org.apache.poi.hpsf.HPSFException;
31 import org.apache.poi.hpsf.MarkUnsupportedException;
32 import org.apache.poi.hpsf.NoPropertySetStreamException;
33 import org.apache.poi.hpsf.PropertySet;
34 import org.apache.poi.hpsf.PropertySetFactory;
35 import org.apache.poi.hpsf.SummaryInformation;
36
37 /**
38  * <p>Test case for OLE2 files with empty properties. An empty property's type
39  * is {@link Variant.VT_EMPTY}.</p>
40  *
41  * @author Rainer Klute <a
42  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
43  * @since 2003-07-25
44  * @version $Id: TestEmptyProperties.java,v 1.5 2004/06/22 16:15:10 klute Exp $
45  */

46 public class TestEmptyProperties extends TestCase
47 {
48
49     /**
50      * <p>This test file's summary information stream contains some empty
51      * properties.</p>
52      */

53     static final String JavaDoc POI_FS = "TestCorel.shw";
54
55     static final String JavaDoc[] POI_FILES = new String JavaDoc[]
56         {
57             "PerfectOffice_MAIN",
58             "\005SummaryInformation",
59             "Main"
60         };
61
62     POIFile[] poiFiles;
63
64
65
66     /**
67      * <p>Constructor</p>
68      *
69      * @param name The name of the test case
70      */

71     public TestEmptyProperties(final String JavaDoc name)
72     {
73         super(name);
74     }
75
76
77
78     /**
79      * <p>Read a the test file from the "data" directory.</p>
80      *
81      * @exception FileNotFoundException if the file containing the test data
82      * does not exist
83      * @exception IOException if an I/O exception occurs
84      */

85     public void setUp() throws FileNotFoundException JavaDoc, IOException JavaDoc
86     {
87         final File JavaDoc dataDir =
88             new File JavaDoc(System.getProperty("HPSF.testdata.path"));
89         final File JavaDoc data = new File JavaDoc(dataDir, POI_FS);
90
91         poiFiles = Util.readPOIFiles(data);
92     }
93
94
95
96     /**
97      * <p>Checks the names of the files in the POI filesystem. They
98      * are expected to be in a certain order.</p>
99      *
100      * @exception IOException if an I/O exception occurs
101      */

102     public void testReadFiles() throws IOException JavaDoc
103     {
104         String JavaDoc[] expected = POI_FILES;
105         for (int i = 0; i < expected.length; i++)
106             Assert.assertEquals(poiFiles[i].getName(), expected[i]);
107     }
108
109
110
111     /**
112      * <p>Tests whether property sets can be created from the POI
113      * files in the POI file system. This test case expects the first
114      * file to be a {@link SummaryInformation}, the second file to be
115      * a {@link DocumentSummaryInformation} and the rest to be no
116      * property sets. In the latter cases a {@link
117      * NoPropertySetStreamException} will be thrown when trying to
118      * create a {@link PropertySet}.</p>
119      *
120      * @exception IOException if an I/O exception occurs.
121      *
122      * @exception UnsupportedEncodingException if a character encoding is not
123      * supported.
124      */

125     public void testCreatePropertySets()
126     throws UnsupportedEncodingException JavaDoc, IOException JavaDoc
127     {
128         Class JavaDoc[] expected = new Class JavaDoc[]
129             {
130                 NoPropertySetStreamException.class,
131                 SummaryInformation.class,
132                 NoPropertySetStreamException.class
133             };
134         for (int i = 0; i < expected.length; i++)
135         {
136             InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(poiFiles[i].getBytes());
137             Object JavaDoc o;
138             try
139             {
140                 o = PropertySetFactory.create(in);
141             }
142             catch (NoPropertySetStreamException ex)
143             {
144                 o = ex;
145             }
146             catch (MarkUnsupportedException ex)
147             {
148                 o = ex;
149             }
150             in.close();
151             Assert.assertEquals(o.getClass(), expected[i]);
152         }
153     }
154
155
156
157     /**
158      * <p>Tests the {@link PropertySet} methods. The test file has two
159      * property sets: the first one is a {@link SummaryInformation},
160      * the second one is a {@link DocumentSummaryInformation}.</p>
161      *
162      * @exception IOException if an I/O exception occurs
163      * @exception HPSFException if an HPSF operation fails
164      */

165     public void testPropertySetMethods() throws IOException JavaDoc, HPSFException
166     {
167         byte[] b = poiFiles[1].getBytes();
168         PropertySet ps =
169             PropertySetFactory.create(new ByteArrayInputStream JavaDoc(b));
170         SummaryInformation s = (SummaryInformation) ps;
171         assertNull(s.getTitle());
172         assertNull(s.getSubject());
173         assertNotNull(s.getAuthor());
174         assertNull(s.getKeywords());
175         assertNull(s.getComments());
176         assertNotNull(s.getTemplate());
177         assertNotNull(s.getLastAuthor());
178         assertNotNull(s.getRevNumber());
179         assertEquals(s.getEditTime(), 0);
180         assertNull(s.getLastPrinted());
181         assertNull(s.getCreateDateTime());
182         assertNull(s.getLastSaveDateTime());
183         assertEquals(s.getPageCount(), 0);
184         assertEquals(s.getWordCount(), 0);
185         assertEquals(s.getCharCount(), 0);
186         assertNull(s.getThumbnail());
187         assertNull(s.getApplicationName());
188     }
189
190
191
192     /**
193      * <p>Runs the test cases stand-alone.</p>
194      *
195      * @param args the command-line arguments (unused)
196      *
197      * @exception Throwable if any exception or error occurs
198      */

199     public static void main(final String JavaDoc[] args) throws Throwable JavaDoc
200     {
201         System.setProperty("HPSF.testdata.path",
202                            "./src/testcases/org/apache/poi/hpsf/data");
203         junit.textui.TestRunner.run(TestBasic.class);
204     }
205
206 }
207
Popular Tags