aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2016-05-16 23:28:25 +0000
committerAndreas Beeker <kiwiwings@apache.org>2016-05-16 23:28:25 +0000
commit0bab7f3aba94f7111fcefae3e726b0c4ab250f38 (patch)
treec796255defd4ede4468546077ff762cef6f3686e /src
parent6aa54cb23070feff2de323a3ea020d7f0dc294a5 (diff)
downloadpoi-0bab7f3aba94f7111fcefae3e726b0c4ab250f38.tar.gz
poi-0bab7f3aba94f7111fcefae3e726b0c4ab250f38.zip
Move reflection-equals to POITestCase
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744169 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java110
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java60
-rw-r--r--src/testcases/org/apache/poi/POITestCase.java52
3 files changed, 95 insertions, 127 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java
index b600f07763..9dfc572080 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java
@@ -17,100 +17,42 @@
package org.apache.poi.hwpf.model;
-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 static org.apache.poi.POITestCase.assertReflectEquals;
import org.apache.poi.hwpf.HWPFDocFixture;
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 ???
-public final class TestDocumentProperties
- extends TestCase
-{
- private DocumentProperties _documentProperties = null;
- private HWPFDocFixture _hWPFDocFixture;
-
- public void testReadWrite()
- throws Exception
- {
- int size = DOPAbstractType.getSize();
- byte[] buf = new byte[size];
-
- _documentProperties.serialize(buf, 0);
+public final class TestDocumentProperties {
+ private DocumentProperties _documentProperties = null;
+ private HWPFDocFixture _hWPFDocFixture;
- DocumentProperties newDocProperties =
- new DocumentProperties(buf, 0, size);
+ @Test
+ public void testReadWrite() throws Exception {
+ int size = DOPAbstractType.getSize();
+ byte[] buf = new byte[size];
+ _documentProperties.serialize(buf, 0);
+ 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();
+ assertReflectEquals(_documentProperties, newDocProperties);
}
-
- 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 {
+ /** 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 setUp()
- throws Exception
- {
- super.setUp();
- /**@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;
+ }
}
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java
index 56a85c3bfb..e6e21458cf 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java
@@ -17,70 +17,44 @@
package org.apache.poi.hwpf.model;
-import java.lang.reflect.AccessibleObject;
-import java.lang.reflect.Field;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
-
-import junit.framework.TestCase;
+import static org.apache.poi.POITestCase.assertReflectEquals;
+import static org.junit.Assert.assertNotNull;
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 HWPFDocFixture _hWPFDocFixture;
+ @Test
public void testReadWrite() throws Exception {
+ final FibBase expected = _fileInformationBlock.getFibBase();
int size = _fileInformationBlock.getSize();
byte[] buf = new byte[size];
+ expected.serialize(buf, 0);
- _fileInformationBlock.getFibBase().serialize(buf, 0);
-
- FileInformationBlock newFileInformationBlock = new FileInformationBlock(
- 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();
- }
+ FileInformationBlock newFileInformationBlock = new FileInformationBlock(buf);
+ FibBase actual = newFileInformationBlock.getFibBase();
- for (int x = 0; x < fields.length; x++) {
- assertEquals(fields[x].get(_fileInformationBlock),
- fields[x].get(newFileInformationBlock));
- }
-
+ assertReflectEquals(expected, actual);
assertNotNull(_fileInformationBlock.toString());
}
- @Override
- protected void setUp() throws Exception {
- super.setUp();
+ @Before
+ public void setUp() throws Exception {
/** @todo verify the constructors */
- _hWPFDocFixture = new HWPFDocFixture(this,
- HWPFDocFixture.DEFAULT_TEST_FILE);
-
+ _hWPFDocFixture = new HWPFDocFixture(this, HWPFDocFixture.DEFAULT_TEST_FILE);
_hWPFDocFixture.setUp();
_fileInformationBlock = _hWPFDocFixture._fib;
}
- @Override
- protected void tearDown() throws Exception {
+ @After
+ public void tearDown() throws Exception {
_fileInformationBlock = null;
_hWPFDocFixture.tearDown();
-
_hWPFDocFixture = null;
- super.tearDown();
}
}
diff --git a/src/testcases/org/apache/poi/POITestCase.java b/src/testcases/org/apache/poi/POITestCase.java
index 61e45833da..8fa16c88a8 100644
--- a/src/testcases/org/apache/poi/POITestCase.java
+++ b/src/testcases/org/apache/poi/POITestCase.java
@@ -17,16 +17,21 @@
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.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.lang.reflect.AccessibleObject;
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.ArrayList;
+import java.util.List;
import java.util.Map;
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());
}
}
+
+ /**
+ * 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));
+ }
+ }
+ }
}