]> source.dussan.org Git - poi.git/commitdiff
Move reflection-equals to POITestCase
authorAndreas Beeker <kiwiwings@apache.org>
Mon, 16 May 2016 23:28:25 +0000 (23:28 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Mon, 16 May 2016 23:28:25 +0000 (23:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744169 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java
src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java
src/testcases/org/apache/poi/POITestCase.java

index b600f07763876e977a08ad643a0fa1c2e306ecbf..9dfc57208057863eb0272e7ab1e17289334ac575 100644 (file)
 
 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;
+    }
 }
index 56a85c3bfb23e5287d2fa26ffa8ab5c8a1cedbb0..e6e21458cf100d23b7a1f2f06de3c9227939dc1e 100644 (file)
 
 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();
     }
 }
index 61e45833da0b6647188e3a759f659281a67dab9e..8fa16c88a8e74a4ccfb60519c93e109693388265 100644 (file)
 
 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));
+            }
+        }
+    }
 }