private byte[] _data;
private boolean _eof;
private boolean _hasData;
- private static POILogger log = POILogFactory.getLogger(RawDataBlock.class);
+ static POILogger log = POILogFactory.getLogger(RawDataBlock.class);
/**
* Constructor RawDataBlock
* The name of the class to use. Initialised the
* first time we need it
*/
- private static String _loggerClassName = null;
+ static String _loggerClassName = null;
/**
* Construct a POILogFactory.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.zip.ZipOutputStream;
import org.apache.poi.EncryptedDocumentException;
+import org.apache.poi.POITestCase;
import org.apache.poi.POIXMLException;
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
assertTrue(tempFile2.delete());
}
- private static ContentTypeManager getContentTypeManager(OPCPackage pkg)
- throws IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
- Field f = OPCPackage.class.getDeclaredField("contentTypeManager");
- f.setAccessible(true);
- return (ContentTypeManager)f.get(pkg);
+ private static ContentTypeManager getContentTypeManager(OPCPackage pkg) {
+ return POITestCase.getFieldValue(OPCPackage.class, pkg, ContentTypeManager.class, "contentTypeManager");
}
@Test
import java.io.IOException;\r
import java.io.InputStream;\r
import java.io.OutputStream;\r
-import java.lang.reflect.Method;\r
import java.net.ConnectException;\r
import java.net.HttpURLConnection;\r
import java.net.MalformedURLException;\r
import java.util.List;\r
\r
import org.apache.poi.POIDataSamples;\r
+import org.apache.poi.POITestCase;\r
import org.apache.poi.openxml4j.opc.OPCPackage;\r
import org.apache.poi.openxml4j.opc.PackageAccess;\r
import org.apache.poi.poifs.crypt.dsig.DigestInfo;\r
XSSFWorkbook wb = new XSSFWorkbook(pkg);\r
wb.setSheetName(0, "manipulated");\r
// ... I don't know, why commit is protected ...\r
- Method m = XSSFWorkbook.class.getDeclaredMethod("commit");\r
- m.setAccessible(true);\r
- m.invoke(wb);\r
+ POITestCase.callMethod(XSSFWorkbook.class, wb, Void.class, "commit", new Class[0], new Object[0]);\r
\r
// todo: test a manipulation on a package part, which is not signed\r
// ... maybe in combination with #56164 \r
package org.apache.poi.xslf.usermodel;\r
\r
import java.io.File;\r
-import java.lang.reflect.Field;\r
\r
import org.apache.poi.POIDataSamples;\r
import org.apache.poi.xslf.util.PPTX2PNG;\r
-import org.junit.AfterClass;\r
-import org.junit.BeforeClass;\r
import org.junit.Test;\r
\r
/**\r
* @author Yegor Kozlov\r
*/\r
public class TestPPTX2PNG {\r
- private static boolean jaxpDebugEnable = false;\r
- \r
- @BeforeClass\r
- public static void activateJaxpDebug() {\r
- jaxpDebugEnable = setDebugFld(true);\r
- }\r
-\r
- @AfterClass\r
- public static void resetJaxpDebug() {\r
- setDebugFld(jaxpDebugEnable);\r
- } \r
- \r
- private static boolean setDebugFld(boolean enable) {\r
- // enable jaxp debugging because of jaxb/stax error in gump build\r
- try {\r
- Class<?> clz = Class.forName("javax.xml.stream.FactoryFinder");\r
- Field fld = clz.getDeclaredField("debug");\r
- fld.setAccessible(true);\r
- boolean isDebug = (Boolean)fld.get(null);\r
- fld.set(null, enable);\r
- return isDebug;\r
- } catch (Exception e) {\r
- // ignore\r
- return false;\r
- }\r
- }\r
\r
@Test\r
public void render() throws Exception {\r
import java.io.File;
import java.io.FileOutputStream;
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.Cell;
import org.apache.poi.ss.usermodel.Row;
@SuppressWarnings("resource")
SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true);
- Field f = SXSSFWorkbook.class.getDeclaredField("_sharedStringSource");
- f.setAccessible(true);
- SharedStringsTable sss = (SharedStringsTable)f.get(wb);
+ SharedStringsTable sss = POITestCase.getFieldValue(SXSSFWorkbook.class, wb, SharedStringsTable.class, "_sharedStringSource");
assertNotNull(sss);
row.createCell(2).setCellValue("A");
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());
assertTrue(wb.dispose());
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import junit.framework.TestCase;
import org.apache.poi.hwpf.HWPFDocFixture;
import org.apache.poi.hwpf.model.types.DOPAbstractType;
+import org.apache.poi.util.SuppressForbidden;
+
+// TODO: Add DocumentProperties#equals ???
public final class TestDocumentProperties
extends TestCase
DocumentProperties newDocProperties =
new DocumentProperties(buf, 0, size);
- Field[] fields = DocumentProperties.class.getSuperclass().getDeclaredFields();
- AccessibleObject.setAccessible(fields, true);
-
+ final Field[] fields;
+ 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++)
{
// JaCoCo Code Coverage adds it's own field, don't look at this one here
import static org.junit.Assert.assertTrue;
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 org.apache.poi.util.SuppressForbidden;
+
/**
* Parent class for POI JUnit TestCases, which provide additional
* features
}
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);
+ }
+ }
}
package org.apache.poi.hssf.record.aggregates;
-import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
+import org.apache.poi.POITestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SharedFormulaRecord;
* {@link RowRecordsAggregate}.
*/
public static SharedValueManager extractFromRRA(RowRecordsAggregate rra) {
- Field f;
- 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);
- }
+ return POITestCase.getFieldValue(RowRecordsAggregate.class, rra, SharedValueManager.class, "_sharedValueManager");
}
public void testBug52527() {
import java.lang.reflect.Field;
+import org.apache.poi.POITestCase;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.HSSFFormulaParser;
* @return a reference to the wrapped {@link NameRecord}
*/
public static NameRecord getNameRecord(HSSFName definedName) {
-
- 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);
- }
+ return POITestCase.getFieldValue(HSSFName.class, definedName, NameRecord.class, "_definedNameRec");
}
public TestHSSFName() {
package org.apache.poi.hssf.usermodel;\r
\r
import junit.framework.TestCase;\r
+\r
+import org.apache.poi.POITestCase;\r
import org.apache.poi.ddf.EscherContainerRecord;\r
import org.apache.poi.ddf.EscherSpgrRecord;\r
import org.apache.poi.hssf.HSSFTestDataSamples;\r
}\r
\r
private static EscherSpgrRecord getSpgrRecord(HSSFShapeGroup group) {\r
- Field spgrField = null;\r
- try {\r
- spgrField = group.getClass().getDeclaredField("_spgrRecord");\r
- spgrField.setAccessible(true);\r
- return (EscherSpgrRecord) spgrField.get(group);\r
- } catch (NoSuchFieldException e) {\r
- e.printStackTrace();\r
- } catch (IllegalAccessException e) {\r
- e.printStackTrace();\r
- }\r
- return null;\r
+ return POITestCase.getFieldValue(HSSFShapeGroup.class, group, EscherSpgrRecord.class, "_spgrRecord");\r
}\r
\r
public void testClearShapes(){\r
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.lang.reflect.Field;
import java.util.Random;
import junit.framework.TestCase;
import org.apache.poi.util.DummyPOILogger;
+import org.apache.poi.util.POILogger;
/**
* Class to test RawDataBlock functionality
*/
public void testShortConstructor() throws Exception {
// Get the logger to be used
+ POILogger oldLogger = RawDataBlock.log;
DummyPOILogger logger = new DummyPOILogger();
- Field fld = RawDataBlock.class.getDeclaredField("log");
- fld.setAccessible(true);
- fld.set(null, logger);
- assertEquals(0, logger.logged.size());
-
- // Test for various data sizes
- for (int k = 1; k <= 512; k++)
- {
- byte[] data = new byte[ k ];
-
- for (int j = 0; j < k; j++)
- {
- data[ j ] = ( byte ) j;
- }
- RawDataBlock block = null;
-
- logger.reset();
- assertEquals(0, logger.logged.size());
-
- // Have it created
- block = new RawDataBlock(new ByteArrayInputStream(data));
- assertNotNull(block);
-
- // Check for the warning is there for <512
- if(k < 512) {
- assertEquals(
- "Warning on " + k + " byte short block",
- 1, logger.logged.size()
- );
-
- // Build the expected warning message, and check
- String bts = k + " byte";
- if(k > 1) {
- bts += "s";
- }
-
- 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!",
- logger.logged.get(0)
- );
- } else {
- assertEquals(0, logger.logged.size());
- }
+ try {
+ RawDataBlock.log = logger;
+ assertEquals(0, logger.logged.size());
+
+ // Test for various data sizes
+ for (int k = 1; k <= 512; k++)
+ {
+ byte[] data = new byte[ k ];
+
+ for (int j = 0; j < k; j++)
+ {
+ data[ j ] = ( byte ) j;
+ }
+ RawDataBlock block = null;
+
+ logger.reset();
+ assertEquals(0, logger.logged.size());
+
+ // Have it created
+ block = new RawDataBlock(new ByteArrayInputStream(data));
+ assertNotNull(block);
+
+ // Check for the warning is there for <512
+ if(k < 512) {
+ assertEquals(
+ "Warning on " + k + " byte short block",
+ 1, logger.logged.size()
+ );
+
+ // Build the expected warning message, and check
+ String bts = k + " byte";
+ if(k > 1) {
+ bts += "s";
+ }
+
+ 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!",
+ logger.logged.get(0)
+ );
+ } else {
+ assertEquals(0, logger.logged.size());
+ }
+ }
+ } finally {
+ RawDataBlock.log = oldLogger;
}
}
*/
public void testSlowInputStream() throws Exception {
// Get the logger to be used
- DummyPOILogger logger = new DummyPOILogger();
- Field fld = RawDataBlock.class.getDeclaredField("log");
- fld.setAccessible(true);
- fld.set(null, logger);
- assertEquals(0, logger.logged.size());
-
- // Test for various ok data sizes
- for (int k = 1; k < 512; k++) {
- byte[] data = new byte[ 512 ];
- for (int j = 0; j < data.length; j++) {
- data[j] = (byte) j;
- }
-
- // Shouldn't complain, as there is enough data,
- // even if it dribbles through
- RawDataBlock block =
- new RawDataBlock(new SlowInputStream(data, k));
- assertFalse(block.eof());
- }
-
- // But if there wasn't enough data available, will
- // complain
- for (int k = 1; k < 512; k++) {
- byte[] data = new byte[ 511 ];
- for (int j = 0; j < data.length; j++) {
- data[j] = (byte) j;
- }
-
- logger.reset();
- assertEquals(0, logger.logged.size());
-
- // Should complain, as there isn't enough data
- RawDataBlock block =
- new RawDataBlock(new SlowInputStream(data, k));
- assertNotNull(block);
- assertEquals(
- "Warning on " + k + " byte short block",
- 1, logger.logged.size()
- );
- }
+ POILogger oldLogger = RawDataBlock.log;
+ DummyPOILogger logger = new DummyPOILogger();
+ try {
+ RawDataBlock.log = logger;
+ assertEquals(0, logger.logged.size());
+
+ // Test for various ok data sizes
+ for (int k = 1; k < 512; k++) {
+ byte[] data = new byte[ 512 ];
+ for (int j = 0; j < data.length; j++) {
+ data[j] = (byte) j;
+ }
+
+ // Shouldn't complain, as there is enough data,
+ // even if it dribbles through
+ RawDataBlock block =
+ new RawDataBlock(new SlowInputStream(data, k));
+ assertFalse(block.eof());
+ }
+
+ // But if there wasn't enough data available, will
+ // complain
+ for (int k = 1; k < 512; k++) {
+ byte[] data = new byte[ 511 ];
+ for (int j = 0; j < data.length; j++) {
+ data[j] = (byte) j;
+ }
+
+ logger.reset();
+ assertEquals(0, logger.logged.size());
+
+ // Should complain, as there isn't enough data
+ RawDataBlock block =
+ new RawDataBlock(new SlowInputStream(data, k));
+ assertNotNull(block);
+ assertEquals(
+ "Warning on " + k + " byte short block",
+ 1, logger.logged.size()
+ );
+ }
+ } finally {
+ RawDataBlock.log = oldLogger;
+ }
}
/**
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.lang.reflect.Field;
-
import junit.framework.TestCase;
import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.DummyPOILogger;
+import org.apache.poi.util.POILogger;
/**
* Class to test RawDataBlockList functionality
*/
public void testShortConstructor() throws Exception {
// Get the logger to be used
+ POILogger oldLogger = RawDataBlock.log;
DummyPOILogger logger = new DummyPOILogger();
- Field fld = RawDataBlock.class.getDeclaredField("log");
- fld.setAccessible(true);
- fld.set(null, logger);
- assertEquals(0, logger.logged.size());
-
- // Test for various short sizes
- for (int k = 2049; k < 2560; k++)
- {
- byte[] data = new byte[ k ];
-
- for (int j = 0; j < k; j++)
+ try {
+ RawDataBlock.log = logger;
+ assertEquals(0, logger.logged.size());
+
+ // Test for various short sizes
+ for (int k = 2049; k < 2560; k++)
{
- data[ j ] = ( byte ) j;
+ byte[] data = new byte[ k ];
+
+ for (int j = 0; j < k; j++)
+ {
+ data[ j ] = ( byte ) j;
+ }
+
+ // Check we logged the error
+ logger.reset();
+ new RawDataBlockList(new ByteArrayInputStream(data), POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
+ 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;
}
}
}
d = 2d/3; s = 3.33;
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);
- }
}
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
public void testMain() throws Exception {
File file = TempFile.createTempFile("HexDump", ".dat");
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import java.lang.reflect.Field;
-
import org.junit.Test;
/**
*/
@Test
public void testVariousLogTypes() throws Exception {
- Field f = POILogFactory.class.getDeclaredField("_loggerClassName");
- f.setAccessible(true);
- String oldLCN = (String)f.get(null);
+ String oldLCN = POILogFactory._loggerClassName;
try {
- f.set(null, TestPOILogger.class.getName());
+ POILogFactory._loggerClassName = TestPOILogger.class.getName();
POILogger log = POILogFactory.getLogger( "foo" );
assertTrue(log instanceof TestPOILogger);
log.log(POILogger.ERROR, "log\nforging", "\nevil","\nlog");
assertEquals("log forging evil log", tlog.lastLog);
} finally {
- f.set(null, oldLCN);
+ POILogFactory._loggerClassName = oldLCN;
}
}