added test cases for directorymonitor.
[utils] / support / src / test / java / org / wamblee / io / DirectoryMonitorTest.java
index 6ea0d451d4c220904e74707217536904feb81e2f..b778adac1432604fd0208d07d61bd56f063fe5e2 100644 (file)
-/*
- * Copyright 2006 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */ 
-
 package org.wamblee.io;
 
-import java.io.File;
+import org.apache.oro.io.AwkFilenameFilter;
+import org.easymock.EasyMock;
 
 import junit.framework.TestCase;
 
-import org.apache.oro.io.AwkFilenameFilter;
-import org.apache.oro.io.RegexFilenameFilter;
-
-/**
- * 
- */
 public class DirectoryMonitorTest extends TestCase {
-    
-    public static void main(String[] aArgs) throws Exception { 
-        
-        DirectoryMonitor monitor = new DirectoryMonitor(new File("."), 
-                new AwkFilenameFilter(".*\\.txt"), new DirectoryMonitor.Listener() { 
-            public void fileChanged(File aFile) {
-                System.out.println("changed " + aFile);            
-            }
-            public void fileCreated(File aFile) {
-                System.out.println("created " + aFile);   
-            }
-            public void fileDeleted(File aFile) {
-                System.out.println("deleted " + aFile);   
-            }
-        });
-        
-        for (;;) { 
-            monitor.poll();
-            Thread.sleep(1000);
-        }
-    }
 
+       private static final String REGEX = "^.*\\.txt$";
+       private static final String FILE1 = "file1.txt";
+
+       private TestData _data;
+       private DirectoryMonitor.Listener _listener;
+       private DirectoryMonitor _monitor;
+
+       @Override
+       protected void setUp() throws Exception {
+               super.setUp();
+               _data = new TestData(this);
+               _data.clean();
+               _listener = EasyMock.createStrictMock(DirectoryMonitor.Listener.class);
+               _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
+                               REGEX), _listener);
+       }
+
+       public void testEmptyDir() {
+               // Nothing is expected to be called.
+               for (int i = 0; i < 10; i++) {
+                       EasyMock.replay(_listener);
+                       _monitor.poll();
+                       EasyMock.verify(_listener);
+                       EasyMock.reset(_listener);
+               }
+       }
+
+       public void testFileCreated() {
+               _listener.fileCreated(_data.getFile(FILE1));
+               EasyMock.replay(_listener);
+               _data.createFile(FILE1, "hello");
+               _monitor.poll();
+               EasyMock.verify(_listener);
+       }
+
+       public void testFileDeleted() {
+               _data.createFile(FILE1, "hello");
+               _monitor.poll();
+
+               EasyMock.reset(_listener);
+
+               _data.deleteFile(FILE1);
+               _listener.fileDeleted(_data.getFile(FILE1));
+               EasyMock.replay(_listener);
+               _monitor.poll();
+               EasyMock.verify(_listener);
+       }
+       
+       public void testFileChanged() throws InterruptedException { 
+               _data.createFile(FILE1, "hello");
+               _monitor.poll();
+               EasyMock.reset(_listener);
+               
+               Thread.sleep(2000);
+               _data.deleteFile(FILE1);
+               _data.createFile(FILE1, "bla");
+               
+               _listener.fileChanged(_data.getFile(FILE1));
+               EasyMock.replay(_listener);
+               _monitor.poll();
+               EasyMock.verify(_listener);
+       }
+       
+       public void testFileFilterIsUsed() {
+               _monitor.poll(); 
+               
+               _data.createFile("file.xml", "hello");
+               EasyMock.replay(_listener); 
+               _monitor.poll();
+               EasyMock.verify(_listener);
+       }
+       
+       public void testDirectoryIsIgnored() { 
+               _monitor.poll();
+               _data.createDir(FILE1);
+               EasyMock.replay(_listener); 
+               _monitor.poll();
+               EasyMock.verify(_listener);
+       }
+       
+       public void testExceptionsWIllLeadToRepeatedNotifications() { 
+               _monitor.poll();
+               _data.createFile(FILE1, "hello");
+               
+               _listener.fileCreated(_data.getFile(FILE1));
+               EasyMock.expectLastCall().andThrow(new RuntimeException());
+               EasyMock.replay(_listener);
+               try { 
+                   _monitor.poll();
+               } catch (RuntimeException e) { 
+                       EasyMock.verify(_listener);
+                       EasyMock.reset(_listener);
+                       
+                       // polling again should lead to the same filecreated call. 
+                       // this time no exception is thrown. 
+                       
+                       _listener.fileCreated(_data.getFile(FILE1));
+                       EasyMock.replay(_listener);
+                       _monitor.poll();
+                       EasyMock.verify(_listener);
+                       return; 
+               }
+               fail(); // should not get here. 
+               
+       
+       }
 }