Skip to content

Commit c061ccf

Browse files
author
Stephen Colebourne
committed
Handle null List constructor more gracefully
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@232900 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2c88ec5 commit c061ccf

3 files changed

Lines changed: 22 additions & 15 deletions

File tree

src/java/org/apache/commons/io/filefilter/AndFileFilter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class AndFileFilter
4242

4343
/**
4444
* Constructs a new instance of <code>AndFileFilter</code>.
45+
*
4546
* @since Commons IO 1.1
4647
*/
4748
public AndFileFilter() {
@@ -51,14 +52,16 @@ public AndFileFilter() {
5152
/**
5253
* Constructs a new instance of <code>AndFileFilter</code>
5354
* with the specified list of filters.
54-
* @param fileFilters a List of IOFileFilter instances
55+
*
56+
* @param fileFilters a List of IOFileFilter instances, copied, null ignored
5557
* @since Commons IO 1.1
5658
*/
5759
public AndFileFilter(final List fileFilters) {
5860
if (fileFilters == null) {
59-
throw new IllegalArgumentException("The filters List must not be null");
61+
this.fileFilters = new ArrayList();
62+
} else {
63+
this.fileFilters = new ArrayList(fileFilters);
6064
}
61-
this.fileFilters = new ArrayList(fileFilters);
6265
}
6366

6467
/**

src/java/org/apache/commons/io/filefilter/OrFileFilter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class OrFileFilter
4242

4343
/**
4444
* Constructs a new instance of <code>OrFileFilter</code>.
45+
*
4546
* @since Commons IO 1.1
4647
*/
4748
public OrFileFilter() {
@@ -52,11 +53,15 @@ public OrFileFilter() {
5253
* Constructs a new instance of <code>OrFileFilter</code>
5354
* with the specified filters.
5455
*
55-
* @param fileFilters the file filters for this filter
56+
* @param fileFilters the file filters for this filter, copied, null ignored
5657
* @since Commons IO 1.1
5758
*/
5859
public OrFileFilter(final List fileFilters) {
59-
this.fileFilters = new ArrayList(fileFilters);
60+
if (fileFilters == null) {
61+
this.fileFilters = new ArrayList();
62+
} else {
63+
this.fileFilters = new ArrayList(fileFilters);
64+
}
6065
}
6166

6267
/**

src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void testNameFilter() throws Exception {
209209
public void testNameFilterNullArgument() throws Exception {
210210
String test = null;
211211
try {
212-
IOFileFilter filter = new NameFileFilter(test);
212+
new NameFileFilter(test);
213213
fail( "constructing a NameFileFilter with a null String argument should fail.");
214214
} catch( IllegalArgumentException iae ) {
215215
}
@@ -218,7 +218,7 @@ public void testNameFilterNullArgument() throws Exception {
218218
public void testNameFilterNullArrayArgument() throws Exception {
219219
String[] test = null;
220220
try {
221-
IOFileFilter filter = new NameFileFilter(test);
221+
new NameFileFilter(test);
222222
fail( "constructing a NameFileFilter with a null String[] argument should fail.");
223223
} catch( IllegalArgumentException iae ) {
224224
}
@@ -227,7 +227,7 @@ public void testNameFilterNullArrayArgument() throws Exception {
227227
public void testNameFilterNullListArgument() throws Exception {
228228
List test = null;
229229
try {
230-
IOFileFilter filter = new NameFileFilter(test);
230+
new NameFileFilter(test);
231231
fail( "constructing a NameFileFilter with a null List argument should fail.");
232232
} catch( IllegalArgumentException iae ) {
233233
}
@@ -277,12 +277,9 @@ public void testAnd() throws Exception {
277277
} catch (IllegalArgumentException ex) {
278278
}
279279

280-
try {
281-
new AndFileFilter((List) null);
282-
fail();
283-
} catch (IllegalArgumentException ex) {
284-
}
285-
}
280+
AndFileFilter f = new AndFileFilter((List) null);
281+
assertEquals(true, f.getFileFilters().isEmpty());
282+
}
286283

287284
public void testOr() throws Exception {
288285
IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
@@ -316,9 +313,11 @@ public void testOr() throws Exception {
316313
fail();
317314
} catch (IllegalArgumentException ex) {
318315
}
316+
317+
OrFileFilter f = new OrFileFilter((List) null);
318+
assertEquals(true, f.getFileFilters().isEmpty());
319319
}
320320

321-
322321
public void testWildcard() throws Exception {
323322
IOFileFilter filter = new WildcardFilter("*.txt");
324323
List patternList = Arrays.asList( new String[] { "*.txt", "*.xml", "*.gif" } );

0 commit comments

Comments
 (0)