Clean up AbstractQName.checkLocalName() 11/92211/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 20 Aug 2020 18:40:12 +0000 (20:40 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 21 Aug 2020 06:57:18 +0000 (08:57 +0200)
Do not throw IAE on null, use NPE as usual.

Change-Id: I7cb34ffe5b6cb12408ca87088498295d4f5b4c1a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/AbstractQName.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnqualifiedQName.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/QNameTest.java

index 6709ad94a7db79c306c68166c4fd98948d68fc00..d45cdbed5bf4d1f46f4482663813808d2cbc1bdf 100644 (file)
@@ -75,7 +75,6 @@ public abstract class AbstractQName implements Identifier, WritableObject {
     abstract Object writeReplace();
 
     static final String checkLocalName(final @Nullable String localName) {
-        checkArgument(localName != null, "Parameter 'localName' may not be null.");
         checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
         checkArgument(IDENTIFIER_START.matches(localName.charAt(0)) && NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1,
                 "String '%s' is not a valid identifier", localName);
index f128d2cddf89655eee2c645b41de5e4dfa812acc..fbd77df09a05b65c248cb6602596295513aa0c0d 100644 (file)
@@ -106,11 +106,11 @@ public final class QName extends AbstractQName implements Comparable<QName> {
     /**
      * Creates new QName.
      *
-     * @param qnameModule
-     *            Namespace and revision enclosed as a QNameModule
-     * @param localName
-     *            Local name part of QName. MUST NOT BE null.
+     * @param qnameModule Namespace and revision enclosed as a QNameModule
+     * @param localName Local name part of QName. MUST NOT BE null.
      * @return Instance of QName
+     * @throws NullPointerException if any argument is null
+     * @throws IllegalArgumentException if localName is not a valid YANG identifier
      */
     public static @NonNull QName create(final QNameModule qnameModule, final String localName) {
         return new QName(requireNonNull(qnameModule, "module may not be null"), checkLocalName(localName));
index c596dd86b2d9f1a796f970185fbbf4b1915e2641..0b6bffb54e2e65442683294a9e83ed23ffa412e3 100644 (file)
@@ -37,7 +37,8 @@ public final class UnqualifiedQName extends AbstractQName implements Comparable<
      *
      * @param localName The local name of this unqualified QName
      * @return An UnqualifiedQName instance
-     * @throws IllegalArgumentException if localName is null or it does not conform to YANG localName requirements.
+     * @throws NullPointerException if localName is null
+     * @throws IllegalArgumentException if localName is not a valid YANG identifier
      */
     public static UnqualifiedQName of(final String localName) {
         return new UnqualifiedQName(checkLocalName(localName));
index 055de3f9c19251386da68ce1bb9b24073dc87d6a..97a79d19a3a4ad88e325e391394dbc1d13da88ac 100644 (file)
@@ -9,8 +9,8 @@ package org.opendaylight.yangtools.yang.common;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -24,31 +24,29 @@ public class QNameTest {
 
     @Test
     public void testStringSerialization() throws Exception {
-        {
-            QName qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
-            assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_REVISION_DELIMITER
-                    + REVISION + QName.QNAME_RIGHT_PARENTHESIS + LOCALNAME, qname.toString());
-            QName copied = QName.create(qname.toString());
-            assertEquals(qname, copied);
-        }
+        QName qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
+        assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_REVISION_DELIMITER + REVISION
+            + QName.QNAME_RIGHT_PARENTHESIS + LOCALNAME, qname.toString());
+        assertEquals(qname, QName.create(qname.toString()));
+    }
+
+    @Test
+    public void testStringSerializationNoRevision() throws Exception {
         // no revision
-        {
-            QName qname = QName.create(NS, LOCALNAME);
-            assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_RIGHT_PARENTHESIS
-                    + LOCALNAME, qname.toString());
-            QName copied = QName.create(qname.toString());
-            assertEquals(qname, copied);
-        }
+        QName qname = QName.create(NS, LOCALNAME);
+        assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_RIGHT_PARENTHESIS + LOCALNAME,
+            qname.toString());
+        assertEquals(qname, QName.create(qname.toString()));
     }
 
     @Test
     public void testIllegalLocalNames() {
-        assertLocalNameFails(null);
-        assertLocalNameFails("");
-        assertLocalNameFails("(");
-        assertLocalNameFails(")");
-        assertLocalNameFails("?");
-        assertLocalNameFails("&");
+        assertThrows(NullPointerException.class, () -> QName.create(NS, null));
+        assertThrows(IllegalArgumentException.class, () -> QName.create(NS, ""));
+        assertThrows(IllegalArgumentException.class, () -> QName.create(NS, "("));
+        assertThrows(IllegalArgumentException.class, () -> QName.create(NS, ")"));
+        assertThrows(IllegalArgumentException.class, () -> QName.create(NS, "?"));
+        assertThrows(IllegalArgumentException.class, () -> QName.create(NS, "&"));
     }
 
     @Test
@@ -88,8 +86,7 @@ public class QNameTest {
         assertEquals(qname1, qname.withoutRevision());
         assertEquals(qname1, qname2);
         assertTrue(qname.isEqualWithoutRevision(qname1));
-        assertNotNull(QName.formattedRevision(Revision.ofNullable("2000-01-01")));
-        assertNotNull(qname.hashCode());
+        assertEquals("2000-01-01", QName.formattedRevision(Revision.ofNullable("2000-01-01")));
         assertEquals(qname, qname.intern());
     }
 
@@ -99,13 +96,4 @@ public class QNameTest {
         assertNotNull(qnameModule.toString());
         assertNotNull(qnameModule.getRevisionNamespace());
     }
-
-    private static void assertLocalNameFails(final String localName) {
-        try {
-            QName.create(NS, localName);
-            fail("Local name should fail:" + localName);
-        } catch (IllegalArgumentException e) {
-            // Expected
-        }
-    }
 }