Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
//
// AppDomainTools.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using NUnit.Framework;
namespace MonoTests.Common
{
class AppDomainTools
{
public static void RunInSeparateDomain (Action handler, string format, params object[] parms)
{
var setup = AppDomain.CurrentDomain.SetupInformation;
setup.ShadowCopyDirectories = null;
setup.ShadowCopyFiles = null;
setup.ApplicationBase = Path.GetDirectoryName (typeof (AppDomainTools).Assembly.Location);
var ad = AppDomain.CreateDomain ("Test", new Evidence (AppDomain.CurrentDomain.Evidence) , setup);
ad.SetData ("testHandler", handler);
string message;
if (parms != null && parms.Length > 0)
message = String.Format (format, parms);
else
message = format;
ad.SetData ("failureMessage", message);
//ad.AssemblyResolve += ResolveAssemblyEventHandler;
ad.DoCallBack (RunTest);
}
static Assembly ResolveAssemblyEventHandler (object sender, ResolveEventArgs args)
{
string path = Path.Combine (AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "nunit.framework.dll");
if (File.Exists (path))
return Assembly.LoadFrom (path);
return null;
}
static void RunTest ()
{
Action handler = AppDomain.CurrentDomain.GetData ("testHandler") as Action;
if (handler == null) {
string message = AppDomain.CurrentDomain.GetData ("failureMessage") as string;
Assert.Fail (message);
}
handler ();
}
}
}

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace MonoTests.Common
{
delegate void AssertThrowsDelegate();
static class AssertExtensions
{
public static void AreEqual (byte[] expected, byte[] data, string message)
{
if (expected == null) {
if (data == null)
return;
Assert.Fail ("{0}{1}Expected: null{1}Got: byte array with {2} elements and of rank {3}{1}",
message, Environment.NewLine, data.Length, data.Rank);
}
if (data == null)
Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: null{1}",
message, Environment.NewLine, expected.Length, expected.Rank);
if (expected.Rank > 1)
Assert.Fail ("Only single-dimensional arrays are supported.");
if (expected.Rank != data.Rank || expected.Length != data.Length)
Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: byte array with {4} elements and rank {5}{1}",
message, Environment.NewLine, expected.Length, expected.Rank, data.Length, data.Rank);
int max = expected.Length;
for (int i = 0; i < max; i++) {
if (expected[i] != data[i])
Assert.Fail ("{0}{1}Arrays differ at index {2}.{1}Expected 0x{3:X} got 0x{4:X}{1}",
message, Environment.NewLine, i, expected[i], data[i]);
}
}
public static void Throws<ET> (AssertThrowsDelegate code, string message)
{
Throws(typeof(ET), code, message);
}
public static void Throws(Type exceptionType, AssertThrowsDelegate code, string message)
{
if (code == null)
Assert.Fail("No code provided for the test.");
Exception exception = null;
try
{
code();
}
catch (Exception ex)
{
exception = ex;
}
if (exceptionType == null)
{
if (exception == null)
Assert.Fail("{0}{1}Expected: any exception thrown{1}But was: no exception thrown{1}",
message, Environment.NewLine);
return;
}
if (exception == null || exception.GetType() != exceptionType)
Assert.Fail("{0}{1}Expected: {2}{1}But was: {3}{1}{4}{1}",
message,
Environment.NewLine,
exceptionType,
exception == null ? "no exception" : exception.GetType().ToString(),
exception == null ? "no exception" : exception.ToString());
}
public static void Throws(AssertThrowsDelegate code, string message)
{
Throws(null, code, message);
}
}
}

View File

@@ -0,0 +1,6 @@
2010-04-24 Marek Habersack <mhabersack@novell.com>
* AppDomainTools.cs, AssertExtensions.cs, PokerChangeMonitor.cs,
PokerMemoryCache.cs, PokerObjectCache.cs,
TestNotificationSystem.cs: added

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Text;
namespace MonoTests.Common
{
class PokerChangeMonitor : ChangeMonitor
{
List <string> calls;
string uniqueId;
public List<string> Calls
{
get
{
if (calls == null)
calls = new List<string> ();
return calls;
}
}
public override string UniqueId
{
get { return uniqueId; }
}
public PokerChangeMonitor ()
{
uniqueId = "UniqueID";
InitializationComplete ();
}
public void SignalChange ()
{
OnChanged (null);
}
protected override void Dispose (bool disposing)
{
Calls.Add ("Dispose (bool disposing)");
}
}
}

View File

@@ -0,0 +1,158 @@
//
// PokerMemoryCache.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Runtime.Caching;
using System.Text;
namespace MonoTests.Common
{
class PokerMemoryCache : MemoryCache
{
List<string> calls;
public List<string> Calls
{
get
{
if (calls == null)
calls = new List<string> ();
return calls;
}
}
public override object this [string key]
{
get
{
Calls.Add ("get_this [string key]");
return base [key];
}
set
{
Calls.Add ("set_this [string key]");
base [key] = value;
}
}
public PokerMemoryCache (string name, NameValueCollection config = null)
: base (name, config)
{ }
public override CacheItem AddOrGetExisting (CacheItem item, CacheItemPolicy policy)
{
Calls.Add ("AddOrGetExisting (CacheItem item, CacheItemPolicy policy)");
return base.AddOrGetExisting (item, policy);
}
public override object AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)
{
Calls.Add ("AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)");
return base.AddOrGetExisting (key, value, policy, regionName);
}
public override object AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
Calls.Add ("AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)");
return base.AddOrGetExisting (key, value, absoluteExpiration, regionName);
}
public override object Get (string key, string regionName = null)
{
Calls.Add ("Get (string key, string regionName = null)");
return base.Get (key, regionName);
}
public override CacheItem GetCacheItem (string key, string regionName = null)
{
Calls.Add ("GetCacheItem (string key, string regionName = null)");
return base.GetCacheItem (key, regionName);
}
public override long GetCount (string regionName = null)
{
Calls.Add ("GetCount (string regionName = null)");
return base.GetCount (regionName);
}
public override bool Contains (string key, string regionName = null)
{
Calls.Add ("Contains (string key, string regionName = null)");
return base.Contains (key, regionName);
}
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor (IEnumerable<string> keys, string regionName = null)
{
Calls.Add ("CreateCacheEntryChangeMonitor (IEnumerable<string> keys, string regionName = null)");
return base.CreateCacheEntryChangeMonitor (keys, regionName);
}
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator ()
{
Calls.Add ("IEnumerator<KeyValuePair<string, object>> GetEnumerator ()");
return base.GetEnumerator ();
}
public IEnumerator<KeyValuePair<string, object>> DoGetEnumerator ()
{
return GetEnumerator ();
}
public override IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)
{
Calls.Add ("IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)");
return base.GetValues (keys, regionName);
}
public override IDictionary<string, object> GetValues (string regionName, params string [] keys)
{
Calls.Add ("IDictionary<string, object> GetValues (string regionName, params string [] keys)");
return base.GetValues (regionName, keys);
}
public override void Set (CacheItem item, CacheItemPolicy policy)
{
Calls.Add ("Set (CacheItem item, CacheItemPolicy policy)");
base.Set (item, policy);
}
public override void Set (string key, object value, CacheItemPolicy policy, string regionName = null)
{
Calls.Add ("Set (string key, object value, CacheItemPolicy policy, string regionName = null)");
base.Set (key, value, policy, regionName);
}
public override void Set (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
Calls.Add ("Set (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)");
base.Set (key, value, absoluteExpiration, regionName);
}
}
}

View File

@@ -0,0 +1,186 @@
//
// PokerObjectCache.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Caching;
namespace MonoTests.Common
{
class PokerObjectCache : ObjectCache
{
Dictionary <string, object> cache;
Dictionary<string, object> Cache {
get
{
if (cache == null)
cache = new Dictionary<string, object> ();
return cache;
}
}
public string MethodCalled { get; private set; }
public override object AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)
{
MethodCalled = "AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)";
if (String.IsNullOrEmpty (key) || value == null)
return null;
object item;
if (Cache.TryGetValue (key, out item))
return item;
Cache.Add (key, value);
return null;
}
public override CacheItem AddOrGetExisting (CacheItem value, CacheItemPolicy policy)
{
MethodCalled = "AddOrGetExisting (CacheItem value, CacheItemPolicy policy)";
if (value == null)
return null;
object item;
if (Cache.TryGetValue (value.Key, out item))
return item as CacheItem;
Cache.Add (value.Key, value);
return null;
}
public override object AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
MethodCalled = "AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)";
if (String.IsNullOrEmpty (key) || value == null)
return null;
object item;
if (Cache.TryGetValue (key, out item))
return item;
Cache.Add (key, value);
return null;
}
public override bool Contains (string key, string regionName = null)
{
throw new NotImplementedException ();
}
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor (IEnumerable<string> keys, string regionName = null)
{
throw new NotImplementedException ();
}
public override DefaultCacheCapabilities DefaultCacheCapabilities
{
get { throw new NotImplementedException (); }
}
public override object Get (string key, string regionName = null)
{
throw new NotImplementedException ();
}
public override CacheItem GetCacheItem (string key, string regionName = null)
{
throw new NotImplementedException ();
}
public override long GetCount (string regionName = null)
{
throw new NotImplementedException ();
}
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator ()
{
throw new NotImplementedException ();
}
public override IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)
{
MethodCalled = "IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)";
var ret = new Dictionary<string, object> ();
if (keys == null)
return ret;
Dictionary <string, object> cache = Cache;
if (cache.Count == 0)
return ret;
object value;
foreach (string key in keys) {
if (!cache.TryGetValue (key, out value))
continue;
ret.Add (key, value);
}
return ret;
}
public override string Name
{
get { throw new NotImplementedException (); }
}
public override object Remove (string key, string regionName = null)
{
throw new NotImplementedException ();
}
public override void Set (string key, object value, CacheItemPolicy policy, string regionName = null)
{
throw new NotImplementedException ();
}
public override void Set (CacheItem item, CacheItemPolicy policy)
{
throw new NotImplementedException ();
}
public override void Set (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
throw new NotImplementedException ();
}
public override object this [string key]
{
get
{
throw new NotImplementedException ();
}
set
{
throw new NotImplementedException ();
}
}
}
}

View File

@@ -0,0 +1,91 @@
//
// TestNotificationSystem.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Runtime.Caching;
using System.Runtime.Caching.Hosting;
namespace MonoTests.Common
{
class TestNotificationSystem : IServiceProvider, IFileChangeNotificationSystem
{
OnChangedCallback callback;
public bool StartMonitoringCalled { get; private set; }
public uint StartMonitoringCallCount { get; private set; }
public bool StopMonitoringCalled { get; private set; }
public uint StopMonitoringCallCount { get; private set; }
public bool UseNullState { get; set; }
public object GetService (Type serviceType)
{
return this;
}
object IServiceProvider.GetService (Type serviceType)
{
return GetService (serviceType);
}
public void FakeChanged (string filePath)
{
if (callback == null)
return;
callback (null);
}
public void StartMonitoring (string filePath, OnChangedCallback onChangedCallback, out object state, out DateTimeOffset lastWriteTime, out long fileSize)
{
if (UseNullState)
state = null;
else
state = filePath;
lastWriteTime = DateTimeOffset.FromFileTime (DateTime.Now.Ticks);
callback = onChangedCallback;
fileSize = 10;
StartMonitoringCalled = true;
StartMonitoringCallCount++;
}
public void StopMonitoring (string filePath, object state)
{
StopMonitoringCalled = true;
StopMonitoringCallCount++;
}
void IFileChangeNotificationSystem.StartMonitoring (string filePath, OnChangedCallback onChangedCallback, out object state, out DateTimeOffset lastWriteTime, out long fileSize)
{
StartMonitoring (filePath, onChangedCallback, out state, out lastWriteTime, out fileSize);
}
void IFileChangeNotificationSystem.StopMonitoring (string filePath, object state)
{
StopMonitoring (filePath, state);
}
}
}

View File

@@ -0,0 +1,10 @@
2010-04-26 Marek Habersack <mhabersack@novell.com>
* MemoryCacheTest.cs: added tests for LRU removal of entries.
2010-04-24 Marek Habersack <mhabersack@novell.com>
* MemoryCacheTest.cs, ObjectCacheTest.cs: added
* HostFileChangeMonitorTest.cs: added more tests.

View File

@@ -0,0 +1,305 @@
//
// HostFileChangeMonitorTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Caching;
using System.Runtime.Caching.Hosting;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.Runtime.Caching
{
[TestFixture]
public class HostFileChangeMonitorTest
{
[Test]
public void Constructor_Exceptions ()
{
HostFileChangeMonitor monitor;
string relPath = Path.Combine ("relative", "file", "path");
var paths = new List<string> {
relPath
};
AssertExtensions.Throws<ArgumentException> (() => {
monitor = new HostFileChangeMonitor (paths);
}, "#A1");
paths.Clear ();
paths.Add (null);
AssertExtensions.Throws<ArgumentException> (() => {
monitor = new HostFileChangeMonitor (paths);
}, "#A2");
paths.Clear ();
paths.Add (String.Empty);
AssertExtensions.Throws<ArgumentException> (() => {
monitor = new HostFileChangeMonitor (paths);
}, "#A3");
AssertExtensions.Throws<ArgumentNullException> (() => {
monitor = new HostFileChangeMonitor (null);
}, "#A4");
paths.Clear ();
AssertExtensions.Throws<ArgumentException> (() => {
monitor = new HostFileChangeMonitor (paths);
}, "#A5");
}
[Test]
public void Constructor_MissingFiles ()
{
AppDomainTools.RunInSeparateDomain (Constructor_MissingFiles_Handler, "Constructor_MissingFiles");
}
static void Constructor_MissingFiles_Handler ()
{
HostFileChangeMonitor monitor;
PlatformID pid = Environment.OSVersion.Platform;
bool runningOnWindows = ((int) pid != 128 && pid != PlatformID.Unix && pid != PlatformID.MacOSX);
string missingFile = Path.Combine ("missing", "file", "path");
if (runningOnWindows)
missingFile = "c:\\" + missingFile;
else
missingFile = "/" + missingFile;
var paths = new List<string> {
missingFile
};
// Actually thrown by FileSystemWatcher constructor - note that the exception message suggests the file's
// parent directory is being watched, not the file itself:
//
// MonoTests.System.Runtime.Caching.HostFileChangeMonitorTest.Constructor_MissingFiles:
// System.ArgumentException : The directory name c:\missing\file is invalid.
// at System.IO.FileSystemWatcher..ctor(String path, String filter)
// at System.IO.FileSystemWatcher..ctor(String path)
// at System.Runtime.Caching.FileChangeNotificationSystem.System.Runtime.Caching.Hosting.IFileChangeNotificationSystem.StartMonitoring(String filePath, OnChangedCallback onChangedCallback, Object& state, DateTimeOffset& lastWriteTime, Int64& fileSize)
// at System.Runtime.Caching.HostFileChangeMonitor.InitDisposableMembers()
// at System.Runtime.Caching.HostFileChangeMonitor..ctor(IList`1 filePaths)
// 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
AssertExtensions.Throws<ArgumentException> (() => {
monitor = new HostFileChangeMonitor (paths);
}, "#A1");
if (runningOnWindows)
missingFile = "c:\\file.txt";
else
missingFile = "/file.txt";
paths.Clear ();
paths.Add (missingFile);
monitor = new HostFileChangeMonitor (paths);
Assert.AreEqual (1, monitor.FilePaths.Count, "#A2-1");
Assert.AreEqual (missingFile, monitor.FilePaths [0], "#A2-2");
Assert.AreEqual (missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId, "#A2-4");
paths.Add (missingFile);
monitor = new HostFileChangeMonitor (paths);
Assert.AreEqual (2, monitor.FilePaths.Count, "#A3-1");
Assert.AreEqual (missingFile, monitor.FilePaths [0], "#A3-2");
Assert.AreEqual (missingFile, monitor.FilePaths [1], "#A3-3");
Assert.AreEqual (missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId, "#A3-4");
}
[Test]
public void Constructor_Duplicates ()
{
HostFileChangeMonitor monitor;
PlatformID pid = Environment.OSVersion.Platform;
bool runningOnWindows = ((int) pid != 128 && pid != PlatformID.Unix && pid != PlatformID.MacOSX);
string missingFile = Path.Combine ("missing", "file", "path");
if (runningOnWindows)
missingFile = "c:\\file.txt";
else
missingFile = "/file.txt";
var paths = new List<string> {
missingFile,
missingFile
};
// Just checks if it doesn't throw any exception for dupes
monitor = new HostFileChangeMonitor (paths);
}
static Tuple <string, string, string, IList <string>> SetupMonitoring ()
{
string testPath = Path.Combine (Path.GetTempPath (), "Dispose_Calls_StopMonitoring");
if (!Directory.Exists (testPath))
Directory.CreateDirectory (testPath);
string firstFile = Path.Combine (testPath, "FirstFile.txt");
string secondFile = Path.Combine (testPath, "SecondFile.txt");
File.WriteAllText (firstFile, "I am the first file.");
File.WriteAllText (secondFile, "I am the second file.");
var paths = new List<string> {
firstFile,
secondFile
};
return new Tuple<string, string, string, IList<string>> (testPath, firstFile, secondFile, paths);
}
static void CleanupMonitoring (Tuple<string, string, string, IList<string>> setup)
{
string testPath = setup != null ? setup.Item1 : null;
if (String.IsNullOrEmpty (testPath) || !Directory.Exists (testPath))
return;
foreach (string f in Directory.EnumerateFiles(testPath)) {
try {
File.Delete (f);
} catch {
// ignore
}
}
}
[Test]
public void Constructor_Calls_StartMonitoring ()
{
AppDomainTools.RunInSeparateDomain (Constructor_Calls_StartMonitoring_Handler, "Constructor_Calls_StartMonitoring_Handler");
}
static void Constructor_Calls_StartMonitoring_Handler ()
{
Tuple<string, string, string, IList<string>> setup = null;
try {
var tns = new TestNotificationSystem ();
ObjectCache.Host = tns;
setup = SetupMonitoring ();
var monitor = new HostFileChangeMonitor (setup.Item4);
Assert.IsTrue (tns.StartMonitoringCalled, "#A1-1");
Assert.AreEqual (2, tns.StartMonitoringCallCount, "#A1-2");
} finally {
CleanupMonitoring (setup);
}
}
[Test]
public void Dispose_Calls_StopMonitoring ()
{
AppDomainTools.RunInSeparateDomain (Dispose_Calls_StopMonitoring_Handler, "Dispose_Calls_StopMonitoring_Handler");
}
static void Dispose_Calls_StopMonitoring_Handler ()
{
Tuple<string, string, string, IList<string>> setup = null;
try {
var tns = new TestNotificationSystem ();
ObjectCache.Host = tns;
setup = SetupMonitoring ();
var monitor = new HostFileChangeMonitor (setup.Item4);
tns.FakeChanged (setup.Item2);
Assert.IsTrue (tns.StopMonitoringCalled, "#A1-1");
Assert.AreEqual (2, tns.StopMonitoringCallCount, "#A1-2");
} finally {
CleanupMonitoring (setup);
}
}
[Test]
public void Dispose_NullState_NoStopMonitoring ()
{
AppDomainTools.RunInSeparateDomain (Dispose_NullState_NoStopMonitoring_Handler, "Dispose_NullState_NoStopMonitoring_Handler");
}
static void Dispose_NullState_NoStopMonitoring_Handler ()
{
Tuple<string, string, string, IList<string>> setup = null;
try {
var tns = new TestNotificationSystem ();
tns.UseNullState = true;
ObjectCache.Host = tns;
setup = SetupMonitoring ();
var monitor = new HostFileChangeMonitor (setup.Item4);
tns.FakeChanged (setup.Item2);
Assert.IsFalse (tns.StopMonitoringCalled, "#A1-1");
Assert.AreEqual (0, tns.StopMonitoringCallCount, "#A1-2");
} finally {
CleanupMonitoring (setup);
}
}
[Test]
public void UniqueId ()
{
Tuple<string, string, string, IList<string>> setup = null;
try {
setup = SetupMonitoring ();
FileInfo fi;
var monitor = new HostFileChangeMonitor (setup.Item4);
var sb = new StringBuilder ();
fi = new FileInfo (setup.Item2);
sb.AppendFormat ("{0}{1:X}{2:X}",
setup.Item2,
fi.LastWriteTimeUtc.Ticks,
fi.Length);
fi = new FileInfo (setup.Item3);
sb.AppendFormat ("{0}{1:X}{2:X}",
setup.Item3,
fi.LastWriteTimeUtc.Ticks,
fi.Length);
Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A1");
var list = new List<string> (setup.Item4);
list.Add (setup.Item1);
monitor = new HostFileChangeMonitor (list);
var di = new DirectoryInfo (setup.Item1);
sb.AppendFormat ("{0}{1:X}{2:X}",
setup.Item1,
di.LastWriteTimeUtc.Ticks,
-1L);
Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A2");
list.Add (setup.Item1);
monitor = new HostFileChangeMonitor (list);
Assert.AreEqual (sb.ToString (), monitor.UniqueId, "#A3");
} finally {
CleanupMonitoring (setup);
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,159 @@
//
// ObjectCacheTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.Runtime.Caching
{
[TestFixture]
public class ObjectCacheTest
{
[Test]
public void Host ()
{
Assert.IsTrue (ObjectCache.Host == null, "#A1");
AppDomainTools.RunInSeparateDomain (Host_SetToNull, "Host_SetToNull");
AppDomainTools.RunInSeparateDomain (Host_SetToProvider, "Host_SetToProvider");
}
static void Host_SetToNull ()
{
AssertExtensions.Throws<ArgumentNullException> (() => {
ObjectCache.Host = null;
}, "#A2");
}
static void Host_SetToProvider ()
{
var tns1 = new TestNotificationSystem ();
var tns2 = new TestNotificationSystem ();
ObjectCache.Host = tns1;
Assert.IsNotNull (ObjectCache.Host, "#A3-1");
Assert.AreEqual (tns1, ObjectCache.Host, "#A3-2");
AssertExtensions.Throws<InvalidOperationException> (() => {
ObjectCache.Host = tns2;
}, "#A4");
}
[Test]
public void Add_CacheItem_CacheItemPolicy ()
{
var poker = new PokerObjectCache ();
bool ret;
ret = poker.Add (null, null);
Assert.IsTrue (ret, "#A1-1");
Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A1-2");
var item = new CacheItem ("key", 1234);
ret = poker.Add (item, null);
Assert.IsTrue (ret, "#A2-1");
Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A2-2");
ret = poker.Add (item, null);
Assert.IsFalse (ret, "#A3-1");
Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A3-2");
}
[Test]
public void Add_String_Object_CacheItemPolicy_String ()
{
var poker = new PokerObjectCache ();
bool ret;
ret = poker.Add (null, null, null, null);
Assert.IsTrue (ret, "#A1-1");
Assert.AreEqual ("AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)", poker.MethodCalled, "#A1-2");
ret = poker.Add ("key", 1234, null, null);
Assert.IsTrue (ret, "#A2-1");
Assert.AreEqual ("AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)", poker.MethodCalled, "#A2-2");
ret = poker.Add ("key", 1234, null, null);
Assert.IsFalse (ret, "#A2-1");
Assert.AreEqual ("AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)", poker.MethodCalled, "#A2-2");
}
[Test]
public void Add_String_Object_DateTimeOffset_String ()
{
var poker = new PokerObjectCache ();
bool ret;
ret = poker.Add (null, null, DateTimeOffset.Now, null);
Assert.IsTrue (ret, "#A1-1");
Assert.AreEqual ("AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)", poker.MethodCalled, "#A1-2");
ret = poker.Add ("key", 1234, DateTimeOffset.Now, null);
Assert.IsTrue (ret, "#A2-1");
Assert.AreEqual ("AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)", poker.MethodCalled, "#A2-2");
ret = poker.Add ("key", 1234, DateTimeOffset.Now, null);
Assert.IsFalse (ret, "#A2-1");
Assert.AreEqual ("AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)", poker.MethodCalled, "#A2-2");
}
[Test]
public void GetValues ()
{
var poker = new PokerObjectCache ();
IDictionary<string, object> values = poker.GetValues (null, (string []) null);
Assert.IsNotNull (values, "#A1-1");
Assert.AreEqual (0, values.Count, "#A1-2");
Assert.AreEqual ("IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)", poker.MethodCalled, "#A1-3");
poker.Add ("key1", 1, null);
poker.Add ("key2", 2, null);
poker.Add ("key3", 3, null);
values = poker.GetValues (new string [] { "key1", "key2", "key3" });
Assert.IsNotNull (values, "#A2-1");
Assert.AreEqual (3, values.Count, "#A2-2");
Assert.AreEqual ("IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)", poker.MethodCalled, "#A2-3");
values = poker.GetValues (new string [] { "key1", "key22", "key3" });
Assert.IsNotNull (values, "#A3-1");
Assert.AreEqual (2, values.Count, "#A3-2");
Assert.AreEqual ("IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)", poker.MethodCalled, "#A3-3");
}
[Test]
public void Defaults ()
{
Assert.AreEqual (DateTimeOffset.MaxValue, ObjectCache.InfiniteAbsoluteExpiration, "#A1");
Assert.AreEqual (TimeSpan.Zero, ObjectCache.NoSlidingExpiration, "#A2");
}
}
}