Skip to content

Commit

Permalink
(doc) Throw more descriptive NPEx (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
pzygielo authored Mar 11, 2022
1 parent ad8dd6a commit 412b279
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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 ) )
Expand All @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 412b279

Please sign in to comment.