Skip to content

Commit f2b7819

Browse files
#12 JsonConfig<TConfig>.Global and JsonConfig<TConfig>.Current can be set with incorrect values; Update CustomSettings_GlobalThenCurrent test
1 parent ee6e42b commit f2b7819

File tree

8 files changed

+101
-30
lines changed

8 files changed

+101
-30
lines changed

src/Atata.Configuration.Json.Tests/Atata.Configuration.Json.Tests.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
<AppDesignerFolder>Properties</AppDesignerFolder>
1212
<RootNamespace>Atata.Configuration.Json.Tests</RootNamespace>
1313
<AssemblyName>Atata.Configuration.Json.Tests</AssemblyName>
14-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1515
<FileAlignment>512</FileAlignment>
1616
<NuGetPackageImportStamp>
1717
</NuGetPackageImportStamp>
18+
<TargetFrameworkProfile />
1819
</PropertyGroup>
1920
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2021
<DebugSymbols>true</DebugSymbols>
@@ -25,6 +26,7 @@
2526
<ErrorReport>prompt</ErrorReport>
2627
<WarningLevel>4</WarningLevel>
2728
<CodeAnalysisRuleSet>..\Atata.ruleset</CodeAnalysisRuleSet>
29+
<Prefer32Bit>false</Prefer32Bit>
2830
</PropertyGroup>
2931
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3032
<DebugType>pdbonly</DebugType>
@@ -33,6 +35,7 @@
3335
<DefineConstants>TRACE</DefineConstants>
3436
<ErrorReport>prompt</ErrorReport>
3537
<WarningLevel>4</WarningLevel>
38+
<Prefer32Bit>false</Prefer32Bit>
3639
</PropertyGroup>
3740
<ItemGroup>
3841
<Reference Include="Atata, Version=0.17.0.0, Culture=neutral, processorArchitecture=MSIL">
@@ -115,6 +118,9 @@
115118
<None Include="Atata.json">
116119
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
117120
</None>
121+
<None Include="Configs\CustomSettingsOverride2.json">
122+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
123+
</None>
118124
<None Include="Configs\CustomSettingsOverride.json">
119125
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
120126
</None>

src/Atata.Configuration.Json.Tests/Configs/CustomSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"driver": {
33
"type": "Chrome",
44
"options": {
5-
"arguments": [ "disable-extensions", "no-sandbox", "start-maximized" ]
5+
"arguments": [ "disable-extensions", "start-maximized" ]
66
}
77
},
88
"baseUrl": "https://atata-framework.github.io/atata-sample-app/#!/",

src/Atata.Configuration.Json.Tests/Configs/CustomSettingsOverride.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"driver": {
33
"type": "Chrome",
44
"options": {
5-
"arguments": [ "disable-extensions", "no-sandbox" ]
5+
"arguments": [ "disable-extensions" ]
66
}
77
},
88
"baseUrl": "https://atata-framework.github.io/atata-sample-app/#!/override",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"driver": {
3+
"type": "Chrome",
4+
"options": {
5+
"arguments": [ "disable-extensions" ]
6+
}
7+
},
8+
"baseUrl": "https://atata-framework.github.io/atata-sample-app/#!/override2",
9+
"stringProperty": "str3",
10+
"stringArrayValues": [ "str5" ],
11+
"stringListValues": [ "str5" ],
12+
"items": [
13+
{
14+
"name": "item4",
15+
"value": 12
16+
}
17+
]
18+
}

src/Atata.Configuration.Json.Tests/CustomSettingsTests.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FluentAssertions;
1+
using System.Threading.Tasks;
2+
using FluentAssertions;
23
using NUnit.Framework;
34

45
namespace Atata.Configuration.Json.Tests
@@ -76,7 +77,7 @@ public void CustomSettings_Merged()
7677
});
7778

7879
CustomJsonConfig.Current.Drivers.Should().HaveCount(1);
79-
CustomJsonConfig.Current.Drivers[0].Options.Arguments.Should().Equal("disable-extensions", "no-sandbox");
80+
CustomJsonConfig.Current.Drivers[0].Options.Arguments.Should().Equal("disable-extensions");
8081
}
8182

8283
[Test]
@@ -98,14 +99,42 @@ public void CustomSettings_GlobalThenCurrent()
9899
CustomJsonConfig.Global.StringListValues.Should().Equal(new[] { "str1", "str2", "str3" });
99100
CustomJsonConfig.Current.StringListValues.Should().Equal(new[] { "str1", "str2", "str3", "str4" });
100101

101-
AtataContext.Current.CleanUp();
102+
AtataContext parallelAtataContext = null;
103+
CustomJsonConfig parallelCustomJsonConfig = null;
102104

103-
CustomJsonConfig.Global.BaseUrl.Should().Be("https://atata-framework.github.io/atata-sample-app/#!/");
104-
CustomJsonConfig.Current.BaseUrl.Should().Be("https://atata-framework.github.io/atata-sample-app/#!/");
105-
CustomJsonConfig.Global.StringProperty.Should().Be("str");
106-
CustomJsonConfig.Current.StringProperty.Should().Be("str");
107-
CustomJsonConfig.Global.StringListValues.Should().Equal(new[] { "str1", "str2", "str3" });
108-
CustomJsonConfig.Current.StringListValues.Should().Equal(new[] { "str1", "str2", "str3" });
105+
Task.Run(() =>
106+
{
107+
parallelAtataContext = AtataContext.Configure().
108+
ApplyJsonConfig<CustomJsonConfig>(@"Configs/CustomSettingsOverride2.json").
109+
Build();
110+
111+
parallelCustomJsonConfig = CustomJsonConfig.Current;
112+
}).Wait();
113+
114+
try
115+
{
116+
CustomJsonConfig.Global.BaseUrl.Should().Be("https://atata-framework.github.io/atata-sample-app/#!/");
117+
118+
CustomJsonConfig.Current.BaseUrl.Should().Be("https://atata-framework.github.io/atata-sample-app/#!/override");
119+
CustomJsonConfig.Current.StringProperty.Should().Be("str2");
120+
CustomJsonConfig.Current.StringListValues.Should().Equal(new[] { "str1", "str2", "str3", "str4" });
121+
122+
parallelCustomJsonConfig.BaseUrl.Should().Be("https://atata-framework.github.io/atata-sample-app/#!/override2");
123+
parallelCustomJsonConfig.StringProperty.Should().Be("str3");
124+
parallelCustomJsonConfig.StringListValues.Should().Equal(new[] { "str1", "str2", "str3", "str5" });
125+
126+
AtataContext.Current.CleanUp();
127+
128+
CustomJsonConfig.Current.Should().BeNull();
129+
130+
CustomJsonConfig.Global.BaseUrl.Should().Be("https://atata-framework.github.io/atata-sample-app/#!/");
131+
CustomJsonConfig.Global.StringProperty.Should().Be("str");
132+
CustomJsonConfig.Global.StringListValues.Should().Equal(new[] { "str1", "str2", "str3" });
133+
}
134+
finally
135+
{
136+
parallelAtataContext.CleanUp();
137+
}
109138
}
110139
}
111140
}

src/Atata.Configuration.Json.Tests/TestFixture.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class TestFixture
77
[TearDown]
88
public virtual void TearDown()
99
{
10+
AtataContext.Current?.CleanUp();
11+
1012
JsonConfig.Current = null;
1113
JsonConfig.Global = null;
1214
CustomJsonConfig.Current = null;

src/Atata.Configuration.Json/Extensions/JsonAtataContextBuilderExtensions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,22 @@ public static AtataContextBuilder ApplyJsonConfig<TConfig>(this AtataContextBuil
3737

3838
AtataContextBuilder resultBuilder = JsonConfigMapper.Map(config, builder);
3939

40-
JsonConfigManager<TConfig>.UpdateCurrentValue(jsonContent, config);
41-
4240
if (builder == AtataContext.GlobalConfiguration)
4341
{
44-
JsonConfigManager<TConfig>.UpdateGlobalValue(jsonContent);
42+
JsonConfigManager<TConfig>.UpdateGlobalValue(jsonContent, config);
43+
44+
if (!resultBuilder.BuildingContext.OnBuildingActions.Contains(JsonConfigManager<TConfig>.InitCurrentValue))
45+
resultBuilder.BuildingContext.OnBuildingActions.Add(JsonConfigManager<TConfig>.InitCurrentValue);
4546
}
46-
else if (!resultBuilder.BuildingContext.CleanUpActions.Contains(JsonConfigManager<TConfig>.ResetCurrentValue))
47+
else
4748
{
48-
resultBuilder.BuildingContext.CleanUpActions.Add(JsonConfigManager<TConfig>.ResetCurrentValue);
49+
JsonConfigManager<TConfig>.InitCurrentValue();
50+
JsonConfigManager<TConfig>.UpdateCurrentValue(jsonContent, config);
4951
}
5052

53+
if (!resultBuilder.BuildingContext.CleanUpActions.Contains(JsonConfigManager<TConfig>.ResetCurrentValue))
54+
resultBuilder.BuildingContext.CleanUpActions.Add(JsonConfigManager<TConfig>.ResetCurrentValue);
55+
5156
return resultBuilder;
5257
}
5358

src/Atata.Configuration.Json/JsonConfigManager`1.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,46 @@ namespace Atata.Configuration.Json
77
internal static class JsonConfigManager<TConfig>
88
where TConfig : JsonConfig<TConfig>
99
{
10-
internal static void UpdateGlobalValue(string jsonContent)
10+
internal static void UpdateGlobalValue(string jsonContent, TConfig config)
1111
{
12-
PropertyInfo configProperty = GetConfigProperty(nameof(JsonConfig.Global));
12+
PropertyInfo globalConfigProperty = GetConfigProperty(nameof(JsonConfig.Global));
1313

14-
if (configProperty.GetValue(null, null) is TConfig currentConfig)
14+
if (globalConfigProperty.GetValue(null, null) is TConfig currentConfig)
1515
JsonConvert.PopulateObject(jsonContent, currentConfig);
1616
else
17-
currentConfig = JsonConvert.DeserializeObject<TConfig>(jsonContent);
18-
19-
configProperty.SetValue(null, currentConfig, null);
17+
globalConfigProperty.SetValue(null, config, null);
2018
}
2119

2220
internal static void UpdateCurrentValue(string jsonContent, TConfig config)
2321
{
24-
PropertyInfo configProperty = GetConfigProperty(nameof(JsonConfig.Current));
22+
PropertyInfo currentConfigProperty = GetConfigProperty(nameof(JsonConfig.Current));
2523

26-
if (configProperty.GetValue(null, null) is TConfig currentConfig)
24+
if (currentConfigProperty.GetValue(null, null) is TConfig currentConfig)
2725
JsonConvert.PopulateObject(jsonContent, currentConfig);
2826
else
29-
currentConfig = config;
27+
currentConfigProperty.SetValue(null, config, null);
28+
}
3029

31-
configProperty.SetValue(null, currentConfig, null);
30+
internal static void InitCurrentValue()
31+
{
32+
PropertyInfo currentConfigProperty = GetConfigProperty(nameof(JsonConfig.Current));
33+
34+
if (currentConfigProperty.GetValue(null, null) == null)
35+
{
36+
object globalValue = GetConfigProperty(nameof(JsonConfig.Global)).GetValue(null, null);
37+
38+
if (globalValue != null)
39+
{
40+
string serializedGlobalValue = JsonConvert.SerializeObject(globalValue);
41+
object clonedGlobalValue = JsonConvert.DeserializeObject(serializedGlobalValue, globalValue.GetType());
42+
currentConfigProperty.SetValue(null, clonedGlobalValue, null);
43+
}
44+
}
3245
}
3346

3447
internal static void ResetCurrentValue()
3548
{
36-
object globalValue = GetConfigProperty(nameof(JsonConfig.Global)).GetValue(null, null);
37-
38-
GetConfigProperty(nameof(JsonConfig.Current)).SetValue(null, globalValue, null);
49+
GetConfigProperty(nameof(JsonConfig.Current)).SetValue(null, null, null);
3950
}
4051

4152
private static PropertyInfo GetConfigProperty(string name)

0 commit comments

Comments
 (0)