Skip to content

Commit aba6c1b

Browse files
author
Niall Pemberton
committed
Refactor tests to share common code
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1006051 13f79535-47bb-0310-9956-ffa450edef68
1 parent 26e295f commit aba6c1b

3 files changed

Lines changed: 167 additions & 128 deletions

File tree

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.monitor;
18+
19+
import java.io.File;
20+
import java.io.FileFilter;
21+
22+
import org.apache.commons.io.FileUtils;
23+
import org.apache.commons.io.filefilter.FileFilterUtils;
24+
import org.apache.commons.io.filefilter.HiddenFileFilter;
25+
import org.apache.commons.io.filefilter.IOFileFilter;
26+
27+
import junit.framework.TestCase;
28+
29+
/**
30+
* {@link FilesystemObserver} Test Case.
31+
*/
32+
public abstract class AbstractMonitorTestCase extends TestCase {
33+
34+
/** Filesystem observer */
35+
protected FilesystemObserver observer;
36+
37+
/** Listener which collects file changes */
38+
protected CollectionFilesystemListener listener = new CollectionFilesystemListener(true);
39+
40+
/** Test diretory name */
41+
protected String testDirName = null;
42+
43+
/** Directory for test files */
44+
protected File testDir;
45+
46+
/** Time in milliseconds to pause in tests */
47+
protected long pauseTime = 100L;
48+
49+
/**
50+
* Construct a new test case.
51+
*
52+
* @param name The name of the test
53+
*/
54+
public AbstractMonitorTestCase(String name) {
55+
super(name);
56+
}
57+
58+
@Override
59+
protected void setUp() throws Exception {
60+
testDir = new File(new File("."), testDirName);
61+
if (testDir.exists()) {
62+
FileUtils.cleanDirectory(testDir);
63+
} else {
64+
testDir.mkdir();
65+
}
66+
67+
IOFileFilter files = FileFilterUtils.fileFileFilter();
68+
IOFileFilter javaSuffix = FileFilterUtils.suffixFileFilter(".java");
69+
IOFileFilter fileFilter = FileFilterUtils.and(files, javaSuffix);
70+
71+
IOFileFilter directories = FileFilterUtils.directoryFileFilter();
72+
IOFileFilter visible = HiddenFileFilter.VISIBLE;
73+
IOFileFilter dirFilter = FileFilterUtils.and(directories, visible);
74+
75+
IOFileFilter filter = FileFilterUtils.or(dirFilter, fileFilter);
76+
77+
createObserver(testDir, filter);
78+
}
79+
80+
/**
81+
* Create a {@link FilesystemObserver}.
82+
*
83+
* @param file The directory to observe
84+
* @param fileFilter The file filter to apply
85+
*/
86+
protected void createObserver(File file, FileFilter fileFilter) {
87+
observer = new FilesystemObserver(file, fileFilter);
88+
observer.addListener(listener);
89+
try {
90+
observer.initialize();
91+
} catch (Exception e) {
92+
fail("Observer init() threw " + e);
93+
}
94+
}
95+
96+
@Override
97+
protected void tearDown() throws Exception {
98+
FileUtils.deleteDirectory(testDir);
99+
}
100+
101+
/**
102+
* Check all the Collections are empty
103+
*/
104+
protected void checkCollectionsEmpty(String label) {
105+
checkCollectionSizes("EMPTY-" + label, 0, 0, 0, 0, 0, 0);
106+
}
107+
108+
/**
109+
* Check all the Collections have the expected sizes.
110+
*/
111+
protected void checkCollectionSizes(String label, int dirCreate, int dirChange, int dirDelete, int fileCreate, int fileChange, int fileDelete) {
112+
label = label + "[" + listener.getCreatedDirectories().size() +
113+
" " + listener.getChangedDirectories().size() +
114+
" " + listener.getDeletedDirectories().size() +
115+
" " + listener.getCreatedFiles().size() +
116+
" " + listener.getChangedFiles().size() +
117+
" " + listener.getDeletedFiles().size() + "]";
118+
assertEquals(label + ": No. of directories created", dirCreate, listener.getCreatedDirectories().size());
119+
assertEquals(label + ": No. of directories changed", dirChange, listener.getChangedDirectories().size());
120+
assertEquals(label + ": No. of directories deleted", dirDelete, listener.getDeletedDirectories().size());
121+
assertEquals(label + ": No. of files created", fileCreate, listener.getCreatedFiles().size());
122+
assertEquals(label + ": No. of files changed", fileChange, listener.getChangedFiles().size());
123+
assertEquals(label + ": No. of files deleted", fileDelete, listener.getDeletedFiles().size());
124+
}
125+
126+
/**
127+
* Either creates a file if it doesn't exist or updates the last modified date/time
128+
* if it does.
129+
*
130+
* @param file The file to touch
131+
* @return The file
132+
*/
133+
protected File touch(File file) {
134+
long lastModified = file.exists() ? file.lastModified() : 0;
135+
try {
136+
FileUtils.touch(file);
137+
file = new File(file.getParent(), file.getName());
138+
while (lastModified == file.lastModified()) {
139+
sleepHandleInterruped(50L);
140+
FileUtils.touch(file);
141+
file = new File(file.getParent(), file.getName());
142+
}
143+
} catch (Exception e) {
144+
fail("Touching " + file + ": " + e);
145+
}
146+
return file;
147+
}
148+
149+
/**
150+
* Thread.sleep(timeInMilliseconds) - ignore InterruptedException
151+
*/
152+
protected void sleepHandleInterruped(long timeInMilliseconds) {
153+
try {
154+
Thread.sleep(timeInMilliseconds);
155+
} catch(InterruptedException ie) {
156+
// ignore
157+
}
158+
}
159+
}

src/test/java/org/apache/commons/io/monitor/FileSystemMonitorTestCase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* {@link FilesystemMonitor} Test Case.
2626
*/
27-
public class FileSystemMonitorTestCase extends FilesystemObserverTestCase {
27+
public class FileSystemMonitorTestCase extends AbstractMonitorTestCase {
2828

2929
/**
3030
* Construct a new test case.
@@ -53,19 +53,19 @@ public void testMonitor() {
5353
// Create a File
5454
checkCollectionsEmpty("A");
5555
File file1 = touch(new File(testDir, "file1.java"));
56-
checkFile("Create", file1, interval, listener.getCreatedFiles());
56+
checkFile("Create", file1, listener.getCreatedFiles());
5757
listener.clear();
5858

5959
// Update a file
6060
checkCollectionsEmpty("B");
6161
file1 = touch(file1);
62-
checkFile("Update", file1, interval, listener.getChangedFiles());
62+
checkFile("Update", file1, listener.getChangedFiles());
6363
listener.clear();
6464

6565
// Delete a file
6666
checkCollectionsEmpty("C");
6767
file1.delete();
68-
checkFile("Delete", file1, interval, listener.getDeletedFiles());
68+
checkFile("Delete", file1, listener.getDeletedFiles());
6969
listener.clear();
7070

7171
// Stop monitoring
@@ -80,12 +80,12 @@ public void testMonitor() {
8080
/**
8181
* Check all the File Collections have the expected sizes.
8282
*/
83-
private void checkFile(String label, File file, long interval, Collection<File> files) {
83+
private void checkFile(String label, File file, Collection<File> files) {
8484
for (int i = 0; i < 5; i++) {
8585
if (files.contains(file)) {
8686
return; // found, test passes
8787
}
88-
sleepHandleInterruped(interval);
88+
sleepHandleInterruped(pauseTime);
8989
}
9090
fail(label + " " + file + " not found");
9191
}

src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,11 @@
2121

2222
import org.apache.commons.io.FileUtils;
2323
import org.apache.commons.io.filefilter.FileFilterUtils;
24-
import org.apache.commons.io.filefilter.HiddenFileFilter;
25-
import org.apache.commons.io.filefilter.IOFileFilter;
26-
27-
import junit.framework.TestCase;
2824

2925
/**
3026
* {@link FilesystemObserver} Test Case.
3127
*/
32-
public class FilesystemObserverTestCase extends TestCase {
33-
34-
/** Filesystem observer */
35-
protected FilesystemObserver observer;
36-
37-
/** Listener which collects file changes */
38-
protected CollectionFilesystemListener listener = new CollectionFilesystemListener(true);
39-
40-
/** Test diretory name */
41-
protected String testDirName = "test-observer";
42-
43-
/** Directory for test files */
44-
protected File testDir;
45-
46-
/** Time in milliseconds to pause in tests */
47-
protected long pauseTime = 100L;
28+
public class FilesystemObserverTestCase extends AbstractMonitorTestCase {
4829

4930
/**
5031
* Construct a new test case.
@@ -53,49 +34,7 @@ public class FilesystemObserverTestCase extends TestCase {
5334
*/
5435
public FilesystemObserverTestCase(String name) {
5536
super(name);
56-
}
57-
58-
@Override
59-
protected void setUp() throws Exception {
60-
testDir = new File(new File("."), testDirName);
61-
if (testDir.exists()) {
62-
FileUtils.cleanDirectory(testDir);
63-
} else {
64-
testDir.mkdir();
65-
}
66-
67-
IOFileFilter files = FileFilterUtils.fileFileFilter();
68-
IOFileFilter javaSuffix = FileFilterUtils.suffixFileFilter(".java");
69-
IOFileFilter fileFilter = FileFilterUtils.and(files, javaSuffix);
70-
71-
IOFileFilter directories = FileFilterUtils.directoryFileFilter();
72-
IOFileFilter visible = HiddenFileFilter.VISIBLE;
73-
IOFileFilter dirFilter = FileFilterUtils.and(directories, visible);
74-
75-
IOFileFilter filter = FileFilterUtils.or(dirFilter, fileFilter);
76-
77-
createObserver(testDir, filter);
78-
}
79-
80-
/**
81-
* Create a {@link FilesystemObserver}.
82-
*
83-
* @param file The directory to observe
84-
* @param fileFilter The file filter to apply
85-
*/
86-
protected void createObserver(File file, FileFilter fileFilter) {
87-
observer = new FilesystemObserver(file, fileFilter);
88-
observer.addListener(listener);
89-
try {
90-
observer.initialize();
91-
} catch (Exception e) {
92-
fail("Observer init() threw " + e);
93-
}
94-
}
95-
96-
@Override
97-
protected void tearDown() throws Exception {
98-
FileUtils.deleteDirectory(testDir);
37+
testDirName = "test-observer";
9938
}
10039

10140
/**
@@ -408,63 +347,4 @@ public void testObserveSingleFile() {
408347
protected void checkAndNotify() throws Exception {
409348
observer.checkAndNotify();
410349
}
411-
412-
/**
413-
* Check all the Collections are empty
414-
*/
415-
protected void checkCollectionsEmpty(String label) {
416-
checkCollectionSizes("EMPTY-" + label, 0, 0, 0, 0, 0, 0);
417-
}
418-
419-
/**
420-
* Check all the Collections have the expected sizes.
421-
*/
422-
protected void checkCollectionSizes(String label, int dirCreate, int dirChange, int dirDelete, int fileCreate, int fileChange, int fileDelete) {
423-
label = label + "[" + listener.getCreatedDirectories().size() +
424-
" " + listener.getChangedDirectories().size() +
425-
" " + listener.getDeletedDirectories().size() +
426-
" " + listener.getCreatedFiles().size() +
427-
" " + listener.getChangedFiles().size() +
428-
" " + listener.getDeletedFiles().size() + "]";
429-
assertEquals(label + ": No. of directories created", dirCreate, listener.getCreatedDirectories().size());
430-
assertEquals(label + ": No. of directories changed", dirChange, listener.getChangedDirectories().size());
431-
assertEquals(label + ": No. of directories deleted", dirDelete, listener.getDeletedDirectories().size());
432-
assertEquals(label + ": No. of files created", fileCreate, listener.getCreatedFiles().size());
433-
assertEquals(label + ": No. of files changed", fileChange, listener.getChangedFiles().size());
434-
assertEquals(label + ": No. of files deleted", fileDelete, listener.getDeletedFiles().size());
435-
}
436-
437-
/**
438-
* Either creates a file if it doesn't exist or updates the last modified date/time
439-
* if it does.
440-
*
441-
* @param file The file to touch
442-
* @return The file
443-
*/
444-
protected File touch(File file) {
445-
long lastModified = file.exists() ? file.lastModified() : 0;
446-
try {
447-
FileUtils.touch(file);
448-
file = new File(file.getParent(), file.getName());
449-
while (lastModified == file.lastModified()) {
450-
sleepHandleInterruped(50L);
451-
FileUtils.touch(file);
452-
file = new File(file.getParent(), file.getName());
453-
}
454-
} catch (Exception e) {
455-
fail("Touching " + file + ": " + e);
456-
}
457-
return file;
458-
}
459-
460-
/**
461-
* Thread.sleep(timeInMilliseconds) - ignore InterruptedException
462-
*/
463-
protected void sleepHandleInterruped(long timeInMilliseconds) {
464-
try {
465-
Thread.sleep(timeInMilliseconds);
466-
} catch(InterruptedException ie) {
467-
// ignore
468-
}
469-
}
470350
}

0 commit comments

Comments
 (0)