diff --git a/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java b/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java
index 8282c0057..f30dc8be0 100644
--- a/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java
+++ b/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java
@@ -36,7 +36,6 @@ import org.apache.poi.hpsf.MutablePropertySet;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
-import org.apache.poi.hpsf.Util;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
@@ -352,13 +351,11 @@ public class CopyCompare
/* According to the definition of the processPOIFSReaderEvent method
* we cannot pass checked exceptions to the caller. The following
- * lines check whether a checked exception occured and throws an
+ * lines check whether a checked exception occurred and throws an
* unchecked exception. The message of that exception is that of
* the underlying checked exception. */
if (t != null) {
- throw new HPSFRuntimeException
- ("Could not read file \"" + path + "/" + name +
- "\". Reason: " + Util.toString(t));
+ throw new HPSFRuntimeException("Could not read file \"" + path + "/" + name, t);
}
}
diff --git a/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java b/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java
index b5734c043..e5454aa93 100644
--- a/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java
+++ b/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java
@@ -36,7 +36,6 @@ import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
-import org.apache.poi.hpsf.Util;
import org.apache.poi.hpsf.Variant;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
@@ -211,9 +210,7 @@ public class WriteAuthorAndTitle
* unchecked exception. The message of that exception is that of
* the underlying checked exception. */
if (t != null) {
- throw new HPSFRuntimeException
- ("Could not read file \"" + path + "/" + name +
- "\". Reason: " + Util.toString(t));
+ throw new HPSFRuntimeException("Could not read file \"" + path + "/" + name, t);
}
}
diff --git a/src/java/org/apache/poi/hpsf/Filetime.java b/src/java/org/apache/poi/hpsf/Filetime.java
index 2ed36afcc..ba5687a7b 100644
--- a/src/java/org/apache/poi/hpsf/Filetime.java
+++ b/src/java/org/apache/poi/hpsf/Filetime.java
@@ -18,24 +18,40 @@ package org.apache.poi.hpsf;
import java.io.IOException;
import java.io.OutputStream;
+import java.util.Date;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
import org.apache.poi.util.LittleEndianConsts;
-class Filetime {
- private static final int SIZE = LittleEndian.INT_SIZE * 2;
+public class Filetime {
+ /**
+ * The difference between the Windows epoch (1601-01-01
+ * 00:00:00) and the Unix epoch (1970-01-01 00:00:00) in
+ * milliseconds.
+ */
+ private static final long EPOCH_DIFF = -11644473600000L;
+ private static final int SIZE = LittleEndian.INT_SIZE * 2;
+ private static final long UINT_MASK = 0x00000000FFFFFFFFL;
+ private static final long NANO_100 = 1000L * 10L;
+
private int _dwHighDateTime;
private int _dwLowDateTime;
-
- Filetime() {}
+ Filetime() {}
+
Filetime( int low, int high ) {
_dwLowDateTime = low;
_dwHighDateTime = high;
}
+ Filetime( Date date ) {
+ long filetime = Filetime.dateToFileTime(date);
+ _dwHighDateTime = (int) ((filetime >>> 32) & UINT_MASK);
+ _dwLowDateTime = (int) (filetime & UINT_MASK);
+ }
+
void read( LittleEndianByteArrayInputStream lei ) {
_dwLowDateTime = lei.readInt();
@@ -53,8 +69,7 @@ class Filetime {
byte[] toByteArray() {
byte[] result = new byte[SIZE];
LittleEndian.putInt( result, 0 * LittleEndianConsts.INT_SIZE, _dwLowDateTime );
- LittleEndian
- .putInt( result, 1 * LittleEndianConsts.INT_SIZE, _dwHighDateTime );
+ LittleEndian.putInt( result, 1 * LittleEndianConsts.INT_SIZE, _dwHighDateTime );
return result;
}
@@ -63,4 +78,49 @@ class Filetime {
LittleEndian.putInt( _dwHighDateTime, out );
return SIZE;
}
+
+ Date getJavaValue() {
+ long l = (((long)_dwHighDateTime) << 32) | (_dwLowDateTime & UINT_MASK);
+ return filetimeToDate( l );
+ }
+
+ /**
+ * Converts a Windows FILETIME into a {@link Date}. The Windows
+ * FILETIME structure holds a date and time associated with a
+ * file. The structure identifies a 64-bit integer specifying the
+ * number of 100-nanosecond intervals which have passed since
+ * January 1, 1601.
+ *
+ * @param filetime The filetime to convert.
+ * @return The Windows FILETIME as a {@link Date}.
+ */
+ public static Date filetimeToDate(final long filetime) {
+ final long ms_since_16010101 = filetime / NANO_100;
+ final long ms_since_19700101 = ms_since_16010101 + EPOCH_DIFF;
+ return new Date(ms_since_19700101);
+ }
+
+ /**
+ * Converts a {@link Date} into a filetime.
+ *
+ * @param date The date to be converted
+ * @return The filetime
+ *
+ * @see #filetimeToDate(long)
+ */
+ public static long dateToFileTime(final Date date) {
+ long ms_since_19700101 = date.getTime();
+ long ms_since_16010101 = ms_since_19700101 - EPOCH_DIFF;
+ return ms_since_16010101 * NANO_100;
+ }
+
+ /**
+ * Return {@code true} if the date is undefined
+ *
+ * @param date the date
+ * @return {@code true} if the date is undefined
+ */
+ public static boolean isUndefined(Date date) {
+ return (date == null || dateToFileTime(date) == 0);
+ }
}
diff --git a/src/java/org/apache/poi/hpsf/Property.java b/src/java/org/apache/poi/hpsf/Property.java
index 60a46b804..e05a06dac 100644
--- a/src/java/org/apache/poi/hpsf/Property.java
+++ b/src/java/org/apache/poi/hpsf/Property.java
@@ -22,7 +22,11 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
-import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Locale;
+import java.util.concurrent.TimeUnit;
+
+import javax.xml.bind.DatatypeConverter;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.util.CodePageUtil;
@@ -377,15 +381,18 @@ public class Property {
*/
@Override
public String toString() {
- return toString(Property.DEFAULT_CODEPAGE);
+ return toString(Property.DEFAULT_CODEPAGE, null);
}
- public String toString(int codepage) {
- final StringBuffer b = new StringBuffer();
+ public String toString(int codepage, PropertyIDMap idMap) {
+ final StringBuilder b = new StringBuilder();
b.append("Property[");
b.append("id: ");
- b.append(getID());
- String idName = getNameFromID();
+ b.append(id);
+ String idName = (idMap == null) ? null : idMap.get(id);
+ if (idName == null) {
+ idName = PropertyIDMap.getFallbackProperties().get(id);
+ }
if (idName != null) {
b.append(" (");
b.append(idName);
@@ -399,6 +406,7 @@ public class Property {
final Object value = getValue();
b.append(", value: ");
if (value instanceof String) {
+ b.append((String)value);
b.append("\n");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
@@ -407,13 +415,11 @@ public class Property {
LOG.log(POILogger.WARN, "can't serialize string", e);
}
- b.append(" [");
// skip length field
if(bos.size() > 2*LittleEndianConsts.INT_SIZE) {
final String hex = HexDump.dump(bos.toByteArray(), -2*LittleEndianConsts.INT_SIZE, 2*LittleEndianConsts.INT_SIZE);
b.append(hex);
}
- b.append("]");
} else if (value instanceof byte[]) {
b.append("\n");
byte[] bytes = (byte[])value;
@@ -421,7 +427,32 @@ public class Property {
String hex = HexDump.dump(bytes, 0L, 0);
b.append(hex);
}
- } else if (type == Variant.VT_EMPTY || type == Variant.VT_NULL) {
+ } else if (value instanceof java.util.Date) {
+ java.util.Date d = (java.util.Date)value;
+ long filetime = Filetime.dateToFileTime(d);
+ if (Filetime.isUndefined(d)) {
+ b.append(" Provides various static utility methods. The difference between the Windows epoch (1601-01-01
- * 00:00:00) and the Unix epoch (1970-01-01 00:00:00) in
- * milliseconds: 11644473600000L. (Use your favorite spreadsheet
- * program to verify the correctness of this value. By the way,
- * did you notice that you can tell from the epochs which
- * operating system is the modern one? :-)) Converts a Windows FILETIME into a {@link Date}. The Windows
- * FILETIME structure holds a date and time associated with a
- * file. The structure identifies a 64-bit integer specifying the
- * number of 100-nanosecond intervals which have passed since
- * January 1, 1601. This 64-bit value is split into the two double
- * words stored in the structure. Converts a Windows FILETIME into a {@link Date}. The Windows
- * FILETIME structure holds a date and time associated with a
- * file. The structure identifies a 64-bit integer specifying the
- * number of 100-nanosecond intervals which have passed since
- * January 1, 1601. Converts a {@link Date} into a filetime. Pads a byte array with 0x00 bytes so that its length is a multiple of
- * 4. Returns a textual representation of a {@link Throwable}, including a
- * stacktrace.
*
- *
*
@@ -695,7 +688,9 @@ public class Section {
* @param id The ID of the property to be removed
*/
public void removeProperty(final long id) {
- dirty |= (properties.remove(id) != null);
+ if (properties.remove(id) != null) {
+ sectionBytes.reset();
+ }
}
/**
@@ -716,9 +711,9 @@ public class Section {
public int write(final OutputStream out) throws WritingNotSupportedException, IOException {
/* Check whether we have already generated the bytes making out the
* section. */
- if (!dirty && sectionBytes != null) {
- out.write(sectionBytes);
- return sectionBytes.length;
+ if (sectionBytes.size() > 0) {
+ sectionBytes.writeTo(out);
+ return sectionBytes.size();
}
/* Writing the section's dictionary it tricky. If there is a dictionary
@@ -971,6 +966,10 @@ public class Section {
*/
@Override
public String toString() {
+ return toString(null);
+ }
+
+ public String toString(PropertyIDMap idMap) {
final StringBuffer b = new StringBuffer();
final Property[] pa = getProperties();
b.append("\n\n\n");
@@ -990,7 +989,7 @@ public class Section {
codepage = Property.DEFAULT_CODEPAGE;
}
for (Property p : pa) {
- b.append(p.toString(codepage));
+ b.append(p.toString(codepage, idMap));
b.append(",\n");
}
b.append(']');
diff --git a/src/java/org/apache/poi/hpsf/SummaryInformation.java b/src/java/org/apache/poi/hpsf/SummaryInformation.java
index f59bd4872..8d9e43179 100644
--- a/src/java/org/apache/poi/hpsf/SummaryInformation.java
+++ b/src/java/org/apache/poi/hpsf/SummaryInformation.java
@@ -351,7 +351,7 @@ public final class SummaryInformation extends SpecialPropertySet {
if (d == null) {
return 0;
}
- return Util.dateToFileTime(d);
+ return Filetime.dateToFileTime(d);
}
@@ -362,7 +362,7 @@ public final class SummaryInformation extends SpecialPropertySet {
* @param time The time to set.
*/
public void setEditTime(final long time) {
- final Date d = Util.filetimeToDate(time);
+ final Date d = Filetime.filetimeToDate(time);
getFirstSection().setProperty(PropertyIDMap.PID_EDITTIME, Variant.VT_FILETIME, d);
}
diff --git a/src/java/org/apache/poi/hpsf/Util.java b/src/java/org/apache/poi/hpsf/Util.java
deleted file mode 100644
index d18fe811b..000000000
--- a/src/java/org/apache/poi/hpsf/Util.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-package org.apache.poi.hpsf;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.Date;
-
-import org.apache.poi.util.Internal;
-import org.apache.poi.util.SuppressForbidden;
-
-/**
- * t.printStacktrace()
.
- */
- @SuppressForbidden("uses printStackTrace")
- public static String toString(final Throwable t)
- {
- final StringWriter sw = new StringWriter();
- final PrintWriter pw = new PrintWriter(sw);
- t.printStackTrace(pw);
- pw.close();
- try
- {
- sw.close();
- return sw.toString();
- }
- catch (IOException e)
- {
- final StringBuffer b = new StringBuffer(t.getMessage());
- b.append("\n");
- b.append("Could not create a stacktrace. Reason: ");
- b.append(e.getMessage());
- return b.toString();
- }
- }
-
-}
diff --git a/src/java/org/apache/poi/hpsf/VariantSupport.java b/src/java/org/apache/poi/hpsf/VariantSupport.java
index 8c351cc39..61e6bb8db 100644
--- a/src/java/org/apache/poi/hpsf/VariantSupport.java
+++ b/src/java/org/apache/poi/hpsf/VariantSupport.java
@@ -207,7 +207,7 @@ public class VariantSupport extends Variant {
case Variant.VT_FILETIME:
Filetime filetime = (Filetime) typedPropertyValue.getValue();
- return Util.filetimeToDate( (int) filetime.getHigh(), (int) filetime.getLow() );
+ return filetime.getJavaValue();
case Variant.VT_LPSTR:
CodePageString cpString = (CodePageString) typedPropertyValue.getValue();
@@ -413,13 +413,8 @@ public class VariantSupport extends Variant {
break;
case Variant.VT_FILETIME:
- if (value instanceof Date) {
- long filetime = Util.dateToFileTime((Date) value);
- int high = (int) ((filetime >> 32) & 0x00000000FFFFFFFFL);
- int low = (int) (filetime & 0x00000000FFFFFFFFL);
- Filetime filetimeValue = new Filetime( low, high);
- length = filetimeValue.write( out );
- }
+ Filetime filetimeValue = (value instanceof Date) ? new Filetime((Date)value) : new Filetime();
+ length = filetimeValue.write( out );
break;
default:
diff --git a/src/java/org/apache/poi/hpsf/wellknown/PropertyIDMap.java b/src/java/org/apache/poi/hpsf/wellknown/PropertyIDMap.java
index 4a7da335d..b3aaa87a3 100644
--- a/src/java/org/apache/poi/hpsf/wellknown/PropertyIDMap.java
+++ b/src/java/org/apache/poi/hpsf/wellknown/PropertyIDMap.java
@@ -17,9 +17,14 @@
package org.apache.poi.hpsf.wellknown;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
+
+import org.apache.poi.hpsf.DocumentSummaryInformation;
+import org.apache.poi.hpsf.SummaryInformation;
/**
* This is a dictionary which maps property ID values to property
@@ -31,7 +36,7 @@ import java.util.Map;
* should treat them as unmodifiable, copy them and modifiy the
* copies.
*/
-public class PropertyIDMap extends HashMapnull
if there was no mapping for
- * key.
- */
- public Object put(final long id, final String idString)
- {
- return put(Long.valueOf(id), idString);
- }
-
-
-
- /**
- * Gets the ID string for an ID from the {@link
- * PropertyIDMap}.
- *
- * @param id The ID.
- * @return The ID string associated with id.
- */
- public Object get(final long id)
- {
- return get(Long.valueOf(id));
- }
-
-
-
/**
* @return the Summary Information properties singleton
*/
- public static synchronized PropertyIDMap getSummaryInformationProperties()
- {
- if (summaryInformationProperties == null)
- {
- PropertyIDMap m = new PropertyIDMap(18, (float) 1.0);
- m.put(PID_TITLE, "PID_TITLE");
- m.put(PID_SUBJECT, "PID_SUBJECT");
- m.put(PID_AUTHOR, "PID_AUTHOR");
- m.put(PID_KEYWORDS, "PID_KEYWORDS");
- m.put(PID_COMMENTS, "PID_COMMENTS");
- m.put(PID_TEMPLATE, "PID_TEMPLATE");
- m.put(PID_LASTAUTHOR, "PID_LASTAUTHOR");
- m.put(PID_REVNUMBER, "PID_REVNUMBER");
- m.put(PID_EDITTIME, "PID_EDITTIME");
- m.put(PID_LASTPRINTED, "PID_LASTPRINTED");
- m.put(PID_CREATE_DTM, "PID_CREATE_DTM");
- m.put(PID_LASTSAVE_DTM, "PID_LASTSAVE_DTM");
- m.put(PID_PAGECOUNT, "PID_PAGECOUNT");
- m.put(PID_WORDCOUNT, "PID_WORDCOUNT");
- m.put(PID_CHARCOUNT, "PID_CHARCOUNT");
- m.put(PID_THUMBNAIL, "PID_THUMBNAIL");
- m.put(PID_APPNAME, "PID_APPNAME");
- m.put(PID_SECURITY, "PID_SECURITY");
- summaryInformationProperties =
- new PropertyIDMap(Collections.unmodifiableMap(m));
+ public static synchronized PropertyIDMap getSummaryInformationProperties() {
+ if (summaryInformationProperties == null) {
+ summaryInformationProperties = new PropertyIDMap(summaryInformationIdValues);
}
return summaryInformationProperties;
}
-
-
/**
- * Returns the Document Summary Information properties
- * singleton.
- *
* @return The Document Summary Information properties singleton.
*/
- public static synchronized PropertyIDMap getDocumentSummaryInformationProperties()
- {
- if (documentSummaryInformationProperties == null)
- {
- PropertyIDMap m = new PropertyIDMap(17, (float) 1.0);
- m.put(PID_DICTIONARY, "PID_DICTIONARY");
- m.put(PID_CODEPAGE, "PID_CODEPAGE");
- m.put(PID_CATEGORY, "PID_CATEGORY");
- m.put(PID_PRESFORMAT, "PID_PRESFORMAT");
- m.put(PID_BYTECOUNT, "PID_BYTECOUNT");
- m.put(PID_LINECOUNT, "PID_LINECOUNT");
- m.put(PID_PARCOUNT, "PID_PARCOUNT");
- m.put(PID_SLIDECOUNT, "PID_SLIDECOUNT");
- m.put(PID_NOTECOUNT, "PID_NOTECOUNT");
- m.put(PID_HIDDENCOUNT, "PID_HIDDENCOUNT");
- m.put(PID_MMCLIPCOUNT, "PID_MMCLIPCOUNT");
- m.put(PID_SCALE, "PID_SCALE");
- m.put(PID_HEADINGPAIR, "PID_HEADINGPAIR");
- m.put(PID_DOCPARTS, "PID_DOCPARTS");
- m.put(PID_MANAGER, "PID_MANAGER");
- m.put(PID_COMPANY, "PID_COMPANY");
- m.put(PID_LINKSDIRTY, "PID_LINKSDIRTY");
- documentSummaryInformationProperties =
- new PropertyIDMap(Collections.unmodifiableMap(m));
+ public static synchronized PropertyIDMap getDocumentSummaryInformationProperties() {
+ if (documentSummaryInformationProperties == null) {
+ documentSummaryInformationProperties = new PropertyIDMap(documentSummaryInformationIdValues);
}
return documentSummaryInformationProperties;
}
+ /**
+ * Returns a property map, which is only used as a fallback, i.e. if available, the correct map
+ * for {@link DocumentSummaryInformation} or {@link SummaryInformation} should be used.
+ */
+ public static synchronized PropertyIDMap getFallbackProperties() {
+ if (fallbackProperties == null) {
+ fallbackProperties = new PropertyIDMap(fallbackIdValues);
+ }
+ return fallbackProperties;
+ }
+
+ @Override
+ public int size() {
+ return idMap.size();
+ }
+ @Override
+ public boolean isEmpty() {
+ return idMap.isEmpty();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return idMap.containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ return idMap.containsValue(value);
+ }
+
+ @Override
+ public String get(Object key) {
+ return idMap.get(key);
+ }
+
+ @Override
+ public String put(Long key, String value) {
+ return idMap.put(key, value);
+ }
+
+ @Override
+ public String remove(Object key) {
+ return idMap.remove(key);
+ }
+
+ @Override
+ public void putAll(Map extends Long, ? extends String> m) {
+ idMap.putAll(m);
+ }
+
+ @Override
+ public void clear() {
+ idMap.clear();
+ }
+
+ @Override
+ public Set
Test case for OLE2 files with empty properties. An empty property's type - * is {@link Variant#VT_EMPTY}.
+ * Test case for OLE2 files with empty properties. + * An empty property's type is {@link Variant#VT_EMPTY}. */ -public final class TestEmptyProperties extends TestCase { +public final class TestEmptyProperties { + + private static final POIDataSamples samples = POIDataSamples.getHPSFInstance(); /** - *This test file's summary information stream contains some empty - * properties.
+ * This test file's summary information stream contains some empty properties. */ private static final String POI_FS = "TestCorel.shw"; - private static final String[] POI_FILES = new String[] - { - "PerfectOffice_MAIN", - "\005SummaryInformation", - "Main" - }; + private static final String[] POI_FILES = { + "PerfectOffice_MAIN", + "\005SummaryInformation", + "Main" + }; - private POIFile[] poiFiles; + private ListRead a the test file from the "data" directory.
@@ -64,24 +69,21 @@ public final class TestEmptyProperties extends TestCase { * does not exist * @exception IOException if an I/O exception occurs */ - @Override - public void setUp() throws FileNotFoundException, IOException - { - POIDataSamples samples = POIDataSamples.getHPSFInstance(); + @Before + public void setUp() throws IOException { final File data = samples.getFile(POI_FS); - poiFiles = Util.readPOIFiles(data); } /** - *Checks the names of the files in the POI filesystem. They - * are expected to be in a certain order.
+ * Checks the names of the files in the POI filesystem. They + * are expected to be in a certain order. */ - public void testReadFiles() - { + @Test + public void testReadFiles() { String[] expected = POI_FILES; for (int i = 0; i < expected.length; i++) - assertEquals(poiFiles[i].getName(), expected[i]); + assertEquals(poiFiles.get(i).getName(), expected[i]); } /** @@ -98,29 +100,22 @@ public final class TestEmptyProperties extends TestCase { * @exception UnsupportedEncodingException if a character encoding is not * supported. */ + @Test public void testCreatePropertySets() - throws UnsupportedEncodingException, IOException - { - Class>[] expected = new Class[] - { - NoPropertySetStreamException.class, - SummaryInformation.class, - NoPropertySetStreamException.class - }; - for (int i = 0; i < expected.length; i++) - { - InputStream in = new ByteArrayInputStream(poiFiles[i].getBytes()); + throws UnsupportedEncodingException, IOException { + Class>[] expected = { + NoPropertySetStreamException.class, + SummaryInformation.class, + NoPropertySetStreamException.class + }; + for (int i = 0; i < expected.length; i++) { + InputStream in = new ByteArrayInputStream(poiFiles.get(i).getBytes()); Object o; - try - { + try { o = PropertySetFactory.create(in); - } - catch (NoPropertySetStreamException ex) - { + } catch (NoPropertySetStreamException ex) { o = ex; - } - catch (MarkUnsupportedException ex) - { + } catch (MarkUnsupportedException ex) { o = ex; } in.close(); @@ -136,11 +131,10 @@ public final class TestEmptyProperties extends TestCase { * @exception IOException if an I/O exception occurs * @exception HPSFException if an HPSF operation fails */ - public void testPropertySetMethods() throws IOException, HPSFException - { - byte[] b = poiFiles[1].getBytes(); - PropertySet ps = - PropertySetFactory.create(new ByteArrayInputStream(b)); + @Test + public void testPropertySetMethods() throws IOException, HPSFException { + byte[] b = poiFiles.get(1).getBytes(); + PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b)); SummaryInformation s = (SummaryInformation) ps; assertNull(s.getTitle()); assertNull(s.getSubject()); diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestUnicode.java b/src/testcases/org/apache/poi/hpsf/basic/TestUnicode.java index 6f7e6db1c..69e4367bb 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestUnicode.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestUnicode.java @@ -17,13 +17,14 @@ package org.apache.poi.hpsf.basic; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import junit.framework.TestCase; - import org.apache.poi.POIDataSamples; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.HPSFException; @@ -32,30 +33,30 @@ import org.apache.poi.hpsf.PropertySetFactory; import org.apache.poi.hpsf.Section; import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.util.CodePageUtil; +import org.junit.Before; +import org.junit.Test; /** - *Tests whether Unicode string can be read from a - * DocumentSummaryInformation.
+ * Tests whether Unicode string can be read from a DocumentSummaryInformation. */ -public class TestUnicode extends TestCase { +public class TestUnicode { static final String POI_FS = "TestUnicode.xls"; - static final String[] POI_FILES = new String[] - { - "\005DocumentSummaryInformation", - }; + static final String[] POI_FILES = { + "\005DocumentSummaryInformation", + }; File data; POIFile[] poiFiles; /** - *Read a the test file from the "data" directory.
+ * Read a the test file from the "data" directory. * * @exception FileNotFoundException if the file to be read does not exist. * @exception IOException if any other I/O exception occurs */ - @Override - protected void setUp() { + @Before + public void setUp() { POIDataSamples samples = POIDataSamples.getHPSFInstance(); data = samples.getFile(POI_FS); } @@ -63,31 +64,25 @@ public class TestUnicode extends TestCase { /** - *Tests the {@link PropertySet} methods. The test file has two + * Tests the {@link PropertySet} methods. The test file has two * property set: the first one is a {@link SummaryInformation}, - * the second one is a {@link DocumentSummaryInformation}.
+ * the second one is a {@link DocumentSummaryInformation}. * * @exception IOException if an I/O exception occurs * @exception HPSFException if an HPSF exception occurs */ - public void testPropertySetMethods() throws IOException, HPSFException - { - POIFile poiFile = Util.readPOIFiles(data, POI_FILES)[0]; + @Test + public void testPropertySetMethods() throws IOException, HPSFException { + POIFile poiFile = Util.readPOIFiles(data, POI_FILES).get(0); byte[] b = poiFile.getBytes(); - PropertySet ps = - PropertySetFactory.create(new ByteArrayInputStream(b)); + PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b)); assertTrue(ps.isDocumentSummaryInformation()); assertEquals(ps.getSectionCount(), 2); Section s = ps.getSections().get(1); - assertEquals(s.getProperty(1), - Integer.valueOf(CodePageUtil.CP_UTF16)); - assertEquals(s.getProperty(2), - Integer.valueOf(-96070278)); - assertEquals(s.getProperty(3), - "MCon_Info zu Office bei Schreiner"); - assertEquals(s.getProperty(4), - "petrovitsch@schreiner-online.de"); - assertEquals(s.getProperty(5), - "Petrovitsch, Wilhelm"); + assertEquals(s.getProperty(1), CodePageUtil.CP_UTF16); + assertEquals(s.getProperty(2), -96070278); + assertEquals(s.getProperty(3), "MCon_Info zu Office bei Schreiner"); + assertEquals(s.getProperty(4), "petrovitsch@schreiner-online.de"); + assertEquals(s.getProperty(5), "Petrovitsch, Wilhelm"); } } diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java index 43737a3b0..4ef331f09 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java @@ -237,7 +237,7 @@ public class TestWrite { try { psa[0] = PropertySetFactory.create(event.getStream()); } catch (Exception ex) { - fail(org.apache.poi.hpsf.Util.toString(ex)); + fail(ex.getMessage()); } }}, SummaryInformation.DEFAULT_STREAM_NAME @@ -340,7 +340,7 @@ public class TestWrite { try { PropertySetFactory.create(event.getStream()); } catch (Exception ex) { - fail(org.apache.poi.hpsf.Util.toString(ex)); + fail(ex.getMessage()); } } } diff --git a/src/testcases/org/apache/poi/hpsf/basic/Util.java b/src/testcases/org/apache/poi/hpsf/basic/Util.java index 40b4e229d..cc489d853 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/Util.java +++ b/src/testcases/org/apache/poi/hpsf/basic/Util.java @@ -18,17 +18,13 @@ package org.apache.poi.hpsf.basic; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; import java.util.List; -import java.util.Properties; import org.apache.poi.hpsf.PropertySet; import org.apache.poi.poifs.eventfilesystem.POIFSReader; @@ -43,31 +39,6 @@ import org.apache.poi.util.IOUtils; */ final class Util { - /** - *Reads all files from a POI filesystem and returns them as an - * array of {@link POIFile} instances. This method loads all files - * into memory and thus does not cope well with large POI - * filessystems.
- * - * @param poiFs The name of the POI filesystem as seen by the - * operating system. (This is the "filename".) - * - * @return The POI files. The elements are ordered in the same way - * as the files in the POI filesystem. - * - * @exception FileNotFoundException if the file containing the POI - * filesystem does not exist - * - * @exception IOException if an I/O exception occurs - */ - public static POIFile[] readPOIFiles(final File poiFs) - throws FileNotFoundException, IOException - { - return readPOIFiles(poiFs, null); - } - - - /** *Reads a set of files from a POI filesystem and returns them
* as an array of {@link POIFile} instances. This method loads all
@@ -87,42 +58,34 @@ final class Util {
*
* @exception IOException if an I/O exception occurs
*/
- public static POIFile[] readPOIFiles(final File poiFs,
- final String[] poiFiles)
- throws FileNotFoundException, IOException
- {
+ public static List Prints the system properties to System.out.