Move reflection-equals to POITestCase
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744169 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
61321b4b37
commit
7b282db0e2
@ -17,100 +17,42 @@
|
|||||||
|
|
||||||
package org.apache.poi.hwpf.model;
|
package org.apache.poi.hwpf.model;
|
||||||
|
|
||||||
import java.lang.reflect.AccessibleObject;
|
import static org.apache.poi.POITestCase.assertReflectEquals;
|
||||||
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.HWPFDocFixture;
|
||||||
import org.apache.poi.hwpf.model.types.DOPAbstractType;
|
import org.apache.poi.hwpf.model.types.DOPAbstractType;
|
||||||
import org.apache.poi.util.SuppressForbidden;
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
// TODO: Add DocumentProperties#equals ???
|
// TODO: Add DocumentProperties#equals ???
|
||||||
|
|
||||||
public final class TestDocumentProperties
|
public final class TestDocumentProperties {
|
||||||
extends TestCase
|
private DocumentProperties _documentProperties = null;
|
||||||
{
|
private HWPFDocFixture _hWPFDocFixture;
|
||||||
private DocumentProperties _documentProperties = null;
|
|
||||||
private HWPFDocFixture _hWPFDocFixture;
|
|
||||||
|
|
||||||
public void testReadWrite()
|
@Test
|
||||||
throws Exception
|
public void testReadWrite() throws Exception {
|
||||||
{
|
int size = DOPAbstractType.getSize();
|
||||||
int size = DOPAbstractType.getSize();
|
byte[] buf = new byte[size];
|
||||||
byte[] buf = new byte[size];
|
_documentProperties.serialize(buf, 0);
|
||||||
|
DocumentProperties newDocProperties = new DocumentProperties(buf, 0, size);
|
||||||
|
|
||||||
_documentProperties.serialize(buf, 0);
|
assertReflectEquals(_documentProperties, newDocProperties);
|
||||||
|
|
||||||
DocumentProperties newDocProperties =
|
|
||||||
new DocumentProperties(buf, 0, size);
|
|
||||||
|
|
||||||
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
|
|
||||||
if(fields[x].getName().equals("$jacocoData")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fields[x].getType().isArray())
|
|
||||||
{
|
|
||||||
assertEquals(fields[x].get(_documentProperties),
|
|
||||||
fields[x].get(newDocProperties));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// ensure that the class was not changed/enhanced, e.g. by code instrumentation like coverage tools
|
|
||||||
assertEquals("Invalid type for field: " + fields[x].getName(),
|
|
||||||
"[B", fields[x].getType().getName());
|
|
||||||
|
|
||||||
byte[] buf1 = (byte[])fields[x].get(_documentProperties);
|
|
||||||
byte[] buf2 = (byte[])fields[x].get(newDocProperties);
|
|
||||||
Arrays.equals(buf1, buf2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
protected void setUp()
|
/** TODO verify the constructors*/
|
||||||
throws Exception
|
_hWPFDocFixture = new HWPFDocFixture(this, HWPFDocFixture.DEFAULT_TEST_FILE);
|
||||||
{
|
_hWPFDocFixture.setUp();
|
||||||
super.setUp();
|
_documentProperties = new DocumentProperties(_hWPFDocFixture._tableStream, _hWPFDocFixture._fib.getFcDop(), _hWPFDocFixture._fib.getLcbDop());
|
||||||
/**@todo verify the constructors*/
|
}
|
||||||
|
|
||||||
_hWPFDocFixture = new HWPFDocFixture(this, HWPFDocFixture.DEFAULT_TEST_FILE);
|
|
||||||
|
|
||||||
_hWPFDocFixture.setUp();
|
|
||||||
|
|
||||||
_documentProperties = new DocumentProperties(_hWPFDocFixture._tableStream, _hWPFDocFixture._fib.getFcDop(), _hWPFDocFixture._fib.getLcbDop());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void tearDown()
|
|
||||||
throws Exception
|
|
||||||
{
|
|
||||||
_documentProperties = null;
|
|
||||||
_hWPFDocFixture.tearDown();
|
|
||||||
|
|
||||||
_hWPFDocFixture = null;
|
|
||||||
super.tearDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
_documentProperties = null;
|
||||||
|
_hWPFDocFixture.tearDown();
|
||||||
|
_hWPFDocFixture = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,70 +17,44 @@
|
|||||||
|
|
||||||
package org.apache.poi.hwpf.model;
|
package org.apache.poi.hwpf.model;
|
||||||
|
|
||||||
import java.lang.reflect.AccessibleObject;
|
import static org.apache.poi.POITestCase.assertReflectEquals;
|
||||||
import java.lang.reflect.Field;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import java.security.AccessController;
|
|
||||||
import java.security.PrivilegedActionException;
|
|
||||||
import java.security.PrivilegedExceptionAction;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
|
|
||||||
import org.apache.poi.hwpf.HWPFDocFixture;
|
import org.apache.poi.hwpf.HWPFDocFixture;
|
||||||
import org.apache.poi.util.SuppressForbidden;
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public final class TestFileInformationBlock extends TestCase {
|
public final class TestFileInformationBlock {
|
||||||
private FileInformationBlock _fileInformationBlock = null;
|
private FileInformationBlock _fileInformationBlock = null;
|
||||||
private HWPFDocFixture _hWPFDocFixture;
|
private HWPFDocFixture _hWPFDocFixture;
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testReadWrite() throws Exception {
|
public void testReadWrite() throws Exception {
|
||||||
|
final FibBase expected = _fileInformationBlock.getFibBase();
|
||||||
int size = _fileInformationBlock.getSize();
|
int size = _fileInformationBlock.getSize();
|
||||||
byte[] buf = new byte[size];
|
byte[] buf = new byte[size];
|
||||||
|
expected.serialize(buf, 0);
|
||||||
|
|
||||||
_fileInformationBlock.getFibBase().serialize(buf, 0);
|
FileInformationBlock newFileInformationBlock = new FileInformationBlock(buf);
|
||||||
|
FibBase actual = newFileInformationBlock.getFibBase();
|
||||||
|
|
||||||
FileInformationBlock newFileInformationBlock = new FileInformationBlock(
|
assertReflectEquals(expected, actual);
|
||||||
buf);
|
|
||||||
|
|
||||||
final Field[] fields;
|
|
||||||
try {
|
|
||||||
fields = AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() {
|
|
||||||
@Override
|
|
||||||
@SuppressForbidden("Test only")
|
|
||||||
public Field[] run() throws Exception {
|
|
||||||
final Field[] fields = FileInformationBlock.class.getSuperclass().getDeclaredFields();
|
|
||||||
AccessibleObject.setAccessible(fields, true);
|
|
||||||
return fields;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (PrivilegedActionException pae) {
|
|
||||||
throw pae.getException();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = 0; x < fields.length; x++) {
|
|
||||||
assertEquals(fields[x].get(_fileInformationBlock),
|
|
||||||
fields[x].get(newFileInformationBlock));
|
|
||||||
}
|
|
||||||
|
|
||||||
assertNotNull(_fileInformationBlock.toString());
|
assertNotNull(_fileInformationBlock.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Before
|
||||||
protected void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
|
||||||
/** @todo verify the constructors */
|
/** @todo verify the constructors */
|
||||||
_hWPFDocFixture = new HWPFDocFixture(this,
|
_hWPFDocFixture = new HWPFDocFixture(this, HWPFDocFixture.DEFAULT_TEST_FILE);
|
||||||
HWPFDocFixture.DEFAULT_TEST_FILE);
|
|
||||||
|
|
||||||
_hWPFDocFixture.setUp();
|
_hWPFDocFixture.setUp();
|
||||||
_fileInformationBlock = _hWPFDocFixture._fib;
|
_fileInformationBlock = _hWPFDocFixture._fib;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@After
|
||||||
protected void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
_fileInformationBlock = null;
|
_fileInformationBlock = null;
|
||||||
_hWPFDocFixture.tearDown();
|
_hWPFDocFixture.tearDown();
|
||||||
|
|
||||||
_hWPFDocFixture = null;
|
_hWPFDocFixture = null;
|
||||||
super.tearDown();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,16 +17,21 @@
|
|||||||
|
|
||||||
package org.apache.poi;
|
package org.apache.poi;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
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 static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
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.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.poi.util.SuppressForbidden;
|
import org.apache.poi.util.SuppressForbidden;
|
||||||
@ -106,4 +111,51 @@ public final class POITestCase {
|
|||||||
throw new RuntimeException("Cannot access method '" + methodName + "' of class " + clazz, pae.getException());
|
throw new RuntimeException("Cannot access method '" + methodName + "' of class " + clazz, pae.getException());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility method to shallow compare all fields of the objects
|
||||||
|
* Only use this method in test cases!!!
|
||||||
|
*/
|
||||||
|
public static void assertReflectEquals(final Object expected, Object actual) throws Exception {
|
||||||
|
final List<Field> fields;
|
||||||
|
try {
|
||||||
|
fields = AccessController.doPrivileged(new PrivilegedExceptionAction<List<Field>>() {
|
||||||
|
@Override
|
||||||
|
@SuppressForbidden("Test only")
|
||||||
|
public List<Field> run() throws Exception {
|
||||||
|
List<Field> flds = new ArrayList<Field>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user