Skip to content

Commit a70e66e

Browse files
authored
[MJAVADOC-825] Prefer NullPointerExceptions for null arguments (#350)
* Prefer NullPointerExceptions for null arguments
1 parent 220340f commit a70e66e

File tree

2 files changed

+62
-44
lines changed

2 files changed

+62
-44
lines changed

src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,9 @@ protected static JavaVersion getJavadocVersion(File javadocExe)
497497
Pattern.compile("(?s).*?[^a-zA-Z](([0-9]+\\.?[0-9]*)(\\.[0-9]+)?).*");
498498

499499
/**
500-
* Parse the output for 'javadoc -J-version' and return the javadoc version recognized. <br>
501-
* Here are some output for 'javadoc -J-version' depending the JDK used:
502-
* <table><caption>Output for 'javadoc -J-version' per JDK</caption>
500+
* Parse the output of 'javadoc -J-version' and return the javadoc version recognized. <br>
501+
* Here are some output for 'javadoc -J-version' depending on the JDK used:
502+
* <table><caption>Output of 'javadoc -J-version' per JDK</caption>
503503
* <tr>
504504
* <th>JDK</th>
505505
* <th>Output for 'javadoc -J-version'</th>
@@ -530,15 +530,20 @@ protected static JavaVersion getJavadocVersion(File javadocExe)
530530
* </tr>
531531
* </table>
532532
*
533-
* @param output for 'javadoc -J-version'
533+
* @param output of 'javadoc -J-version'
534534
* @return the version of the javadoc for the output, only digits and dots
535-
* @throws PatternSyntaxException if the output doesn't match with the output pattern
535+
* @throws PatternSyntaxException if the output doesn't match the output pattern
536536
* {@code (?s).*?[^a-zA-Z]([0-9]+\\.?[0-9]*)(\\.([0-9]+))?.*}.
537-
* @throws IllegalArgumentException if the output is null
537+
* @throws NullPointerException if the output is null
538+
* @throws IllegalArgumentException if the output is empty
538539
*/
539-
protected static String extractJavadocVersion(String output) throws IllegalArgumentException {
540-
if (output == null || output.isEmpty()) {
541-
throw new IllegalArgumentException("The output could not be null.");
540+
protected static String extractJavadocVersion(String output) {
541+
if (output == null) {
542+
throw new NullPointerException("The output cannot be null.");
543+
}
544+
545+
if (output.isEmpty()) {
546+
throw new IllegalArgumentException("The output cannot be empty.");
542547
}
543548

544549
Pattern pattern = EXTRACT_JAVADOC_VERSION_PATTERN;
@@ -591,14 +596,18 @@ protected static String extractJavadocVersion(String output) throws IllegalArgum
591596
* </table>
592597
*
593598
* @param memory the memory to be parsed, not null.
594-
* @return the memory parsed with a supported unit. If no unit specified in the <code>memory</code> parameter, the
599+
* @return the memory parsed with a supported unit. If no unit is specified in the <code>memory</code> argument, the
595600
* default unit is <code>m</code>. The units <code>g | gb</code> or <code>t | tb</code> will be converted in
596601
* <code>m</code>.
597-
* @throws IllegalArgumentException if the <code>memory</code> parameter is null or doesn't match any pattern.
602+
* @throws NullPointerException if the <code>memory</code> argument is null
603+
* @throws IllegalArgumentException if the <code>memory</code> argument doesn't match any pattern.
598604
*/
599-
protected static String parseJavadocMemory(String memory) throws IllegalArgumentException {
600-
if (memory == null || memory.isEmpty()) {
601-
throw new IllegalArgumentException("The memory could not be null.");
605+
protected static String parseJavadocMemory(String memory) {
606+
if (memory == null) {
607+
throw new NullPointerException("The memory cannot be null.");
608+
}
609+
if (memory.isEmpty()) {
610+
throw new IllegalArgumentException("The memory cannot be empty.");
602611
}
603612

604613
Matcher m0 = PARSE_JAVADOC_MEMORY_PATTERN_0.matcher(memory);

src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.Set;
43-
import java.util.regex.PatternSyntaxException;
4443

4544
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
4645
import org.apache.maven.plugins.javadoc.ProxyServer.AuthAsyncProxyServlet;
@@ -61,21 +60,31 @@
6160
* @author <a href="mailto:[email protected]">Vincent Siveton</a>
6261
*/
6362
public class JavadocUtilTest extends PlexusTestCase {
64-
/**
65-
* Method to test the javadoc version parsing.
66-
*
67-
*/
68-
public void testParseJavadocVersion() {
69-
String version = null;
63+
64+
public void testParseJavadocVersion_Null() {
7065
try {
71-
JavadocUtil.extractJavadocVersion(version);
66+
JavadocUtil.extractJavadocVersion(null);
7267
fail("Not catch null");
73-
} catch (IllegalArgumentException e) {
74-
assertTrue(true);
68+
} catch (NullPointerException ex) {
69+
assertNotNull(ex.getMessage());
7570
}
71+
}
7672

73+
public void testParseJavadocVersion_EmptyString() {
74+
try {
75+
JavadocUtil.extractJavadocVersion("");
76+
fail("Not catch empty version");
77+
} catch (IllegalArgumentException ex) {
78+
assertNotNull(ex.getMessage());
79+
}
80+
}
81+
82+
/**
83+
* Test the javadoc version parsing.
84+
*/
85+
public void testParseJavadocVersion() {
7786
// Sun JDK 1.4
78-
version = "java full version \"1.4.2_12-b03\"";
87+
String version = "java full version \"1.4.2_12-b03\"";
7988
assertEquals("1.4.2", JavadocUtil.extractJavadocVersion(version));
8089

8190
// Sun JDK 1.5
@@ -126,15 +135,6 @@ public void testParseJavadocVersion() {
126135
version = "java full version \"1.4\"";
127136
assertEquals("1.4", JavadocUtil.extractJavadocVersion(version));
128137

129-
version = "java full version \"1.A.B_07-164\"";
130-
try {
131-
JavadocUtil.extractJavadocVersion(version);
132-
// does not fail since JEP 223 support addition
133-
// assertTrue( "Not catch wrong pattern", false );
134-
} catch (PatternSyntaxException e) {
135-
assertTrue(true);
136-
}
137-
138138
version = "SCO-UNIX-J2SE-1.5.0_09*FCS-UW714-OSR6*_20061114";
139139
assertEquals("1.5.0", JavadocUtil.extractJavadocVersion(version));
140140

@@ -163,20 +163,29 @@ public void testParseJavadocVersion() {
163163
assertEquals("10.0.1", JavadocUtil.extractJavadocVersion(version));
164164
}
165165

166-
/**
167-
* Method to test the javadoc memory parsing.
168-
*
169-
*/
170-
public void testParseJavadocMemory() {
171-
String memory = null;
166+
public void testParseJavadocMemory_null() {
172167
try {
173-
JavadocUtil.parseJavadocMemory(memory);
168+
JavadocUtil.parseJavadocMemory(null);
174169
fail("Not catch null");
175-
} catch (IllegalArgumentException e) {
176-
assertTrue(true);
170+
} catch (NullPointerException ex) {
171+
assertNotNull(ex.getMessage());
172+
}
173+
}
174+
175+
public void testParseJavadocMemory_empty() {
176+
try {
177+
JavadocUtil.parseJavadocMemory("");
178+
fail("Not catch null");
179+
} catch (IllegalArgumentException ex) {
180+
assertNotNull(ex.getMessage());
177181
}
182+
}
178183

179-
memory = "128";
184+
/**
185+
* Method to test the javadoc memory parsing.
186+
*/
187+
public void testParseJavadocMemory() {
188+
String memory = "128";
180189
assertEquals(JavadocUtil.parseJavadocMemory(memory), "128m");
181190

182191
memory = "128k";

0 commit comments

Comments
 (0)