/
ConfigurationManagerExtensionsTest.cs
192 lines (151 loc) 路 9.08 KB
/
ConfigurationManagerExtensionsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
锘縰sing Microsoft.VisualStudio.TestTools.UnitTesting;
using Songhay.Extensions;
using System.IO;
using System.Configuration;
using System.Xml.Linq;
using System.Linq;
using Songhay.Models;
namespace Songhay.Tests.Extensions
{
/// <summary>
/// Tests for <see cref="ConfigurationManagerExtensions"/>
/// </summary>
[TestClass]
public class ConfigurationManagerExtensionsTest
{
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext { get; set; }
[TestMethod]
[TestProperty("expectedConnectionString", @"Data Source=|DataDirectory|\Chinook.dev.sqlite")]
[TestProperty("externalConfigurationFile", @"Extensions\ConfigurationManagerExtensionsTest.xml")]
[TestProperty("unqualifiedName", "Chinook")]
public void ShouldGetExternalConnectionStringSettings()
{
var projectsFolder = this.TestContext.ShouldGetAssemblyDirectoryParent(this.GetType(), expectedLevels: 2);
#region test properties:
var expectedConnectionString = this.TestContext.Properties["expectedConnectionString"].ToString();
this.TestContext.WriteLine("expected: {0}", expectedConnectionString);
var externalConfigurationFile = this.TestContext.Properties["externalConfigurationFile"].ToString();
externalConfigurationFile = Path.Combine(projectsFolder, externalConfigurationFile);
this.TestContext.ShouldFindFile(externalConfigurationFile);
var unqualifiedName = this.TestContext.Properties["unqualifiedName"].ToString();
#endregion
var environmentName = ConfigurationManager
.AppSettings
.GetEnvironmentName(DeploymentEnvironment.ConfigurationKey, "defaultEnvironmentName");
var externalConfigurationDoc = XDocument.Load(externalConfigurationFile);
var collection = ConfigurationManager
.ConnectionStrings
.WithConnectionStringSettingsCollection(externalConfigurationDoc);
Assert.IsNotNull(collection, string.Format("The expected ConnectionStringSettingsCollection is not here."));
Assert.IsTrue(collection.OfType<ConnectionStringSettings>().Any(), "The expected ConnectionStringSettings are not here.");
collection.OfType<ConnectionStringSettings>().ForEachInEnumerable(i => this.TestContext.WriteLine(i.ToString()));
var name = collection.GetConnectionNameFromEnvironment(unqualifiedName, environmentName);
this.TestContext.WriteLine("name: {0}", name);
var settings = collection.GetConnectionStringSettings(name);
Assert.IsNotNull(settings, "The expected settings are not here.");
var actual = settings.ConnectionString;
this.TestContext.WriteLine("actual: {0}", actual);
Assert.AreEqual(expectedConnectionString, actual, "The expectedConnectionString is not here.");
}
[TestMethod]
[TestProperty("expectedSetting", "the external setting for DEV")]
[TestProperty("externalConfigurationFile", @"Extensions\ConfigurationManagerExtensionsTest.xml")]
[TestProperty("unqualifiedKey", "ex-setting")]
public void ShouldGetExternalSetting()
{
var projectsFolder = this.TestContext.ShouldGetAssemblyDirectoryParent(this.GetType(), expectedLevels: 2);
#region test properties:
var expectedSetting = this.TestContext.Properties["expectedSetting"].ToString();
this.TestContext.WriteLine("expected: {0}", expectedSetting);
var externalConfigurationFile = this.TestContext.Properties["externalConfigurationFile"].ToString();
externalConfigurationFile = Path.Combine(projectsFolder, externalConfigurationFile);
this.TestContext.ShouldFindFile(externalConfigurationFile);
var unqualifiedKey = this.TestContext.Properties["unqualifiedKey"].ToString();
#endregion
var environmentName = ConfigurationManager
.AppSettings
.GetEnvironmentName(DeploymentEnvironment.ConfigurationKey, "defaultEnvironmentName");
var externalConfigurationDoc = XDocument.Load(externalConfigurationFile);
var collection = ConfigurationManager
.AppSettings
.WithAppSettings(externalConfigurationDoc);
this.TestContext.WriteLine("appSettings keys:");
Assert.IsTrue(collection.AllKeys.Any(), "The expected appSettings keys are not here.");
collection.AllKeys.ForEachInEnumerable(i => this.TestContext.WriteLine("key: {0}, value: {1}", i, collection[i]));
var key = collection.GetKeyWithEnvironmentName(unqualifiedKey, environmentName);
this.TestContext.WriteLine("key: {0}", key);
var actual = collection.GetSetting(key);
this.TestContext.WriteLine("actual: {0}", actual);
Assert.AreEqual(expectedSetting, actual, "The expectedSetting is not here.");
}
[TestMethod]
[TestProperty("expectedConnectionString", @"Data Source=|DataDirectory|\Northwind.dev.sqlite")]
[TestProperty("unqualifiedName", "Northwind")]
public void ShouldGetConnectionStringSettings()
{
#region test properties:
var expectedConnectionString = this.TestContext.Properties["expectedConnectionString"].ToString();
this.TestContext.WriteLine("expected: {0}", expectedConnectionString);
var unqualifiedName = this.TestContext.Properties["unqualifiedName"].ToString();
#endregion
var environmentName = ConfigurationManager
.AppSettings
.GetEnvironmentName(DeploymentEnvironment.ConfigurationKey, "defaultEnvironmentName");
var name = ConfigurationManager
.ConnectionStrings
.GetConnectionNameFromEnvironment(unqualifiedName, environmentName);
this.TestContext.WriteLine("name: {0}", name);
var settings = ConfigurationManager
.ConnectionStrings
.GetConnectionStringSettings(name);
Assert.IsNotNull(settings, "The expected settings are not here.");
var actual = settings.ConnectionString;
this.TestContext.WriteLine("actual: {0}", actual);
Assert.AreEqual(expectedConnectionString, actual, "The expectedConnectionString is not here.");
}
[TestMethod]
[TestProperty("expectedEnvironmentName", DeploymentEnvironment.DevelopmentEnvironmentName)]
public void ShouldGetEnvironmentName()
{
var expectedEnvironmentName = this.TestContext.Properties["expectedEnvironmentName"].ToString();
this.TestContext.WriteLine("expected: {0}", expectedEnvironmentName);
var actual = ConfigurationManager.AppSettings.GetEnvironmentName(DeploymentEnvironment.ConfigurationKey, "defaultEnvironmentName");
this.TestContext.WriteLine("actual: {0}", actual);
Assert.AreEqual(expectedEnvironmentName, actual, "The expectedEnvironmentName is not here.");
}
[TestMethod]
[TestProperty("expectedKey", "dev.setting")]
[TestProperty("unqualifiedKey", "setting")]
public void ShouldGetKeyWithEnvironmentName()
{
var expectedKey = this.TestContext.Properties["expectedKey"].ToString();
this.TestContext.WriteLine("expected: {0}", expectedKey);
var unqualifiedKey = this.TestContext.Properties["unqualifiedKey"].ToString();
var environmentName = ConfigurationManager.AppSettings.GetEnvironmentName(DeploymentEnvironment.ConfigurationKey, "defaultEnvironmentName");
var actual = ConfigurationManager.AppSettings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName);
this.TestContext.WriteLine("actual: {0}", actual);
Assert.AreEqual(expectedKey, actual, "The expectedKey is not here.");
}
[TestMethod]
[TestProperty("expectedSetting", "the setting for DEV")]
[TestProperty("unqualifiedKey", "setting")]
public void ShouldGetSetting()
{
var expectedSetting = this.TestContext.Properties["expectedSetting"].ToString();
this.TestContext.WriteLine("expected: {0}", expectedSetting);
var unqualifiedKey = this.TestContext.Properties["unqualifiedKey"].ToString();
var environmentName = ConfigurationManager
.AppSettings
.GetEnvironmentName(DeploymentEnvironment.ConfigurationKey, "defaultEnvironmentName");
var key = ConfigurationManager.AppSettings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName);
this.TestContext.WriteLine("key: {0}", key);
var actual = ConfigurationManager.AppSettings.GetSetting(key);
this.TestContext.WriteLine("actual: {0}", actual);
Assert.AreEqual(expectedSetting, actual, "The expectedSetting is not here.");
}
}
}