poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1744169 - in /poi/trunk/src: scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java testcases/org/apache/poi/POITestCase.java
Date Mon, 16 May 2016 23:28:25 GMT
Author: kiwiwings
Date: Mon May 16 23:28:25 2016
New Revision: 1744169

URL: http://svn.apache.org/viewvc?rev=1744169&view=rev
Log:
Move reflection-equals to POITestCase

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

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java?rev=1744169&r1=1744168&r2=1744169&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java
(original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestDocumentProperties.java
Mon May 16 23:28:25 2016
@@ -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);
-
-    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);
-      }
-    }
+public final class TestDocumentProperties {
+    private DocumentProperties _documentProperties = null;
+    private HWPFDocFixture _hWPFDocFixture;
+
+    @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);
 
-  }
+        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;
+    }
 }

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java?rev=1744169&r1=1744168&r2=1744169&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java
(original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestFileInformationBlock.java
Mon May 16 23:28:25 2016
@@ -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);
+        FileInformationBlock newFileInformationBlock = new FileInformationBlock(buf);
+        FibBase actual = newFileInformationBlock.getFibBase();
 
-        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));
-        }
-        
+        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();
     }
 }

Modified: poi/trunk/src/testcases/org/apache/poi/POITestCase.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/POITestCase.java?rev=1744169&r1=1744168&r2=1744169&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/POITestCase.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/POITestCase.java Mon May 16 23:28:25 2016
@@ -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));
+            }
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message