From 412b279a9b24d4ae08889743dd9e6fb275d12973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20=C5=BBygie=C5=82o?= <11896137+pzygielo@users.noreply.github.com> Date: Fri, 11 Mar 2022 14:52:13 +0100 Subject: [PATCH] (doc) Throw more descriptive NPEx (#95) --- .../shared/utils/io/DirectoryScanner.java | 13 +++++++ .../shared/utils/io/DirectoryScannerTest.java | 36 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java b/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java index bd8bd7d9..8f92fd2b 100644 --- a/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java +++ b/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java @@ -321,6 +321,10 @@ public void setIncludes( final String... includes ) this.includes = new String[includes.length]; for ( int i = 0; i < includes.length; i++ ) { + if ( includes[i] == null ) + { + throw new NullPointerException( messageForNullListElement( "includes" ) ); + } String pattern; pattern = includes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ); if ( pattern.endsWith( File.separator ) ) @@ -353,6 +357,10 @@ public void setExcludes( final String... excludes ) this.excludes = new String[excludes.length]; for ( int i = 0; i < excludes.length; i++ ) { + if ( excludes[i] == null ) + { + throw new NullPointerException( messageForNullListElement( "excludes" ) ); + } String pattern; pattern = excludes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ); if ( pattern.endsWith( File.separator ) ) @@ -364,6 +372,11 @@ public void setExcludes( final String... excludes ) } } + private static String messageForNullListElement( String listName ) + { + return "If a non-null " + listName + " list is given, all elements must be non-null"; + } + /** * @param scanConductor {@link #scanConductor} */ diff --git a/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java b/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java index 01110069..9dffdad5 100644 --- a/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java +++ b/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java @@ -25,6 +25,7 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import java.io.File; @@ -128,6 +129,41 @@ public void testSimpleIncludes() /* expExclDirs */ NONE ); } + @Rule + public ExpectedException xcludesNPExRule = ExpectedException.none(); + + @Test + public void testIncludesWithNull() + throws Exception + { + testXcludesWithNull( new String[]{ null }, null, "includes" ); + } + + @Test + public void testExcludesWithNull() + throws Exception + { + testXcludesWithNull( null, new String[]{ null }, "excludes" ); + } + + private void testXcludesWithNull( String[] includes, String[] excludes, String listName ) + throws Exception + { + createTestData(); + xcludesNPExRule.expect( NullPointerException.class ); + xcludesNPExRule.expectMessage( "If a non-null " + listName + " list is given, all elements must be non-null" ); + + fitScanTest( true, true, true, + /* includes */ includes, + /* excludes */ excludes, + /* expInclFiles */ new String[]{ "file3.dat", "folder1/file5.dat" }, + /* expInclDirs */ NONE, + /* expNotInclFiles */ new String[]{ "file1.txt", "file2.txt", "folder1/file4.txt" }, + /* expNotInclDirs */ new String[]{ "", "folder1" }, + /* expExclFiles */ NONE, + /* expExclDirs */ NONE ); + } + @Test public void checkSymlinkBehaviour() {