Skip to content

Commit 2c88ec5

Browse files
author
Tim O'Brien
committed
Increased test coverage for file filter tests, and also fixed some nits in CountingInputStreamTest.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@201764 13f79535-47bb-0310-9956-ffa450edef68
1 parent ae49ca8 commit 2c88ec5

5 files changed

Lines changed: 183 additions & 19 deletions

File tree

src/test/org/apache/commons/io/IOUtilsWriteTestCase.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ public void testWrite_stringToOutputStream() throws Exception {
205205
}
206206

207207
public void testWrite_stringToOutputStream_nullData() throws Exception {
208-
String str = new String(inData, "US-ASCII");
209-
210208
ByteArrayOutputStream baout = new ByteArrayOutputStream();
211209
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
212210

@@ -242,8 +240,6 @@ public void testWrite_stringToOutputStream_Encoding() throws Exception {
242240
}
243241

244242
public void testWrite_stringToOutputStream_Encoding_nullData() throws Exception {
245-
String str = new String(inData, "US-ASCII");
246-
247243
ByteArrayOutputStream baout = new ByteArrayOutputStream();
248244
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
249245

@@ -293,8 +289,6 @@ public void testWrite_stringToWriter() throws Exception {
293289
}
294290

295291
public void testWrite_stringToWriter_Encoding_nullData() throws Exception {
296-
String str = new String(inData, "US-ASCII");
297-
298292
ByteArrayOutputStream baout = new ByteArrayOutputStream();
299293
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
300294
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
@@ -330,8 +324,6 @@ public void testWrite_charArrayToOutputStream() throws Exception {
330324
}
331325

332326
public void testWrite_charArrayToOutputStream_nullData() throws Exception {
333-
String str = new String(inData, "US-ASCII");
334-
335327
ByteArrayOutputStream baout = new ByteArrayOutputStream();
336328
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
337329

@@ -418,8 +410,6 @@ public void testWrite_charArrayToWriter() throws Exception {
418410
}
419411

420412
public void testWrite_charArrayToWriter_Encoding_nullData() throws Exception {
421-
String str = new String(inData, "US-ASCII");
422-
423413
ByteArrayOutputStream baout = new ByteArrayOutputStream();
424414
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
425415
Writer writer = new OutputStreamWriter(baout, "US-ASCII");

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

Lines changed: 180 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
package org.apache.commons.io.filefilter;
1717

1818
import java.io.File;
19+
import java.io.FileFilter;
20+
import java.io.FilenameFilter;
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.List;
1924

2025
import junit.framework.TestSuite;
2126
import junit.textui.TestRunner;
@@ -68,10 +73,16 @@ public void assertFiltering(IOFileFilter filter, File file, boolean expected) th
6873

6974
public void testSuffix() throws Exception {
7075
IOFileFilter filter = new SuffixFileFilter(new String[] { "tes", "est" });
76+
File testFile = new File( "test" );
77+
File fredFile = new File( "fred" );
7178
assertFiltering(filter, new File("fred.tes"), true);
7279
assertFiltering(filter, new File("fred.est"), true);
7380
assertFiltering(filter, new File("fred.EST"), false); //case-sensitive
7481
assertFiltering(filter, new File("fred.exe"), false);
82+
83+
filter = FileFilterUtils.orFileFilter(
84+
FileFilterUtils.suffixFileFilter( "tes" ),
85+
FileFilterUtils.suffixFileFilter( "est" ) );
7586
assertFiltering(filter, new File("fred"), false);
7687
assertFiltering(filter, new File(".tes"), true);
7788
assertFiltering(filter, new File("fred.test"), true);
@@ -80,12 +91,33 @@ public void testSuffix() throws Exception {
8091
assertFiltering(filter, new File("test"), true);
8192
assertFiltering(filter, new File("fred"), false);
8293

94+
assertTrue( filter.accept( testFile.getParentFile(), testFile.getName() ) );
95+
assertTrue( !filter.accept( fredFile.getParentFile(), fredFile.getName() ) );
96+
97+
List prefixes = Arrays.asList( new String[] { "ood", "red" } );
98+
IOFileFilter listFilter = new SuffixFileFilter( prefixes );
99+
100+
assertTrue( !listFilter.accept( testFile.getParentFile(), testFile.getName() ) );
101+
assertTrue( listFilter.accept( fredFile.getParentFile(), fredFile.getName() ) );
102+
83103
try {
84104
new SuffixFileFilter((String) null);
85105
fail();
86106
} catch (IllegalArgumentException ex) {
87107
}
88-
}
108+
109+
try {
110+
new SuffixFileFilter((String[]) null);
111+
fail();
112+
} catch (IllegalArgumentException ex) {
113+
}
114+
115+
try {
116+
new SuffixFileFilter((List) null);
117+
fail();
118+
} catch (IllegalArgumentException ex) {
119+
}
120+
}
89121

90122
public void testDirectory() throws Exception {
91123
IOFileFilter filter = new DirectoryFileFilter();
@@ -103,11 +135,16 @@ public void testDirectory() throws Exception {
103135

104136
public void testPrefix() throws Exception {
105137
IOFileFilter filter = new PrefixFileFilter(new String[] { "foo", "bar" });
138+
File testFile = new File( "test" );
139+
File fredFile = new File( "fred" );
140+
106141
assertFiltering(filter, new File("foo.test"), true);
107142
assertFiltering(filter, new File("FOO.test"), false); //case-sensitive
108143
assertFiltering(filter, new File("foo"), true);
109144
assertFiltering(filter, new File("bar"), true);
110145
assertFiltering(filter, new File("food/"), true);
146+
147+
filter = FileFilterUtils.prefixFileFilter( "bar" );
111148
assertFiltering(filter, new File("barred\\"), true);
112149
assertFiltering(filter, new File("test"), false);
113150
assertFiltering(filter, new File("fo_o.test"), false);
@@ -117,11 +154,33 @@ public void testPrefix() throws Exception {
117154
assertFiltering(filter, new File("test"), true);
118155
assertFiltering(filter, new File("fred"), false);
119156

157+
assertTrue( filter.accept( testFile.getParentFile(), testFile.getName() ) );
158+
assertTrue( !filter.accept( fredFile.getParentFile(), fredFile.getName() ) );
159+
160+
List prefixes = Arrays.asList( new String[] { "foo", "fre" } );
161+
IOFileFilter listFilter = new PrefixFileFilter( prefixes );
162+
163+
assertTrue( !listFilter.accept( testFile.getParentFile(), testFile.getName() ) );
164+
assertTrue( listFilter.accept( fredFile.getParentFile(), fredFile.getName() ) );
165+
166+
120167
try {
121168
new PrefixFileFilter((String) null);
122169
fail();
123170
} catch (IllegalArgumentException ex) {
124171
}
172+
173+
try {
174+
new PrefixFileFilter((String[]) null);
175+
fail();
176+
} catch (IllegalArgumentException ex) {
177+
}
178+
179+
try {
180+
new PrefixFileFilter((List) null);
181+
fail();
182+
} catch (IllegalArgumentException ex) {
183+
}
125184
}
126185

127186
public void testNameFilter() throws Exception {
@@ -147,6 +206,33 @@ public void testNameFilter() throws Exception {
147206
assertFiltering(filter, new File("fred"), false);
148207
}
149208

209+
public void testNameFilterNullArgument() throws Exception {
210+
String test = null;
211+
try {
212+
IOFileFilter filter = new NameFileFilter(test);
213+
fail( "constructing a NameFileFilter with a null String argument should fail.");
214+
} catch( IllegalArgumentException iae ) {
215+
}
216+
}
217+
218+
public void testNameFilterNullArrayArgument() throws Exception {
219+
String[] test = null;
220+
try {
221+
IOFileFilter filter = new NameFileFilter(test);
222+
fail( "constructing a NameFileFilter with a null String[] argument should fail.");
223+
} catch( IllegalArgumentException iae ) {
224+
}
225+
}
226+
227+
public void testNameFilterNullListArgument() throws Exception {
228+
List test = null;
229+
try {
230+
IOFileFilter filter = new NameFileFilter(test);
231+
fail( "constructing a NameFileFilter with a null List argument should fail.");
232+
} catch( IllegalArgumentException iae ) {
233+
}
234+
}
235+
150236
public void testTrue() throws Exception {
151237
IOFileFilter filter = FileFilterUtils.trueFileFilter();
152238
assertFiltering(filter, new File("foo.test"), true);
@@ -180,20 +266,51 @@ public void testAnd() throws Exception {
180266
assertFiltering(new AndFileFilter(trueFilter, falseFilter), new File("foo.test"), false);
181267
assertFiltering(new AndFileFilter(falseFilter, trueFilter), new File("foo.test"), false);
182268
assertFiltering(new AndFileFilter(falseFilter, falseFilter), new File("foo.test"), false);
269+
270+
List filters = new ArrayList();
271+
assertFiltering( new AndFileFilter( filters ), new File( "test" ), false );
272+
assertFiltering( new AndFileFilter(), new File( "test" ), false );
273+
183274
try {
184275
new AndFileFilter(falseFilter, null);
185276
fail();
186277
} catch (IllegalArgumentException ex) {
187278
}
188-
}
279+
280+
try {
281+
new AndFileFilter((List) null);
282+
fail();
283+
} catch (IllegalArgumentException ex) {
284+
}
285+
}
189286

190287
public void testOr() throws Exception {
191288
IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
192289
IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
193-
assertFiltering(new OrFileFilter(trueFilter, trueFilter), new File("foo.test"), true);
194-
assertFiltering(new OrFileFilter(trueFilter, falseFilter), new File("foo.test"), true);
195-
assertFiltering(new OrFileFilter(falseFilter, trueFilter), new File("foo.test"), true);
196-
assertFiltering(new OrFileFilter(falseFilter, falseFilter), new File("foo.test"), false);
290+
File testFile = new File( "foo.test" );
291+
assertFiltering(new OrFileFilter(trueFilter, trueFilter), testFile, true);
292+
assertFiltering(new OrFileFilter(trueFilter, falseFilter), testFile, true);
293+
assertFiltering(new OrFileFilter(falseFilter, trueFilter), testFile, true);
294+
assertFiltering(new OrFileFilter(falseFilter, falseFilter), testFile, false);
295+
assertFiltering(new OrFileFilter(), testFile, false);
296+
297+
List filters = new ArrayList();
298+
filters.add( trueFilter );
299+
filters.add( falseFilter );
300+
301+
OrFileFilter orFilter = new OrFileFilter( filters );
302+
303+
assertFiltering(orFilter, testFile, true);
304+
assertEquals( orFilter.getFileFilters(), filters );
305+
orFilter.removeFileFilter( trueFilter );
306+
assertFiltering(orFilter, testFile, false);
307+
orFilter.setFileFilters( filters );
308+
assertFiltering(orFilter, testFile, true);
309+
310+
assertTrue( orFilter.accept( testFile.getParentFile(), testFile.getName() ) );
311+
orFilter.removeFileFilter( trueFilter );
312+
assertTrue( !orFilter.accept( testFile.getParentFile(), testFile.getName() ) );
313+
197314
try {
198315
new OrFileFilter(falseFilter, null);
199316
fail();
@@ -204,6 +321,12 @@ public void testOr() throws Exception {
204321

205322
public void testWildcard() throws Exception {
206323
IOFileFilter filter = new WildcardFilter("*.txt");
324+
List patternList = Arrays.asList( new String[] { "*.txt", "*.xml", "*.gif" } );
325+
IOFileFilter listFilter = new WildcardFilter( patternList );
326+
File txtFile = new File( "test.txt" );
327+
File bmpFile = new File( "test.bmp" );
328+
File dir = new File( "src/java" );
329+
207330
assertFiltering(filter, new File("log.txt"), true);
208331
// assertFiltering(filter, new File("log.txt.bak"), false);
209332

@@ -222,12 +345,63 @@ public void testWildcard() throws Exception {
222345
assertFiltering(filter, new File("Test.class"), true);
223346
assertFiltering(filter, new File("Test.jsp"), false);
224347

348+
assertFiltering(listFilter, new File("Test.txt"), true);
349+
assertFiltering(listFilter, new File("Test.xml"), true);
350+
assertFiltering(listFilter, new File("Test.gif"), true);
351+
assertFiltering(listFilter, new File("Test.bmp"), false);
352+
353+
assertTrue( listFilter.accept( txtFile ) );
354+
assertTrue( !listFilter.accept( bmpFile ) );
355+
assertTrue( !listFilter.accept( dir ) );
356+
357+
assertTrue( listFilter.accept( txtFile.getParentFile(), txtFile.getName() ) );
358+
assertTrue( !listFilter.accept( bmpFile.getParentFile(), bmpFile.getName() ) );
359+
assertTrue( !listFilter.accept( dir.getParentFile(), dir.getName() ) );
360+
225361
try {
226362
new WildcardFilter((String) null);
227363
fail();
228364
} catch (IllegalArgumentException ex) {
229365
// expected
230366
}
367+
368+
try {
369+
new WildcardFilter((String[]) null);
370+
fail();
371+
} catch (IllegalArgumentException ex) {
372+
// expected
373+
}
374+
375+
try {
376+
new WildcardFilter((List) null);
377+
fail();
378+
} catch (IllegalArgumentException ex) {
379+
// expected
380+
}
381+
}
382+
383+
public void testDelegateFileFilter() throws Exception {
384+
OrFileFilter orFilter = new OrFileFilter();
385+
File testFile = new File( "test.txt" );
386+
387+
IOFileFilter filter = new DelegateFileFilter((FileFilter) orFilter);
388+
assertFiltering( filter, testFile, false );
389+
390+
filter = new DelegateFileFilter((FilenameFilter) orFilter);
391+
assertFiltering( filter, testFile, false );
392+
393+
try {
394+
new DelegateFileFilter((FileFilter) null);
395+
fail();
396+
} catch( IllegalArgumentException iae ) {
397+
}
398+
399+
try {
400+
new DelegateFileFilter((FilenameFilter) null);
401+
fail();
402+
} catch( IllegalArgumentException iae ) {
403+
}
404+
231405
}
232406

233407
public void testMakeCVSAware() throws Exception {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.io.File;
1919
import junit.framework.TestCase;
2020

21-
public class IOFileFilterAbstractTestCase extends TestCase {
21+
public abstract class IOFileFilterAbstractTestCase extends TestCase {
2222

2323
/**
2424
* Constructs a new instance of

src/test/org/apache/commons/io/input/CountingInputStreamTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
21-
import java.util.Arrays;
2221

2322
import junit.framework.TestCase;
2423

@@ -81,7 +80,7 @@ public void testResetting() throws Exception {
8180

8281
int count = cis.resetCount();
8382
found = cis.read(result, 6, 5);
84-
assertEquals( found, cis.getCount() );
83+
assertEquals( found, count );
8584
}
8685

8786
public void testZeroLength1() throws Exception {

src/test/org/apache/commons/io/input/PackageTestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static void main(String[] args) {
3333

3434
public static Test suite() {
3535
TestSuite suite = new TestSuite("IO Utilities - input");
36+
suite.addTest(new TestSuite(ClassLoaderObjectInputStreamTest.class));
3637
suite.addTest(new TestSuite(CountingInputStreamTest.class));
3738
suite.addTest(new TestSuite(SwappedDataInputStreamTest.class));
3839
return suite;

0 commit comments

Comments
 (0)