PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Blocks/Logging/Tests/Configuration.Manageability/TraceListeners/SystemDiagnosticsTraceListenerDataManageabilityProviderFixture.cs

#
C# | 237 lines | 184 code | 38 blank | 15 comment | 1 complexity | a5db6ab7e07439e7517f50dd47cc0b88 MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices Enterprise Library
  3. // Logging 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.Diagnostics;
  14. using System.Reflection;
  15. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
  16. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;
  17. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm;
  18. using Microsoft.Practices.EnterpriseLibrary.Common.TestSupport.Configuration.Manageability.Mocks;
  19. using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Manageability.TraceListeners;
  20. using Microsoft.VisualStudio.TestTools.UnitTesting;
  21. namespace Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Manageability.Tests.TraceListeners
  22. {
  23. [TestClass]
  24. public class SystemDiagnosticsTraceListenerDataManageabilityProviderFixture
  25. {
  26. SystemDiagnosticsTraceListenerDataManageabilityProvider provider;
  27. MockRegistryKey machineKey;
  28. MockRegistryKey machineOptionsKey;
  29. MockRegistryKey userKey;
  30. MockRegistryKey userOptionsKey;
  31. SystemDiagnosticsTraceListenerData configurationObject;
  32. [TestInitialize]
  33. public void SetUp()
  34. {
  35. provider = new SystemDiagnosticsTraceListenerDataManageabilityProvider();
  36. machineKey = new MockRegistryKey(true);
  37. machineOptionsKey = new MockRegistryKey(false);
  38. userKey = new MockRegistryKey(true);
  39. userOptionsKey = new MockRegistryKey(false);
  40. configurationObject = new SystemDiagnosticsTraceListenerData();
  41. }
  42. [TestMethod]
  43. public void ManageabilityProviderIsProperlyRegistered()
  44. {
  45. ConfigurationElementManageabilityProviderAttribute selectedAttribute = null;
  46. Assembly assembly = typeof(SystemDiagnosticsTraceListenerDataManageabilityProvider).Assembly;
  47. foreach (ConfigurationElementManageabilityProviderAttribute providerAttribute
  48. in assembly.GetCustomAttributes(typeof(ConfigurationElementManageabilityProviderAttribute), false))
  49. {
  50. if (providerAttribute.ManageabilityProviderType.Equals(typeof(SystemDiagnosticsTraceListenerDataManageabilityProvider)))
  51. {
  52. selectedAttribute = providerAttribute;
  53. break;
  54. }
  55. }
  56. Assert.IsNotNull(selectedAttribute);
  57. Assert.AreSame(typeof(LoggingSettingsManageabilityProvider), selectedAttribute.SectionManageabilityProviderType);
  58. Assert.AreSame(typeof(SystemDiagnosticsTraceListenerData), selectedAttribute.TargetType);
  59. }
  60. [TestMethod]
  61. public void ConfigurationObjectIsNotModifiedIfThereAreNoPolicyOverrides()
  62. {
  63. // no need to test for attributes, it's tested for parent class
  64. configurationObject.Type = typeof(Object);
  65. configurationObject.InitData = "init data";
  66. configurationObject.TraceOutputOptions = TraceOptions.None;
  67. configurationObject.Filter = SourceLevels.Error;
  68. provider.OverrideWithGroupPolicies(configurationObject, true, null, null);
  69. Assert.AreSame(typeof(Object), configurationObject.Type);
  70. Assert.AreEqual("init data", configurationObject.InitData);
  71. Assert.AreEqual(TraceOptions.None, configurationObject.TraceOutputOptions);
  72. Assert.AreEqual(SourceLevels.Error, configurationObject.Filter);
  73. }
  74. [TestMethod]
  75. public void ConfigurationObjectIsModifiedIfThereAreMachinePolicyOverrides()
  76. {
  77. // no need to test for attributes, it's tested for parent class
  78. configurationObject.Type = typeof(Object);
  79. configurationObject.InitData = "init data";
  80. configurationObject.TraceOutputOptions = TraceOptions.None;
  81. configurationObject.Filter = SourceLevels.Error;
  82. machineKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.ProviderTypePropertyName, typeof(Object).AssemblyQualifiedName);
  83. machineKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.AttributesPropertyName, "");
  84. machineKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.InitDataPropertyName, "overriden init data");
  85. machineKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.FilterPropertyName, "Critical");
  86. machineKey.AddSubKey(MsmqTraceListenerDataManageabilityProvider.TraceOutputOptionsPropertyName, machineOptionsKey);
  87. machineOptionsKey.AddIntValue(TraceOptions.ProcessId.ToString(), 1);
  88. machineOptionsKey.AddIntValue(TraceOptions.ThreadId.ToString(), 1);
  89. provider.OverrideWithGroupPolicies(configurationObject, true, machineKey, null);
  90. Assert.AreEqual("overriden init data", configurationObject.InitData);
  91. Assert.AreEqual(TraceOptions.ProcessId | TraceOptions.ThreadId, configurationObject.TraceOutputOptions);
  92. Assert.AreEqual(SourceLevels.Critical, configurationObject.Filter);
  93. }
  94. [TestMethod]
  95. public void ConfigurationObjectIsModifiedIfThereAreUserPolicyOverrides()
  96. {
  97. // no need to test for attributes, it's tested for parent class
  98. configurationObject.Type = typeof(Object);
  99. configurationObject.InitData = "init data";
  100. configurationObject.TraceOutputOptions = TraceOptions.None;
  101. configurationObject.Filter = SourceLevels.Error;
  102. userKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.ProviderTypePropertyName, typeof(Object).AssemblyQualifiedName);
  103. userKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.AttributesPropertyName, "");
  104. userKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.InitDataPropertyName, "overriden init data");
  105. userKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.FilterPropertyName, "Critical");
  106. userKey.AddSubKey(MsmqTraceListenerDataManageabilityProvider.TraceOutputOptionsPropertyName, userOptionsKey);
  107. userOptionsKey.AddIntValue(TraceOptions.ProcessId.ToString(), 1);
  108. userOptionsKey.AddIntValue(TraceOptions.ThreadId.ToString(), 1);
  109. provider.OverrideWithGroupPolicies(configurationObject, true, null, userKey);
  110. Assert.AreEqual("overriden init data", configurationObject.InitData);
  111. Assert.AreEqual(TraceOptions.ProcessId | TraceOptions.ThreadId, configurationObject.TraceOutputOptions);
  112. Assert.AreEqual(SourceLevels.Critical, configurationObject.Filter);
  113. }
  114. [TestMethod]
  115. public void ConfigurationObjectIsNotModifiedIfThereArePolicyOverridesButGroupPoliciesAreDisabled()
  116. {
  117. // no need to test for attributes, it's tested for parent class
  118. configurationObject.Type = typeof(Object);
  119. configurationObject.InitData = "init data";
  120. configurationObject.TraceOutputOptions = TraceOptions.None;
  121. configurationObject.Filter = SourceLevels.Error;
  122. machineKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.InitDataPropertyName, "overriden init data");
  123. machineKey.AddStringValue(SystemDiagnosticsTraceListenerDataManageabilityProvider.FilterPropertyName, "Critical");
  124. machineKey.AddSubKey(MsmqTraceListenerDataManageabilityProvider.TraceOutputOptionsPropertyName, machineOptionsKey);
  125. machineOptionsKey.AddIntValue(TraceOptions.ProcessId.ToString(), 1);
  126. machineOptionsKey.AddIntValue(TraceOptions.ThreadId.ToString(), 1);
  127. provider.OverrideWithGroupPolicies(configurationObject, false, machineKey, userKey);
  128. Assert.AreSame(typeof(Object), configurationObject.Type);
  129. Assert.AreEqual("init data", configurationObject.InitData);
  130. Assert.AreEqual(TraceOptions.None, configurationObject.TraceOutputOptions);
  131. Assert.AreEqual(SourceLevels.Error, configurationObject.Filter);
  132. }
  133. [TestMethod]
  134. public void ManageabilityProviderGeneratesProperAdmContent()
  135. {
  136. DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();
  137. LoggingSettings section = new LoggingSettings();
  138. configurationSource.Add(LoggingSettings.SectionName, section);
  139. configurationObject.Type = typeof(object);
  140. MockAdmContentBuilder contentBuilder = new MockAdmContentBuilder();
  141. contentBuilder.StartCategory("category");
  142. provider.AddAdministrativeTemplateDirectives(contentBuilder, configurationObject, configurationSource, "TestApp");
  143. contentBuilder.EndCategory();
  144. MockAdmContent content = contentBuilder.GetMockContent();
  145. IEnumerator<AdmCategory> categoriesEnumerator = content.Categories.GetEnumerator();
  146. categoriesEnumerator.MoveNext();
  147. IEnumerator<AdmPolicy> policiesEnumerator = categoriesEnumerator.Current.Policies.GetEnumerator();
  148. Assert.IsTrue(policiesEnumerator.MoveNext());
  149. IEnumerator<AdmPart> partsEnumerator = policiesEnumerator.Current.Parts.GetEnumerator();
  150. Assert.IsTrue(partsEnumerator.MoveNext());
  151. Assert.AreSame(typeof(AdmEditTextPart), partsEnumerator.Current.GetType());
  152. Assert.IsNull(partsEnumerator.Current.KeyName);
  153. Assert.IsTrue(partsEnumerator.MoveNext());
  154. Assert.AreSame(typeof(AdmEditTextPart), partsEnumerator.Current.GetType());
  155. Assert.IsNull(partsEnumerator.Current.KeyName);
  156. Assert.IsTrue(partsEnumerator.MoveNext());
  157. Assert.AreSame(typeof(AdmEditTextPart), partsEnumerator.Current.GetType());
  158. Assert.IsNull(partsEnumerator.Current.KeyName);
  159. Assert.AreEqual(SystemDiagnosticsTraceListenerDataManageabilityProvider.InitDataPropertyName,
  160. partsEnumerator.Current.ValueName);
  161. Assert.IsTrue(partsEnumerator.MoveNext());
  162. Assert.AreSame(typeof(AdmTextPart), partsEnumerator.Current.GetType());
  163. Assert.IsNull(partsEnumerator.Current.KeyName);
  164. Assert.IsNull(partsEnumerator.Current.ValueName);
  165. // trace output options checkboxes
  166. Assert.IsTrue(partsEnumerator.MoveNext());
  167. Assert.AreSame(typeof(AdmCheckboxPart), partsEnumerator.Current.GetType());
  168. Assert.IsNotNull(partsEnumerator.Current.KeyName);
  169. Assert.AreEqual("LogicalOperationStack", partsEnumerator.Current.ValueName);
  170. Assert.IsTrue(partsEnumerator.MoveNext());
  171. Assert.AreSame(typeof(AdmCheckboxPart), partsEnumerator.Current.GetType());
  172. Assert.IsNotNull(partsEnumerator.Current.KeyName);
  173. Assert.AreEqual("DateTime", partsEnumerator.Current.ValueName);
  174. Assert.IsTrue(partsEnumerator.MoveNext());
  175. Assert.AreSame(typeof(AdmCheckboxPart), partsEnumerator.Current.GetType());
  176. Assert.IsNotNull(partsEnumerator.Current.KeyName);
  177. Assert.AreEqual("Timestamp", partsEnumerator.Current.ValueName);
  178. Assert.IsTrue(partsEnumerator.MoveNext());
  179. Assert.AreSame(typeof(AdmCheckboxPart), partsEnumerator.Current.GetType());
  180. Assert.IsNotNull(partsEnumerator.Current.KeyName);
  181. Assert.AreEqual("ProcessId", partsEnumerator.Current.ValueName);
  182. Assert.IsTrue(partsEnumerator.MoveNext());
  183. Assert.AreSame(typeof(AdmCheckboxPart), partsEnumerator.Current.GetType());
  184. Assert.IsNotNull(partsEnumerator.Current.KeyName);
  185. Assert.AreEqual("ThreadId", partsEnumerator.Current.ValueName);
  186. Assert.IsTrue(partsEnumerator.MoveNext());
  187. Assert.AreSame(typeof(AdmCheckboxPart), partsEnumerator.Current.GetType());
  188. Assert.IsNotNull(partsEnumerator.Current.KeyName);
  189. Assert.AreEqual("Callstack", partsEnumerator.Current.ValueName);
  190. Assert.IsTrue(partsEnumerator.MoveNext());
  191. Assert.AreSame(typeof(AdmDropDownListPart), partsEnumerator.Current.GetType());
  192. Assert.IsNull(partsEnumerator.Current.KeyName);
  193. Assert.AreEqual(SystemDiagnosticsTraceListenerDataManageabilityProvider.FilterPropertyName,
  194. partsEnumerator.Current.ValueName);
  195. Assert.IsFalse(partsEnumerator.MoveNext());
  196. Assert.IsFalse(policiesEnumerator.MoveNext());
  197. }
  198. }
  199. }