2 // HostFileChangeMonitorTest.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Runtime.Caching;
33 using System.Runtime.Caching.Hosting;
36 using NUnit.Framework;
37 using MonoTests.Common;
39 namespace MonoTests.System.Runtime.Caching
42 public class HostFileChangeMonitorTest
45 public void Constructor_Exceptions ()
47 HostFileChangeMonitor monitor;
48 string relPath = Path.Combine ("relative", "file", "path");
49 var paths = new List<string> {
53 AssertExtensions.Throws<ArgumentException> (() => {
54 monitor = new HostFileChangeMonitor (paths);
59 AssertExtensions.Throws<ArgumentException> (() => {
60 monitor = new HostFileChangeMonitor (paths);
64 paths.Add (String.Empty);
65 AssertExtensions.Throws<ArgumentException> (() => {
66 monitor = new HostFileChangeMonitor (paths);
69 AssertExtensions.Throws<ArgumentNullException> (() => {
70 monitor = new HostFileChangeMonitor (null);
74 AssertExtensions.Throws<ArgumentException> (() => {
75 monitor = new HostFileChangeMonitor (paths);
80 public void Constructor_MissingFiles ()
82 AppDomainTools.RunInSeparateDomain (Constructor_MissingFiles_Handler, "Constructor_MissingFiles");
85 static void Constructor_MissingFiles_Handler ()
87 HostFileChangeMonitor monitor;
88 PlatformID pid = Environment.OSVersion.Platform;
89 bool runningOnWindows = ((int) pid != 128 && pid != PlatformID.Unix && pid != PlatformID.MacOSX);
90 string missingFile = Path.Combine ("missing", "file", "path");
93 missingFile = "c:\\" + missingFile;
95 missingFile = "/" + missingFile;
97 var paths = new List<string> {
101 // Actually thrown by FileSystemWatcher constructor - note that the exception message suggests the file's
102 // parent directory is being watched, not the file itself:
104 // MonoTests.System.Runtime.Caching.HostFileChangeMonitorTest.Constructor_MissingFiles:
105 // System.ArgumentException : The directory name c:\missing\file is invalid.
106 // at System.IO.FileSystemWatcher..ctor(String path, String filter)
107 // at System.IO.FileSystemWatcher..ctor(String path)
108 // at System.Runtime.Caching.FileChangeNotificationSystem.System.Runtime.Caching.Hosting.IFileChangeNotificationSystem.StartMonitoring(String filePath, OnChangedCallback onChangedCallback, Object& state, DateTimeOffset& lastWriteTime, Int64& fileSize)
109 // at System.Runtime.Caching.HostFileChangeMonitor.InitDisposableMembers()
110 // at System.Runtime.Caching.HostFileChangeMonitor..ctor(IList`1 filePaths)
111 // at MonoTests.System.Runtime.Caching.HostFileChangeMonitorTest.Constructor_MissingFiles() in c:\users\grendel\documents\visual studio 2010\Projects\System.Runtime.Caching.Test\System.Runtime.Caching.Test\System.Runtime.Caching\HostFileChangeMonitorTest.cs:line 68
112 AssertExtensions.Throws<ArgumentException> (() => {
113 monitor = new HostFileChangeMonitor (paths);
116 if (runningOnWindows)
117 missingFile = "c:\\file.txt";
119 missingFile = "/file.txt";
122 paths.Add (missingFile);
123 monitor = new HostFileChangeMonitor (paths);
124 Assert.AreEqual (1, monitor.FilePaths.Count, "#A2-1");
125 Assert.AreEqual (missingFile, monitor.FilePaths [0], "#A2-2");
126 Assert.AreEqual (missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId, "#A2-4");
128 paths.Add (missingFile);
129 monitor = new HostFileChangeMonitor (paths);
130 Assert.AreEqual (2, monitor.FilePaths.Count, "#A3-1");
131 Assert.AreEqual (missingFile, monitor.FilePaths [0], "#A3-2");
132 Assert.AreEqual (missingFile, monitor.FilePaths [1], "#A3-3");
133 Assert.AreEqual (missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId, "#A3-4");
137 public void Constructor_Duplicates ()
139 HostFileChangeMonitor monitor;
140 PlatformID pid = Environment.OSVersion.Platform;
141 bool runningOnWindows = ((int) pid != 128 && pid != PlatformID.Unix && pid != PlatformID.MacOSX);
142 string missingFile = Path.Combine ("missing", "file", "path");
144 if (runningOnWindows)
145 missingFile = "c:\\file.txt";
147 missingFile = "/file.txt";
149 var paths = new List<string> {
154 // Just checks if it doesn't throw any exception for dupes
155 monitor = new HostFileChangeMonitor (paths);
158 static Tuple <string, string, string, IList <string>> SetupMonitoring ()
160 string testPath = Path.Combine (Path.GetTempPath (), "Dispose_Calls_StopMonitoring");
161 if (!Directory.Exists (testPath))
162 Directory.CreateDirectory (testPath);
164 string firstFile = Path.Combine (testPath, "FirstFile.txt");
165 string secondFile = Path.Combine (testPath, "SecondFile.txt");
167 File.WriteAllText (firstFile, "I am the first file.");
168 File.WriteAllText (secondFile, "I am the second file.");
170 var paths = new List<string> {
175 return new Tuple<string, string, string, IList<string>> (testPath, firstFile, secondFile, paths);
178 static void CleanupMonitoring (Tuple<string, string, string, IList<string>> setup)
180 string testPath = setup != null ? setup.Item1 : null;
181 if (String.IsNullOrEmpty (testPath) || !Directory.Exists (testPath))
184 foreach (string f in Directory.EnumerateFiles(testPath)) {
194 public void Constructor_Calls_StartMonitoring ()
196 AppDomainTools.RunInSeparateDomain (Constructor_Calls_StartMonitoring_Handler, "Constructor_Calls_StartMonitoring_Handler");
199 static void Constructor_Calls_StartMonitoring_Handler ()
201 Tuple<string, string, string, IList<string>> setup = null;
203 var tns = new TestNotificationSystem ();
204 ObjectCache.Host = tns;
205 setup = SetupMonitoring ();
206 var monitor = new HostFileChangeMonitor (setup.Item4);
208 Assert.IsTrue (tns.StartMonitoringCalled, "#A1-1");
209 Assert.AreEqual (2, tns.StartMonitoringCallCount, "#A1-2");
211 CleanupMonitoring (setup);
216 public void Dispose_Calls_StopMonitoring ()
218 AppDomainTools.RunInSeparateDomain (Dispose_Calls_StopMonitoring_Handler, "Dispose_Calls_StopMonitoring_Handler");
221 static void Dispose_Calls_StopMonitoring_Handler ()
223 Tuple<string, string, string, IList<string>> setup = null;
225 var tns = new TestNotificationSystem ();
226 ObjectCache.Host = tns;
227 setup = SetupMonitoring ();
228 var monitor = new HostFileChangeMonitor (setup.Item4);
229 tns.FakeChanged (setup.Item2);
231 Assert.IsTrue (tns.StopMonitoringCalled, "#A1-1");
232 Assert.AreEqual (2, tns.StopMonitoringCallCount, "#A1-2");
234 CleanupMonitoring (setup);
239 public void Dispose_NullState_NoStopMonitoring ()
241 AppDomainTools.RunInSeparateDomain (Dispose_NullState_NoStopMonitoring_Handler, "Dispose_NullState_NoStopMonitoring_Handler");
244 static void Dispose_NullState_NoStopMonitoring_Handler ()
246 Tuple<string, string, string, IList<string>> setup = null;
248 var tns = new TestNotificationSystem ();
249 tns.UseNullState = true;
250 ObjectCache.Host = tns;
251 setup = SetupMonitoring ();
252 var monitor = new HostFileChangeMonitor (setup.Item4);
253 tns.FakeChanged (setup.Item2);
255 Assert.IsFalse (tns.StopMonitoringCalled, "#A1-1");
256 Assert.AreEqual (0, tns.StopMonitoringCallCount, "#A1-2");
258 CleanupMonitoring (setup);
263 public void UniqueId ()
265 Tuple<string, string, string, IList<string>> setup = null;
267 setup = SetupMonitoring ();
269 var monitor = new HostFileChangeMonitor (setup.Item4);
270 var sb = new StringBuilder ();
272 fi = new FileInfo (setup.Item2);
273 sb.AppendFormat ("{0}{1:X}{2:X}",
275 fi.LastWriteTimeUtc.Ticks,
278 fi = new FileInfo (setup.Item3);
279 sb.AppendFormat ("{0}{1:X}{2:X}",
281 fi.LastWriteTimeUtc.Ticks,
284 Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A1");
286 var list = new List<string> (setup.Item4);
287 list.Add (setup.Item1);
289 monitor = new HostFileChangeMonitor (list);
290 var di = new DirectoryInfo (setup.Item1);
291 sb.AppendFormat ("{0}{1:X}{2:X}",
293 di.LastWriteTimeUtc.Ticks,
295 Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A2");
297 list.Add (setup.Item1);
298 monitor = new HostFileChangeMonitor (list);
299 Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A3");
301 CleanupMonitoring (setup);