From cbaadf54ab70e3d74ad87bff1ed1c36c560c2a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20=C5=BBygie=C5=82o?= Date: Fri, 11 Mar 2022 13:15:06 +0100 Subject: [PATCH 1/3] (doc) UTs for in/ex-cludes with null element This verifies/locks current behavior of throwing NPEx on null being dereferenced in DirectoryScanner.setIncludes/Excludes. --- .../shared/utils/io/DirectoryScannerTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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..ffa35854 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,40 @@ public void testSimpleIncludes() /* expExclDirs */ NONE ); } + @Rule + public ExpectedException xcludesNPExRule = ExpectedException.none(); + + @Test + public void testIncludesWithNull() + throws Exception + { + testXcludesWithNull( new String[]{ null }, null ); + } + + @Test + public void testExcludesWithNull() + throws Exception + { + testXcludesWithNull( null, new String[]{ null } ); + } + + private void testXcludesWithNull( String[] includes, String[] excludes ) + throws Exception + { + createTestData(); + xcludesNPExRule.expect( NullPointerException.class ); + + 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() { From 3f5644220b69c467e7bf6e054e49d2c78869b856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20=C5=BBygie=C5=82o?= Date: Fri, 4 Mar 2022 11:54:11 +0100 Subject: [PATCH 2/3] (doc) Throw more descriptive NPEx --- .../maven/shared/utils/io/DirectoryScanner.java | 13 +++++++++++++ 1 file changed, 13 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} */ From 83b9e9137f78034a0219bee768bad759115a7863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20=C5=BBygie=C5=82o?= Date: Fri, 11 Mar 2022 13:21:20 +0100 Subject: [PATCH 3/3] (doc) Update UT to verify customised message --- .../apache/maven/shared/utils/io/DirectoryScannerTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 ffa35854..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 @@ -136,21 +136,22 @@ public void testSimpleIncludes() public void testIncludesWithNull() throws Exception { - testXcludesWithNull( new String[]{ null }, null ); + testXcludesWithNull( new String[]{ null }, null, "includes" ); } @Test public void testExcludesWithNull() throws Exception { - testXcludesWithNull( null, new String[]{ null } ); + testXcludesWithNull( null, new String[]{ null }, "excludes" ); } - private void testXcludesWithNull( String[] includes, String[] 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,