[Mono-dev] [PATCH] - System.IO.FileSystemWatcher Unit Tests for Events

Louis R. Marascio louis at fitnr.com
Sat Apr 7 20:38:33 EDT 2007


Hi all,

Attached is a patch that adds several unit tests for FileSystemWatcher.
These tests focus on verifying that the Created, Deleted, and Changed
events work properly. This work was driven from my trying to track down
bug #80564 wherein the Changed events don't fire properly on OS X.

I tested this patch on gentoo with the a mono build from svn, revision
75497. All new tests pass on Linux. As expected, one test is failing on
OS X due to bug #80564. I did not run the new tests on Windows.

This is my first patch so please let me know if I've done anything 
wrong.

Thanks,

Louis

-- 
Louis R. Marascio - www.fitnr.com
... fixed in the next release ...
-------------- next part --------------
Index: FileSystemWatcherTest.cs
===================================================================
--- FileSystemWatcherTest.cs	(revision 75497)
+++ FileSystemWatcherTest.cs	(working copy)
@@ -2,6 +2,7 @@
 //
 // Authors:
 // 	Gonzalo Paniagua Javier (gonzalo at ximian.com)
+//	Louis R. Marascio (louis at fitnr.com)
 //
 // (C) 2004 Novell, Inc.  http://www.novell.com
 // 
@@ -9,6 +10,7 @@
 using NUnit.Framework;
 using System;
 using System.IO;
+using System.Threading;
 
 namespace MonoTests.System.IO
 {
@@ -81,6 +83,141 @@
 			FileSystemWatcher fw = new FileSystemWatcher (Path.GetTempPath (), "*");
 			fw.Path = "*";
 		}
+
+		#region FileSystemWatcher Event Tests
+
+		private AutoResetEvent eventFired  = new AutoResetEvent (false);
+		private static string tempPath     = Path.Combine (Path.GetTempPath(), 
+								   "MonoTests.System.IO.FileSystemWatcherTest");
+		private static string tempFile     = "FSWTempFile";
+		private static string tempFilePath = Path.Combine (tempPath, tempFile);
+
+		// Used to store state from each event call back
+		private string lastName, lastFullPath;
+		private WatcherChangeTypes lastChangeType;
+
+		[Test]
+		public void CheckChangedEvent ()
+		{
+			// Test various combinations of the Changed event. We pass in a test name, our test
+			// filename, a test filter, and whether we expect the event to fire or not.
+			DoChangedEventTest ("ChangedWildcard",        tempFilePath,        "*.*",        true);
+			DoChangedEventTest ("ChangedSpecificFile",    tempFilePath+"2",    tempFile+"2", true);
+			DoChangedEventTest ("ChangedOutsideOfFilter", tempFilePath,        "fooFilter",  false);
+			DoChangedEventTest ("ChangedFilterExtGood",   tempFilePath+".abc", "*.abc",      true);
+			DoChangedEventTest ("ChangedFilterExtBad",    tempFilePath+".xyz", "*.abc",      false);
+		}
+
+		// Test various combinations of the changed event handler.
+		public void DoChangedEventTest (string testName, string filename, string filter, bool eventExpected)
+		{
+			MakeTempDir ();
+
+			// Open our temporary file and write some initial data, before we start monitoring for events.
+			using (StreamWriter w = new StreamWriter (filename)) {
+				w.WriteLine ("foo");
+			}
+		
+			FileSystemWatcher fw = new FileSystemWatcher (tempPath, filter);
+			fw.Changed += new FileSystemEventHandler (OnFileSystemWatcherEvent);
+			fw.EnableRaisingEvents = true;
+
+			// Now that we're monitoring for Changed events, write some more data to the same file.
+			using (StreamWriter w = new StreamWriter (filename, true)) {
+				w.WriteLine ("bar");
+			}
+
+			bool gotEvent = eventFired.WaitOne (1000, true);
+			
+			fw.EnableRaisingEvents = false;
+			fw.Dispose ();
+	
+			AssertEquals (testName+"-#15", gotEvent, eventExpected);
+			if (eventExpected) {
+				AssertEquals (testName+"-#16", filename, lastFullPath);
+				AssertEquals (testName+"-#17", WatcherChangeTypes.Changed, lastChangeType);
+			}
+
+			lastName = lastFullPath = null;
+		}
+
+		[Test]
+		public void CheckCreatedEvent ()
+		{
+			MakeTempDir ();
+
+			// Make sure the file isn't there before we try to create it
+			if (File.Exists(tempFilePath))
+				File.Delete (tempFilePath);
+
+			FileSystemWatcher fw = new FileSystemWatcher (tempPath);
+			fw.Created += new FileSystemEventHandler (OnFileSystemWatcherEvent);
+			fw.EnableRaisingEvents = true;
+
+			// Now that we're monitoring for Created events, create the file.
+			File.Create (tempFilePath);
+
+			bool gotEvent = eventFired.WaitOne (1000, true);
+			
+			fw.EnableRaisingEvents = false;
+			fw.Dispose ();
+		
+			AssertEquals ("#07", gotEvent, true);
+			AssertEquals ("#08", tempFile, lastName);
+			AssertEquals ("#09", tempFilePath, lastFullPath);
+			AssertEquals ("#10", WatcherChangeTypes.Created, lastChangeType);
+			
+			lastName = lastFullPath = null;
+		}
+
+		[Test]
+		public void CheckDeletedEvent ()
+		{
+			MakeTempDir ();
+
+			// Make sure the file does exist before we try to delete it
+			if (!File.Exists(tempFilePath)) 
+				File.Create (tempFilePath);
+
+			FileSystemWatcher fw = new FileSystemWatcher (tempPath);
+			fw.Deleted += new FileSystemEventHandler (OnFileSystemWatcherEvent);
+			fw.EnableRaisingEvents = true;
+
+			// Now that we're monitoring for Deleted events, remove the file.
+			File.Delete (tempFilePath);
+
+			bool gotEvent = eventFired.WaitOne (1000, true);
+			
+			fw.EnableRaisingEvents = false;
+			fw.Dispose ();
+		
+			AssertEquals ("#11", gotEvent, true);
+			AssertEquals ("#12", tempFile, lastName);
+			AssertEquals ("#13", tempFilePath, lastFullPath);
+			AssertEquals ("#14", WatcherChangeTypes.Deleted, lastChangeType);
+
+			lastName = lastFullPath = null;
+		}
+
+		// One event handler for all of our tests. Just save the relevant data
+		// so the individual test can assert on it.
+		private void OnFileSystemWatcherEvent (object source, FileSystemEventArgs e)
+		{
+			lastName = e.Name;
+			lastFullPath = e.FullPath;
+			lastChangeType = e.ChangeType;
+			eventFired.Set ();		
+		}
+
+		// Helper to make sure the temporary directory is happy 
+		private void MakeTempDir ()
+		{
+			if(Directory.Exists (tempPath))
+				Directory.Delete (tempPath, true);
+			Directory.CreateDirectory (tempPath);
+		}
+
+		#endregion
 	}
 }
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 75497)
+++ ChangeLog	(working copy)
@@ -1,3 +1,7 @@
+2007-04-07  Louis R. Marascio <louis at fitnr.com>
+	* FileSystemWatcherTest.cs: add unit tests for Created, Deleted
+	  and Changed events.
+
 2004-01-16  Gonzalo Paniagua Javier <gonzalo at ximian.com>
 
 	* FileSystemWatcherTest.cs: new test.


More information about the Mono-devel-list mailing list