TestConfigurationProcessorFactory.cs (6084B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="TestConfigurationProcessorFactory.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Helpers 8 { 9 using System; 10 using System.Collections.Generic; 11 12 /// <summary> 13 /// A test implementation of IConfigurationSetProcessorFactory. 14 /// </summary> 15 internal partial class TestConfigurationProcessorFactory : IConfigurationSetProcessorFactory 16 { 17 /// <summary> 18 /// Delegate type for CreateSetProcessor. 19 /// </summary> 20 /// <param name="factory">The TestConfigurationProcessorFactory that is calling this function.</param> 21 /// <param name="configurationSet">The set.</param> 22 /// <returns>A new TestConfigurationSetProcessor for the set.</returns> 23 internal delegate IConfigurationSetProcessor CreateSetProcessorDelegateType(TestConfigurationProcessorFactory factory, ConfigurationSet configurationSet); 24 25 /// <summary> 26 /// Diagnostics event; useful for logging and/or verbose output. 27 /// </summary> 28 #pragma warning disable CS0067 // The event is never used 29 public event EventHandler<IDiagnosticInformation>? Diagnostics; 30 #pragma warning restore CS0067 // The event is never used 31 32 /// <summary> 33 /// Gets or sets the minimum diagnostic level to send. 34 /// </summary> 35 public DiagnosticLevel MinimumLevel { get; set; } = DiagnosticLevel.Informational; 36 37 /// <summary> 38 /// Gets or sets the processor used when the incoming configuration set is null. 39 /// </summary> 40 internal TestConfigurationSetProcessor? NullProcessor { get; set; } 41 42 /// <summary> 43 /// Gets or sets the processors to be used by this factory. 44 /// </summary> 45 internal Dictionary<ConfigurationSet, TestConfigurationSetProcessor> Processors { get; set; } = 46 new Dictionary<ConfigurationSet, TestConfigurationSetProcessor>(); 47 48 /// <summary> 49 /// Gets or sets the exception used when the incoming configuration set is null. 50 /// </summary> 51 internal Exception? NullException { get; set; } 52 53 /// <summary> 54 /// Gets or sets the exceptions to be used by this factory. 55 /// </summary> 56 internal Dictionary<ConfigurationSet, Exception> Exceptions { get; set; } = 57 new Dictionary<ConfigurationSet, Exception>(); 58 59 /// <summary> 60 /// Gets or sets the delegate use to replace the default CreateSetProcessor functionality. 61 /// </summary> 62 internal CreateSetProcessorDelegateType? CreateSetProcessorDelegate { get; set; } 63 64 /// <summary> 65 /// Creates a new TestConfigurationSetProcessor for the set. 66 /// </summary> 67 /// <param name="configurationSet">The set.</param> 68 /// <returns>A new TestConfigurationSetProcessor for the set.</returns> 69 public IConfigurationSetProcessor CreateSetProcessor(ConfigurationSet configurationSet) 70 { 71 if (this.CreateSetProcessorDelegate != null) 72 { 73 return this.CreateSetProcessorDelegate(this, configurationSet); 74 } 75 76 return this.DefaultCreateSetProcessor(configurationSet); 77 } 78 79 /// <summary> 80 /// The default test implementation that creates a new TestConfigurationSetProcessor for the set. 81 /// </summary> 82 /// <param name="configurationSet">The set.</param> 83 /// <returns>A new TestConfigurationSetProcessor for the set.</returns> 84 internal IConfigurationSetProcessor DefaultCreateSetProcessor(ConfigurationSet configurationSet) 85 { 86 if (configurationSet == null) 87 { 88 if (this.NullException != null) 89 { 90 throw this.NullException; 91 } 92 93 if (this.NullProcessor == null) 94 { 95 this.NullProcessor = new TestConfigurationSetProcessor(null); 96 } 97 98 return this.NullProcessor; 99 } 100 101 if (this.Exceptions.ContainsKey(configurationSet)) 102 { 103 throw this.Exceptions[configurationSet]; 104 } 105 106 if (!this.Processors.ContainsKey(configurationSet)) 107 { 108 this.Processors.Add(configurationSet, new TestConfigurationSetProcessor(configurationSet)); 109 } 110 111 return this.Processors[configurationSet]; 112 } 113 114 /// <summary> 115 /// A convenience function to create a new processor for the given set and store it in the dictionary for use in the test. 116 /// </summary> 117 /// <param name="configurationSet">The set.</param> 118 /// <returns>A new TestConfigurationSetProcessor for the set.</returns> 119 internal TestConfigurationSetProcessor CreateTestProcessor(ConfigurationSet configurationSet) 120 { 121 this.Processors[configurationSet] = new TestConfigurationSetProcessor(configurationSet); 122 return this.Processors[configurationSet]; 123 } 124 125 /// <summary> 126 /// A convenience function to create a new group processor for the given set and store it in the dictionary for use in the test. 127 /// </summary> 128 /// <param name="configurationSet">The set.</param> 129 /// <returns>A new TestConfigurationSetGroupProcessor for the set.</returns> 130 internal TestConfigurationSetGroupProcessor CreateTestGroupProcessor(ConfigurationSet configurationSet) 131 { 132 TestConfigurationSetGroupProcessor result = new (configurationSet); 133 this.Processors[configurationSet] = result; 134 return result; 135 } 136 } 137 }