aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2018-09-21 10:37:29 -0400
committerJim Hahn <jrh3@att.com>2018-09-21 10:37:29 -0400
commit9a42498247f49a09b56c381ddb009e02127c2234 (patch)
tree1f6202eb73a5282bdb4a9613ad59994a7d96ca59
parentea37c0eb6f4f93596135035b0e34f29599735c32 (diff)
new sonar issues in policy/common
Only throw one type of exception in Serializer. Try to increase test coverage in Serializer. Change-Id: I170de0ab727041aa42731c08d6cc454731d29a20 Issue-ID: POLICY-1130 Signed-off-by: Jim Hahn <jrh3@att.com>
-rw-r--r--utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java12
-rw-r--r--utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java73
2 files changed, 56 insertions, 29 deletions
diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java b/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java
index 227b810a..31e0b850 100644
--- a/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java
+++ b/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java
@@ -83,9 +83,6 @@ public class Serializer {
* so we'll just do it with a factory method.
*/
return clazz.cast(factory.readObject(ois));
-
- } catch (ClassNotFoundException e) {
- throw new IOException(e);
}
}
@@ -127,8 +124,13 @@ public class Serializer {
oos.writeObject(object);
}
- public Object readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
- return ois.readObject();
+ public Object readObject(ObjectInputStream ois) throws IOException {
+ try {
+ return ois.readObject();
+
+ } catch (ClassNotFoundException e) {
+ throw new IOException(e);
+ }
}
}
diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java
index bdf0dba9..613897d9 100644
--- a/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java
+++ b/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java
@@ -163,6 +163,54 @@ public class SerializerTest {
}
@Test
+ public void testSerialize_BothCloseEx() throws Exception {
+ IOException ex = new IOException("testSerialize_BothCloseEx");
+ IOException ex2 = new IOException("testSerialize_BothCloseEx_2");
+ ObjectOutputStream oos = mock(ObjectOutputStream.class);
+ doThrow(ex2).when(oos).close();
+
+ /*
+ * This stream will throw an exception when close() is invoked. However, close()
+ * is called twice, once by the ObjectOutputStream and once by the code we want to
+ * test. As a result, we'll have the first close() succeed and the second one
+ * fail.
+ */
+ ByteArrayOutputStream out = new ByteArrayOutputStream() {
+ private int nclose = 0;
+
+ @Override
+ public void close() throws IOException {
+ if (++nclose > 1) {
+ throw ex;
+ }
+ }
+ };
+
+ /*
+ * Use a factory that returns our special stream.
+ */
+ setFactory(new Factory() {
+ @Override
+ public ByteArrayOutputStream makeByteArrayOutputStream() {
+ return out;
+ }
+
+ @Override
+ public ObjectOutputStream makeObjectOutputStream(ByteArrayOutputStream out) throws IOException {
+ return oos;
+ }
+
+ @Override
+ public void writeObject(Object object, ObjectOutputStream oos) throws IOException {
+ return;
+ }
+ });
+
+ assertEquals(ex2, expectException(() -> Serializer.serialize(new MyObject(130))));
+
+ }
+
+ @Test
public void testDeserialize() throws Exception {
MyObject obj1 = new MyObject(3);
byte[] data = Serializer.serialize(obj1);
@@ -202,29 +250,6 @@ public class SerializerTest {
}
@Test
- public void testDeserialize_ObjectReadClassEx() throws Exception {
- ClassNotFoundException ex = new ClassNotFoundException("testDeserialize_ObjectReadClassEx");
-
- /*
- * Use a factory that throws a ClassNotFoundException when readObject() is
- * invoked.
- */
- setFactory(new Factory() {
- @Override
- public Object readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
- throw ex;
- }
- });
-
- byte[] data = Serializer.serialize(new MyObject(305));
-
- Exception exwrap = expectException(() -> Serializer.deserialize(MyObject.class, data));
- assertTrue(exwrap instanceof IOException);
- assertNotNull(exwrap.getCause());
- assertEquals(ex, exwrap.getCause());
- }
-
- @Test
public void testDeserialize_ObjectReadEx() throws Exception {
IOException ex = new IOException("testDeserialize_ObjectReadEx");
@@ -233,7 +258,7 @@ public class SerializerTest {
*/
setFactory(new Factory() {
@Override
- public Object readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
+ public Object readObject(ObjectInputStream ois) throws IOException {
throw ex;
}
});