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;
30 using System.Runtime.Caching;
32 using NUnit.Framework;
33 using MonoTests.Common;
35 namespace MonoTests.System.Runtime.Caching
38 public class ObjectCacheTest
43 Assert.IsTrue (ObjectCache.Host == null, "#A1");
45 AppDomainTools.RunInSeparateDomain (Host_SetToNull, "Host_SetToNull");
46 AppDomainTools.RunInSeparateDomain (Host_SetToProvider, "Host_SetToProvider");
49 static void Host_SetToNull ()
51 AssertExtensions.Throws<ArgumentNullException> (() => {
52 ObjectCache.Host = null;
56 static void Host_SetToProvider ()
58 var tns1 = new TestNotificationSystem ();
59 var tns2 = new TestNotificationSystem ();
60 ObjectCache.Host = tns1;
61 Assert.IsNotNull (ObjectCache.Host, "#A3-1");
62 Assert.AreEqual (tns1, ObjectCache.Host, "#A3-2");
64 AssertExtensions.Throws<InvalidOperationException> (() => {
65 ObjectCache.Host = tns2;
70 public void Add_CacheItem_CacheItemPolicy ()
72 var poker = new PokerObjectCache ();
75 ret = poker.Add (null, null);
76 Assert.IsTrue (ret, "#A1-1");
77 Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A1-2");
79 var item = new CacheItem ("key", 1234);
80 ret = poker.Add (item, null);
81 Assert.IsTrue (ret, "#A2-1");
82 Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A2-2");
84 ret = poker.Add (item, null);
85 Assert.IsFalse (ret, "#A3-1");
86 Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A3-2");
90 public void Add_String_Object_CacheItemPolicy_String ()
92 var poker = new PokerObjectCache ();
95 ret = poker.Add (null, null, null, null);
96 Assert.IsTrue (ret, "#A1-1");
97 Assert.AreEqual ("AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)", poker.MethodCalled, "#A1-2");
99 ret = poker.Add ("key", 1234, null, null);
100 Assert.IsTrue (ret, "#A2-1");
101 Assert.AreEqual ("AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)", poker.MethodCalled, "#A2-2");
103 ret = poker.Add ("key", 1234, null, null);
104 Assert.IsFalse (ret, "#A2-1");
105 Assert.AreEqual ("AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)", poker.MethodCalled, "#A2-2");
109 public void Add_String_Object_DateTimeOffset_String ()
111 var poker = new PokerObjectCache ();
114 ret = poker.Add (null, null, DateTimeOffset.Now, null);
115 Assert.IsTrue (ret, "#A1-1");
116 Assert.AreEqual ("AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)", poker.MethodCalled, "#A1-2");
118 ret = poker.Add ("key", 1234, DateTimeOffset.Now, null);
119 Assert.IsTrue (ret, "#A2-1");
120 Assert.AreEqual ("AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)", poker.MethodCalled, "#A2-2");
122 ret = poker.Add ("key", 1234, DateTimeOffset.Now, null);
123 Assert.IsFalse (ret, "#A2-1");
124 Assert.AreEqual ("AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)", poker.MethodCalled, "#A2-2");
128 public void GetValues ()
130 var poker = new PokerObjectCache ();
132 IDictionary<string, object> values = poker.GetValues (null, (string []) null);
133 Assert.IsNotNull (values, "#A1-1");
134 Assert.AreEqual (0, values.Count, "#A1-2");
135 Assert.AreEqual ("IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)", poker.MethodCalled, "#A1-3");
137 poker.Add ("key1", 1, null);
138 poker.Add ("key2", 2, null);
139 poker.Add ("key3", 3, null);
141 values = poker.GetValues (new string [] { "key1", "key2", "key3" });
142 Assert.IsNotNull (values, "#A2-1");
143 Assert.AreEqual (3, values.Count, "#A2-2");
144 Assert.AreEqual ("IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)", poker.MethodCalled, "#A2-3");
146 values = poker.GetValues (new string [] { "key1", "key22", "key3" });
147 Assert.IsNotNull (values, "#A3-1");
148 Assert.AreEqual (2, values.Count, "#A3-2");
149 Assert.AreEqual ("IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)", poker.MethodCalled, "#A3-3");
153 public void Defaults ()
155 Assert.AreEqual (DateTimeOffset.MaxValue, ObjectCache.InfiniteAbsoluteExpiration, "#A1");
156 Assert.AreEqual (TimeSpan.Zero, ObjectCache.NoSlidingExpiration, "#A2");