1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Threading . Tasks ;
5+ using JavaVersionSwitcher . Tests . Fixtures ;
6+ using Shouldly ;
7+ using Xunit ;
8+
9+ namespace JavaVersionSwitcher . Tests
10+ {
11+ public class ConfigurationServiceTests
12+ {
13+ [ Fact ]
14+ public async Task SetConfiguration_throws_on_wrong_provider ( )
15+ {
16+ // arrange
17+ using var fixture = new ConfigurationServiceFixture ( ) ;
18+ const string providerName = "non-existent-provider" ;
19+
20+ // act
21+ // ReSharper disable once AccessToDisposedClosure
22+ async Task Act ( ) => await fixture . Service . SetConfiguration ( providerName , null , null ) ;
23+
24+ // assert
25+ ( await Should . ThrowAsync < KeyNotFoundException > ( ( Func < Task > ) Act ) )
26+ . Message
27+ . ShouldSatisfyAllConditions (
28+ m => m . ShouldStartWith ( "No ConfigurationProvider" ) ,
29+ m => m . ShouldContain ( providerName ) ) ;
30+ }
31+
32+ [ Fact ]
33+ public async Task SetConfiguration_throws_on_wrong_setting ( )
34+ {
35+ // arrange
36+ const string providerName = "provider" ;
37+ using var fixture = new ConfigurationServiceFixture ( ) ;
38+ fixture . WithConfigurationProvider ( providerName ) ;
39+ const string setting = "non-existent-setting" ;
40+
41+ // act'
42+ // ReSharper disable once AccessToDisposedClosure
43+ async Task Act ( ) => await fixture . Service . SetConfiguration ( providerName , setting , null ) ;
44+
45+ // assert
46+ ( await Should . ThrowAsync < KeyNotFoundException > ( ( Func < Task > ) Act ) )
47+ . Message
48+ . ShouldSatisfyAllConditions (
49+ m => m . ShouldStartWith ( "No Configuration with the name" ) ,
50+ m => m . ShouldContain ( setting ) ) ;
51+ }
52+
53+ [ Fact ]
54+ public async Task SetConfiguration_writes_value_to_xml ( )
55+ {
56+ // arrange
57+ const string providerName = "pName" ;
58+ const string settingsName = "settingsName" ;
59+ const string value = "a value" ;
60+ using var fixture = new ConfigurationServiceFixture ( ) ;
61+ fixture . WithConfigurationProvider ( providerName , settingsName ) ;
62+
63+ // act'
64+ await fixture . Service . SetConfiguration ( providerName , settingsName , value ) ;
65+
66+ // assert
67+ var xml = fixture . ReadXml ( providerName , settingsName ) ;
68+ xml . Value . ShouldBe ( value ) ;
69+ }
70+
71+ [ Fact ]
72+ public async Task GetConfiguration_returns_empty_for_not_set_setting ( )
73+ {
74+ // arrange
75+ const string providerName = "pName" ;
76+ const string settingsName = "settingsName" ;
77+ using var fixture = new ConfigurationServiceFixture ( ) ;
78+ fixture . WithConfigurationProvider ( providerName , settingsName ) ;
79+
80+ // act'
81+ var actual = await fixture . Service . GetConfiguration ( providerName , settingsName ) ;
82+
83+ // assert
84+ actual . ShouldBe ( string . Empty ) ;
85+ }
86+
87+ [ Fact ]
88+ public async Task GetConfiguration_returns_the_value_from_xml ( )
89+ {
90+ // arrange
91+ const string providerName = "pName" ;
92+ const string settingsName = "settingsName" ;
93+ const string expected = "some value" ;
94+ using var fixture = new ConfigurationServiceFixture ( ) ;
95+ fixture . WithConfigurationProvider ( providerName , settingsName ) ;
96+ fixture . EnsureSetting ( providerName , settingsName , expected ) ;
97+
98+ // act'
99+ var actual = await fixture . Service . GetConfiguration ( providerName , settingsName ) ;
100+
101+ // assert
102+ actual . ShouldBe ( expected ) ;
103+ }
104+
105+ [ Fact ]
106+ public async Task SetConfiguration_removes_empty_settings ( )
107+ {
108+ // arrange
109+ const string providerName = "pName" ;
110+ const string settingsName = "settingsName" ;
111+ using var fixture = new ConfigurationServiceFixture ( ) ;
112+ fixture . WithConfigurationProvider ( providerName , settingsName ) ;
113+ fixture . EnsureSetting ( providerName , settingsName , "some value" ) ;
114+
115+ // act'
116+ await fixture . Service . SetConfiguration ( providerName , settingsName , null ) ;
117+
118+ // assert
119+ var xml = fixture . ReadXml ( ) ;
120+ xml . Parent . ShouldBeNull ( "this should be the root node." ) ;
121+ xml . Elements ( ) . Count ( ) . ShouldBe ( 0 ) ;
122+ }
123+ }
124+ }
0 commit comments