From ea262e6da52fd4da0733f02998f87aebaf502ddb Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 12 Jun 2019 14:32:25 -0400 Subject: Apply simple sonar fixes Note: A number of these were identified, by SonarLint, in the Test classes, which are not typically scanned by Sonar. Removed unnecessary imports. Removed unneeded "throws Xxx". Replaced lambda with method references. Replaced duplicate strings with constants. Replaced try-fail-catch with assert-j methods to eliminate sonar complaints about duplicate failure messages. Added missing @Override annotations. Use map.computeIfAbsent() where appropriate. Also fixed some minor checkstyle issues. Removed unneeded "volatile" declarations. Replaced some if-else constructs with "?:" construct, per sonar. Replaced Object.wait() with CountDownLatch.await(); according to sonar (and javadocs), Object.wait() can return due to "spurious wakeups". Fixed issue whereby CryptoUtilsTest wouldn't run in my Eclipse. Change-Id: Ib6b71ed65662cfd6209400dac57ed69279bf29ec Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../policy/common/utils/gson/GsonTestUtils.java | 6 +- .../utils/gson/GsonTestUtilsBuilderTest.java | 4 +- .../policy/common/utils/io/SerializerTest.java | 15 +-- .../common/utils/test/ConstructionErrorTest.java | 8 +- .../common/utils/test/ThrowablesTesterTest.java | 8 +- .../test/log/logback/ExtractAppenderTest.java | 102 +++++++++++---------- 6 files changed, 72 insertions(+), 71 deletions(-) (limited to 'utils-test') diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java index d3354e49..f37f32a1 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-management * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -61,7 +61,7 @@ public class GsonTestUtils { /** * Engine used to interpolate strings before they're compared. */ - private static volatile ScriptEngine engineInstance = null; + private static ScriptEngine engineInstance = null; /** * Used to encode and decode an object via gson. @@ -258,7 +258,7 @@ public class GsonTestUtils { */ public String gsonEncode(Object object) { String sgson = gson.toJson(object); - logger.debug("gson=" + sgson); + logger.debug("gson={}", sgson); return sgson; } diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsBuilderTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsBuilderTest.java index 3e4244dd..466041e3 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsBuilderTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsBuilderTest.java @@ -35,13 +35,11 @@ import org.junit.Test; public class GsonTestUtilsBuilderTest { - private GsonTestUtilsBuilder bldr; private GsonTestUtils utils; @Before public void setUp() { - bldr = new MyBuilder(); - utils = bldr.build(); + utils = new MyBuilder().build(); } @Test 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 ee66195c..95abd4db 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 @@ -44,6 +44,7 @@ import org.onap.policy.common.utils.io.Serializer.Factory; import org.powermock.reflect.Whitebox; public class SerializerTest { + private static final String FACTORY = "factory"; /** * Saved and restored when tests complete. Also restored at the start of each test. @@ -52,12 +53,12 @@ public class SerializerTest { @BeforeClass public static void setUpBeforeClass() { - saveFactory = Whitebox.getInternalState(Serializer.class, "factory"); + saveFactory = Whitebox.getInternalState(Serializer.class, FACTORY); } @AfterClass public static void tearDownAfterClass() { - Whitebox.setInternalState(Serializer.class, "factory", saveFactory); + Whitebox.setInternalState(Serializer.class, FACTORY, saveFactory); } @Before @@ -89,7 +90,7 @@ public class SerializerTest { } @Test - public void testSerialize_ArrayCloseEx() throws Exception { + public void testSerialize_ArrayCloseEx() { IOException ex = new IOException("testSerialize_ArrayCloseEx"); /* @@ -123,7 +124,7 @@ public class SerializerTest { } @Test - public void testSerialize_ObjectWriteEx() throws Exception { + public void testSerialize_ObjectWriteEx() { IOException ex = new IOException("testSerialize_ObjectWriteEx"); /* @@ -158,7 +159,7 @@ public class SerializerTest { @Override public void writeObject(Object object, ObjectOutputStream oos) throws IOException { - return; + // do nothing } }); @@ -205,7 +206,7 @@ public class SerializerTest { @Override public void writeObject(Object object, ObjectOutputStream oos) throws IOException { - return; + // do nothing } }); @@ -370,7 +371,7 @@ public class SerializerTest { * @param factory new factory to be set */ private void setFactory(Factory factory) { - Whitebox.setInternalState(Serializer.class, "factory", factory); + Whitebox.setInternalState(Serializer.class, FACTORY, factory); } /** diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.java index 710926d3..8a60a5d2 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.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. @@ -27,7 +27,7 @@ import org.junit.Test; public class ConstructionErrorTest extends ErrorsTester { @Test - public void test() throws Exception { + public void test() { assertEquals(4, testAllError(ConstructionError.class)); } diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java index 4d289c6a..3cb53d81 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java @@ -2,7 +2,7 @@ * ============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. @@ -71,7 +71,7 @@ public class ThrowablesTesterTest { @Test(expected = ConstructionError.class) public void testThrowInstantiationException() { - new ThrowablesTester().testAllThrowable(ThrowInstantiationException.class); + new ThrowablesTester().testAllThrowable(ThrowInstantiationThrowable.class); } /** @@ -159,10 +159,10 @@ public class ThrowablesTesterTest { /** * Used to test a failure case - throws InstantiationException when constructed. */ - public static class ThrowInstantiationException extends Throwable { + public static class ThrowInstantiationThrowable extends Throwable { private static final long serialVersionUID = 1L; - public ThrowInstantiationException(String message, Throwable cause, boolean enableSuppression, + public ThrowInstantiationThrowable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) throws InstantiationException { throw new InstantiationException(ThrowablesTester.EXPECTED_EXCEPTION_MSG); diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java index e01ed417..cd6edf76 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java @@ -40,6 +40,16 @@ import org.junit.Test; import org.slf4j.LoggerFactory; public class ExtractAppenderTest { + private static final String ABC_DIGIT = "abc[0-9]"; + private static final String ABC_DIGIT1 = "abc[1-9]"; + private static final String DEF_DIGIT = "def[0-9]"; + private static final String HELLO = "hello"; + private static final String HELLO_ABC = "hello abc"; + private static final String HELLO_ABC1_WORLD = "hello abc1 world"; + private static final String HELLO_ABC3 = "hello abc3"; + private static final String WORLD = "world"; + private static final String WORLD_ABC = "world abc"; + private static final String WORLD_GHI2_WORLD = "world ghi2 world"; /** * Milliseconds to wait for a thread to terminate. @@ -51,13 +61,13 @@ public class ExtractAppenderTest { private List threads; @BeforeClass - public static void setUpBeforeClass() throws Exception { + public static void setUpBeforeClass() { logger = (Logger) LoggerFactory.getLogger(ExtractAppenderTest.class); logger.setLevel(Level.INFO); } @Before - public void setUp() throws Exception { + public void setUp() { threads = new LinkedList<>(); } @@ -88,31 +98,31 @@ public class ExtractAppenderTest { addAppender(appender); - logger.info("hello"); - logger.info("world"); + logger.info(HELLO); + logger.info(WORLD); // "append" should always be called assertEquals(2, count.get()); // appender with no patterns - everything should match - assertEquals(strList("hello", "world"), appender.getExtracted()); + assertEquals(strList(HELLO, WORLD), appender.getExtracted()); // add a pattern and verify match - appender.setPattern("abc[0-9]"); + appender.setPattern(ABC_DIGIT); logger.info("hello abc1"); // this should not match logger.info("hello def2"); assertEquals(4, count.get()); - assertEquals(strList("hello", "world", "abc1"), appender.getExtracted()); + assertEquals(strList(HELLO, WORLD, "abc1"), appender.getExtracted()); } @Test public void testExtractAppenderStringArray() { AtomicInteger count = new AtomicInteger(0); - ExtractAppender appender = new ExtractAppender("abc[0-9]", "def[0-9]") { + ExtractAppender appender = new ExtractAppender(ABC_DIGIT, DEF_DIGIT) { @Override protected void append(ILoggingEvent event) { count.incrementAndGet(); @@ -122,8 +132,8 @@ public class ExtractAppenderTest { addAppender(appender); - logger.info("hello abc1 world"); - logger.info("world ghi2 world"); // no match + logger.info(HELLO_ABC1_WORLD); + logger.info(WORLD_GHI2_WORLD); // no match logger.info("world def3 world"); logger.info("hello abc4"); logger.info("abc5 world"); @@ -168,7 +178,7 @@ public class ExtractAppenderTest { } }; - ExtractAppender appender = new ExtractAppender(queue, "abc[0-9]"); + ExtractAppender appender = new ExtractAppender(queue, ABC_DIGIT); addAppender(appender); // these shouldn't match @@ -182,7 +192,7 @@ public class ExtractAppenderTest { for (int x = 0; x < nmatches; ++x) { String msg = "abc" + x; - logger.info(msg + " world"); + logger.info("{} world", msg); if (x < nallowed) { expected.add(msg); @@ -199,10 +209,10 @@ public class ExtractAppenderTest { public void testAppendILoggingEvent_NoPatterns() { ExtractAppender appender = makeAppender(); - logger.info("hello"); - logger.info("world"); + logger.info(HELLO); + logger.info(WORLD); - assertEquals(strList("hello", "world"), appender.getExtracted()); + assertEquals(strList(HELLO, WORLD), appender.getExtracted()); } @Test @@ -216,7 +226,7 @@ public class ExtractAppenderTest { @Test public void testAppendILoggingEvent_MatchFirstPattern() { - ExtractAppender appender = makeAppender("abc[0-9]", "def[0-9]"); + ExtractAppender appender = makeAppender(ABC_DIGIT, DEF_DIGIT); logger.info("hello abc1"); logger.info("world xyz2"); @@ -226,7 +236,7 @@ public class ExtractAppenderTest { @Test public void testAppendILoggingEvent_MatchLastPattern() { - ExtractAppender appender = makeAppender("abc[0-9]", "def[0-9]"); + ExtractAppender appender = makeAppender(ABC_DIGIT, DEF_DIGIT); logger.info("hello def1"); logger.info("world xyz2"); @@ -236,41 +246,41 @@ public class ExtractAppenderTest { @Test public void testAppendILoggingEvent_Group1() { - ExtractAppender appender = makeAppender("hello (abc)|(xyz)", "def[0-9]"); + ExtractAppender appender = makeAppender("hello (abc)|(xyz)", DEF_DIGIT); logger.info("hello abc, world!"); - logger.info("world abc"); + logger.info(WORLD_ABC); assertEquals(strList("abc"), appender.getExtracted()); } @Test public void testAppendILoggingEvent_Group3() { - ExtractAppender appender = makeAppender("hello (abc)|(pdq)|(xyz)", "def[0-9]"); + ExtractAppender appender = makeAppender("hello (abc)|(pdq)|(xyz)", DEF_DIGIT); logger.info("say hello xyz, world!"); - logger.info("world abc"); + logger.info(WORLD_ABC); assertEquals(strList("xyz"), appender.getExtracted()); } @Test public void testAppendILoggingEvent_NoGroup() { - ExtractAppender appender = makeAppender("hello abc"); + ExtractAppender appender = makeAppender(HELLO_ABC); logger.info("say hello abc, world!"); - logger.info("world abc"); + logger.info(WORLD_ABC); - assertEquals(strList("hello abc"), appender.getExtracted()); + assertEquals(strList(HELLO_ABC), appender.getExtracted()); } @Test public void testGetExtracted() { - ExtractAppender appender = makeAppender("abc[1-9]"); + ExtractAppender appender = makeAppender(ABC_DIGIT1); - logger.info("hello abc1 world"); - logger.info("world ghi2 world"); // no match - logger.info("hello abc3"); + logger.info(HELLO_ABC1_WORLD); + logger.info(WORLD_GHI2_WORLD); // no match + logger.info(HELLO_ABC3); List oldlst = appender.getExtracted(); assertEquals(strList("abc1", "abc3"), oldlst); @@ -282,11 +292,11 @@ public class ExtractAppenderTest { @Test public void testClearExtractions() { - final ExtractAppender appender = makeAppender("abc[1-9]"); + final ExtractAppender appender = makeAppender(ABC_DIGIT1); - logger.info("hello abc1 world"); - logger.info("world ghi2 world"); - logger.info("hello abc3"); + logger.info(HELLO_ABC1_WORLD); + logger.info(WORLD_GHI2_WORLD); + logger.info(HELLO_ABC3); assertEquals(strList("abc1", "abc3"), appender.getExtracted()); @@ -305,11 +315,11 @@ public class ExtractAppenderTest { @Test public void testSetPattern() { - final ExtractAppender appender = makeAppender("abc[1-9]"); + final ExtractAppender appender = makeAppender(ABC_DIGIT1); - logger.info("hello abc1 world"); - logger.info("world ghi2 world"); // no match - logger.info("hello abc3"); + logger.info(HELLO_ABC1_WORLD); + logger.info(WORLD_GHI2_WORLD); // no match + logger.info(HELLO_ABC3); assertEquals(strList("abc1", "abc3"), appender.getExtracted()); @@ -351,28 +361,20 @@ public class ExtractAppenderTest { } }; - ExtractAppender app = new ExtractAppender(queue, "abc[1-9]"); + ExtractAppender app = new ExtractAppender(queue, ABC_DIGIT1); addAppender(app); // create some threads to add another pattern - addThread(tend, err, xtxt -> { - app.setPattern("def[0-9]"); - }); + addThread(tend, err, xtxt -> app.setPattern(DEF_DIGIT)); // create some threads to log "abc" messages - addThread(tend, err, xtxt -> { - logger.info("hello abc" + xtxt + "world!"); - }); + addThread(tend, err, xtxt -> logger.info("{}{}world!", HELLO_ABC, xtxt)); // create some threads to log "def" messages - addThread(tend, err, xtxt -> { - logger.info("hello def" + xtxt + "world!"); - }); + addThread(tend, err, xtxt -> logger.info("hello def{}world!", xtxt)); // create some threads to get extractions - addThread(tend, err, xtxt -> { - app.getExtracted(); - }); + addThread(tend, err, xtxt -> app.getExtracted()); // create some threads to clear extractions addThread(tend, err, xtxt -> { @@ -381,7 +383,7 @@ public class ExtractAppenderTest { // don't want to clear the list too frequently // so sleep a bit in between try { - Thread.sleep(10 + Integer.valueOf(xtxt)); + Thread.sleep(10L + Integer.valueOf(xtxt)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); -- cgit 1.2.3-korg