You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2018/04/04 07:32:51 UTC

[1/3] [lang] SerializationUtilsTest expected exceptions

Repository: commons-lang
Updated Branches:
  refs/heads/master 9901bf98e -> e51bd8920


SerializationUtilsTest expected exceptions

Use the expected argument of the @Test annotation instead of
boiler-plate implementing this behavior with a try-catch-fail
construct in order to clean up the code and make it more obvious to
the reader.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/1415c9a2
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/1415c9a2
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/1415c9a2

Branch: refs/heads/master
Commit: 1415c9a2a6317dbdca6f1fbb530db1f558d9bef5
Parents: aff0fae
Author: Allon Mureinik <am...@redhat.com>
Authored: Wed Apr 4 07:07:20 2018 +0300
Committer: pascalschumacher <pa...@gmx.net>
Committed: Wed Apr 4 09:32:22 2018 +0200

----------------------------------------------------------------------
 .../commons/lang3/SerializationUtilsTest.java   | 88 +++++---------------
 1 file changed, 21 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/1415c9a2/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
index cd20602..ff65cb4 100644
--- a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -116,16 +115,11 @@ public class SerializationUtilsTest {
         }
     }
 
-    @Test
+    @Test(expected = SerializationException.class)
     public void testSerializeStreamUnserializable() throws Exception {
         final ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
-        try {
-            iMap.put(new Object(), new Object());
-            SerializationUtils.serialize(iMap, streamTest);
-        } catch (final SerializationException ex) {
-            return;
-        }
-        fail();
+        iMap.put(new Object(), new Object());
+        SerializationUtils.serialize(iMap, streamTest);
     }
 
     @Test
@@ -147,24 +141,14 @@ public class SerializationUtilsTest {
         }
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public void testSerializeStreamObjNull() throws Exception {
-        try {
-            SerializationUtils.serialize(iMap, null);
-        } catch (final IllegalArgumentException ex) {
-            return;
-        }
-        fail();
+        SerializationUtils.serialize(iMap, null);
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public void testSerializeStreamNullNull() throws Exception {
-        try {
-            SerializationUtils.serialize(null, null);
-        } catch (final IllegalArgumentException ex) {
-            return;
-        }
-        fail();
+        SerializationUtils.serialize(null, null);
     }
 
     @Test
@@ -230,24 +214,14 @@ public class SerializationUtilsTest {
         assertNull(test);
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public void testDeserializeStreamNull() throws Exception {
-        try {
-            SerializationUtils.deserialize((InputStream) null);
-        } catch (final IllegalArgumentException ex) {
-            return;
-        }
-        fail();
+        SerializationUtils.deserialize((InputStream) null);
     }
 
-    @Test
+    @Test(expected = SerializationException.class)
     public void testDeserializeStreamBadStream() throws Exception {
-        try {
-            SerializationUtils.deserialize(new ByteArrayInputStream(new byte[0]));
-        } catch (final SerializationException ex) {
-            return;
-        }
-        fail();
+        SerializationUtils.deserialize(new ByteArrayInputStream(new byte[0]));
     }
 
     @Test
@@ -293,15 +267,10 @@ public class SerializationUtilsTest {
         }
     }
 
-    @Test
+    @Test(expected = SerializationException.class)
     public void testSerializeBytesUnserializable() throws Exception {
-        try {
-            iMap.put(new Object(), new Object());
-            SerializationUtils.serialize(iMap);
-        } catch (final SerializationException ex) {
-            return;
-        }
-        fail();
+        iMap.put(new Object(), new Object());
+        SerializationUtils.serialize(iMap);
     }
 
     @Test
@@ -355,24 +324,14 @@ public class SerializationUtilsTest {
         assertNull(test);
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public void testDeserializeBytesNull() throws Exception {
-        try {
-            SerializationUtils.deserialize((byte[]) null);
-        } catch (final IllegalArgumentException ex) {
-            return;
-        }
-        fail();
+        SerializationUtils.deserialize((byte[]) null);
     }
 
-    @Test
+    @Test(expected = SerializationException.class)
     public void testDeserializeBytesBadStream() throws Exception {
-        try {
-            SerializationUtils.deserialize(new byte[0]);
-        } catch (final SerializationException ex) {
-            return;
-        }
-        fail();
+        SerializationUtils.deserialize(new byte[0]);
     }
 
     //-----------------------------------------------------------------------
@@ -397,15 +356,10 @@ public class SerializationUtilsTest {
         assertNull(test);
     }
 
-    @Test
+    @Test(expected = SerializationException.class)
     public void testCloneUnserializable() throws Exception {
-        try {
-            iMap.put(new Object(), new Object());
-            SerializationUtils.clone(iMap);
-        } catch (final SerializationException ex) {
-            return;
-        }
-        fail();
+        iMap.put(new Object(), new Object());
+        SerializationUtils.clone(iMap);
     }
 
     @Test


[2/3] [lang] SerializationUtilsTest identity assertions

Posted by pa...@apache.org.
SerializationUtilsTest identity assertions

Replaced calls to assertTrue with a != condition with calls to
assertNotSame calls.
This change retains the functionality, but will produce a more
detailed error message in case the assertion fails.
It also (arguably) makes the test code more straight-forward.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/aff0fae2
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/aff0fae2
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/aff0fae2

Branch: refs/heads/master
Commit: aff0fae2ec3a7d74356776c183c6986eb9eed520
Parents: 9901bf9
Author: Allon Mureinik <am...@redhat.com>
Authored: Wed Apr 4 06:58:09 2018 +0300
Committer: pascalschumacher <pa...@gmx.net>
Committed: Wed Apr 4 09:32:22 2018 +0200

----------------------------------------------------------------------
 .../commons/lang3/SerializationUtilsTest.java    | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/aff0fae2/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
index cc2614f..cd20602 100644
--- a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.lang3;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -197,12 +198,12 @@ public class SerializationUtilsTest {
         final Object test = SerializationUtils.deserialize(inTest);
         assertNotNull(test);
         assertTrue(test instanceof HashMap<?, ?>);
-        assertTrue(test != iMap);
+        assertNotSame(test, iMap);
         final HashMap<?, ?> testMap = (HashMap<?, ?>) test;
         assertEquals(iString, testMap.get("FOO"));
-        assertTrue(iString != testMap.get("FOO"));
+        assertNotSame(iString, testMap.get("FOO"));
         assertEquals(iInteger, testMap.get("BAR"));
-        assertTrue(iInteger != testMap.get("BAR"));
+        assertNotSame(iInteger, testMap.get("BAR"));
         assertEquals(iMap, testMap);
     }
 
@@ -333,12 +334,12 @@ public class SerializationUtilsTest {
         final Object test = SerializationUtils.deserialize(streamReal.toByteArray());
         assertNotNull(test);
         assertTrue(test instanceof HashMap<?, ?>);
-        assertTrue(test != iMap);
+        assertNotSame(test, iMap);
         final HashMap<?, ?> testMap = (HashMap<?, ?>) test;
         assertEquals(iString, testMap.get("FOO"));
-        assertTrue(iString != testMap.get("FOO"));
+        assertNotSame(iString, testMap.get("FOO"));
         assertEquals(iInteger, testMap.get("BAR"));
-        assertTrue(iInteger != testMap.get("BAR"));
+        assertNotSame(iInteger, testMap.get("BAR"));
         assertEquals(iMap, testMap);
     }
 
@@ -381,12 +382,12 @@ public class SerializationUtilsTest {
         final Object test = SerializationUtils.clone(iMap);
         assertNotNull(test);
         assertTrue(test instanceof HashMap<?,?>);
-        assertTrue(test != iMap);
+        assertNotSame(test, iMap);
         final HashMap<?, ?> testMap = (HashMap<?, ?>) test;
         assertEquals(iString, testMap.get("FOO"));
-        assertTrue(iString != testMap.get("FOO"));
+        assertNotSame(iString, testMap.get("FOO"));
         assertEquals(iInteger, testMap.get("BAR"));
-        assertTrue(iInteger != testMap.get("BAR"));
+        assertNotSame(iInteger, testMap.get("BAR"));
         assertEquals(iMap, testMap);
     }
 


[3/3] [lang] SerializatoinUtilsTest assertArraysEquals (closes #321)

Posted by pa...@apache.org.
SerializatoinUtilsTest assertArraysEquals (closes #321)

Utilize assertArraysEquals to compare arrays instead of boiler plate
implementing it with a for loop.

This change both makes the test code cleaner and improves the output
in case of an assertion failure by showing all the differences between
the two arrays instead of stopping at the first.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/e51bd892
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/e51bd892
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/e51bd892

Branch: refs/heads/master
Commit: e51bd89201477092c32c180e4f8d00569db17ac1
Parents: 1415c9a
Author: Allon Mureinik <am...@redhat.com>
Authored: Wed Apr 4 07:10:28 2018 +0300
Committer: pascalschumacher <pa...@gmx.net>
Committed: Wed Apr 4 09:32:29 2018 +0200

----------------------------------------------------------------------
 .../commons/lang3/SerializationUtilsTest.java      | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e51bd892/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
index ff65cb4..000151d 100644
--- a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.lang3;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -110,9 +111,7 @@ public class SerializationUtilsTest {
         final byte[] testBytes = streamTest.toByteArray();
         final byte[] realBytes = streamReal.toByteArray();
         assertEquals(testBytes.length, realBytes.length);
-        for (int i = 0; i < realBytes.length; i++) {
-            assertEquals(realBytes[i], testBytes[i]);
-        }
+        assertArrayEquals(realBytes, testBytes);
     }
 
     @Test(expected = SerializationException.class)
@@ -136,9 +135,7 @@ public class SerializationUtilsTest {
         final byte[] testBytes = streamTest.toByteArray();
         final byte[] realBytes = streamReal.toByteArray();
         assertEquals(testBytes.length, realBytes.length);
-        for (int i = 0; i < realBytes.length; i++) {
-            assertEquals(realBytes[i], testBytes[i]);
-        }
+        assertArrayEquals(realBytes, testBytes);
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -262,9 +259,7 @@ public class SerializationUtilsTest {
 
         final byte[] realBytes = streamReal.toByteArray();
         assertEquals(testBytes.length, realBytes.length);
-        for (int i = 0; i < realBytes.length; i++) {
-            assertEquals(realBytes[i], testBytes[i]);
-        }
+        assertArrayEquals(realBytes, testBytes);
     }
 
     @Test(expected = SerializationException.class)
@@ -285,9 +280,7 @@ public class SerializationUtilsTest {
 
         final byte[] realBytes = streamReal.toByteArray();
         assertEquals(testBytes.length, realBytes.length);
-        for (int i = 0; i < realBytes.length; i++) {
-            assertEquals(realBytes[i], testBytes[i]);
-        }
+        assertArrayEquals(realBytes, testBytes);
     }
 
     //-----------------------------------------------------------------------