11using System ;
22using System . Reflection ;
3+ using System . Threading ;
34using Aquality . Selenium . Core . Applications ;
45using Aquality . Selenium . Core . Configurations ;
56using Aquality . Selenium . Core . Utilities ;
@@ -13,53 +14,95 @@ public class ApplicationManagerTests
1314 private const string SpecialSettingsFile = "special" ;
1415 private const string SpecialLanguageValue = "special" ;
1516 private static readonly TimeSpan SpecialTimeoutValue = TimeSpan . FromDays ( 1 ) ;
17+ private const string SpecialLogger = "SpecialLogger" ;
1618
1719 [ Test ]
1820 public void Should_BePossibleTo_RegisterCustomServices ( )
1921 {
20- Assert . IsInstanceOf < CustomTimeoutConfiguration > ( ApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) ) ;
22+ Assert . IsInstanceOf < TestTimeoutConfiguration > ( TestApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) ) ;
2123 }
2224
2325 [ Test ]
2426 public void Should_BePossibleTo_GetCustomValues ( )
2527 {
26- var timeoutConfiguration = ApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) as CustomTimeoutConfiguration ;
28+ var timeoutConfiguration = TestApplicationManager . ServiceProvider . GetService < ITimeoutConfiguration > ( ) as TestTimeoutConfiguration ;
2729 Assert . AreEqual ( SpecialTimeoutValue , timeoutConfiguration . CustomTimeout ) ;
2830 }
2931
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+
3040 [ Test ]
3141 public void Should_BePossibleTo_RegisterCustomServices_WithCustomSettingsFile ( )
3242 {
33- Assert . AreEqual ( SpecialLanguageValue , ApplicationManager . ServiceProvider . GetService < ILoggerConfiguration > ( ) . Language ) ;
43+ Assert . AreEqual ( SpecialLanguageValue , TestApplicationManager . ServiceProvider . GetService < ILoggerConfiguration > ( ) . Language ) ;
3444 }
3545
36- private class ApplicationManager : ApplicationManager < IApplication >
46+ private class TestApplicationManager : ApplicationManager < IApplication >
3747 {
38- public static IApplication Application => GetApplication ( StartApplicationFunction , ( ) => RegisterServices ( services => Application ) ) ;
48+ private static ThreadLocal < TestStartup > startup = new ThreadLocal < TestStartup > ( ) ;
3949
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 ) ) ;
4151
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 )
4369 {
44- var services = new ServiceCollection ( ) ;
45- var startup = new Startup ( ) ;
4670 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 ) ) ;
4973 return services ;
5074 }
75+ }
5176
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+ }
5386 }
5487
55- private class CustomTimeoutConfiguration : TimeoutConfiguration
88+ private class TestTimeoutConfiguration : TimeoutConfiguration
5689 {
57- public CustomTimeoutConfiguration ( ISettingsFile settingsFile ) : base ( settingsFile )
90+ public TestTimeoutConfiguration ( ISettingsFile settingsFile ) : base ( settingsFile )
5891 {
5992 CustomTimeout = SpecialTimeoutValue ;
6093 }
6194
6295 public TimeSpan CustomTimeout { get ; }
6396 }
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+ }
64107 }
65108}
0 commit comments