New tests.
[mono.git] / mcs / class / System.Runtime.Caching / Test / System.Runtime.Caching / HostFileChangeMonitorTest.cs
1 //
2 // HostFileChangeMonitorTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Runtime.Caching;
33 using System.Runtime.Caching.Hosting;
34 using System.Text;
35
36 using NUnit.Framework;
37 using MonoTests.Common;
38
39 namespace MonoTests.System.Runtime.Caching
40 {       
41         [TestFixture]
42         public class HostFileChangeMonitorTest
43         {
44                 [Test]
45                 public void Constructor_Exceptions ()
46                 {
47                         HostFileChangeMonitor monitor;
48                         string relPath = Path.Combine ("relative", "file", "path");
49                         var paths = new List<string> {
50                                 relPath
51                         };
52
53                         AssertExtensions.Throws<ArgumentException> (() => {
54                                 monitor = new HostFileChangeMonitor (paths);
55                         }, "#A1");
56
57                         paths.Clear ();
58                         paths.Add (null);
59                         AssertExtensions.Throws<ArgumentException> (() => {
60                                 monitor = new HostFileChangeMonitor (paths);
61                         }, "#A2");
62
63                         paths.Clear ();
64                         paths.Add (String.Empty);
65                         AssertExtensions.Throws<ArgumentException> (() => {
66                                 monitor = new HostFileChangeMonitor (paths);
67                         }, "#A3");
68
69                         AssertExtensions.Throws<ArgumentNullException> (() => {
70                                 monitor = new HostFileChangeMonitor (null);
71                         }, "#A4");
72
73                         paths.Clear ();
74                         AssertExtensions.Throws<ArgumentException> (() => {
75                                 monitor = new HostFileChangeMonitor (paths);
76                         }, "#A5");
77                 }
78
79                 [Test]
80                 public void Constructor_MissingFiles ()
81                 {
82                         AppDomainTools.RunInSeparateDomain (Constructor_MissingFiles_Handler, "Constructor_MissingFiles");
83                 }
84
85                 static void Constructor_MissingFiles_Handler ()
86                 {
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");
91
92                         if (runningOnWindows)
93                                 missingFile = "c:\\" + missingFile;
94                         else
95                                 missingFile = "/" + missingFile;
96
97                         var paths = new List<string> {
98                                 missingFile
99                         };
100
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:
103                         //
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);
114                         }, "#A1");
115
116                         if (runningOnWindows)
117                                 missingFile = "c:\\file.txt";
118                         else
119                                 missingFile = "/file.txt";
120
121                         paths.Clear ();
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");
127
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");
134                 }
135
136                 [Test]
137                 public void Constructor_Duplicates ()
138                 {
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");
143
144                         if (runningOnWindows)
145                                 missingFile = "c:\\file.txt";
146                         else
147                                 missingFile = "/file.txt";
148
149                         var paths = new List<string> {
150                                 missingFile,
151                                 missingFile
152                         };
153
154                         // Just checks if it doesn't throw any exception for dupes
155                         monitor = new HostFileChangeMonitor (paths);
156                 }
157
158                 static Tuple <string, string, string, IList <string>> SetupMonitoring ()
159                 {
160                         string testPath = Path.Combine (Path.GetTempPath (), "Dispose_Calls_StopMonitoring");
161                         if (!Directory.Exists (testPath))
162                                 Directory.CreateDirectory (testPath);
163
164                         string firstFile = Path.Combine (testPath, "FirstFile.txt");
165                         string secondFile = Path.Combine (testPath, "SecondFile.txt");
166
167                         File.WriteAllText (firstFile, "I am the first file.");
168                         File.WriteAllText (secondFile, "I am the second file.");
169
170                         var paths = new List<string> {
171                                 firstFile,
172                                 secondFile
173                         };
174
175                         return new Tuple<string, string, string, IList<string>> (testPath, firstFile, secondFile, paths);
176                 }
177
178                 static void CleanupMonitoring (Tuple<string, string, string, IList<string>> setup)
179                 {
180                         string testPath = setup != null ? setup.Item1 : null;
181                         if (String.IsNullOrEmpty (testPath) || !Directory.Exists (testPath))
182                                 return;
183
184                         foreach (string f in Directory.EnumerateFiles(testPath)) {
185                                 try {
186                                         File.Delete (f);
187                                 } catch {
188                                         // ignore
189                                 }
190                         }
191                 }
192
193                 [Test]
194                 public void Constructor_Calls_StartMonitoring ()
195                 {
196                         AppDomainTools.RunInSeparateDomain (Constructor_Calls_StartMonitoring_Handler, "Constructor_Calls_StartMonitoring_Handler");
197                 }
198
199                 static void Constructor_Calls_StartMonitoring_Handler ()
200                 {
201                         Tuple<string, string, string, IList<string>> setup = null;
202                         try {
203                                 var tns = new TestNotificationSystem ();
204                                 ObjectCache.Host = tns;
205                                 setup = SetupMonitoring ();
206                                 var monitor = new HostFileChangeMonitor (setup.Item4);
207
208                                 Assert.IsTrue (tns.StartMonitoringCalled, "#A1-1");
209                                 Assert.AreEqual (2, tns.StartMonitoringCallCount, "#A1-2");
210                         } finally {
211                                 CleanupMonitoring (setup);
212                         }
213                 }
214
215                 [Test]
216                 public void Dispose_Calls_StopMonitoring ()
217                 {
218                         AppDomainTools.RunInSeparateDomain (Dispose_Calls_StopMonitoring_Handler, "Dispose_Calls_StopMonitoring_Handler");
219                 }
220
221                 static void Dispose_Calls_StopMonitoring_Handler ()
222                 {
223                         Tuple<string, string, string, IList<string>> setup = null;
224                         try {
225                                 var tns = new TestNotificationSystem ();
226                                 ObjectCache.Host = tns;
227                                 setup = SetupMonitoring ();
228                                 var monitor = new HostFileChangeMonitor (setup.Item4);
229                                 tns.FakeChanged (setup.Item2);
230
231                                 Assert.IsTrue (tns.StopMonitoringCalled, "#A1-1");
232                                 Assert.AreEqual (2, tns.StopMonitoringCallCount, "#A1-2");
233                         } finally {
234                                 CleanupMonitoring (setup);
235                         }
236                 }
237
238                 [Test]
239                 public void Dispose_NullState_NoStopMonitoring ()
240                 {
241                         AppDomainTools.RunInSeparateDomain (Dispose_NullState_NoStopMonitoring_Handler, "Dispose_NullState_NoStopMonitoring_Handler");
242                 }
243
244                 static void Dispose_NullState_NoStopMonitoring_Handler ()
245                 {
246                         Tuple<string, string, string, IList<string>> setup = null;
247                         try {
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);
254
255                                 Assert.IsFalse (tns.StopMonitoringCalled, "#A1-1");
256                                 Assert.AreEqual (0, tns.StopMonitoringCallCount, "#A1-2");
257                         } finally {
258                                 CleanupMonitoring (setup);
259                         }
260                 }
261
262                 [Test]
263                 public void UniqueId ()
264                 {
265                         Tuple<string, string, string, IList<string>> setup = null;
266                         try {
267                                 setup = SetupMonitoring ();
268                                 FileInfo fi;
269                                 var monitor = new HostFileChangeMonitor (setup.Item4);
270                                 var sb = new StringBuilder ();
271
272                                 fi = new FileInfo (setup.Item2);
273                                 sb.AppendFormat ("{0}{1:X}{2:X}",
274                                         setup.Item2,
275                                         fi.LastWriteTimeUtc.Ticks,
276                                         fi.Length);
277
278                                 fi = new FileInfo (setup.Item3);
279                                 sb.AppendFormat ("{0}{1:X}{2:X}",
280                                         setup.Item3,
281                                         fi.LastWriteTimeUtc.Ticks,
282                                         fi.Length);
283
284                                 Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A1");
285
286                                 var list = new List<string> (setup.Item4);
287                                 list.Add (setup.Item1);
288
289                                 monitor = new HostFileChangeMonitor (list);
290                                 var di = new DirectoryInfo (setup.Item1);
291                                 sb.AppendFormat ("{0}{1:X}{2:X}",
292                                         setup.Item1,
293                                         di.LastWriteTimeUtc.Ticks,
294                                         -1L);
295                                 Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A2");
296
297                                 list.Add (setup.Item1);
298                                 monitor = new HostFileChangeMonitor (list);
299                                 Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A3");
300                         } finally {
301                                 CleanupMonitoring (setup);
302                         }
303                 }
304         }
305 }