From 7b282db0e283911f461fdfc25595d79eef1ec8ad Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 16 May 2016 23:28:25 +0000 Subject: [PATCH] Move reflection-equals to POITestCase git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744169 13f79535-47bb-0310-9956-ffa450edef68 --- .../hwpf/model/TestDocumentProperties.java | 112 +++++------------- .../hwpf/model/TestFileInformationBlock.java | 60 +++------- src/testcases/org/apache/poi/POITestCase.java | 52 ++++++++ 3 files changed, 96 insertions(+), 128 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 b600f0776..9dfc57208 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 final class TestDocumentProperties { + private DocumentProperties _documentProperties = null; + private HWPFDocFixture _hWPFDocFixture; - public void testReadWrite() - throws Exception - { - int size = DOPAbstractType.getSize(); - byte[] buf = new byte[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); - _documentProperties.serialize(buf, 0); - - DocumentProperties newDocProperties = - new DocumentProperties(buf, 0, size); - - final Field[] fields; - try { - fields = AccessController.doPrivileged(new PrivilegedExceptionAction() { - @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); - } + assertReflectEquals(_documentProperties, newDocProperties); } - } - - 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(); - } + @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()); + } + @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 56a85c3bf..e6e21458c 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); + FibBase actual = newFileInformationBlock.getFibBase(); - FileInformationBlock newFileInformationBlock = new FileInformationBlock( - buf); - - final Field[] fields; - try { - fields = AccessController.doPrivileged(new PrivilegedExceptionAction() { - @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)); - } - + 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 61e45833d..8fa16c88a 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 fields; + try { + fields = AccessController.doPrivileged(new PrivilegedExceptionAction>() { + @Override + @SuppressForbidden("Test only") + public List run() throws Exception { + List 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); + } + } + 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)); + } + } + } }