-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathConnectionStringsSectionHandler2Tests.cs
331 lines (297 loc) · 26.2 KB
/
ConnectionStringsSectionHandler2Tests.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using Microsoft.Configuration.ConfigurationBuilders;
using Xunit;
namespace Test
{
public class ConnectionStringsSectionHandler2Tests : IDisposable
{
private static readonly string cssh2ConfigTemplate = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
<configSections>
<section name=""connectionStrings"" type=""System.Configuration.ConnectionStringsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"" restartOnExternalChanges=""false"" requirePermission=""false""/>
<section name=""configBuilders"" type=""System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"" restartOnExternalChanges=""false"" requirePermission=""false"" />
<section name=""Microsoft.Configuration.ConfigurationBuilders.SectionHandlers"" type=""Microsoft.Configuration.ConfigurationBuilders.SectionHandlersSection, Microsoft.Configuration.ConfigurationBuilders.Base"" restartOnExternalChanges=""false"" requirePermission=""false"" />
</configSections>
<Microsoft.Configuration.ConfigurationBuilders.SectionHandlers>
<handlers>
<remove name=""DefaultConnectionStringsHandler"" />
<add name=""NewConnectionStringsHandler"" type=""Microsoft.Configuration.ConfigurationBuilders.ConnectionStringsSectionHandler2, Microsoft.Configuration.ConfigurationBuilders.Base"" />
</handlers>
</Microsoft.Configuration.ConfigurationBuilders.SectionHandlers>
<configBuilders>
<builders>
###DEFINED_BUILDERS###
</builders>
</configBuilders>
<connectionStrings configBuilders=""###APPLIED_BUILDERS###"">
<add name=""connStr0"" connectionString=""pre-existing 0 connStr"" providerName=""should not be touched"" />
<add name=""connStr2"" connectionString=""pre-existing 2 connStr"" />
<add name=""connStr3"" connectionString=""pre-existing 3 connStr"" providerName=""pre-defined 3 pName"" />
<add name=""${aNameFromJson}"" connectionString=""${connStr2:connectionString}"" providerName=""${connStr3:connectionString}"" />
<add name=""aNameFromJson"" connectionString=""${token_value}"" providerName=""leave me alone"" />
<add name=""weird"" connectionString=""${aNameFromJson}"" />
</connectionStrings>
</configuration>";
private readonly string jsonTestFileName = Path.Combine(Environment.CurrentDirectory, "testConfigFiles", "simpleJsonConnStrTest.json");
private List<string> tempFilesToCleanup = new List<string>();
[Fact]
public void CSSH2_Strict()
{
string builders = $@"
<add name=""Json"" mode=""Strict"" jsonMode=""Flat"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("A value for cs2 from the root", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("A value for cs2 provider name from the root", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("A value for cs3 from the root - no provider name", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("${connStr2:connectionString}", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ConnectionString);
Assert.Equal("${connStr3:connectionString}", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ProviderName);
Assert.Equal("Contains_a_${token_value}", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("${aNameFromJson}", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
}
[Fact]
public void CSSH2_StrictSection()
{
string builders = $@"
<add name=""Json"" mode=""Strict"" jsonMode=""Sectional"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("A value for cs2 from the CS subsection - no provider name", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("Just a simple CS from the CS subsection", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("${connStr2:connectionString}", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ConnectionString);
Assert.Equal("${connStr3:connectionString}", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ProviderName);
Assert.Equal("${token_value}", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("${aNameFromJson}", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
}
[Fact]
public void CSSH2_GreedySectional()
{
string builders = $@"
<add name=""Json"" mode=""Greedy"" jsonMode=""Sectional"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6 + 5, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("A value for cs2 from the CS subsection - no provider name", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("Just a simple CS from the CS subsection", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("${connStr2:connectionString}", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ConnectionString);
Assert.Equal("${connStr3:connectionString}", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ProviderName);
Assert.Equal("${token_value}", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("${aNameFromJson}", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
Assert.Equal("Remember this is a flat key/value config source - this random is its own key/value", config.ConnectionStrings.ConnectionStrings["connStr1:randomAttr"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr1:randomAttr"].ProviderName);
Assert.Equal("A value for cs1 from the CS subsection", config.ConnectionStrings.ConnectionStrings["connStr1"].ConnectionString);
Assert.Equal("A value for cs1 provider name from the CS subsection", config.ConnectionStrings.ConnectionStrings["connStr1"].ProviderName);
Assert.Equal("This is weird", config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connectionString"].ProviderName);
Assert.Equal("Brought to you by weird", config.ConnectionStrings.ConnectionStrings["providerName"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["providerName"].ProviderName);
Assert.Equal("NOT_A_TOKEN_ANYMORE", config.ConnectionStrings.ConnectionStrings["token_value"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["token_value"].ProviderName);
}
[Fact]
public void CSSH2_Token()
{
string builders = $@"
<add name=""Json"" mode=""Token"" jsonMode=""Flat"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("pre-existing 2 connStr", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("pre-existing 3 connStr", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("A value for cs2 from the root", config.ConnectionStrings.ConnectionStrings["Contains_a_${token_value}"].ConnectionString);
Assert.Equal("A value for cs3 from the root - no provider name", config.ConnectionStrings.ConnectionStrings["Contains_a_${token_value}"].ProviderName);
Assert.Equal("${token_value}", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("Contains_a_${token_value}", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
}
[Fact]
public void CSSH2_Token_StrictSectional()
{
string builders = $@"
<add name=""Json1"" mode=""Token"" jsonMode=""Flat"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
<add name=""Json2"" mode=""Strict"" jsonMode=""Sectional"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json1,Json2";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("A value for cs2 from the CS subsection - no provider name", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("Just a simple CS from the CS subsection", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("A value for cs2 from the root", config.ConnectionStrings.ConnectionStrings["Contains_a_${token_value}"].ConnectionString);
Assert.Equal("A value for cs3 from the root - no provider name", config.ConnectionStrings.ConnectionStrings["Contains_a_${token_value}"].ProviderName);
Assert.Equal("${token_value}", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("Contains_a_${token_value}", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
}
[Fact]
public void CSSH2_Token_Greedy()
{
string builders = $@"
<add name=""Json1"" mode=""Token"" jsonMode=""Flat"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
<add name=""Json2"" mode=""Greedy"" jsonMode=""Flat"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json1,Json2";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6 + 3 + 6, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("A value for cs2 from the root", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("A value for cs2 provider name from the root", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("A value for cs3 from the root - no provider name", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("Weird double replaced value but not token", config.ConnectionStrings.ConnectionStrings["Contains_a_${token_value}"].ConnectionString);
Assert.Equal("A value for cs3 from the root - no provider name", config.ConnectionStrings.ConnectionStrings["Contains_a_${token_value}"].ProviderName);
Assert.Equal("Contains_a_${token_value}", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("Contains_a_${token_value}", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
Assert.Equal("Just a single string at the root", config.ConnectionStrings.ConnectionStrings["connStr1"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr1"].ProviderName);
Assert.Equal("A random attribute isn't actually ignored", config.ConnectionStrings.ConnectionStrings["connStr2:randomAttr"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr2:randomAttr"].ProviderName);
Assert.Equal("Yes, we can fake it like this in the root", config.ConnectionStrings.ConnectionStrings["connStr4"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr4"].ProviderName);
Assert.Equal("Remember this is a flat key/value config source - this random is its own key/value", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr1:randomAttr"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr1:randomAttr"].ProviderName);
Assert.Equal("A value for cs1 from the CS subsection", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr1"].ConnectionString);
Assert.Equal("A value for cs1 provider name from the CS subsection", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr1"].ProviderName);
Assert.Equal("A value for cs2 from the CS subsection - no provider name", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr2"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr2"].ProviderName);
Assert.Equal("Just a simple CS from the CS subsection", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr3"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connectionStrings:connStr3"].ProviderName);
Assert.Equal("This is weird", config.ConnectionStrings.ConnectionStrings["connectionStrings"].ConnectionString);
Assert.Equal("Brought to you by weird", config.ConnectionStrings.ConnectionStrings["connectionStrings"].ProviderName);
Assert.Equal("NOT_A_TOKEN_ANYMORE", config.ConnectionStrings.ConnectionStrings["connectionStrings:token_value"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connectionStrings:token_value"].ProviderName);
}
[Fact]
public void CSSH2_Token_TokenSectional()
{
string builders = $@"
<add name=""Json1"" mode=""Token"" jsonMode=""Flat"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
<add name=""Json2"" mode=""Token"" jsonMode=""Sectional"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json1,Json2";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("pre-existing 2 connStr", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("pre-existing 3 connStr", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("A value for cs2 from the root", config.ConnectionStrings.ConnectionStrings["Contains_a_NOT_A_TOKEN_ANYMORE"].ConnectionString);
Assert.Equal("A value for cs3 from the root - no provider name", config.ConnectionStrings.ConnectionStrings["Contains_a_NOT_A_TOKEN_ANYMORE"].ProviderName);
Assert.Equal("NOT_A_TOKEN_ANYMORE", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("Contains_a_NOT_A_TOKEN_ANYMORE", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
}
[Fact]
public void CSSH2_Strict_TokenSectional()
{
string builders = $@"
<add name=""Json1"" mode=""Strict"" jsonMode=""Flat"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
<add name=""Json2"" mode=""Token"" jsonMode=""Sectional"" jsonFile=""{jsonTestFileName}"" type=""Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json"" />
";
string builderList = "Json1,Json2";
string configString = cssh2ConfigTemplate.Replace("###DEFINED_BUILDERS###", builders).Replace("###APPLIED_BUILDERS###", builderList);
var config = LoadConfigFromString(configString);
Assert.Equal(6, config.ConnectionStrings.ConnectionStrings.Count);
Assert.Equal("pre-existing 0 connStr", config.ConnectionStrings.ConnectionStrings["connStr0"].ConnectionString);
Assert.Equal("should not be touched", config.ConnectionStrings.ConnectionStrings["connStr0"].ProviderName);
Assert.Equal("A value for cs2 from the root", config.ConnectionStrings.ConnectionStrings["connStr2"].ConnectionString);
Assert.Equal("A value for cs2 provider name from the root", config.ConnectionStrings.ConnectionStrings["connStr2"].ProviderName);
Assert.Equal("A value for cs3 from the root - no provider name", config.ConnectionStrings.ConnectionStrings["connStr3"].ConnectionString);
Assert.Equal("pre-defined 3 pName", config.ConnectionStrings.ConnectionStrings["connStr3"].ProviderName);
Assert.Equal("A value for cs2 from the CS subsection - no provider name", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ConnectionString);
Assert.Equal("${connStr3:connectionString}", config.ConnectionStrings.ConnectionStrings["${aNameFromJson}"].ProviderName);
Assert.Equal("Contains_a_NOT_A_TOKEN_ANYMORE", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ConnectionString);
Assert.Equal("leave me alone", config.ConnectionStrings.ConnectionStrings["aNameFromJson"].ProviderName);
Assert.Equal("${aNameFromJson}", config.ConnectionStrings.ConnectionStrings["weird"].ConnectionString);
Assert.Equal("", config.ConnectionStrings.ConnectionStrings["weird"].ProviderName);
}
private Configuration LoadConfigFromString(string configString)
{
string cfg = configString;
var config = TestHelper.LoadConfigFromString(ref cfg);
tempFilesToCleanup.Add(cfg);
return config;
}
// ======================================================================
// IDisposable Pattern
// ======================================================================
private bool disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
foreach (string filename in tempFilesToCleanup)
if (!String.IsNullOrEmpty(filename))
File.Delete(filename);
disposedValue = true;
}
}
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
~ConnectionStringsSectionHandler2Tests()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}