1
1
using System ;
2
2
using System . Reflection ;
3
+ using System . Threading ;
3
4
using Aquality . Selenium . Core . Applications ;
4
5
using Aquality . Selenium . Core . Configurations ;
5
6
using Aquality . Selenium . Core . Utilities ;
@@ -13,53 +14,95 @@ public class ApplicationManagerTests
13
14
private const string SpecialSettingsFile = "special" ;
14
15
private const string SpecialLanguageValue = "special" ;
15
16
private static readonly TimeSpan SpecialTimeoutValue = TimeSpan . FromDays ( 1 ) ;
17
+ private const string SpecialLogger = "SpecialLogger" ;
16
18
17
19
[ Test ]
18
20
public void Should_BePossibleTo_RegisterCustomServices ( )
19
21
{
20
- Assert . IsInstanceOf < CustomTimeoutConfiguration > ( ApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) ) ;
22
+ Assert . IsInstanceOf < TestTimeoutConfiguration > ( TestApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) ) ;
21
23
}
22
24
23
25
[ Test ]
24
26
public void Should_BePossibleTo_GetCustomValues ( )
25
27
{
26
- var timeoutConfiguration = ApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) as CustomTimeoutConfiguration ;
28
+ var timeoutConfiguration = TestApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) as TestTimeoutConfiguration ;
27
29
Assert . AreEqual ( SpecialTimeoutValue , timeoutConfiguration . CustomTimeout ) ;
28
30
}
29
31
32
+ [ Test ]
33
+ public void Should_BePossibleTo_GetCustomLoggerValues ( )
34
+ {
35
+ TestApplicationManager . SetStartup ( new CustomStartup ( ) ) ;
36
+ var timeoutConfiguration = TestApplicationManager . ServiceProvider . GetService < ILoggerConfiguration > ( ) as CustomLoggerConfiguration ;
37
+ Assert . AreEqual ( SpecialLogger , timeoutConfiguration . CustomLogger ) ;
38
+ }
39
+
30
40
[ Test ]
31
41
public void Should_BePossibleTo_RegisterCustomServices_WithCustomSettingsFile ( )
32
42
{
33
- Assert . AreEqual ( SpecialLanguageValue , ApplicationManager . ServiceProvider . GetService < ILoggerConfiguration > ( ) . Language ) ;
43
+ Assert . AreEqual ( SpecialLanguageValue , TestApplicationManager . ServiceProvider . GetService < ILoggerConfiguration > ( ) . Language ) ;
34
44
}
35
45
36
- private class ApplicationManager : ApplicationManager < IApplication >
46
+ private class TestApplicationManager : ApplicationManager < IApplication >
37
47
{
38
- public static IApplication Application => GetApplication ( StartApplicationFunction , ( ) => RegisterServices ( services => Application ) ) ;
48
+ private static ThreadLocal < TestStartup > startup = new ThreadLocal < TestStartup > ( ) ;
39
49
40
- public static IServiceProvider ServiceProvider => GetServiceProvider ( services => Application , ( ) => RegisterServices ( services => Application ) ) ;
50
+ private static IApplication Application => GetApplication ( StartApplicationFunction , ( ) => startup . Value . ConfigureServices ( new ServiceCollection ( ) , services => Application ) ) ;
41
51
42
- private static IServiceCollection RegisterServices ( Func < IServiceProvider , IApplication > applicationSupplier )
52
+ public static IServiceProvider ServiceProvider => GetServiceProvider ( services => Application ,
53
+ ( ) => startup . Value . ConfigureServices ( new ServiceCollection ( ) , services => Application ) ) ;
54
+
55
+ public static void SetStartup ( Startup startup )
56
+ {
57
+ if ( startup != null )
58
+ {
59
+ TestApplicationManager . startup . Value = ( TestStartup ) startup ;
60
+ }
61
+ }
62
+
63
+ private static Func < IServiceProvider , IApplication > StartApplicationFunction => ( services ) => throw new NotImplementedException ( ) ;
64
+ }
65
+
66
+ private class TestStartup : Startup
67
+ {
68
+ public override IServiceCollection ConfigureServices ( IServiceCollection services , Func < IServiceProvider , IApplication > applicationProvider , ISettingsFile settings = null )
43
69
{
44
- var services = new ServiceCollection ( ) ;
45
- var startup = new Startup ( ) ;
46
70
var settingsFile = new JsonSettingsFile ( $ "Resources.settings.{ SpecialSettingsFile } .json", Assembly . GetExecutingAssembly ( ) ) ;
47
- startup . ConfigureServices ( services , applicationSupplier , settingsFile ) ;
48
- services . AddSingleton < ITimeoutConfiguration > ( new CustomTimeoutConfiguration ( settingsFile ) ) ;
71
+ base . ConfigureServices ( services , applicationProvider , settingsFile ) ;
72
+ services . AddSingleton < ITimeoutConfiguration > ( new TestTimeoutConfiguration ( settingsFile ) ) ;
49
73
return services ;
50
74
}
75
+ }
51
76
52
- private static Func < IServiceProvider , IApplication > StartApplicationFunction => ( services ) => throw new NotImplementedException ( ) ;
77
+ private class CustomStartup : TestStartup
78
+ {
79
+ public override IServiceCollection ConfigureServices ( IServiceCollection services , Func < IServiceProvider , IApplication > applicationProvider , ISettingsFile settings = null )
80
+ {
81
+ var settingsFile = new JsonSettingsFile ( $ "Resources.settings.{ SpecialSettingsFile } .json", Assembly . GetExecutingAssembly ( ) ) ;
82
+ base . ConfigureServices ( services , applicationProvider , settingsFile ) ;
83
+ services . AddSingleton < ILoggerConfiguration > ( new CustomLoggerConfiguration ( settingsFile ) ) ;
84
+ return services ;
85
+ }
53
86
}
54
87
55
- private class CustomTimeoutConfiguration : TimeoutConfiguration
88
+ private class TestTimeoutConfiguration : TimeoutConfiguration
56
89
{
57
- public CustomTimeoutConfiguration ( ISettingsFile settingsFile ) : base ( settingsFile )
90
+ public TestTimeoutConfiguration ( ISettingsFile settingsFile ) : base ( settingsFile )
58
91
{
59
92
CustomTimeout = SpecialTimeoutValue ;
60
93
}
61
94
62
95
public TimeSpan CustomTimeout { get ; }
63
96
}
97
+
98
+ private class CustomLoggerConfiguration : LoggerConfiguration
99
+ {
100
+ public CustomLoggerConfiguration ( ISettingsFile settingsFile ) : base ( settingsFile )
101
+ {
102
+ CustomLogger = SpecialLogger ;
103
+ }
104
+
105
+ public string CustomLogger { get ; }
106
+ }
64
107
}
65
108
}
0 commit comments