View Javadoc

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  
18  package org.apache.commons.jci.monitor;
19  
20  import java.io.File;
21  import java.io.FileOutputStream;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.jci.listeners.AbstractFilesystemAlterationListener;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  
33  /**
34   * 
35   * @author tcurdt
36   */
37  public final class FilesystemAlterationMonitorTestCase extends TestCase {
38  
39      private final Log log = LogFactory.getLog(FilesystemAlterationMonitorTestCase.class);
40  
41      private FilesystemAlterationMonitor fam;
42      private MyFilesystemAlterationListener listener;
43  
44      private File directory;
45  
46      
47      @Override
48      protected void setUp() throws Exception {
49          directory = createTempDirectory();
50          assertTrue(directory.exists());
51          assertTrue(directory.isDirectory());
52      }
53      
54      @Override
55      protected void tearDown() throws Exception {
56          FileUtils.deleteDirectory(directory);
57      }
58      
59      
60      protected File createDirectory( final String pName ) throws Exception {
61          final File newDirectory = new File(directory, pName);
62          assertTrue(newDirectory.mkdir());
63          assertTrue(newDirectory.exists());
64          assertTrue(newDirectory.isDirectory());
65          return newDirectory;
66      }
67      
68      protected File writeFile( final String pName, final byte[] pData ) throws Exception {
69          final File file = new File(directory, pName);
70          final File parent = file.getParentFile();
71          if (!parent.mkdirs() && !parent.isDirectory()) {
72              throw new IOException("could not create" + parent);
73          }
74          
75          log.debug("writing file " + pName + " (" + pData.length + " bytes)");
76          
77          final FileOutputStream os = new FileOutputStream(file);
78          os.write(pData);
79          os.close();
80          
81          assertTrue(file.exists());
82          assertTrue(file.isFile());
83          
84          return file;
85      }
86  
87      protected File writeFile( final String pName, final String pText ) throws Exception {
88          final File file = new File(directory, pName);
89          final File parent = file.getParentFile();
90          if (!parent.mkdirs() && !parent.isDirectory()) {
91              throw new IOException("could not create" + parent);
92          }
93          log.debug("writing " + file);
94          final FileWriter writer = new FileWriter(file);
95          writer.write(pText);
96          writer.close();
97          
98          assertTrue(file.exists());
99          assertTrue(file.isFile());
100         
101         return file;
102     }
103 
104     protected File createTempDirectory() throws IOException {
105         final File tempFile = File.createTempFile("jci", null);
106         
107         if (!tempFile.delete()) {
108             throw new IOException();
109         }
110         
111         if (!tempFile.mkdir()) {
112             throw new IOException();
113         }
114         
115         return tempFile;         
116     }
117 
118 
119     protected void delay() {
120         try {
121             Thread.sleep(1500);
122         } catch (final InterruptedException e) {
123         }
124     }
125 
126 
127     
128     private static class MyFilesystemAlterationListener extends AbstractFilesystemAlterationListener {
129     }
130 
131     private void start() throws Exception {
132         fam = new FilesystemAlterationMonitor();
133         listener = new MyFilesystemAlterationListener();
134         fam.addListener(directory, listener);
135         fam.start();
136         listener.waitForFirstCheck();
137     }
138     
139     private void stop() {
140         fam.stop();
141     }
142     
143     public void testListenerDoublication() throws Exception {
144         fam = new FilesystemAlterationMonitor();
145         listener = new MyFilesystemAlterationListener();
146         
147         fam.addListener(directory, listener);
148         assertEquals(1, fam.getListenersFor(directory).length);
149         
150         fam.addListener(directory, listener); 
151         assertEquals(1, fam.getListenersFor(directory).length);
152         
153         fam.removeListener(listener);
154         assertEquals(0, fam.getListenersFor(directory).length);
155 }
156 
157     public void testDirectoryDoublication() throws Exception {
158         fam = new FilesystemAlterationMonitor();
159 
160         fam.addListener(directory, new MyFilesystemAlterationListener()); 
161         assertEquals(1, fam.getListenersFor(directory).length);
162         
163         fam.addListener(directory, new MyFilesystemAlterationListener()); 
164         assertEquals(2, fam.getListenersFor(directory).length);
165     }
166 
167     public void testCreateFileDetection() throws Exception {
168         start();
169         
170         writeFile("file", "file");
171         
172         listener.waitForCheck();
173         
174         assertEquals(1, listener.getCreatedFiles().size());
175         
176         stop();
177     }
178 
179     public void testTimeout() throws Exception {
180     	listener = new MyFilesystemAlterationListener();
181     	
182     	try {
183         	listener.waitForFirstCheck();
184         	fail("should be an timeout");
185         } catch(Exception e) {
186         	assertEquals("timeout", e.getMessage());
187         }
188 
189         start();
190 
191         try {
192         	listener.waitForEvent();
193         	fail("should be an timeout");
194         } catch(Exception e) {
195         	assertEquals("timeout", e.getMessage());
196         }
197         
198         stop();
199 
200         try {
201         	listener.waitForCheck();
202         	fail("should be an timeout");
203         } catch(Exception e) {
204         	assertEquals("timeout", e.getMessage());
205         }
206     
207     }
208 
209     public void testCreateDirectoryDetection() throws Exception {
210         start();
211 
212         createDirectory("dir");
213         
214         listener.waitForCheck();
215         
216         assertEquals(1, listener.getCreatedDirectories().size());
217         
218         stop();
219     }
220 
221     public void testDeleteFileDetection() throws Exception {
222         start();
223 
224         final File file = writeFile("file", "file");
225 
226         assertTrue("file should exist", file.exists());
227         
228         listener.waitForCheck();
229         
230         assertEquals("expecting 1 file created", 1, listener.getCreatedFiles().size());
231         //assertEquals("expecting 0 directories changed", 0, listener.getChangedDirectories().size()); // todo investigate why this is failing on Windows
232         
233         file.delete();
234         assertFalse("file should not exist", file.exists());
235 
236         listener.waitForCheck();
237         
238         assertEquals("expecting 1 file deleted", 1, listener.getDeletedFiles().size());
239         
240         stop();        
241     }
242     public void testDeleteDirectoryDetection() throws Exception {
243         start();
244 
245         final File dir = createDirectory("dir");
246         createDirectory("dir/sub");
247         final File file = writeFile("dir/sub/file", "file");
248 
249         listener.waitForCheck();
250         
251         assertEquals(2, listener.getCreatedDirectories().size());
252         assertEquals(1, listener.getCreatedFiles().size());
253 
254         delay();
255         
256         FileUtils.deleteDirectory(dir);
257         assertTrue(!dir.exists());
258         assertTrue(!file.exists());
259 
260         listener.waitForCheck();
261         
262         assertEquals(2, listener.getDeletedDirectories().size());
263         assertEquals(1, listener.getDeletedFiles().size());
264 
265         stop();
266     }
267 
268     public void testModifyFileDetection() throws Exception {
269         start();
270 
271         writeFile("file", "file");
272         
273         listener.waitForCheck();
274         
275         assertEquals(1, listener.getCreatedFiles().size());
276 
277         delay();
278 
279         writeFile("file", "changed file");
280 
281         listener.waitForCheck();
282         
283         assertEquals(1, listener.getChangedFiles().size());
284         
285         stop();
286     }
287 
288     public void testCreatingLocalDirectoryChangesLastModified() throws Exception {
289         final long modified = directory.lastModified();
290 
291         delay();
292         
293         createDirectory("directory");
294 
295         delay();
296                
297         assertTrue(directory.lastModified() != modified);
298     }
299 
300     public void testCreatingLocalFileChangesLastModified() throws Exception {
301         final long modified = directory.lastModified();
302 
303         delay();
304         
305         writeFile("file", "file");
306 
307         delay();
308 
309         assertTrue(directory.lastModified() != modified);
310     }
311 
312     public void testCreatingSubDirectoryChangesLastModified() throws Exception {
313         createDirectory("dir");
314 
315         final long modified = directory.lastModified();
316 
317         delay();
318 
319         createDirectory("dir/sub");
320 
321         assertTrue(directory.lastModified() == modified);
322     }
323 
324     public void testCreatingFileInSubDirectoryChangesLastModified() throws Exception {
325         createDirectory("dir");
326 
327         final long modified = directory.lastModified();
328 
329         delay();
330                 
331         writeFile("dir/file", "file");
332 
333         assertTrue(directory.lastModified() == modified);
334     }
335     
336     public void testInterval() throws Exception {
337 
338         final long interval = 1000;
339 
340         start();
341         fam.setInterval(interval);
342 
343         listener.waitForCheck();
344         long t1 = System.currentTimeMillis();
345 
346         listener.waitForCheck();
347         long t2 = System.currentTimeMillis();
348         
349         long diff = t2-t1;
350         
351         // interval should be at around the same interval
352         assertTrue("the interval was set to " + interval + " but the time difference was " + diff, (diff > (interval-50)) && (diff < (interval+50)));
353         
354         stop();
355     }    
356 }