From 362b50c1dd4bea09b7caec3ff6b40a0c07e9e662 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Mon, 7 Mar 2016 18:38:26 +0100 Subject: [PATCH] Remove unnecessary String() constructor invocations Document legimitate uses, and clean up the containing test classes (which mostly involves disabling IntelliJ's warnings about invalid uses of equals()). Clean up a few only-null variables and a couple of other small issues. Change-Id: I7a6177b266589c15b9b169a99a9f2a9054da94ed Signed-off-by: Stephen Kitt --- .../yangtools/objcache/spi/CacheTest.java | 2 ++ .../objcache/guava/GuavaObjectCacheTest.java | 2 ++ ...tImmutableNormalizedValueAttrNodeTest.java | 26 +++++++++---------- .../yang/model/util/BitImplTest.java | 15 +++++------ .../model/util/MustDefinitionImplTest.java | 4 ++- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/common/object-cache-api/src/test/java/org/opendaylight/yangtools/objcache/spi/CacheTest.java b/common/object-cache-api/src/test/java/org/opendaylight/yangtools/objcache/spi/CacheTest.java index 37434e5bc2..483ef26b68 100644 --- a/common/object-cache-api/src/test/java/org/opendaylight/yangtools/objcache/spi/CacheTest.java +++ b/common/object-cache-api/src/test/java/org/opendaylight/yangtools/objcache/spi/CacheTest.java @@ -46,6 +46,8 @@ public class CacheTest { } @Test + // This test is based on using different references + @SuppressWarnings("RedundantStringConstructorCall") public void testPresentKey() { final String key1 = new String("abcd"); final String key2 = new String("abcd"); diff --git a/common/object-cache-guava/src/test/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCacheTest.java b/common/object-cache-guava/src/test/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCacheTest.java index bc41da9ab3..b32ac84e9f 100644 --- a/common/object-cache-guava/src/test/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCacheTest.java +++ b/common/object-cache-guava/src/test/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCacheTest.java @@ -37,6 +37,8 @@ public class GuavaObjectCacheTest { } @Test + // This test is based on using different references + @SuppressWarnings("RedundantStringConstructorCall") public void testMultipleReferences() { final String s1 = "abcd"; final String s2 = new String(s1); diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/AbstractImmutableNormalizedValueAttrNodeTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/AbstractImmutableNormalizedValueAttrNodeTest.java index 778bf25a26..2353d3771e 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/AbstractImmutableNormalizedValueAttrNodeTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/AbstractImmutableNormalizedValueAttrNodeTest.java @@ -20,15 +20,14 @@ public class AbstractImmutableNormalizedValueAttrNodeTest { "my-other-leaf"); @Test + // This test is based on using different references; we're testing equals() + @SuppressWarnings({"RedundantStringConstructorCall", "EqualsWithItself"}) public void equalsByteTest() { - byte[] valueNull = null; - byte[] equalValueNull = null; - LeafNode leafNodeNull = ImmutableNodes.leafNode(LEAF_QNAME, - valueNull); + null); LeafNode equalLeafNodeNull = ImmutableNodes.leafNode( - SAME_LEAF_QNAME, equalValueNull); + SAME_LEAF_QNAME, null); assertTrue(leafNodeNull.equals(leafNodeNull)); assertTrue(leafNodeNull.equals(equalLeafNodeNull)); @@ -97,7 +96,7 @@ public class AbstractImmutableNormalizedValueAttrNodeTest { assertTrue(leafNode6.equals(equalLeafNode6)); assertTrue(equalLeafNode6.equals(leafNode6)); - String value5 = new String("test"); + String value5 = "test"; String equalValue5 = new String("test"); LeafNode leafNode5 = ImmutableNodes @@ -112,6 +111,8 @@ public class AbstractImmutableNormalizedValueAttrNodeTest { } @Test + // We're testing equals() + @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"}) public void notEqualByteTest() { byte[] value = "test".getBytes(); @@ -185,8 +186,8 @@ public class AbstractImmutableNormalizedValueAttrNodeTest { assertFalse(leafNode6.equals(otherLeafNode6)); assertFalse(otherLeafNode6.equals(leafNode6)); - String value5 = new String("test"); - String otherValue5 = new String("test2"); + String value5 = "test"; + String otherValue5 = "test2"; LeafNode leafNode5 = ImmutableNodes .leafNode(LEAF_QNAME, value5); @@ -208,15 +209,12 @@ public class AbstractImmutableNormalizedValueAttrNodeTest { assertFalse(leafNode4.equals(leafNode5)); assertFalse(leafNode6.equals(leafNode5)); - byte[] valueNull = null; - LeafNode leafNodeNull = ImmutableNodes.leafNode( - SAME_LEAF_QNAME, valueNull); + SAME_LEAF_QNAME, null); assertFalse(leafNodeNull.equals(leafNode)); assertFalse(leafNode.equals(leafNodeNull)); - byte[] byteValue = new byte[] { new Byte("1").byteValue(), - new Byte("1").byteValue() }; + byte[] byteValue = new byte[] { 1, 1 }; LeafNode byteLeafNode = ImmutableNodes.leafNode( SAME_LEAF_QNAME, byteValue); @@ -226,6 +224,8 @@ public class AbstractImmutableNormalizedValueAttrNodeTest { } @Test + // We're testing equals() + @SuppressWarnings({"EqualsWithItself", "EqualsBetweenInconvertibleTypes"}) public void equalsOtherTypesTest() { char[] valueChar = "test".toCharArray(); diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java index 04d086c4b6..274bc0d197 100644 --- a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java @@ -9,7 +9,7 @@ package org.opendaylight.yangtools.yang.model.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.net.URI; import java.net.URISyntaxException; @@ -26,6 +26,8 @@ import org.opendaylight.yangtools.yang.model.api.Status; public class BitImplTest { @Test + // We're testing equals() + @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"}) public void test() { // hashCode method test @@ -35,7 +37,6 @@ public class BitImplTest { URI uriB = null; URI uriB1 = null; URI uriB2 = null; - boolean urisInitiallized = false; try { uriA = new URI("some:uriA"); uriA1 = new URI("some:uriA1"); @@ -43,12 +44,8 @@ public class BitImplTest { uriB = new URI("some:uriB"); uriB1 = new URI("some:uriB1"); uriB2 = new URI("some:uriB2"); - urisInitiallized = true; - } catch (URISyntaxException e) { - e.printStackTrace(); - assertTrue("Not all required uri variables were instantiated.", urisInitiallized); - + fail("Not all required uri variables were instantiated."); } QName qnameA = QName.create(uriA, new Date(5000000), "some name"); @@ -68,12 +65,12 @@ public class BitImplTest { qnamesB.add(qnameB2); SchemaPath schemaPathB = SchemaPath.create(qnamesB, true); - BitImpl biB = null; + BitImpl biB; BitImpl biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null); assertEquals("biA should equals to itsefl", biA, biA); assertFalse("biA shouldn't equal to null", biA.equals(null)); - assertFalse("biA shouldn't equal to object of other type", biA.equals(new String("str"))); + assertFalse("biA shouldn't equal to object of other type", biA.equals("str")); biA = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null); biB = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null); diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/MustDefinitionImplTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/MustDefinitionImplTest.java index c4425325cb..839f1e72ba 100644 --- a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/MustDefinitionImplTest.java +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/MustDefinitionImplTest.java @@ -16,6 +16,8 @@ import org.junit.Test; public class MustDefinitionImplTest { @Test + // We're testing equals() + @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"}) public void test() { MustDefinitionImpl mdiA; MustDefinitionImpl mdiB; @@ -23,7 +25,7 @@ public class MustDefinitionImplTest { assertEquals("mdiA should equals to itsefl", mdiA, mdiA); assertFalse("mdiA shouldn't equal to null", mdiA.equals(null)); - assertFalse("mdiA shouldn't equal to object of other type", mdiA.equals(new String("str"))); + assertFalse("mdiA shouldn't equal to object of other type", mdiA.equals("str")); // test of equals method -- 2.36.6