This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-javadoc-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new a70e66e6 [MJAVADOC-825] Prefer NullPointerExceptions for null 
arguments (#350)
a70e66e6 is described below

commit a70e66e6245b4cd7e875340c59f7028e1f5868fa
Author: Elliotte Rusty Harold <elh...@users.noreply.github.com>
AuthorDate: Sun Dec 8 20:40:08 2024 +0000

    [MJAVADOC-825] Prefer NullPointerExceptions for null arguments (#350)
    
    * Prefer NullPointerExceptions for null arguments
---
 .../apache/maven/plugins/javadoc/JavadocUtil.java  | 37 +++++++-----
 .../maven/plugins/javadoc/JavadocUtilTest.java     | 69 ++++++++++++----------
 2 files changed, 62 insertions(+), 44 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java 
b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
index d6d39fa8..8da760f1 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java
@@ -497,9 +497,9 @@ public class JavadocUtil {
             
Pattern.compile("(?s).*?[^a-zA-Z](([0-9]+\\.?[0-9]*)(\\.[0-9]+)?).*");
 
     /**
-     * Parse the output for 'javadoc -J-version' and return the javadoc 
version recognized. <br>
-     * Here are some output for 'javadoc -J-version' depending the JDK used:
-     * <table><caption>Output for 'javadoc -J-version' per JDK</caption>
+     * Parse the output of 'javadoc -J-version' and return the javadoc version 
recognized. <br>
+     * Here are some output for 'javadoc -J-version' depending on the JDK used:
+     * <table><caption>Output of 'javadoc -J-version' per JDK</caption>
      * <tr>
      * <th>JDK</th>
      * <th>Output for 'javadoc -J-version'</th>
@@ -530,15 +530,20 @@ public class JavadocUtil {
      * </tr>
      * </table>
      *
-     * @param output for 'javadoc -J-version'
+     * @param output of 'javadoc -J-version'
      * @return the version of the javadoc for the output, only digits and dots
-     * @throws PatternSyntaxException if the output doesn't match with the 
output pattern
+     * @throws PatternSyntaxException if the output doesn't match the output 
pattern
      *             {@code (?s).*?[^a-zA-Z]([0-9]+\\.?[0-9]*)(\\.([0-9]+))?.*}.
-     * @throws IllegalArgumentException if the output is null
+     * @throws NullPointerException if the output is null
+     * @throws IllegalArgumentException if the output is empty
      */
-    protected static String extractJavadocVersion(String output) throws 
IllegalArgumentException {
-        if (output == null || output.isEmpty()) {
-            throw new IllegalArgumentException("The output could not be 
null.");
+    protected static String extractJavadocVersion(String output) {
+        if (output == null) {
+            throw new NullPointerException("The output cannot be null.");
+        }
+
+        if (output.isEmpty()) {
+            throw new IllegalArgumentException("The output cannot be empty.");
         }
 
         Pattern pattern = EXTRACT_JAVADOC_VERSION_PATTERN;
@@ -591,14 +596,18 @@ public class JavadocUtil {
      * </table>
      *
      * @param memory the memory to be parsed, not null.
-     * @return the memory parsed with a supported unit. If no unit specified 
in the <code>memory</code> parameter, the
+     * @return the memory parsed with a supported unit. If no unit is 
specified in the <code>memory</code> argument, the
      *         default unit is <code>m</code>. The units <code>g | gb</code> 
or <code>t | tb</code> will be converted in
      *         <code>m</code>.
-     * @throws IllegalArgumentException if the <code>memory</code> parameter 
is null or doesn't match any pattern.
+     * @throws NullPointerException if the <code>memory</code> argument is null
+     * @throws IllegalArgumentException if the <code>memory</code> argument 
doesn't match any pattern.
      */
-    protected static String parseJavadocMemory(String memory) throws 
IllegalArgumentException {
-        if (memory == null || memory.isEmpty()) {
-            throw new IllegalArgumentException("The memory could not be 
null.");
+    protected static String parseJavadocMemory(String memory) {
+        if (memory == null) {
+            throw new NullPointerException("The memory cannot be null.");
+        }
+        if (memory.isEmpty()) {
+            throw new IllegalArgumentException("The memory cannot be empty.");
         }
 
         Matcher m0 = PARSE_JAVADOC_MEMORY_PATTERN_0.matcher(memory);
diff --git 
a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java 
b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
index 25c4c485..df6fcd99 100644
--- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
+++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java
@@ -40,7 +40,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.regex.PatternSyntaxException;
 
 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
 import org.apache.maven.plugins.javadoc.ProxyServer.AuthAsyncProxyServlet;
@@ -61,21 +60,31 @@ import static org.assertj.core.api.Assertions.assertThat;
  * @author <a href="mailto:vincent.sive...@gmail.com";>Vincent Siveton</a>
  */
 public class JavadocUtilTest extends PlexusTestCase {
-    /**
-     * Method to test the javadoc version parsing.
-     *
-     */
-    public void testParseJavadocVersion() {
-        String version = null;
+
+    public void testParseJavadocVersion_Null() {
         try {
-            JavadocUtil.extractJavadocVersion(version);
+            JavadocUtil.extractJavadocVersion(null);
             fail("Not catch null");
-        } catch (IllegalArgumentException e) {
-            assertTrue(true);
+        } catch (NullPointerException ex) {
+            assertNotNull(ex.getMessage());
         }
+    }
 
+    public void testParseJavadocVersion_EmptyString() {
+        try {
+            JavadocUtil.extractJavadocVersion("");
+            fail("Not catch empty version");
+        } catch (IllegalArgumentException ex) {
+            assertNotNull(ex.getMessage());
+        }
+    }
+
+    /**
+     * Test the javadoc version parsing.
+     */
+    public void testParseJavadocVersion() {
         // Sun JDK 1.4
-        version = "java full version \"1.4.2_12-b03\"";
+        String version = "java full version \"1.4.2_12-b03\"";
         assertEquals("1.4.2", JavadocUtil.extractJavadocVersion(version));
 
         // Sun JDK 1.5
@@ -126,15 +135,6 @@ public class JavadocUtilTest extends PlexusTestCase {
         version = "java full version \"1.4\"";
         assertEquals("1.4", JavadocUtil.extractJavadocVersion(version));
 
-        version = "java full version \"1.A.B_07-164\"";
-        try {
-            JavadocUtil.extractJavadocVersion(version);
-            // does not fail since JEP 223 support addition
-            // assertTrue( "Not catch wrong pattern", false );
-        } catch (PatternSyntaxException e) {
-            assertTrue(true);
-        }
-
         version = "SCO-UNIX-J2SE-1.5.0_09*FCS-UW714-OSR6*_20061114";
         assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
 
@@ -163,20 +163,29 @@ public class JavadocUtilTest extends PlexusTestCase {
         assertEquals("10.0.1", JavadocUtil.extractJavadocVersion(version));
     }
 
-    /**
-     * Method to test the javadoc memory parsing.
-     *
-     */
-    public void testParseJavadocMemory() {
-        String memory = null;
+    public void testParseJavadocMemory_null() {
         try {
-            JavadocUtil.parseJavadocMemory(memory);
+            JavadocUtil.parseJavadocMemory(null);
             fail("Not catch null");
-        } catch (IllegalArgumentException e) {
-            assertTrue(true);
+        } catch (NullPointerException ex) {
+            assertNotNull(ex.getMessage());
+        }
+    }
+
+    public void testParseJavadocMemory_empty() {
+        try {
+            JavadocUtil.parseJavadocMemory("");
+            fail("Not catch null");
+        } catch (IllegalArgumentException ex) {
+            assertNotNull(ex.getMessage());
         }
+    }
 
-        memory = "128";
+    /**
+     * Method to test the javadoc memory parsing.
+     */
+    public void testParseJavadocMemory() {
+        String memory = "128";
         assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
 
         memory = "128k";

Reply via email to