Skip to content

Commit 412b279

Browse files
authored
(doc) Throw more descriptive NPEx (#95)
1 parent ad8dd6a commit 412b279

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ public void setIncludes( final String... includes )
321321
this.includes = new String[includes.length];
322322
for ( int i = 0; i < includes.length; i++ )
323323
{
324+
if ( includes[i] == null )
325+
{
326+
throw new NullPointerException( messageForNullListElement( "includes" ) );
327+
}
324328
String pattern;
325329
pattern = includes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
326330
if ( pattern.endsWith( File.separator ) )
@@ -353,6 +357,10 @@ public void setExcludes( final String... excludes )
353357
this.excludes = new String[excludes.length];
354358
for ( int i = 0; i < excludes.length; i++ )
355359
{
360+
if ( excludes[i] == null )
361+
{
362+
throw new NullPointerException( messageForNullListElement( "excludes" ) );
363+
}
356364
String pattern;
357365
pattern = excludes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
358366
if ( pattern.endsWith( File.separator ) )
@@ -364,6 +372,11 @@ public void setExcludes( final String... excludes )
364372
}
365373
}
366374

375+
private static String messageForNullListElement( String listName )
376+
{
377+
return "If a non-null " + listName + " list is given, all elements must be non-null";
378+
}
379+
367380
/**
368381
* @param scanConductor {@link #scanConductor}
369382
*/

src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.Ignore;
2626
import org.junit.Rule;
2727
import org.junit.Test;
28+
import org.junit.rules.ExpectedException;
2829
import org.junit.rules.TemporaryFolder;
2930

3031
import java.io.File;
@@ -128,6 +129,41 @@ public void testSimpleIncludes()
128129
/* expExclDirs */ NONE );
129130
}
130131

132+
@Rule
133+
public ExpectedException xcludesNPExRule = ExpectedException.none();
134+
135+
@Test
136+
public void testIncludesWithNull()
137+
throws Exception
138+
{
139+
testXcludesWithNull( new String[]{ null }, null, "includes" );
140+
}
141+
142+
@Test
143+
public void testExcludesWithNull()
144+
throws Exception
145+
{
146+
testXcludesWithNull( null, new String[]{ null }, "excludes" );
147+
}
148+
149+
private void testXcludesWithNull( String[] includes, String[] excludes, String listName )
150+
throws Exception
151+
{
152+
createTestData();
153+
xcludesNPExRule.expect( NullPointerException.class );
154+
xcludesNPExRule.expectMessage( "If a non-null " + listName + " list is given, all elements must be non-null" );
155+
156+
fitScanTest( true, true, true,
157+
/* includes */ includes,
158+
/* excludes */ excludes,
159+
/* expInclFiles */ new String[]{ "file3.dat", "folder1/file5.dat" },
160+
/* expInclDirs */ NONE,
161+
/* expNotInclFiles */ new String[]{ "file1.txt", "file2.txt", "folder1/file4.txt" },
162+
/* expNotInclDirs */ new String[]{ "", "folder1" },
163+
/* expExclFiles */ NONE,
164+
/* expExclDirs */ NONE );
165+
}
166+
131167
@Test
132168
public void checkSymlinkBehaviour()
133169
{

0 commit comments

Comments
 (0)