#58597: Fix tests to not do setAccessible(), add some doPrivileged, too.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1713891 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
da19b14670
commit
73a8488838
@ -38,7 +38,7 @@ public class RawDataBlock
|
|||||||
private byte[] _data;
|
private byte[] _data;
|
||||||
private boolean _eof;
|
private boolean _eof;
|
||||||
private boolean _hasData;
|
private boolean _hasData;
|
||||||
private static POILogger log = POILogFactory.getLogger(RawDataBlock.class);
|
static POILogger log = POILogFactory.getLogger(RawDataBlock.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor RawDataBlock
|
* Constructor RawDataBlock
|
||||||
|
@ -46,7 +46,7 @@ public final class POILogFactory {
|
|||||||
* The name of the class to use. Initialised the
|
* The name of the class to use. Initialised the
|
||||||
* first time we need it
|
* first time we need it
|
||||||
*/
|
*/
|
||||||
private static String _loggerClassName = null;
|
static String _loggerClassName = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a POILogFactory.
|
* Construct a POILogFactory.
|
||||||
|
@ -32,7 +32,6 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
@ -46,6 +45,7 @@ import java.util.zip.ZipFile;
|
|||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
import org.apache.poi.EncryptedDocumentException;
|
import org.apache.poi.EncryptedDocumentException;
|
||||||
|
import org.apache.poi.POITestCase;
|
||||||
import org.apache.poi.POIXMLException;
|
import org.apache.poi.POIXMLException;
|
||||||
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
|
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
|
||||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||||
@ -579,11 +579,8 @@ public final class TestPackage {
|
|||||||
assertTrue(tempFile2.delete());
|
assertTrue(tempFile2.delete());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ContentTypeManager getContentTypeManager(OPCPackage pkg)
|
private static ContentTypeManager getContentTypeManager(OPCPackage pkg) {
|
||||||
throws IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
|
return POITestCase.getFieldValue(OPCPackage.class, pkg, ContentTypeManager.class, "contentTypeManager");
|
||||||
Field f = OPCPackage.class.getDeclaredField("contentTypeManager");
|
|
||||||
f.setAccessible(true);
|
|
||||||
return (ContentTypeManager)f.get(pkg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -34,7 +34,6 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@ -54,6 +53,7 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.poi.POIDataSamples;
|
import org.apache.poi.POIDataSamples;
|
||||||
|
import org.apache.poi.POITestCase;
|
||||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||||
import org.apache.poi.openxml4j.opc.PackageAccess;
|
import org.apache.poi.openxml4j.opc.PackageAccess;
|
||||||
import org.apache.poi.poifs.crypt.dsig.DigestInfo;
|
import org.apache.poi.poifs.crypt.dsig.DigestInfo;
|
||||||
@ -247,9 +247,7 @@ public class TestSignatureInfo {
|
|||||||
XSSFWorkbook wb = new XSSFWorkbook(pkg);
|
XSSFWorkbook wb = new XSSFWorkbook(pkg);
|
||||||
wb.setSheetName(0, "manipulated");
|
wb.setSheetName(0, "manipulated");
|
||||||
// ... I don't know, why commit is protected ...
|
// ... I don't know, why commit is protected ...
|
||||||
Method m = XSSFWorkbook.class.getDeclaredMethod("commit");
|
POITestCase.callMethod(XSSFWorkbook.class, wb, Void.class, "commit", new Class[0], new Object[0]);
|
||||||
m.setAccessible(true);
|
|
||||||
m.invoke(wb);
|
|
||||||
|
|
||||||
// todo: test a manipulation on a package part, which is not signed
|
// todo: test a manipulation on a package part, which is not signed
|
||||||
// ... maybe in combination with #56164
|
// ... maybe in combination with #56164
|
||||||
|
@ -20,12 +20,9 @@
|
|||||||
package org.apache.poi.xslf.usermodel;
|
package org.apache.poi.xslf.usermodel;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
import org.apache.poi.POIDataSamples;
|
import org.apache.poi.POIDataSamples;
|
||||||
import org.apache.poi.xslf.util.PPTX2PNG;
|
import org.apache.poi.xslf.util.PPTX2PNG;
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,32 +31,6 @@ import org.junit.Test;
|
|||||||
* @author Yegor Kozlov
|
* @author Yegor Kozlov
|
||||||
*/
|
*/
|
||||||
public class TestPPTX2PNG {
|
public class TestPPTX2PNG {
|
||||||
private static boolean jaxpDebugEnable = false;
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void activateJaxpDebug() {
|
|
||||||
jaxpDebugEnable = setDebugFld(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void resetJaxpDebug() {
|
|
||||||
setDebugFld(jaxpDebugEnable);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean setDebugFld(boolean enable) {
|
|
||||||
// enable jaxp debugging because of jaxb/stax error in gump build
|
|
||||||
try {
|
|
||||||
Class<?> clz = Class.forName("javax.xml.stream.FactoryFinder");
|
|
||||||
Field fld = clz.getDeclaredField("debug");
|
|
||||||
fld.setAccessible(true);
|
|
||||||
boolean isDebug = (Boolean)fld.get(null);
|
|
||||||
fld.set(null, enable);
|
|
||||||
return isDebug;
|
|
||||||
} catch (Exception e) {
|
|
||||||
// ignore
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void render() throws Exception {
|
public void render() throws Exception {
|
||||||
|
@ -28,8 +28,8 @@ import static org.junit.Assert.fail;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
|
import org.apache.poi.POITestCase;
|
||||||
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
|
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
@ -106,9 +106,7 @@ public final class TestSXSSFWorkbook extends BaseTestWorkbook {
|
|||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true);
|
SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true);
|
||||||
|
|
||||||
Field f = SXSSFWorkbook.class.getDeclaredField("_sharedStringSource");
|
SharedStringsTable sss = POITestCase.getFieldValue(SXSSFWorkbook.class, wb, SharedStringsTable.class, "_sharedStringSource");
|
||||||
f.setAccessible(true);
|
|
||||||
SharedStringsTable sss = (SharedStringsTable)f.get(wb);
|
|
||||||
|
|
||||||
assertNotNull(sss);
|
assertNotNull(sss);
|
||||||
|
|
||||||
@ -119,7 +117,7 @@ public final class TestSXSSFWorkbook extends BaseTestWorkbook {
|
|||||||
row.createCell(2).setCellValue("A");
|
row.createCell(2).setCellValue("A");
|
||||||
|
|
||||||
XSSFWorkbook xssfWorkbook = (XSSFWorkbook) SXSSFITestDataProvider.instance.writeOutAndReadBack(wb);
|
XSSFWorkbook xssfWorkbook = (XSSFWorkbook) SXSSFITestDataProvider.instance.writeOutAndReadBack(wb);
|
||||||
sss = (SharedStringsTable)f.get(wb);
|
sss = POITestCase.getFieldValue(SXSSFWorkbook.class, wb, SharedStringsTable.class, "_sharedStringSource");
|
||||||
assertEquals(2, sss.getUniqueCount());
|
assertEquals(2, sss.getUniqueCount());
|
||||||
assertTrue(wb.dispose());
|
assertTrue(wb.dispose());
|
||||||
|
|
||||||
|
@ -19,12 +19,18 @@ package org.apache.poi.hwpf.model;
|
|||||||
|
|
||||||
import java.lang.reflect.AccessibleObject;
|
import java.lang.reflect.AccessibleObject;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.security.AccessController;
|
||||||
|
import java.security.PrivilegedActionException;
|
||||||
|
import java.security.PrivilegedExceptionAction;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.hwpf.HWPFDocFixture;
|
import org.apache.poi.hwpf.HWPFDocFixture;
|
||||||
import org.apache.poi.hwpf.model.types.DOPAbstractType;
|
import org.apache.poi.hwpf.model.types.DOPAbstractType;
|
||||||
|
import org.apache.poi.util.SuppressForbidden;
|
||||||
|
|
||||||
|
// TODO: Add DocumentProperties#equals ???
|
||||||
|
|
||||||
public final class TestDocumentProperties
|
public final class TestDocumentProperties
|
||||||
extends TestCase
|
extends TestCase
|
||||||
@ -43,8 +49,20 @@ public final class TestDocumentProperties
|
|||||||
DocumentProperties newDocProperties =
|
DocumentProperties newDocProperties =
|
||||||
new DocumentProperties(buf, 0, size);
|
new DocumentProperties(buf, 0, size);
|
||||||
|
|
||||||
Field[] fields = DocumentProperties.class.getSuperclass().getDeclaredFields();
|
final Field[] fields;
|
||||||
AccessibleObject.setAccessible(fields, true);
|
try {
|
||||||
|
fields = AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() {
|
||||||
|
@Override
|
||||||
|
@SuppressForbidden("Test only")
|
||||||
|
public Field[] run() throws Exception {
|
||||||
|
final Field[] fields = DocumentProperties.class.getSuperclass().getDeclaredFields();
|
||||||
|
AccessibleObject.setAccessible(fields, true);
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (PrivilegedActionException pae) {
|
||||||
|
throw pae.getException();
|
||||||
|
}
|
||||||
|
|
||||||
for (int x = 0; x < fields.length; x++)
|
for (int x = 0; x < fields.length; x++)
|
||||||
{
|
{
|
||||||
|
@ -21,8 +21,15 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.security.AccessController;
|
||||||
|
import java.security.PrivilegedActionException;
|
||||||
|
import java.security.PrivilegedExceptionAction;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.apache.poi.util.SuppressForbidden;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parent class for POI JUnit TestCases, which provide additional
|
* Parent class for POI JUnit TestCases, which provide additional
|
||||||
* features
|
* features
|
||||||
@ -67,4 +74,45 @@ public class POITestCase {
|
|||||||
}
|
}
|
||||||
fail("Unable to find " + needle + " in " + haystack);
|
fail("Unable to find " + needle + " in " + haystack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Utility method to get the value of a private/protected field.
|
||||||
|
* Only use this method in test cases!!!
|
||||||
|
*/
|
||||||
|
public static <R,T> R getFieldValue(final Class<? super T> clazz, final T instance, final Class<R> fieldType, final String fieldName) {
|
||||||
|
try {
|
||||||
|
return AccessController.doPrivileged(new PrivilegedExceptionAction<R>() {
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@SuppressForbidden("For test usage only")
|
||||||
|
public R run() throws Exception {
|
||||||
|
Field f = clazz.getDeclaredField(fieldName);
|
||||||
|
f.setAccessible(true);
|
||||||
|
return (R) f.get(instance);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (PrivilegedActionException pae) {
|
||||||
|
throw new AssertionError("Cannot access field '" + fieldName + "' of class " + clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Utility method to call a private/protected method.
|
||||||
|
* Only use this method in test cases!!!
|
||||||
|
*/
|
||||||
|
public static <R,T> R callMethod(final Class<? super T> clazz, final T instance, final Class<R> returnType, final String methodName,
|
||||||
|
final Class<?>[] parameterTypes, final Object[] parameters) {
|
||||||
|
try {
|
||||||
|
return AccessController.doPrivileged(new PrivilegedExceptionAction<R>() {
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@SuppressForbidden("For test usage only")
|
||||||
|
public R run() throws Exception {
|
||||||
|
Method m = clazz.getDeclaredMethod(methodName, parameterTypes);
|
||||||
|
m.setAccessible(true);
|
||||||
|
return (R) m.invoke(instance, parameters);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (PrivilegedActionException pae) {
|
||||||
|
throw new AssertionError("Cannot access method '" + methodName + "' of class " + clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,13 @@
|
|||||||
|
|
||||||
package org.apache.poi.hssf.record.aggregates;
|
package org.apache.poi.hssf.record.aggregates;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import junit.framework.AssertionFailedError;
|
import junit.framework.AssertionFailedError;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.apache.poi.POITestCase;
|
||||||
import org.apache.poi.hssf.HSSFTestDataSamples;
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
||||||
import org.apache.poi.hssf.record.Record;
|
import org.apache.poi.hssf.record.Record;
|
||||||
import org.apache.poi.hssf.record.SharedFormulaRecord;
|
import org.apache.poi.hssf.record.SharedFormulaRecord;
|
||||||
@ -176,21 +176,7 @@ public final class TestSharedValueManager extends TestCase {
|
|||||||
* {@link RowRecordsAggregate}.
|
* {@link RowRecordsAggregate}.
|
||||||
*/
|
*/
|
||||||
public static SharedValueManager extractFromRRA(RowRecordsAggregate rra) {
|
public static SharedValueManager extractFromRRA(RowRecordsAggregate rra) {
|
||||||
Field f;
|
return POITestCase.getFieldValue(RowRecordsAggregate.class, rra, SharedValueManager.class, "_sharedValueManager");
|
||||||
try {
|
|
||||||
f = RowRecordsAggregate.class.getDeclaredField("_sharedValueManager");
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
f.setAccessible(true);
|
|
||||||
try {
|
|
||||||
return (SharedValueManager) f.get(rra);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBug52527() {
|
public void testBug52527() {
|
||||||
|
@ -23,6 +23,7 @@ import static org.junit.Assert.fail;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import org.apache.poi.POITestCase;
|
||||||
import org.apache.poi.hssf.HSSFITestDataProvider;
|
import org.apache.poi.hssf.HSSFITestDataProvider;
|
||||||
import org.apache.poi.hssf.HSSFTestDataSamples;
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
||||||
import org.apache.poi.hssf.model.HSSFFormulaParser;
|
import org.apache.poi.hssf.model.HSSFFormulaParser;
|
||||||
@ -45,21 +46,7 @@ public final class TestHSSFName extends BaseTestNamedRange {
|
|||||||
* @return a reference to the wrapped {@link NameRecord}
|
* @return a reference to the wrapped {@link NameRecord}
|
||||||
*/
|
*/
|
||||||
public static NameRecord getNameRecord(HSSFName definedName) {
|
public static NameRecord getNameRecord(HSSFName definedName) {
|
||||||
|
return POITestCase.getFieldValue(HSSFName.class, definedName, NameRecord.class, "_definedNameRec");
|
||||||
Field f;
|
|
||||||
try {
|
|
||||||
f = HSSFName.class.getDeclaredField("_definedNameRec");
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
f.setAccessible(true);
|
|
||||||
try {
|
|
||||||
return (NameRecord) f.get(definedName);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TestHSSFName() {
|
public TestHSSFName() {
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
package org.apache.poi.hssf.usermodel;
|
package org.apache.poi.hssf.usermodel;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.apache.poi.POITestCase;
|
||||||
import org.apache.poi.ddf.EscherContainerRecord;
|
import org.apache.poi.ddf.EscherContainerRecord;
|
||||||
import org.apache.poi.ddf.EscherSpgrRecord;
|
import org.apache.poi.ddf.EscherSpgrRecord;
|
||||||
import org.apache.poi.hssf.HSSFTestDataSamples;
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
||||||
@ -228,17 +230,7 @@ public class TestShapeGroup extends TestCase{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static EscherSpgrRecord getSpgrRecord(HSSFShapeGroup group) {
|
private static EscherSpgrRecord getSpgrRecord(HSSFShapeGroup group) {
|
||||||
Field spgrField = null;
|
return POITestCase.getFieldValue(HSSFShapeGroup.class, group, EscherSpgrRecord.class, "_spgrRecord");
|
||||||
try {
|
|
||||||
spgrField = group.getClass().getDeclaredField("_spgrRecord");
|
|
||||||
spgrField.setAccessible(true);
|
|
||||||
return (EscherSpgrRecord) spgrField.get(group);
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testClearShapes(){
|
public void testClearShapes(){
|
||||||
|
@ -20,12 +20,12 @@ package org.apache.poi.poifs.storage;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.util.DummyPOILogger;
|
import org.apache.poi.util.DummyPOILogger;
|
||||||
|
import org.apache.poi.util.POILogger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to test RawDataBlock functionality
|
* Class to test RawDataBlock functionality
|
||||||
@ -82,50 +82,53 @@ public final class TestRawDataBlock extends TestCase {
|
|||||||
*/
|
*/
|
||||||
public void testShortConstructor() throws Exception {
|
public void testShortConstructor() throws Exception {
|
||||||
// Get the logger to be used
|
// Get the logger to be used
|
||||||
|
POILogger oldLogger = RawDataBlock.log;
|
||||||
DummyPOILogger logger = new DummyPOILogger();
|
DummyPOILogger logger = new DummyPOILogger();
|
||||||
Field fld = RawDataBlock.class.getDeclaredField("log");
|
try {
|
||||||
fld.setAccessible(true);
|
RawDataBlock.log = logger;
|
||||||
fld.set(null, logger);
|
assertEquals(0, logger.logged.size());
|
||||||
assertEquals(0, logger.logged.size());
|
|
||||||
|
|
||||||
// Test for various data sizes
|
// Test for various data sizes
|
||||||
for (int k = 1; k <= 512; k++)
|
for (int k = 1; k <= 512; k++)
|
||||||
{
|
{
|
||||||
byte[] data = new byte[ k ];
|
byte[] data = new byte[ k ];
|
||||||
|
|
||||||
for (int j = 0; j < k; j++)
|
for (int j = 0; j < k; j++)
|
||||||
{
|
{
|
||||||
data[ j ] = ( byte ) j;
|
data[ j ] = ( byte ) j;
|
||||||
}
|
}
|
||||||
RawDataBlock block = null;
|
RawDataBlock block = null;
|
||||||
|
|
||||||
logger.reset();
|
logger.reset();
|
||||||
assertEquals(0, logger.logged.size());
|
assertEquals(0, logger.logged.size());
|
||||||
|
|
||||||
// Have it created
|
// Have it created
|
||||||
block = new RawDataBlock(new ByteArrayInputStream(data));
|
block = new RawDataBlock(new ByteArrayInputStream(data));
|
||||||
assertNotNull(block);
|
assertNotNull(block);
|
||||||
|
|
||||||
// Check for the warning is there for <512
|
// Check for the warning is there for <512
|
||||||
if(k < 512) {
|
if(k < 512) {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"Warning on " + k + " byte short block",
|
"Warning on " + k + " byte short block",
|
||||||
1, logger.logged.size()
|
1, logger.logged.size()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Build the expected warning message, and check
|
// Build the expected warning message, and check
|
||||||
String bts = k + " byte";
|
String bts = k + " byte";
|
||||||
if(k > 1) {
|
if(k > 1) {
|
||||||
bts += "s";
|
bts += "s";
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"7 - Unable to read entire block; "+bts+" read before EOF; expected 512 bytes. Your document was either written by software that ignores the spec, or has been truncated!",
|
"7 - Unable to read entire block; "+bts+" read before EOF; expected 512 bytes. Your document was either written by software that ignores the spec, or has been truncated!",
|
||||||
logger.logged.get(0)
|
logger.logged.get(0)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
assertEquals(0, logger.logged.size());
|
assertEquals(0, logger.logged.size());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
RawDataBlock.log = oldLogger;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,46 +139,49 @@ public final class TestRawDataBlock extends TestCase {
|
|||||||
*/
|
*/
|
||||||
public void testSlowInputStream() throws Exception {
|
public void testSlowInputStream() throws Exception {
|
||||||
// Get the logger to be used
|
// Get the logger to be used
|
||||||
DummyPOILogger logger = new DummyPOILogger();
|
POILogger oldLogger = RawDataBlock.log;
|
||||||
Field fld = RawDataBlock.class.getDeclaredField("log");
|
DummyPOILogger logger = new DummyPOILogger();
|
||||||
fld.setAccessible(true);
|
try {
|
||||||
fld.set(null, logger);
|
RawDataBlock.log = logger;
|
||||||
assertEquals(0, logger.logged.size());
|
assertEquals(0, logger.logged.size());
|
||||||
|
|
||||||
// Test for various ok data sizes
|
// Test for various ok data sizes
|
||||||
for (int k = 1; k < 512; k++) {
|
for (int k = 1; k < 512; k++) {
|
||||||
byte[] data = new byte[ 512 ];
|
byte[] data = new byte[ 512 ];
|
||||||
for (int j = 0; j < data.length; j++) {
|
for (int j = 0; j < data.length; j++) {
|
||||||
data[j] = (byte) j;
|
data[j] = (byte) j;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shouldn't complain, as there is enough data,
|
// Shouldn't complain, as there is enough data,
|
||||||
// even if it dribbles through
|
// even if it dribbles through
|
||||||
RawDataBlock block =
|
RawDataBlock block =
|
||||||
new RawDataBlock(new SlowInputStream(data, k));
|
new RawDataBlock(new SlowInputStream(data, k));
|
||||||
assertFalse(block.eof());
|
assertFalse(block.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
// But if there wasn't enough data available, will
|
// But if there wasn't enough data available, will
|
||||||
// complain
|
// complain
|
||||||
for (int k = 1; k < 512; k++) {
|
for (int k = 1; k < 512; k++) {
|
||||||
byte[] data = new byte[ 511 ];
|
byte[] data = new byte[ 511 ];
|
||||||
for (int j = 0; j < data.length; j++) {
|
for (int j = 0; j < data.length; j++) {
|
||||||
data[j] = (byte) j;
|
data[j] = (byte) j;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.reset();
|
logger.reset();
|
||||||
assertEquals(0, logger.logged.size());
|
assertEquals(0, logger.logged.size());
|
||||||
|
|
||||||
// Should complain, as there isn't enough data
|
// Should complain, as there isn't enough data
|
||||||
RawDataBlock block =
|
RawDataBlock block =
|
||||||
new RawDataBlock(new SlowInputStream(data, k));
|
new RawDataBlock(new SlowInputStream(data, k));
|
||||||
assertNotNull(block);
|
assertNotNull(block);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"Warning on " + k + " byte short block",
|
"Warning on " + k + " byte short block",
|
||||||
1, logger.logged.size()
|
1, logger.logged.size()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
RawDataBlock.log = oldLogger;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,12 +19,11 @@ package org.apache.poi.poifs.storage;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.poifs.common.POIFSConstants;
|
import org.apache.poi.poifs.common.POIFSConstants;
|
||||||
import org.apache.poi.util.DummyPOILogger;
|
import org.apache.poi.util.DummyPOILogger;
|
||||||
|
import org.apache.poi.util.POILogger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to test RawDataBlockList functionality
|
* Class to test RawDataBlockList functionality
|
||||||
@ -57,26 +56,29 @@ public final class TestRawDataBlockList extends TestCase {
|
|||||||
*/
|
*/
|
||||||
public void testShortConstructor() throws Exception {
|
public void testShortConstructor() throws Exception {
|
||||||
// Get the logger to be used
|
// Get the logger to be used
|
||||||
|
POILogger oldLogger = RawDataBlock.log;
|
||||||
DummyPOILogger logger = new DummyPOILogger();
|
DummyPOILogger logger = new DummyPOILogger();
|
||||||
Field fld = RawDataBlock.class.getDeclaredField("log");
|
try {
|
||||||
fld.setAccessible(true);
|
RawDataBlock.log = logger;
|
||||||
fld.set(null, logger);
|
assertEquals(0, logger.logged.size());
|
||||||
assertEquals(0, logger.logged.size());
|
|
||||||
|
|
||||||
// Test for various short sizes
|
// Test for various short sizes
|
||||||
for (int k = 2049; k < 2560; k++)
|
for (int k = 2049; k < 2560; k++)
|
||||||
{
|
|
||||||
byte[] data = new byte[ k ];
|
|
||||||
|
|
||||||
for (int j = 0; j < k; j++)
|
|
||||||
{
|
{
|
||||||
data[ j ] = ( byte ) j;
|
byte[] data = new byte[ k ];
|
||||||
}
|
|
||||||
|
|
||||||
// Check we logged the error
|
for (int j = 0; j < k; j++)
|
||||||
logger.reset();
|
{
|
||||||
new RawDataBlockList(new ByteArrayInputStream(data), POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
|
data[ j ] = ( byte ) j;
|
||||||
assertEquals(1, logger.logged.size());
|
}
|
||||||
|
|
||||||
|
// Check we logged the error
|
||||||
|
logger.reset();
|
||||||
|
new RawDataBlockList(new ByteArrayInputStream(data), POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
|
||||||
|
assertEquals(1, logger.logged.size());
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
RawDataBlock.log = oldLogger;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -947,15 +947,4 @@ public class TestMathX extends AbstractNumericTestCase {
|
|||||||
d = 2d/3; s = 3.33;
|
d = 2d/3; s = 3.33;
|
||||||
assertEquals("floor ", 0, MathX.floor(d, s));
|
assertEquals("floor ", 0, MathX.floor(d, s));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCoverage() throws Exception {
|
|
||||||
// get the default constructor
|
|
||||||
final Constructor<MathX> c = MathX.class.getDeclaredConstructor(new Class[] {});
|
|
||||||
|
|
||||||
// make it callable from the outside
|
|
||||||
c.setAccessible(true);
|
|
||||||
|
|
||||||
// call it
|
|
||||||
c.newInstance((Object[]) null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -240,19 +240,6 @@ public class TestHexDump {
|
|||||||
byteOut.close();
|
byteOut.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testConstruct() throws Exception {
|
|
||||||
// to cover private constructor
|
|
||||||
// get the default constructor
|
|
||||||
final Constructor<HexDump> c = HexDump.class.getDeclaredConstructor(new Class[] {});
|
|
||||||
|
|
||||||
// make it callable from the outside
|
|
||||||
c.setAccessible(true);
|
|
||||||
|
|
||||||
// call it
|
|
||||||
assertNotNull(c.newInstance((Object[]) null));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMain() throws Exception {
|
public void testMain() throws Exception {
|
||||||
File file = TempFile.createTempFile("HexDump", ".dat");
|
File file = TempFile.createTempFile("HexDump", ".dat");
|
||||||
|
@ -22,8 +22,6 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,11 +40,9 @@ public final class TestPOILogger extends POILogger {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testVariousLogTypes() throws Exception {
|
public void testVariousLogTypes() throws Exception {
|
||||||
Field f = POILogFactory.class.getDeclaredField("_loggerClassName");
|
String oldLCN = POILogFactory._loggerClassName;
|
||||||
f.setAccessible(true);
|
|
||||||
String oldLCN = (String)f.get(null);
|
|
||||||
try {
|
try {
|
||||||
f.set(null, TestPOILogger.class.getName());
|
POILogFactory._loggerClassName = TestPOILogger.class.getName();
|
||||||
POILogger log = POILogFactory.getLogger( "foo" );
|
POILogger log = POILogFactory.getLogger( "foo" );
|
||||||
assertTrue(log instanceof TestPOILogger);
|
assertTrue(log instanceof TestPOILogger);
|
||||||
|
|
||||||
@ -71,7 +67,7 @@ public final class TestPOILogger extends POILogger {
|
|||||||
log.log(POILogger.ERROR, "log\nforging", "\nevil","\nlog");
|
log.log(POILogger.ERROR, "log\nforging", "\nevil","\nlog");
|
||||||
assertEquals("log forging evil log", tlog.lastLog);
|
assertEquals("log forging evil log", tlog.lastLog);
|
||||||
} finally {
|
} finally {
|
||||||
f.set(null, oldLCN);
|
POILogFactory._loggerClassName = oldLCN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user