New tests.
[mono.git] / mcs / class / System.Runtime.Caching / Test / System.Runtime.Caching / ObjectCacheTest.cs
1 //
2 // ObjectCacheTest.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.Runtime.Caching;
31
32 using NUnit.Framework;
33 using MonoTests.Common;
34
35 namespace MonoTests.System.Runtime.Caching
36 {
37         [TestFixture]
38         public class ObjectCacheTest
39         {
40                 [Test]
41                 public void Host ()
42                 {
43                         Assert.IsTrue (ObjectCache.Host == null, "#A1");
44
45                         AppDomainTools.RunInSeparateDomain (Host_SetToNull, "Host_SetToNull");
46                         AppDomainTools.RunInSeparateDomain (Host_SetToProvider, "Host_SetToProvider");
47                 }
48
49                 static void Host_SetToNull ()
50                 {
51                         AssertExtensions.Throws<ArgumentNullException> (() => {
52                                 ObjectCache.Host = null;
53                         }, "#A2");
54                 }
55
56                 static void Host_SetToProvider ()
57                 {
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");
63
64                         AssertExtensions.Throws<InvalidOperationException> (() => {
65                                 ObjectCache.Host = tns2;
66                         }, "#A4");
67                 }
68
69                 [Test]
70                 public void Add_CacheItem_CacheItemPolicy ()
71                 {
72                         var poker = new PokerObjectCache ();
73                         bool ret;
74
75                         ret = poker.Add (null, null);
76                         Assert.IsTrue (ret, "#A1-1");
77                         Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A1-2");
78
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");
83
84                         ret = poker.Add (item, null);
85                         Assert.IsFalse (ret, "#A3-1");
86                         Assert.AreEqual ("AddOrGetExisting (CacheItem value, CacheItemPolicy policy)", poker.MethodCalled, "#A3-2");
87                 }
88
89                 [Test]
90                 public void Add_String_Object_CacheItemPolicy_String ()
91                 {
92                         var poker = new PokerObjectCache ();
93                         bool ret;
94
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");
98
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");
102
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");
106                 }
107
108                 [Test]
109                 public void Add_String_Object_DateTimeOffset_String ()
110                 {
111                         var poker = new PokerObjectCache ();
112                         bool ret;
113
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");
117
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");
121
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");
125                 }
126
127                 [Test]
128                 public void GetValues ()
129                 {
130                         var poker = new PokerObjectCache ();
131
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");
136
137                         poker.Add ("key1", 1, null);
138                         poker.Add ("key2", 2, null);
139                         poker.Add ("key3", 3, null);
140
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");
145
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");
150                 }
151
152                 [Test]
153                 public void Defaults ()
154                 {
155                         Assert.AreEqual (DateTimeOffset.MaxValue, ObjectCache.InfiniteAbsoluteExpiration, "#A1");
156                         Assert.AreEqual (TimeSpan.Zero, ObjectCache.NoSlidingExpiration, "#A2");
157                 }
158         }
159 }