From f10d38d4af0178d008c69ada7cd1a6abc6ec075a Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 13 Feb 2019 16:16:45 -0500 Subject: Sonar fixes Added test for Serializer to increase junit coverage. Added tests for TestTimeMulti to increase junit coverage. Sonar fixes for PropertyConfiguration: - use equalsIgnoreCase - use the exception Updated license dates. Some fixes for LoggerFactoryWrapper to address sonar issue - utility classes should typically have a private constructor. Change-Id: I8957e9673fe8371ecca7abbb7ece87b0d6f46c1a Issue-ID: POLICY-1519 Signed-off-by: Jim Hahn --- .../onap/policy/common/utils/io/Serializer.java | 6 ++--- .../policy/common/utils/time/TestTimeMulti.java | 14 +++++------ .../policy/common/utils/io/SerializerTest.java | 28 ++++++++++++++++++++-- .../common/utils/time/TestTimeMultiTest.java | 17 ++++++++++--- 4 files changed, 50 insertions(+), 15 deletions(-) (limited to 'utils-test') 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 600bbd39..9ab26d32 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine - Common Modules * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,7 +126,7 @@ public class Serializer { /** * Read the object. - * + * * @param ois input stream * @return the object * @throws IOException throws IO exception if cannot read @@ -134,7 +134,7 @@ public class Serializer { public Object readObject(ObjectInputStream ois) throws IOException { try { return ois.readObject(); - + } catch (ClassNotFoundException e) { throw new IOException(e); } diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java index 2782eb72..b37e49e0 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils-Test * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -57,7 +57,7 @@ public class TestTimeMulti extends CurrentTime { /** * Constructor. - * + * * @param nthreads number of threads that will be sleeping simultaneously */ public TestTimeMulti(int nthreads) { @@ -99,7 +99,7 @@ public class TestTimeMulti extends CurrentTime { * Indicates that a thread has terminated or that it will no longer be invoking * {@link #sleep(long)}. Awakens the next sleeping thread, if the queue is full after * removing the terminated thread. - * + * * @throws IllegalStateException if the queue is already full */ public void threadCompleted() { @@ -159,7 +159,7 @@ public class TestTimeMulti extends CurrentTime { /** * Constructor. - * + * * @param awakenAtMs time, in milliseconds, at which the associated thread should * awaken */ @@ -181,7 +181,7 @@ public class TestTimeMulti extends CurrentTime { /** * Blocks the current thread until awakened (i.e., until its latch is * decremented). - * + * * @throws InterruptedException can be interrupted */ public void await() throws InterruptedException { 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 36812b3b..ee66195c 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 @@ -33,6 +33,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.junit.AfterClass; import org.junit.Before; @@ -195,7 +197,7 @@ public class SerializerTest { public ByteArrayOutputStream makeByteArrayOutputStream() { return out; } - + @Override public ObjectOutputStream makeObjectOutputStream(ByteArrayOutputStream out) throws IOException { return oos; @@ -208,7 +210,7 @@ public class SerializerTest { }); assertThatThrownBy(() -> Serializer.serialize(new MyObject(130))).isEqualTo(ex2); - + } @Test @@ -268,6 +270,28 @@ public class SerializerTest { assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex); } + @Test + public void testDeserialize_ObjectRead_ClassEx() throws Exception { + MyObject obj1 = new MyObject(200); + + // must use binary character set + Charset binary = StandardCharsets.ISO_8859_1; + + // serialize the object + String text = new String(Serializer.serialize(obj1), binary); + + /* + * Replace the class name with a bogus class name, which should cause + * ClassNotFoundException when we attempt to deserialize it. + */ + text = text.replace("MyObject", "AnObject"); + + byte[] data = text.getBytes(binary); + + assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isInstanceOf(IOException.class) + .hasCauseInstanceOf(ClassNotFoundException.class); + } + @Test public void testDeserialize_ObjectCloseEx() throws Exception { IOException ex = new IOException("testDeserialize_ObjectCloseEx"); diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java index 311e276d..f17235a2 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils-Test * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,6 +20,7 @@ package org.onap.policy.common.utils.time; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -72,6 +73,9 @@ public class TestTimeMultiTest { } assertTrue(ttm.getMillis() >= tbeg + NTIMES * MIN_SLEEP_MS); + + // something in the queue, but no threads remain -> exception + assertThatIllegalStateException().isThrownBy(() -> ttm.threadCompleted()); } private class MyThread extends Thread { @@ -95,6 +99,13 @@ public class TestTimeMultiTest { public void run() { try { for (int x = 0; x < NTIMES; ++x) { + // negative sleep should have no effect + texpected = ttm.getMillis(); + ttm.sleep(-1); + if ((tactual = ttm.getMillis()) != texpected) { + break; + } + texpected = ttm.getMillis() + sleepMs; ttm.sleep(sleepMs); -- cgit 1.2.3-korg