mirror of https://github.com/apache/poi.git
Sonar fixes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872092 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a0770034fc
commit
71338b958c
|
@ -345,6 +345,8 @@ public class TestBuildFile {
|
||||||
@Test
|
@Test
|
||||||
public void testPassOnError() {
|
public void testPassOnError() {
|
||||||
executeTarget("test-passonerror");
|
executeTarget("test-passonerror");
|
||||||
|
assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls");
|
||||||
|
assertLogContaining("Test named failonerror failed because 1 of 0 evaluations failed to evaluate correctly.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -17,34 +17,29 @@
|
||||||
|
|
||||||
package org.apache.poi;
|
package org.apache.poi;
|
||||||
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.hamcrest.CoreMatchers.endsWith;
|
||||||
|
import static org.hamcrest.CoreMatchers.hasItem;
|
||||||
|
import static org.hamcrest.CoreMatchers.not;
|
||||||
|
import static org.hamcrest.CoreMatchers.startsWith;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
import static org.junit.Assume.assumeTrue;
|
import static org.junit.Assume.assumeTrue;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
|
||||||
import static org.hamcrest.CoreMatchers.startsWith;
|
|
||||||
import static org.hamcrest.CoreMatchers.endsWith;
|
|
||||||
import static org.hamcrest.CoreMatchers.not;
|
|
||||||
import static org.hamcrest.CoreMatchers.hasItem;
|
|
||||||
|
|
||||||
import java.lang.reflect.AccessibleObject;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.security.AccessController;
|
import java.security.AccessController;
|
||||||
import java.security.PrivilegedActionException;
|
import java.security.PrivilegedActionException;
|
||||||
import java.security.PrivilegedExceptionAction;
|
import java.security.PrivilegedExceptionAction;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.poi.util.SuppressForbidden;
|
|
||||||
import org.apache.poi.util.Internal;
|
import org.apache.poi.util.Internal;
|
||||||
|
import org.apache.poi.util.SuppressForbidden;
|
||||||
|
import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Util class for POI JUnit TestCases, which provide additional features
|
* Util class for POI JUnit TestCases, which provide additional features
|
||||||
|
@ -169,46 +164,10 @@ public final class POITestCase {
|
||||||
* Only use this method in test cases!!!
|
* Only use this method in test cases!!!
|
||||||
*/
|
*/
|
||||||
public static void assertReflectEquals(final Object expected, Object actual) throws Exception {
|
public static void assertReflectEquals(final Object expected, Object actual) throws Exception {
|
||||||
final List<Field> fields;
|
// as long as ReflectionEquals is provided by Mockito, use it ... otherwise use commons.lang for the tests
|
||||||
try {
|
|
||||||
fields = AccessController.doPrivileged(new PrivilegedExceptionAction<List<Field>>() {
|
|
||||||
@Override
|
|
||||||
@SuppressForbidden("Test only")
|
|
||||||
public List<Field> run() throws Exception {
|
|
||||||
List<Field> flds = new ArrayList<>();
|
|
||||||
for (Class<?> c = expected.getClass(); c != null; c = c.getSuperclass()) {
|
|
||||||
Field[] fs = c.getDeclaredFields();
|
|
||||||
AccessibleObject.setAccessible(fs, true);
|
|
||||||
for (Field f : fs) {
|
|
||||||
// JaCoCo Code Coverage adds it's own field, don't look at this one here
|
|
||||||
if(f.getName().equals("$jacocoData")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
flds.add(f);
|
// JaCoCo Code Coverage adds its own field, don't look at this one here
|
||||||
}
|
assertTrue(new ReflectionEquals(expected, "$jacocoData").matches(actual));
|
||||||
}
|
|
||||||
return flds;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (PrivilegedActionException pae) {
|
|
||||||
throw pae.getException();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Field f : fields) {
|
|
||||||
Class<?> t = f.getType();
|
|
||||||
if (t.isArray()) {
|
|
||||||
if (Object[].class.isAssignableFrom(t)) {
|
|
||||||
assertArrayEquals((Object[])f.get(expected), (Object[])f.get(actual));
|
|
||||||
} else if (byte[].class.isAssignableFrom(t)) {
|
|
||||||
assertArrayEquals((byte[])f.get(expected), (byte[])f.get(actual));
|
|
||||||
} else {
|
|
||||||
fail("Array type is not yet implemented ... add it!");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
assertEquals(f.get(expected), f.get(actual));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -275,11 +234,4 @@ public final class POITestCase {
|
||||||
assertTrue(message + ": " + value + " is greater than the maximum value of " + max,
|
assertTrue(message + ": " + value + " is greater than the maximum value of " + max,
|
||||||
value <= max);
|
value <= max);
|
||||||
}
|
}
|
||||||
public static void assertStrictlyBetween(String message, int value, int min, int max) {
|
|
||||||
assertTrue(message + ": " + value + " is less than or equal to the minimum value of " + min,
|
|
||||||
min < value);
|
|
||||||
assertTrue(message + ": " + value + " is greater than or equal to the maximum value of " + max,
|
|
||||||
value < max);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,10 +17,20 @@
|
||||||
|
|
||||||
package org.apache.poi;
|
package org.apache.poi;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.poi.poifs.filesystem.DirectoryNode;
|
||||||
|
import org.apache.poi.poifs.filesystem.DocumentEntry;
|
||||||
|
import org.apache.poi.poifs.filesystem.EntryNode;
|
||||||
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||||
|
import org.apache.poi.poifs.property.PropertyTable;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -83,19 +93,12 @@ public final class TestPOITestCase {
|
||||||
* Utility method to get the value of a private/protected field.
|
* Utility method to get the value of a private/protected field.
|
||||||
* Only use this method in test cases!!!
|
* Only use this method in test cases!!!
|
||||||
*/
|
*/
|
||||||
@Ignore
|
|
||||||
@Test
|
@Test
|
||||||
public void getFieldValue() {
|
public void getFieldValue() throws IOException {
|
||||||
/*
|
try (POIFSFileSystem fs = new POIFSFileSystem()) {
|
||||||
final Class<? super T> clazz;
|
PropertyTable actual = POITestCase.getFieldValue(POIFSFileSystem.class, fs, PropertyTable.class, "_property_table");
|
||||||
final T instance;
|
assertNotNull(actual);
|
||||||
final Class<R> fieldType;
|
}
|
||||||
final String fieldName;
|
|
||||||
|
|
||||||
final R expected;
|
|
||||||
final R actual = POITestCase.getFieldValue(clazz, instance, fieldType, fieldName);
|
|
||||||
assertEquals(expected, actual);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,19 +107,13 @@ public final class TestPOITestCase {
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@Ignore
|
||||||
@Test
|
@Test
|
||||||
public void callMethod() {
|
public void callMethod() throws IOException {
|
||||||
/*
|
try (POIFSFileSystem fs = new POIFSFileSystem()) {
|
||||||
final Class<? super T> clazz;
|
DirectoryNode root = fs.getRoot();
|
||||||
final T instance;
|
DocumentEntry doc = fs.createDocument(new ByteArrayInputStream(new byte[]{1, 2, 3}), "foobaa");
|
||||||
final Class<R> returnType;
|
boolean actual = POITestCase.callMethod(DirectoryNode.class, root, boolean.class, "deleteEntry", new Class[]{EntryNode.class}, new Object[]{doc});
|
||||||
final String methodName;
|
assertTrue(actual);
|
||||||
final Class<?>[] parameterTypes;
|
}
|
||||||
final Object[] parameters;
|
|
||||||
|
|
||||||
final R expected;
|
|
||||||
final R actual = POITestCase.callMethod(clazz, instance, returnType, methodName, parameterTypes, parameters);
|
|
||||||
assertEquals(expected, actual);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package org.apache.poi.ddf;
|
package org.apache.poi.ddf;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotSame;
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
@ -186,12 +187,16 @@ public final class TestEscherContainerRecord {
|
||||||
* but hopefully we now read the correct size.
|
* but hopefully we now read the correct size.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testBug44857() throws Exception {
|
public void testBug44857() {
|
||||||
byte[] data = _samples.readFile("Container.dat");
|
byte[] data = _samples.readFile("Container.dat");
|
||||||
|
|
||||||
// This used to fail with an OutOfMemory
|
// This used to fail with an OutOfMemory
|
||||||
EscherContainerRecord record = new EscherContainerRecord();
|
EscherContainerRecord record = new EscherContainerRecord();
|
||||||
record.fillFields(data, 0, new DefaultEscherRecordFactory());
|
record.fillFields(data, 0, new DefaultEscherRecordFactory());
|
||||||
|
|
||||||
|
Class<?>[] exp = { EscherDggRecord.class, EscherContainerRecord.class, EscherOptRecord.class, EscherSplitMenuColorsRecord.class };
|
||||||
|
Class<?>[] act = record.getChildRecords().stream().map(Object::getClass).toArray(Class[]::new);
|
||||||
|
assertArrayEquals(exp, act);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -18,26 +18,25 @@
|
||||||
package org.apache.poi.ddf;
|
package org.apache.poi.ddf;
|
||||||
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.ArrayList;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.poi.POIDataSamples;
|
import org.apache.poi.POIDataSamples;
|
||||||
import org.apache.poi.hssf.HSSFTestDataSamples;
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
||||||
import org.apache.poi.poifs.storage.RawDataUtil;
|
import org.apache.poi.poifs.storage.RawDataUtil;
|
||||||
import org.apache.poi.util.IOUtils;
|
import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.poi.util.LocaleUtil;
|
import org.junit.Before;
|
||||||
import org.apache.poi.util.NullOutputStream;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestEscherDump {
|
public class TestEscherDump {
|
||||||
private static NullPrinterStream nullPS;
|
|
||||||
|
|
||||||
private static final String recordData =
|
private static final String recordData =
|
||||||
"H4sIAAAAAAAAAL2UaVCSWxjHX0SBChABLRXM1FxSEzXTzHK7dpVIcMmwxXCP9KaGTaWlGYLrtGmGmYEmYmqF2qIt4ppmjNG+" +
|
"H4sIAAAAAAAAAL2UaVCSWxjHX0SBChABLRXM1FxSEzXTzHK7dpVIcMmwxXCP9KaGTaWlGYLrtGmGmYEmYmqF2qIt4ppmjNG+" +
|
||||||
"2dWulUtOUdq1NHjva8v90HT7eM+Z5znP/M9/zpk5v3mONgAoc5AANBDKeVDW0gQAjZkVCti3mKnpAExpB/m8AKTyEiTCNd2J" +
|
"2dWulUtOUdq1NHjva8v90HT7eM+Z5znP/M9/zpk5v3mONgAoc5AANBDKeVDW0gQAjZkVCti3mKnpAExpB/m8AKTyEiTCNd2J" +
|
||||||
|
@ -64,45 +63,52 @@ public class TestEscherDump {
|
||||||
"cT19LR+PfTgjN4CKCS5Es4LS+7nLt9hQ7ejwGQnEyxebOgJzlHjotWUACpoZsFkAgGqBeUDZAzB6h4N2MFCNhmIuFJMAgPsH" +
|
"cT19LR+PfTgjN4CKCS5Es4LS+7nLt9hQ7ejwGQnEyxebOgJzlHjotWUACpoZsFkAgGqBeUDZAzB6h4N2MFCNhmIuFJMAgPsH" +
|
||||||
"eJr+iZEHAAA=";
|
"eJr+iZEHAAA=";
|
||||||
|
|
||||||
|
private EscherDump dumper = new EscherDump();
|
||||||
|
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
private PrintStream stream;
|
||||||
|
|
||||||
@BeforeClass
|
@Before
|
||||||
public static void init() throws UnsupportedEncodingException {
|
public void setup() throws UnsupportedEncodingException {
|
||||||
nullPS = new NullPrinterStream();
|
stream = new PrintStream(baos, true, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
// simple test to at least cover some parts of the class
|
// simple test to at least cover some parts of the class
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
// Create a new instance of the escher dumper
|
|
||||||
EscherDump dumper = new EscherDump();
|
|
||||||
|
|
||||||
// Decode the stream to bytes
|
// Decode the stream to bytes
|
||||||
byte[] bytes = RawDataUtil.decompress(recordData);
|
byte[] bytes = RawDataUtil.decompress(recordData);
|
||||||
// Dump the contents of scher to screen.
|
// Dump the contents of escher to stream.
|
||||||
dumper.dump(bytes, 0, bytes.length, nullPS);
|
dumper.dump(bytes, 0, bytes.length, stream);
|
||||||
|
assertEquals(216, countProperties());
|
||||||
|
|
||||||
dumper.dump(0, new byte[] {}, nullPS);
|
baos.reset();
|
||||||
dumper.dump(new byte[] {}, 0, 0, nullPS);
|
dumper.dump(0, new byte[0], stream);
|
||||||
|
assertEquals(0, countProperties());
|
||||||
|
|
||||||
|
baos.reset();
|
||||||
|
dumper.dump(new byte[0], 0, 0, stream);
|
||||||
|
assertEquals(0, countProperties());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithData() {
|
public void testWithData() {
|
||||||
new EscherDump().dump(8, new byte[] {0, 0, 0, 0, 0, 0, 0, 0}, nullPS);
|
dumper.dump(8, new byte[] {0, 0, 0, 0, 0, 0, 0, 0}, stream);
|
||||||
|
assertEquals(6, countProperties());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithSamplefile() throws Exception {
|
public void testWithSamplefile() throws Exception {
|
||||||
//InputStream stream = HSSFTestDataSamples.openSampleFileStream(")
|
//InputStream stream = HSSFTestDataSamples.openSampleFileStream(")
|
||||||
byte[] data = POIDataSamples.getDDFInstance().readFile("Container.dat");
|
byte[] data = POIDataSamples.getDDFInstance().readFile("Container.dat");
|
||||||
new EscherDump().dump(data.length, data, nullPS);
|
dumper.dump(data.length, data, stream);
|
||||||
//new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), System.out);
|
assertEquals(127, countProperties());
|
||||||
|
|
||||||
data = new byte[2586114];
|
data = new byte[2586114];
|
||||||
try (InputStream stream = HSSFTestDataSamples.openSampleFileStream("44593.xls")) {
|
try (InputStream is = HSSFTestDataSamples.openSampleFileStream("44593.xls")) {
|
||||||
int bytes = IOUtils.readFully(stream, data);
|
int bytes = IOUtils.readFully(is, data);
|
||||||
assertTrue(bytes != -1);
|
assertNotEquals(-1, bytes);
|
||||||
//new EscherDump().dump(bytes, data, System.out);
|
dumper.dump(data, 0, bytes, stream);
|
||||||
//new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out);
|
assertEquals(163, countProperties());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +116,6 @@ public class TestEscherDump {
|
||||||
public void testCopy() throws Exception {
|
public void testCopy() throws Exception {
|
||||||
byte[] data1 = RawDataUtil.decompress(recordData);
|
byte[] data1 = RawDataUtil.decompress(recordData);
|
||||||
|
|
||||||
List<EscherRecord> records = new ArrayList<>();
|
|
||||||
EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
|
EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
|
||||||
EscherRecord r = recordFactory.createRecord(data1, 0);
|
EscherRecord r = recordFactory.createRecord(data1, 0);
|
||||||
r.fillFields(data1, recordFactory);
|
r.fillFields(data1, recordFactory);
|
||||||
|
@ -120,14 +125,13 @@ public class TestEscherDump {
|
||||||
assertArrayEquals(data1, data2);
|
assertArrayEquals(data1, data2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private int countProperties() {
|
||||||
* Implementation of an OutputStream which does nothing, used
|
String data = new String(baos.toByteArray(), StandardCharsets.UTF_8);
|
||||||
* to redirect stdout to avoid spamming the console with output
|
Matcher matcher = Pattern.compile(",? \"[^\"]+\": ").matcher(data);
|
||||||
*/
|
int count = 0;
|
||||||
private static class NullPrinterStream extends PrintStream {
|
while (matcher.find()) {
|
||||||
@SuppressWarnings("resource")
|
count++;
|
||||||
private NullPrinterStream() throws UnsupportedEncodingException {
|
}
|
||||||
super(new NullOutputStream(),true,LocaleUtil.CHARSET_1252.name());
|
return count;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,8 @@ public class TestReadAllFiles {
|
||||||
/* Read the POI filesystem's property set streams: */
|
/* Read the POI filesystem's property set streams: */
|
||||||
for (POIFile pf : Util.readPropertySets(file)) {
|
for (POIFile pf : Util.readPropertySets(file)) {
|
||||||
try (InputStream in = new ByteArrayInputStream(pf.getBytes())) {
|
try (InputStream in = new ByteArrayInputStream(pf.getBytes())) {
|
||||||
PropertySetFactory.create(in);
|
PropertySet ps = PropertySetFactory.create(in);
|
||||||
|
assertNotNull(ps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -43,6 +44,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.apache.poi.POIDataSamples;
|
import org.apache.poi.POIDataSamples;
|
||||||
import org.apache.poi.hpsf.ClassID;
|
import org.apache.poi.hpsf.ClassID;
|
||||||
|
import org.apache.poi.hpsf.ClassIDPredefined;
|
||||||
import org.apache.poi.hpsf.DocumentSummaryInformation;
|
import org.apache.poi.hpsf.DocumentSummaryInformation;
|
||||||
import org.apache.poi.hpsf.HPSFException;
|
import org.apache.poi.hpsf.HPSFException;
|
||||||
import org.apache.poi.hpsf.NoFormatIDException;
|
import org.apache.poi.hpsf.NoFormatIDException;
|
||||||
|
@ -58,7 +60,6 @@ import org.apache.poi.hpsf.VariantSupport;
|
||||||
import org.apache.poi.hpsf.WritingNotSupportedException;
|
import org.apache.poi.hpsf.WritingNotSupportedException;
|
||||||
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
|
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
|
||||||
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
|
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
|
||||||
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
|
|
||||||
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
|
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
|
||||||
import org.apache.poi.poifs.filesystem.DirectoryEntry;
|
import org.apache.poi.poifs.filesystem.DirectoryEntry;
|
||||||
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
||||||
|
@ -67,13 +68,10 @@ import org.apache.poi.poifs.filesystem.DocumentOutputStream;
|
||||||
import org.apache.poi.poifs.filesystem.POIFSDocument;
|
import org.apache.poi.poifs.filesystem.POIFSDocument;
|
||||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||||
import org.apache.poi.util.CodePageUtil;
|
import org.apache.poi.util.CodePageUtil;
|
||||||
import org.apache.poi.util.CommonsLogger;
|
|
||||||
import org.apache.poi.util.IOUtils;
|
import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.poi.util.LittleEndianConsts;
|
import org.apache.poi.util.LittleEndianConsts;
|
||||||
import org.apache.poi.util.TempFile;
|
import org.apache.poi.util.TempFile;
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.Assume;
|
import org.junit.Assume;
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,6 +91,7 @@ public class TestWrite {
|
||||||
"LANG environment variable to a proper value, e.g. " +
|
"LANG environment variable to a proper value, e.g. " +
|
||||||
"\"de_DE\".";
|
"\"de_DE\".";
|
||||||
|
|
||||||
|
/*
|
||||||
private static String loggerBefore;
|
private static String loggerBefore;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
|
@ -114,10 +113,10 @@ public class TestWrite {
|
||||||
System.setProperty("org.apache.poi.util.POILogger", loggerBefore);
|
System.setProperty("org.apache.poi.util.POILogger", loggerBefore);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Writes an empty property set to a POIFS and reads it back
|
* Writes an empty property set to a POIFS and reads it back in.
|
||||||
* in.</p>
|
|
||||||
*
|
*
|
||||||
* @exception IOException if an I/O exception occurs
|
* @exception IOException if an I/O exception occurs
|
||||||
*/
|
*/
|
||||||
|
@ -133,20 +132,16 @@ public class TestWrite {
|
||||||
|
|
||||||
/* Write it to a POIFS and the latter to disk: */
|
/* Write it to a POIFS and the latter to disk: */
|
||||||
try (OutputStream out = new FileOutputStream(filename);
|
try (OutputStream out = new FileOutputStream(filename);
|
||||||
POIFSFileSystem poiFs = new POIFSFileSystem()) {
|
POIFSFileSystem poiFs = new POIFSFileSystem();
|
||||||
final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream psStream = new ByteArrayOutputStream()) {
|
||||||
ps.write(psStream);
|
ps.write(psStream);
|
||||||
psStream.close();
|
poiFs.createDocument(new ByteArrayInputStream(psStream.toByteArray()), SummaryInformation.DEFAULT_STREAM_NAME);
|
||||||
final byte[] streamData = psStream.toByteArray();
|
|
||||||
poiFs.createDocument(new ByteArrayInputStream(streamData),
|
|
||||||
SummaryInformation.DEFAULT_STREAM_NAME);
|
|
||||||
poiFs.writeFilesystem(out);
|
poiFs.writeFilesystem(out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Writes an empty property set to a POIFS and reads it back
|
* Writes an empty property set to a POIFS and reads it back in.
|
||||||
* in.</p>
|
|
||||||
*
|
*
|
||||||
* @exception IOException if an I/O exception occurs
|
* @exception IOException if an I/O exception occurs
|
||||||
* @exception UnsupportedVariantTypeException if HPSF does not yet support
|
* @exception UnsupportedVariantTypeException if HPSF does not yet support
|
||||||
|
@ -160,27 +155,24 @@ public class TestWrite {
|
||||||
filename.deleteOnExit();
|
filename.deleteOnExit();
|
||||||
|
|
||||||
/* Create a mutable property set and write it to a POIFS: */
|
/* Create a mutable property set and write it to a POIFS: */
|
||||||
final OutputStream out = new FileOutputStream(filename);
|
try (OutputStream out = new FileOutputStream(filename);
|
||||||
final POIFSFileSystem poiFs = new POIFSFileSystem();
|
POIFSFileSystem poiFs = new POIFSFileSystem();
|
||||||
|
ByteArrayOutputStream psStream = new ByteArrayOutputStream()) {
|
||||||
final PropertySet ps = new PropertySet();
|
final PropertySet ps = new PropertySet();
|
||||||
final Section s = ps.getSections().get(0);
|
final Section s = ps.getSections().get(0);
|
||||||
s.setFormatID(SummaryInformation.FORMAT_ID);
|
s.setFormatID(SummaryInformation.FORMAT_ID);
|
||||||
|
|
||||||
final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
|
|
||||||
ps.write(psStream);
|
ps.write(psStream);
|
||||||
psStream.close();
|
poiFs.createDocument(new ByteArrayInputStream(psStream.toByteArray()), SummaryInformation.DEFAULT_STREAM_NAME);
|
||||||
final byte[] streamData = psStream.toByteArray();
|
|
||||||
poiFs.createDocument(new ByteArrayInputStream(streamData),
|
|
||||||
SummaryInformation.DEFAULT_STREAM_NAME);
|
|
||||||
poiFs.writeFilesystem(out);
|
poiFs.writeFilesystem(out);
|
||||||
poiFs.close();
|
}
|
||||||
out.close();
|
|
||||||
|
|
||||||
/* Read the POIFS: */
|
/* Read the POIFS: */
|
||||||
final POIFSReader r = new POIFSReader();
|
final POIFSReader r = new POIFSReader();
|
||||||
r.registerListener(new MyPOIFSReaderListener(),
|
final List<PropertySet> psa = new ArrayList<>();
|
||||||
SummaryInformation.DEFAULT_STREAM_NAME);
|
|
||||||
|
r.registerListener(getListener(psa), SummaryInformation.DEFAULT_STREAM_NAME);
|
||||||
r.read(filename);
|
r.read(filename);
|
||||||
|
assertEquals(1, psa.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -199,8 +191,8 @@ public class TestWrite {
|
||||||
final File dataDir = _samples.getFile("");
|
final File dataDir = _samples.getFile("");
|
||||||
final File filename = new File(dataDir, POI_FS);
|
final File filename = new File(dataDir, POI_FS);
|
||||||
filename.deleteOnExit();
|
filename.deleteOnExit();
|
||||||
final OutputStream out = new FileOutputStream(filename);
|
try (OutputStream out = new FileOutputStream(filename);
|
||||||
final POIFSFileSystem poiFs = new POIFSFileSystem();
|
POIFSFileSystem poiFs = new POIFSFileSystem()) {
|
||||||
|
|
||||||
final PropertySet ps = new PropertySet();
|
final PropertySet ps = new PropertySet();
|
||||||
final Section si = new Section();
|
final Section si = new Section();
|
||||||
|
@ -215,29 +207,19 @@ public class TestWrite {
|
||||||
si.setProperty(p);
|
si.setProperty(p);
|
||||||
si.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE);
|
si.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE);
|
||||||
|
|
||||||
poiFs.createDocument(ps.toInputStream(),
|
poiFs.createDocument(ps.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
|
||||||
SummaryInformation.DEFAULT_STREAM_NAME);
|
|
||||||
poiFs.writeFilesystem(out);
|
poiFs.writeFilesystem(out);
|
||||||
poiFs.close();
|
}
|
||||||
out.close();
|
|
||||||
|
|
||||||
/* Read the POIFS: */
|
/* Read the POIFS: */
|
||||||
final PropertySet[] psa = new PropertySet[1];
|
final List<PropertySet> psa = new ArrayList<>();
|
||||||
final POIFSReader r = new POIFSReader();
|
final POIFSReader r = new POIFSReader();
|
||||||
final POIFSReaderListener listener = event -> {
|
r.registerListener(getListener(psa), SummaryInformation.DEFAULT_STREAM_NAME);
|
||||||
try {
|
|
||||||
psa[0] = PropertySetFactory.create(event.getStream());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
fail(ex.getMessage());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
r.registerListener(listener, SummaryInformation.DEFAULT_STREAM_NAME);
|
|
||||||
|
|
||||||
r.read(filename);
|
r.read(filename);
|
||||||
assertNotNull(psa[0]);
|
assertEquals(1, psa.size());
|
||||||
assertTrue(psa[0].isSummaryInformation());
|
assertTrue(psa.get(0).isSummaryInformation());
|
||||||
|
|
||||||
final Section s = (psa[0].getSections().get(0));
|
final Section s = psa.get(0).getSections().get(0);
|
||||||
Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR);
|
Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR);
|
||||||
Object p2 = s.getProperty(PropertyIDMap.PID_TITLE);
|
Object p2 = s.getProperty(PropertyIDMap.PID_TITLE);
|
||||||
assertEquals(AUTHOR, p1);
|
assertEquals(AUTHOR, p1);
|
||||||
|
@ -247,8 +229,8 @@ public class TestWrite {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Writes a simple property set with two sections to a POIFS and reads it
|
* Writes a simple property set with two sections to a POIFS and reads it
|
||||||
* back in.</p>
|
* back in.
|
||||||
*
|
*
|
||||||
* @exception IOException if an I/O exception occurs
|
* @exception IOException if an I/O exception occurs
|
||||||
* @exception WritingNotSupportedException if HPSF does not yet support
|
* @exception WritingNotSupportedException if HPSF does not yet support
|
||||||
|
@ -259,33 +241,30 @@ public class TestWrite {
|
||||||
final String STREAM_NAME = "PropertySetStream";
|
final String STREAM_NAME = "PropertySetStream";
|
||||||
final String SECTION1 = "Section 1";
|
final String SECTION1 = "Section 1";
|
||||||
final String SECTION2 = "Section 2";
|
final String SECTION2 = "Section 2";
|
||||||
|
final ClassID FORMATID = ClassIDPredefined.EXCEL_V12.getClassID();
|
||||||
|
|
||||||
final File dataDir = _samples.getFile("");
|
final File dataDir = _samples.getFile("");
|
||||||
final File filename = new File(dataDir, POI_FS);
|
final File filename = new File(dataDir, POI_FS);
|
||||||
filename.deleteOnExit();
|
filename.deleteOnExit();
|
||||||
final OutputStream out = new FileOutputStream(filename);
|
|
||||||
|
|
||||||
final POIFSFileSystem poiFs = new POIFSFileSystem();
|
try (OutputStream out = new FileOutputStream(filename);
|
||||||
|
POIFSFileSystem poiFs = new POIFSFileSystem()) {
|
||||||
final PropertySet ps = new PropertySet();
|
final PropertySet ps = new PropertySet();
|
||||||
ps.clearSections();
|
ps.clearSections();
|
||||||
|
|
||||||
final ClassID formatID = new ClassID();
|
|
||||||
formatID.setBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7,
|
|
||||||
8, 9, 10, 11, 12, 13, 14, 15});
|
|
||||||
final Section s1 = new Section();
|
final Section s1 = new Section();
|
||||||
s1.setFormatID(formatID);
|
s1.setFormatID(FORMATID);
|
||||||
s1.setProperty(2, SECTION1);
|
s1.setProperty(2, SECTION1);
|
||||||
ps.addSection(s1);
|
ps.addSection(s1);
|
||||||
|
|
||||||
final Section s2 = new Section();
|
final Section s2 = new Section();
|
||||||
s2.setFormatID(formatID);
|
s2.setFormatID(FORMATID);
|
||||||
s2.setProperty(2, SECTION2);
|
s2.setProperty(2, SECTION2);
|
||||||
ps.addSection(s2);
|
ps.addSection(s2);
|
||||||
|
|
||||||
poiFs.createDocument(ps.toInputStream(), STREAM_NAME);
|
poiFs.createDocument(ps.toInputStream(), STREAM_NAME);
|
||||||
poiFs.writeFilesystem(out);
|
poiFs.writeFilesystem(out);
|
||||||
poiFs.close();
|
}
|
||||||
out.close();
|
|
||||||
|
|
||||||
/* Read the POIFS: */
|
/* Read the POIFS: */
|
||||||
final PropertySet[] psa = new PropertySet[1];
|
final PropertySet[] psa = new PropertySet[1];
|
||||||
|
@ -303,7 +282,7 @@ public class TestWrite {
|
||||||
|
|
||||||
assertNotNull(psa[0]);
|
assertNotNull(psa[0]);
|
||||||
Section s = (psa[0].getSections().get(0));
|
Section s = (psa[0].getSections().get(0));
|
||||||
assertEquals(s.getFormatID(), formatID);
|
assertEquals(s.getFormatID(), FORMATID);
|
||||||
Object p = s.getProperty(2);
|
Object p = s.getProperty(2);
|
||||||
assertEquals(SECTION1, p);
|
assertEquals(SECTION1, p);
|
||||||
s = (psa[0].getSections().get(1));
|
s = (psa[0].getSections().get(1));
|
||||||
|
@ -311,16 +290,14 @@ public class TestWrite {
|
||||||
assertEquals(SECTION2, p);
|
assertEquals(SECTION2, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static POIFSReaderListener getListener(List<PropertySet> psa) {
|
||||||
static class MyPOIFSReaderListener implements POIFSReaderListener {
|
return event -> {
|
||||||
@Override
|
|
||||||
public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
|
|
||||||
try {
|
try {
|
||||||
PropertySetFactory.create(event.getStream());
|
psa.add(PropertySetFactory.create(event.getStream()));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
fail(ex.getMessage());
|
fail(ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue