1
2
3
4
5
6
7 package org.abraracourcix.alipes.monitors.file;
8
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import java.util.List;
15
16 import junit.framework.TestCase;
17
18 /***
19 * To change the template for this generated type comment go to
20 * Window - Preferences - Java - Code Generation - Code and Comments
21 *
22 * @author jdt
23 */
24 public class FileStateMonitorTest extends TestCase {
25 /***
26 * Commons logger
27 */
28 private static final org.apache.commons.logging.Log log =
29 org.apache.commons.logging.LogFactory.getLog(
30 FileStateMonitorTest.class);
31 /***
32 * Fire up the text UI
33 * @param args ignored
34 */
35 public static void main(final String[] args) {
36 junit.textui.TestRunner.run(FileStateMonitorTest.class);
37 }
38 /***
39 * The list of files to be created for testing
40 */
41 private static final String[] TEST_FILE_NAMES= {"foo","bar"};
42 /***
43 * Collection of test files
44 */
45 private List testFiles = new ArrayList();
46 /***
47 * The test directory
48 */
49 private File dir;
50 /***
51 * the monitor under test
52 */
53 private AbstractFileStateMonitor monitor;
54
55 /***
56 * Usual setup method
57 * @see TestCase#setUp()
58 */
59 protected void setUp() throws Exception {
60 super.setUp();
61 for (int i = 0; i<TEST_FILE_NAMES.length;++i) {
62 final File file = File.createTempFile(TEST_FILE_NAMES[i],"txt");
63 testFiles.add(file);
64 }
65 final File firstFile = (File) testFiles.get(0);
66 dir = firstFile.getParentFile();
67 monitor = new FileStateMonitor(null,0);
68 }
69 /***
70 * Usual teardown method
71 * @see TestCase#tearDown()
72 */
73 protected final void tearDown() throws IOException {
74 final Iterator it = testFiles.iterator();
75 while (it.hasNext()) {
76 final File file = (File) it.next();
77 file.delete();
78 }
79 }
80
81 /***
82 * Test that the setter and getter are working
83 * @throws IOException shouldn't
84 */
85 public final void testGetSetStaleInterval() throws IOException {
86 final int testVal = 100;
87 monitor.setStaleInterval(testVal);
88 assertEquals("Returned stale interval not correct", testVal, monitor.getStaleInterval());
89 }
90
91 /***
92 * Check that if we add files, we find them
93 * Class to test for void addFile(File)
94 */
95 public final void testAddFileFile() {
96 log.debug("Test files:");
97 final Iterator it = testFiles.iterator();
98 while (it.hasNext()) {
99 final File file = (File) it.next();
100 log.debug(file);
101 monitor.addFile(file);
102 }
103 final List files = new ArrayList(monitor.getMonitoredFiles());
104 log.debug("Returned files:");
105 final Iterator it2 = files.iterator();
106 while(it2.hasNext()) {
107 log.debug(it2.next());
108 }
109 assertTrue(testFiles.equals(files));
110 }
111
112 }
113
114
115
116
117
118
119
120