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.FileNotFoundException;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.UnsupportedEncodingException;
026:
027: import junit.framework.Assert;
028: import junit.framework.TestCase;
029:
030: import org.apache.poi.hpsf.DocumentSummaryInformation;
031: import org.apache.poi.hpsf.HPSFException;
032: import org.apache.poi.hpsf.MarkUnsupportedException;
033: import org.apache.poi.hpsf.NoPropertySetStreamException;
034: import org.apache.poi.hpsf.PropertySet;
035: import org.apache.poi.hpsf.PropertySetFactory;
036: import org.apache.poi.hpsf.SummaryInformation;
037: import org.apache.poi.hpsf.Variant;
038:
039: /**
040: * <p>Test case for OLE2 files with empty properties. An empty property's type
041: * is {@link Variant#VT_EMPTY}.</p>
042: *
043: * @author Rainer Klute <a
044: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
045: * @since 2003-07-25
046: * @version $Id: TestEmptyProperties.java 489730 2006-12-22 19:18:16Z bayard $
047: */
048: public class TestEmptyProperties extends TestCase {
049:
050: /**
051: * <p>This test file's summary information stream contains some empty
052: * properties.</p>
053: */
054: static final String POI_FS = "TestCorel.shw";
055:
056: static final String[] POI_FILES = new String[] {
057: "PerfectOffice_MAIN", "\005SummaryInformation", "Main" };
058:
059: POIFile[] poiFiles;
060:
061: /**
062: * <p>Constructor</p>
063: *
064: * @param name The name of the test case
065: */
066: public TestEmptyProperties(final String name) {
067: super (name);
068: }
069:
070: /**
071: * <p>Read a the test file from the "data" directory.</p>
072: *
073: * @exception FileNotFoundException if the file containing the test data
074: * does not exist
075: * @exception IOException if an I/O exception occurs
076: */
077: public void setUp() throws FileNotFoundException, IOException {
078: final File dataDir = new File(System
079: .getProperty("HPSF.testdata.path"));
080: final File data = new File(dataDir, POI_FS);
081:
082: poiFiles = Util.readPOIFiles(data);
083: }
084:
085: /**
086: * <p>Checks the names of the files in the POI filesystem. They
087: * are expected to be in a certain order.</p>
088: */
089: public void testReadFiles() {
090: String[] expected = POI_FILES;
091: for (int i = 0; i < expected.length; i++)
092: Assert.assertEquals(poiFiles[i].getName(), expected[i]);
093: }
094:
095: /**
096: * <p>Tests whether property sets can be created from the POI
097: * files in the POI file system. This test case expects the first
098: * file to be a {@link SummaryInformation}, the second file to be
099: * a {@link DocumentSummaryInformation} and the rest to be no
100: * property sets. In the latter cases a {@link
101: * NoPropertySetStreamException} will be thrown when trying to
102: * create a {@link PropertySet}.</p>
103: *
104: * @exception IOException if an I/O exception occurs.
105: *
106: * @exception UnsupportedEncodingException if a character encoding is not
107: * supported.
108: */
109: public void testCreatePropertySets()
110: throws UnsupportedEncodingException, IOException {
111: Class[] expected = new Class[] {
112: NoPropertySetStreamException.class,
113: SummaryInformation.class,
114: NoPropertySetStreamException.class };
115: for (int i = 0; i < expected.length; i++) {
116: InputStream in = new ByteArrayInputStream(poiFiles[i]
117: .getBytes());
118: Object o;
119: try {
120: o = PropertySetFactory.create(in);
121: } catch (NoPropertySetStreamException ex) {
122: o = ex;
123: } catch (MarkUnsupportedException ex) {
124: o = ex;
125: }
126: in.close();
127: Assert.assertEquals(o.getClass(), expected[i]);
128: }
129: }
130:
131: /**
132: * <p>Tests the {@link PropertySet} methods. The test file has two
133: * property sets: the first one is a {@link SummaryInformation},
134: * the second one is a {@link DocumentSummaryInformation}.</p>
135: *
136: * @exception IOException if an I/O exception occurs
137: * @exception HPSFException if an HPSF operation fails
138: */
139: public void testPropertySetMethods() throws IOException,
140: HPSFException {
141: byte[] b = poiFiles[1].getBytes();
142: PropertySet ps = PropertySetFactory
143: .create(new ByteArrayInputStream(b));
144: SummaryInformation s = (SummaryInformation) ps;
145: assertNull(s.getTitle());
146: assertNull(s.getSubject());
147: assertNotNull(s.getAuthor());
148: assertNull(s.getKeywords());
149: assertNull(s.getComments());
150: assertNotNull(s.getTemplate());
151: assertNotNull(s.getLastAuthor());
152: assertNotNull(s.getRevNumber());
153: assertEquals(s.getEditTime(), 0);
154: assertNull(s.getLastPrinted());
155: assertNull(s.getCreateDateTime());
156: assertNull(s.getLastSaveDateTime());
157: assertEquals(s.getPageCount(), 0);
158: assertEquals(s.getWordCount(), 0);
159: assertEquals(s.getCharCount(), 0);
160: assertNull(s.getThumbnail());
161: assertNull(s.getApplicationName());
162: }
163:
164: /**
165: * <p>Runs the test cases stand-alone.</p>
166: *
167: * @param args the command-line arguments (unused)
168: *
169: * @exception Throwable if any exception or error occurs
170: */
171: public static void main(final String[] args) throws Throwable {
172: System.setProperty("HPSF.testdata.path",
173: "./src/testcases/org/apache/poi/hpsf/data");
174: junit.textui.TestRunner.run(TestBasic.class);
175: }
176:
177: }
|