Improve CodeHelpers coverage 90/100090/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 12 Mar 2022 07:25:59 +0000 (08:25 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 12 Mar 2022 07:48:17 +0000 (08:48 +0100)
We have introduced a new helper, make sure it is covered by UT.

JIRA: MDSAL-722
Change-Id: I5b949c5aac60cf1b90ba98b38afdce09dd08fff4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/CodeHelpers.java
binding/yang-binding/src/test/java/org/opendaylight/yangtools/yang/binding/CodeHelpersTest.java

index 92f26f8006725ea497373ddb4e426b1fb6a2949f..aba378e49ca11f9d737ea36715585828ee385894 100644 (file)
@@ -390,7 +390,7 @@ public final class CodeHelpers {
      * @param fieldName name of the field being filled
      * @param list List, which items should be checked
      * @return Type-checked List
-     * @throws IllegalArgumentException if a list item is not instance of {@code requiredItemClass}
+     * @throws IllegalArgumentException if a list item is not instance of {@code requiredClass}
      * @throws NullPointerException if {@code requiredClass} or {@code fieldName} is null
      */
     @SuppressWarnings("unchecked")
@@ -425,7 +425,7 @@ public final class CodeHelpers {
             try {
                 collection.forEach(item -> requiredClass.cast(requireNonNull(item)));
             } catch (ClassCastException | NullPointerException e) {
-                throw new IllegalArgumentException("Invalid input list item for property \"" + requireNonNull(fieldName)
+                throw new IllegalArgumentException("Invalid input item for property \"" + requireNonNull(fieldName)
                     + "\"", e);
             }
         }
index 769318ed115dabf0218308d7fc366c27ba344bed..a3d81ea2d2fe2650134c12f64e25f3deafa2d8a1 100644 (file)
@@ -15,6 +15,7 @@ import static org.junit.Assert.assertThrows;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 import org.junit.Test;
 
 public class CodeHelpersTest {
@@ -43,4 +44,20 @@ public class CodeHelpersTest {
             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of(new Object())));
         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
     }
+
+    @Test
+    public void testCheckSetFieldCast() {
+        assertNull(CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", null));
+        assertSame(Set.of(), CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of()));
+        final var list = Set.of(this);
+        assertSame(list, CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", list));
+
+        IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
+            () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Collections.singleton(null)));
+        assertThat(iae.getCause(), instanceOf(NullPointerException.class));
+
+        iae = assertThrows(IllegalArgumentException.class,
+            () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of(new Object())));
+        assertThat(iae.getCause(), instanceOf(ClassCastException.class));
+    }
 }