PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Blocks/Caching/Tests/Caching.Tests/Configuration/Fluent/ForCustomCacheManagerNamedExtensionFixture.cs

#
C# | 307 lines | 247 code | 50 blank | 10 comment | 0 complexity | ccf2709d0d95ed272befa36edd52274f MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices Enterprise Library
  3. // Caching Application Block
  4. //===============================================================================
  5. // Copyright Š Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using Microsoft.VisualStudio.TestTools.UnitTesting;
  16. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
  17. using Microsoft.Practices.EnterpriseLibrary.Caching.Configuration;
  18. using System.Collections.Specialized;
  19. namespace Microsoft.Practices.EnterpriseLibrary.Caching.Tests.Configuration.Fluent
  20. {
  21. [TestClass]
  22. public class When_CallingForCustomCachgeManagerNamedOnCachingConfigurationWithNullName : Given_CachingSettingsInConfigurationSourceBuilder
  23. {
  24. [TestMethod]
  25. [ExpectedException(typeof(ArgumentException))]
  26. public void Then_ThrowsArgumentException()
  27. {
  28. base.CachingConfiguration.ForCustomCacheManagerNamed(null, GetType(), new NameValueCollection());
  29. }
  30. }
  31. [TestClass]
  32. public class When_CallingForCustomCachgeManagerNamedOnCachingConfigurationWithNullType : Given_CachingSettingsInConfigurationSourceBuilder
  33. {
  34. [TestMethod]
  35. [ExpectedException(typeof(ArgumentNullException))]
  36. public void Then_ThrowsArgumentNullException()
  37. {
  38. base.CachingConfiguration.ForCustomCacheManagerNamed("name", null, new NameValueCollection());
  39. }
  40. }
  41. [TestClass]
  42. public class When_CallingForCustomCachgeManagerNamedOnCachingConfigurationWithNulAttributes : Given_CachingSettingsInConfigurationSourceBuilder
  43. {
  44. [TestMethod]
  45. [ExpectedException(typeof(ArgumentNullException))]
  46. public void Then_ThrowsArgumentNullException()
  47. {
  48. base.CachingConfiguration.ForCustomCacheManagerNamed("name", GetType(), null);
  49. }
  50. }
  51. [TestClass]
  52. public class When_CallingForCustomCachgeManagerNamedOnCachingConfiguration : Given_CachingSettingsInConfigurationSourceBuilder
  53. {
  54. protected override void Act()
  55. {
  56. base.CachingConfiguration.ForCustomCacheManagerNamed("custom cache manager name", typeof(TestCustomCacheManager));
  57. }
  58. [TestMethod]
  59. public void Then_CustomCacheManagerConfigurationIsContainedInCachingSettings()
  60. {
  61. var cachingSettings = GetCacheManagerSettings();
  62. Assert.IsTrue(cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().Any());
  63. }
  64. [TestMethod]
  65. public void Then_CustomCacheManagerConfigurationHasAppropriateName()
  66. {
  67. var cachingSettings = GetCacheManagerSettings();
  68. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  69. Assert.AreEqual("custom cache manager name", cacheManager.Name);
  70. }
  71. [TestMethod]
  72. public void Then_CustomCacheManagerConfigurationHasAppropriateType()
  73. {
  74. var cachingSettings = GetCacheManagerSettings();
  75. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  76. Assert.AreEqual(typeof(TestCustomCacheManager), cacheManager.Type);
  77. }
  78. [TestMethod]
  79. public void Then_CustomCacheManagerConfigurationHasNoAttributes()
  80. {
  81. var cachingSettings = GetCacheManagerSettings();
  82. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  83. Assert.AreEqual(0, cacheManager.Attributes.Count);
  84. }
  85. }
  86. [TestClass]
  87. public class When_CallingForCustomCachgeManagerNamedOnCachingConfigurationGeneric : Given_CachingSettingsInConfigurationSourceBuilder
  88. {
  89. protected override void Act()
  90. {
  91. base.CachingConfiguration.ForCustomCacheManagerNamed<TestCustomCacheManager>("custom cache manager name");
  92. }
  93. [TestMethod]
  94. public void Then_CustomCacheManagerConfigurationIsContainedInCachingSettings()
  95. {
  96. var cachingSettings = GetCacheManagerSettings();
  97. Assert.IsTrue(cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().Any());
  98. }
  99. [TestMethod]
  100. public void Then_CustomCacheManagerConfigurationHasAppropriateName()
  101. {
  102. var cachingSettings = GetCacheManagerSettings();
  103. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  104. Assert.AreEqual("custom cache manager name", cacheManager.Name);
  105. }
  106. [TestMethod]
  107. public void Then_CustomCacheManagerConfigurationHasAppropriateType()
  108. {
  109. var cachingSettings = GetCacheManagerSettings();
  110. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  111. Assert.AreEqual(typeof(TestCustomCacheManager), cacheManager.Type);
  112. }
  113. [TestMethod]
  114. public void Then_CustomCacheManagerConfigurationHasNoAttributes()
  115. {
  116. var cachingSettings = GetCacheManagerSettings();
  117. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  118. Assert.AreEqual(0, cacheManager.Attributes.Count);
  119. }
  120. }
  121. [TestClass]
  122. public class When_CallingForCustomCachgeManagerNamedOnCachingConfigurationPassingAttributesGeneric : Given_CachingSettingsInConfigurationSourceBuilder
  123. {
  124. NameValueCollection attributes;
  125. protected override void Act()
  126. {
  127. attributes = new NameValueCollection();
  128. attributes.Add("key1", "value1");
  129. attributes.Add("key2", "value2");
  130. base.CachingConfiguration.ForCustomCacheManagerNamed<TestCustomCacheManager>("custom cache manager name", attributes);
  131. }
  132. [TestMethod]
  133. public void Then_CustomCacheManagerConfigurationIsContainedInCachingSettings()
  134. {
  135. var cachingSettings = GetCacheManagerSettings();
  136. Assert.IsTrue(cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().Any());
  137. }
  138. [TestMethod]
  139. public void Then_CustomCacheManagerConfigurationHasAppropriateName()
  140. {
  141. var cachingSettings = GetCacheManagerSettings();
  142. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  143. Assert.AreEqual("custom cache manager name", cacheManager.Name);
  144. }
  145. [TestMethod]
  146. public void Then_CustomCacheManagerConfigurationHasAppropriateType()
  147. {
  148. var cachingSettings = GetCacheManagerSettings();
  149. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  150. Assert.AreEqual(typeof(TestCustomCacheManager), cacheManager.Type);
  151. }
  152. [TestMethod]
  153. public void Then_CustomCacheManagerConfigurationHasAppropriateAttributes()
  154. {
  155. var cachingSettings = GetCacheManagerSettings();
  156. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  157. Assert.AreEqual(attributes.Count, cacheManager.Attributes.Count);
  158. foreach (string attKey in attributes)
  159. {
  160. Assert.AreEqual(attributes[attKey], cacheManager.Attributes[attKey]);
  161. }
  162. }
  163. }
  164. [TestClass]
  165. public class When_CallingForCustomCachgeManagerNamedOnCachingConfigurationPassingAttributes : Given_CachingSettingsInConfigurationSourceBuilder
  166. {
  167. NameValueCollection attributes;
  168. protected override void Act()
  169. {
  170. attributes = new NameValueCollection();
  171. attributes.Add("key1", "value1");
  172. attributes.Add("key2", "value2");
  173. base.CachingConfiguration.ForCustomCacheManagerNamed("custom cache manager name", typeof(TestCustomCacheManager), attributes);
  174. }
  175. [TestMethod]
  176. public void Then_CustomCacheManagerConfigurationIsContainedInCachingSettings()
  177. {
  178. var cachingSettings = GetCacheManagerSettings();
  179. Assert.IsTrue(cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().Any());
  180. }
  181. [TestMethod]
  182. public void Then_CustomCacheManagerConfigurationHasAppropriateName()
  183. {
  184. var cachingSettings = GetCacheManagerSettings();
  185. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  186. Assert.AreEqual("custom cache manager name", cacheManager.Name);
  187. }
  188. [TestMethod]
  189. public void Then_CustomCacheManagerConfigurationHasAppropriateType()
  190. {
  191. var cachingSettings = GetCacheManagerSettings();
  192. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  193. Assert.AreEqual(typeof(TestCustomCacheManager), cacheManager.Type);
  194. }
  195. [TestMethod]
  196. public void Then_CustomCacheManagerConfigurationHasAppropriateAttributes()
  197. {
  198. var cachingSettings = GetCacheManagerSettings();
  199. var cacheManager = cachingSettings.CacheManagers.OfType<CustomCacheManagerData>().First();
  200. Assert.AreEqual(attributes.Count, cacheManager.Attributes.Count);
  201. foreach (string attKey in attributes)
  202. {
  203. Assert.AreEqual(attributes[attKey], cacheManager.Attributes[attKey]);
  204. }
  205. }
  206. }
  207. [TestClass]
  208. public class When_CallingUseAsDefaultCacheOnCustomCacheManager : Given_CachingSettingsInConfigurationSourceBuilder
  209. {
  210. protected override void Act()
  211. {
  212. base.CachingConfiguration
  213. .ForCustomCacheManagerNamed("custom cache manager name", typeof(TestCustomCacheManager))
  214. .UseAsDefaultCache();
  215. }
  216. [TestMethod]
  217. public void Then_CachingConfigurationHasCustomCacheAsDefaultCacheManager()
  218. {
  219. Assert.AreEqual("custom cache manager name", base.GetCacheManagerSettings().DefaultCacheManager);
  220. }
  221. }
  222. class TestCustomCacheManager : ICacheManager
  223. {
  224. public void Add(string key, object value)
  225. {
  226. throw new NotImplementedException();
  227. }
  228. public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
  229. {
  230. throw new NotImplementedException();
  231. }
  232. public bool Contains(string key)
  233. {
  234. throw new NotImplementedException();
  235. }
  236. public int Count
  237. {
  238. get { throw new NotImplementedException(); }
  239. }
  240. public void Flush()
  241. {
  242. throw new NotImplementedException();
  243. }
  244. public object GetData(string key)
  245. {
  246. throw new NotImplementedException();
  247. }
  248. public void Remove(string key)
  249. {
  250. throw new NotImplementedException();
  251. }
  252. public object this[string key]
  253. {
  254. get { throw new NotImplementedException(); }
  255. }
  256. }
  257. }