001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hpsf.basic;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.File;
022: import java.io.FileFilter;
023: import java.io.FileNotFoundException;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.UnsupportedEncodingException;
027: import java.util.List;
028:
029: import junit.framework.Assert;
030: import junit.framework.TestCase;
031:
032: import org.apache.poi.hpsf.DocumentSummaryInformation;
033: import org.apache.poi.hpsf.HPSFException;
034: import org.apache.poi.hpsf.MarkUnsupportedException;
035: import org.apache.poi.hpsf.NoPropertySetStreamException;
036: import org.apache.poi.hpsf.PropertySet;
037: import org.apache.poi.hpsf.PropertySetFactory;
038: import org.apache.poi.hpsf.Section;
039: import org.apache.poi.hpsf.SummaryInformation;
040: import org.apache.poi.hpsf.wellknown.SectionIDMap;
041:
042: /**
043: * <p>Tests the basic HPSF functionality.</p>
044: *
045: * @author Rainer Klute (klute@rainer-klute.de)
046: * @since 2002-07-20
047: * @version $Id: TestBasic.java 489730 2006-12-22 19:18:16Z bayard $
048: */
049: public class TestBasic extends TestCase {
050:
051: static final String POI_FS = "TestGermanWord90.doc";
052: static final String[] POI_FILES = new String[] {
053: "\005SummaryInformation", "\005DocumentSummaryInformation",
054: "WordDocument", "\001CompObj", "1Table" };
055: static final int BYTE_ORDER = 0xfffe;
056: static final int FORMAT = 0x0000;
057: static final int OS_VERSION = 0x00020A04;
058: static final byte[] CLASS_ID = { (byte) 0x00, (byte) 0x00,
059: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
060: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
061: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
062: (byte) 0x00, (byte) 0x00 };
063: static final int[] SECTION_COUNT = { 1, 2 };
064: static final boolean[] IS_SUMMARY_INFORMATION = { true, false };
065: static final boolean[] IS_DOCUMENT_SUMMARY_INFORMATION = { false,
066: true };
067:
068: POIFile[] poiFiles;
069:
070: /**
071: * <p>Test case constructor.</p>
072: *
073: * @param name The test case's name.
074: */
075: public TestBasic(final String name) {
076: super (name);
077: }
078:
079: /**
080: * <p>Read a the test file from the "data" directory.</p>
081: *
082: * @exception FileNotFoundException if the file to be read does not exist.
083: * @exception IOException if any other I/O exception occurs.
084: */
085: public void setUp() throws FileNotFoundException, IOException {
086: final File dataDir = new File(System
087: .getProperty("HPSF.testdata.path"));
088: final File data = new File(dataDir, POI_FS);
089: poiFiles = Util.readPOIFiles(data);
090: }
091:
092: /**
093: * <p>Checks the names of the files in the POI filesystem. They
094: * are expected to be in a certain order.</p>
095: */
096: public void testReadFiles() {
097: String[] expected = POI_FILES;
098: for (int i = 0; i < expected.length; i++)
099: Assert.assertEquals(poiFiles[i].getName(), expected[i]);
100: }
101:
102: /**
103: * <p>Tests whether property sets can be created from the POI
104: * files in the POI file system. This test case expects the first
105: * file to be a {@link SummaryInformation}, the second file to be
106: * a {@link DocumentSummaryInformation} and the rest to be no
107: * property sets. In the latter cases a {@link
108: * NoPropertySetStreamException} will be thrown when trying to
109: * create a {@link PropertySet}.</p>
110: *
111: * @exception IOException if an I/O exception occurs.
112: *
113: * @exception UnsupportedEncodingException if a character encoding is not
114: * supported.
115: */
116: public void testCreatePropertySets()
117: throws UnsupportedEncodingException, IOException {
118: Class[] expected = new Class[] { SummaryInformation.class,
119: DocumentSummaryInformation.class,
120: NoPropertySetStreamException.class,
121: NoPropertySetStreamException.class,
122: NoPropertySetStreamException.class };
123: for (int i = 0; i < expected.length; i++) {
124: InputStream in = new ByteArrayInputStream(poiFiles[i]
125: .getBytes());
126: Object o;
127: try {
128: o = PropertySetFactory.create(in);
129: } catch (NoPropertySetStreamException ex) {
130: o = ex;
131: } catch (MarkUnsupportedException ex) {
132: o = ex;
133: }
134: in.close();
135: Assert.assertEquals(expected[i], o.getClass());
136: }
137: }
138:
139: /**
140: * <p>Tests the {@link PropertySet} methods. The test file has two
141: * property sets: the first one is a {@link SummaryInformation},
142: * the second one is a {@link DocumentSummaryInformation}.</p>
143: *
144: * @exception IOException if an I/O exception occurs
145: * @exception HPSFException if any HPSF exception occurs
146: */
147: public void testPropertySetMethods() throws IOException,
148: HPSFException {
149: /* Loop over the two property sets. */
150: for (int i = 0; i < 2; i++) {
151: byte[] b = poiFiles[i].getBytes();
152: PropertySet ps = PropertySetFactory
153: .create(new ByteArrayInputStream(b));
154: Assert.assertEquals(ps.getByteOrder(), BYTE_ORDER);
155: Assert.assertEquals(ps.getFormat(), FORMAT);
156: Assert.assertEquals(ps.getOSVersion(), OS_VERSION);
157: Assert.assertEquals(new String(ps.getClassID().getBytes()),
158: new String(CLASS_ID));
159: Assert.assertEquals(ps.getSectionCount(), SECTION_COUNT[i]);
160: Assert.assertEquals(ps.isSummaryInformation(),
161: IS_SUMMARY_INFORMATION[i]);
162: Assert.assertEquals(ps.isDocumentSummaryInformation(),
163: IS_DOCUMENT_SUMMARY_INFORMATION[i]);
164: }
165: }
166:
167: /**
168: * <p>Tests the {@link Section} methods. The test file has two
169: * property sets: the first one is a {@link SummaryInformation},
170: * the second one is a {@link DocumentSummaryInformation}.</p>
171: *
172: * @exception IOException if an I/O exception occurs
173: * @exception HPSFException if any HPSF exception occurs
174: */
175: public void testSectionMethods() throws IOException, HPSFException {
176: final SummaryInformation si = (SummaryInformation) PropertySetFactory
177: .create(new ByteArrayInputStream(poiFiles[0].getBytes()));
178: final List sections = si.getSections();
179: final Section s = (Section) sections.get(0);
180: Assert.assertTrue(org.apache.poi.hpsf.Util.equal(s
181: .getFormatID().getBytes(),
182: SectionIDMap.SUMMARY_INFORMATION_ID));
183: Assert.assertNotNull(s.getProperties());
184: Assert.assertEquals(17, s.getPropertyCount());
185: Assert.assertEquals("Titel", s.getProperty(2));
186: Assert.assertEquals(1748, s.getSize());
187: }
188:
189: /**
190: * <p>This test methods reads all property set streams from all POI
191: * filesystems in the "data" directory.</p>
192: */
193: public void testReadAllFiles() {
194: final File dataDir = new File(System
195: .getProperty("HPSF.testdata.path"));
196: final File[] fileList = dataDir.listFiles(new FileFilter() {
197: public boolean accept(final File f) {
198: return f.isFile();
199: }
200: });
201: try {
202: for (int i = 0; i < fileList.length; i++) {
203: File f = fileList[i];
204: /* Read the POI filesystem's property set streams: */
205: final POIFile[] psf1 = Util.readPropertySets(f);
206:
207: for (int j = 0; j < psf1.length; j++) {
208: final InputStream in = new ByteArrayInputStream(
209: psf1[j].getBytes());
210: PropertySetFactory.create(in);
211: }
212: }
213: } catch (Throwable t) {
214: final String s = org.apache.poi.hpsf.Util.toString(t);
215: fail(s);
216: }
217: }
218:
219: /**
220: * <p>Runs the test cases stand-alone.</p>
221: *
222: * @param args Command-line arguments (ignored)
223: *
224: * @exception Throwable if any sort of exception or error occurs
225: */
226: public static void main(final String[] args) throws Throwable {
227: System.setProperty("HPSF.testdata.path",
228: "./src/testcases/org/apache/poi/hpsf/data");
229: junit.textui.TestRunner.run(TestBasic.class);
230: }
231:
232: }
|