From 3fe03c553804c0296575ff852e2be8e1ca87495a Mon Sep 17 00:00:00 2001 From: Roemer Date: Mon, 11 Jan 2016 12:28:41 +0100 Subject: [PATCH 1/3] Named configuration interfaces and namespaces correctly --- ...portingConfiguration.cs => IReportingConfiguration.cs} | 2 +- .../Configuration/ReportingAppXmlConfiguration.cs | 6 +++--- .../TestStack.White.Reporting.csproj | 5 ++--- ...sitoryConfiguration.cs => IRepositoryConfiguration.cs} | 2 +- .../Configuration/RepositoryAppXmlConfiguration.cs | 6 +++--- .../TestStack.White.ScreenObjects.csproj | 5 ++--- .../Config/WebBrowserConfiguration.cs | 7 ------- .../Configuration/IWebBrowserConfiguration.cs | 7 +++++++ .../WebBrowserAppXmlConfiguration.cs | 8 ++++---- src/TestStack.White.WebBrowser/Firefox.cs | 2 +- .../TestStack.White.WebBrowser.csproj | 7 +++---- 11 files changed, 27 insertions(+), 30 deletions(-) rename src/TestStack.White.Reporting/Configuration/{ReportingConfiguration.cs => IReportingConfiguration.cs} (70%) rename src/TestStack.White.ScreenObjects/Configuration/{RepositoryConfiguration.cs => IRepositoryConfiguration.cs} (83%) delete mode 100644 src/TestStack.White.WebBrowser/Config/WebBrowserConfiguration.cs create mode 100644 src/TestStack.White.WebBrowser/Configuration/IWebBrowserConfiguration.cs rename src/TestStack.White.WebBrowser/{Config => Configuration}/WebBrowserAppXmlConfiguration.cs (82%) diff --git a/src/TestStack.White.Reporting/Configuration/ReportingConfiguration.cs b/src/TestStack.White.Reporting/Configuration/IReportingConfiguration.cs similarity index 70% rename from src/TestStack.White.Reporting/Configuration/ReportingConfiguration.cs rename to src/TestStack.White.Reporting/Configuration/IReportingConfiguration.cs index 74a0cc36..23f7d2dd 100644 --- a/src/TestStack.White.Reporting/Configuration/ReportingConfiguration.cs +++ b/src/TestStack.White.Reporting/Configuration/IReportingConfiguration.cs @@ -1,6 +1,6 @@ namespace TestStack.White.Reporting.Configuration { - public interface ReportingConfiguration + public interface IReportingConfiguration { bool PublishTestReports { get; } } diff --git a/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs b/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs index 88847497..cb6bce9a 100644 --- a/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs +++ b/src/TestStack.White.Reporting/Configuration/ReportingAppXmlConfiguration.cs @@ -5,9 +5,9 @@ namespace TestStack.White.Reporting.Configuration { - public class ReportingAppXmlConfiguration : AssemblyConfiguration, ReportingConfiguration + public class ReportingAppXmlConfiguration : AssemblyConfiguration, IReportingConfiguration { - private static ReportingConfiguration instance; + private static IReportingConfiguration instance; private static readonly Dictionary DefaultValues = new Dictionary(); @@ -19,7 +19,7 @@ static ReportingAppXmlConfiguration() private ReportingAppXmlConfiguration() : base("White", "Reporting", DefaultValues, CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(ReportingAppXmlConfiguration))) {} - public static ReportingConfiguration Instance + public static IReportingConfiguration Instance { get { return instance ?? (instance = new ReportingAppXmlConfiguration()); } } diff --git a/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj b/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj index ede5ba94..64010a37 100644 --- a/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj +++ b/src/TestStack.White.Reporting/TestStack.White.Reporting.csproj @@ -73,7 +73,7 @@ - + @@ -113,5 +113,4 @@ --> - - + \ No newline at end of file diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs b/src/TestStack.White.ScreenObjects/Configuration/IRepositoryConfiguration.cs similarity index 83% rename from src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs rename to src/TestStack.White.ScreenObjects/Configuration/IRepositoryConfiguration.cs index 28e6f549..07acf29c 100644 --- a/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs +++ b/src/TestStack.White.ScreenObjects/Configuration/IRepositoryConfiguration.cs @@ -3,7 +3,7 @@ namespace TestStack.White.ScreenObjects.Configuration { //TODO: Power management - public interface RepositoryConfiguration + public interface IRepositoryConfiguration { bool RecordFlow { get; } DirectoryInfo ServiceCallHistoryLocation { get; } diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs b/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs index 14e66f1a..5c2e605b 100644 --- a/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs +++ b/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs @@ -6,9 +6,9 @@ namespace TestStack.White.ScreenObjects.Configuration { - public class RepositoryAppXmlConfiguration : AssemblyConfiguration, RepositoryConfiguration + public class RepositoryAppXmlConfiguration : AssemblyConfiguration, IRepositoryConfiguration { - public static RepositoryConfiguration instance; + public static IRepositoryConfiguration instance; private static readonly Dictionary DefaultValues = new Dictionary(); private const string UseHistoryKey = "UseHistory"; @@ -22,7 +22,7 @@ static RepositoryAppXmlConfiguration() DefaultValues[UseHistoryKey] = false; } - public static RepositoryConfiguration Instance + public static IRepositoryConfiguration Instance { get { return instance ?? (instance = new RepositoryAppXmlConfiguration()); } } diff --git a/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj b/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj index 62fa3b40..12f43d8a 100644 --- a/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj +++ b/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj @@ -82,7 +82,7 @@ - + @@ -161,5 +161,4 @@ --> - - + \ No newline at end of file diff --git a/src/TestStack.White.WebBrowser/Config/WebBrowserConfiguration.cs b/src/TestStack.White.WebBrowser/Config/WebBrowserConfiguration.cs deleted file mode 100644 index 0f1ebf1c..00000000 --- a/src/TestStack.White.WebBrowser/Config/WebBrowserConfiguration.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace TestStack.White.WebBrowser.Config -{ - public interface WebBrowserConfiguration - { - int FirefoxSingleWindowCheckWait { get; } - } -} \ No newline at end of file diff --git a/src/TestStack.White.WebBrowser/Configuration/IWebBrowserConfiguration.cs b/src/TestStack.White.WebBrowser/Configuration/IWebBrowserConfiguration.cs new file mode 100644 index 00000000..d05ddc00 --- /dev/null +++ b/src/TestStack.White.WebBrowser/Configuration/IWebBrowserConfiguration.cs @@ -0,0 +1,7 @@ +namespace TestStack.White.WebBrowser.Configuration +{ + public interface IWebBrowserConfiguration + { + int FirefoxSingleWindowCheckWait { get; } + } +} \ No newline at end of file diff --git a/src/TestStack.White.WebBrowser/Config/WebBrowserAppXmlConfiguration.cs b/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs similarity index 82% rename from src/TestStack.White.WebBrowser/Config/WebBrowserAppXmlConfiguration.cs rename to src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs index 590efae5..0fcea8b7 100644 --- a/src/TestStack.White.WebBrowser/Config/WebBrowserAppXmlConfiguration.cs +++ b/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs @@ -3,12 +3,12 @@ using TestStack.White.Bricks; using TestStack.White.Configuration; -namespace TestStack.White.WebBrowser.Config +namespace TestStack.White.WebBrowser.Configuration { - public class WebBrowserAppXmlConfiguration : AssemblyConfiguration, WebBrowserConfiguration + public class WebBrowserAppXmlConfiguration : AssemblyConfiguration, IWebBrowserConfiguration { private static readonly Dictionary DefaultValues = new Dictionary(); - private static WebBrowserConfiguration instance; + private static IWebBrowserConfiguration instance; static WebBrowserAppXmlConfiguration() { @@ -18,7 +18,7 @@ static WebBrowserAppXmlConfiguration() private WebBrowserAppXmlConfiguration() : base("White", "WebBrowser", DefaultValues, CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(WebBrowserAppXmlConfiguration))){} - public static WebBrowserConfiguration Instance + public static IWebBrowserConfiguration Instance { get { return instance ?? (instance = new WebBrowserAppXmlConfiguration()); } } diff --git a/src/TestStack.White.WebBrowser/Firefox.cs b/src/TestStack.White.WebBrowser/Firefox.cs index 3eed4cdf..8b199fcb 100644 --- a/src/TestStack.White.WebBrowser/Firefox.cs +++ b/src/TestStack.White.WebBrowser/Firefox.cs @@ -2,7 +2,7 @@ using System.Diagnostics; using System.Threading; using TestStack.White.UIItems.WindowItems; -using TestStack.White.WebBrowser.Config; +using TestStack.White.WebBrowser.Configuration; namespace TestStack.White.WebBrowser { diff --git a/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj b/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj index 9169945e..55879537 100644 --- a/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj +++ b/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj @@ -86,10 +86,10 @@ - + - + @@ -128,5 +128,4 @@ --> - - + \ No newline at end of file From 51855bdc040c0cccb61b169ae3123e8e5c029199 Mon Sep 17 00:00:00 2001 From: Roemer Date: Mon, 11 Jan 2016 12:40:33 +0100 Subject: [PATCH 2/3] Added Locator for WebBrowserConfiguration --- .../WebBrowserAppXmlConfiguration.cs | 11 +++------ .../WebBrowserConfigurationLocator.cs | 23 +++++++++++++++++++ src/TestStack.White.WebBrowser/Firefox.cs | 2 +- .../TestStack.White.WebBrowser.csproj | 1 + 4 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 src/TestStack.White.WebBrowser/Configuration/WebBrowserConfigurationLocator.cs diff --git a/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs b/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs index 0fcea8b7..00aa02a8 100644 --- a/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs +++ b/src/TestStack.White.WebBrowser/Configuration/WebBrowserAppXmlConfiguration.cs @@ -8,20 +8,15 @@ namespace TestStack.White.WebBrowser.Configuration public class WebBrowserAppXmlConfiguration : AssemblyConfiguration, IWebBrowserConfiguration { private static readonly Dictionary DefaultValues = new Dictionary(); - private static IWebBrowserConfiguration instance; static WebBrowserAppXmlConfiguration() { DefaultValues.Add("FirefoxSingleWindowCheckWait", 2000); } - private WebBrowserAppXmlConfiguration() : base("White", "WebBrowser", DefaultValues, - CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(WebBrowserAppXmlConfiguration))){} - - public static IWebBrowserConfiguration Instance - { - get { return instance ?? (instance = new WebBrowserAppXmlConfiguration()); } - } + public WebBrowserAppXmlConfiguration() + : base("White", "WebBrowser", DefaultValues, + CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(WebBrowserAppXmlConfiguration))) { } public virtual int FirefoxSingleWindowCheckWait { diff --git a/src/TestStack.White.WebBrowser/Configuration/WebBrowserConfigurationLocator.cs b/src/TestStack.White.WebBrowser/Configuration/WebBrowserConfigurationLocator.cs new file mode 100644 index 00000000..4e8747af --- /dev/null +++ b/src/TestStack.White.WebBrowser/Configuration/WebBrowserConfigurationLocator.cs @@ -0,0 +1,23 @@ +using System; + +namespace TestStack.White.WebBrowser.Configuration +{ + public static class WebBrowserConfigurationLocator + { + private static IWebBrowserConfiguration webBrowserConfiguration; + + public static IWebBrowserConfiguration Get() + { + if (webBrowserConfiguration == null) + { + webBrowserConfiguration = new WebBrowserAppXmlConfiguration(); + } + return webBrowserConfiguration; + } + + public static void Register(IWebBrowserConfiguration webBrowserConfiguration) + { + WebBrowserConfigurationLocator.webBrowserConfiguration = webBrowserConfiguration; + } + } +} diff --git a/src/TestStack.White.WebBrowser/Firefox.cs b/src/TestStack.White.WebBrowser/Firefox.cs index 8b199fcb..9d62765a 100644 --- a/src/TestStack.White.WebBrowser/Firefox.cs +++ b/src/TestStack.White.WebBrowser/Firefox.cs @@ -26,7 +26,7 @@ public static FirefoxWindow Launch(string url) string commandLine = string.Format("-new-window {0}", url); var processStartInfo = new ProcessStartInfo {FileName = executable, Arguments = commandLine}; Application application = Application.Launch(processStartInfo); - Thread.Sleep(WebBrowserAppXmlConfiguration.Instance.FirefoxSingleWindowCheckWait); + Thread.Sleep(WebBrowserConfigurationLocator.Get().FirefoxSingleWindowCheckWait); int numberOfFirefoxProcessesAfterLaunch = Process.GetProcessesByName(executableName).Length; bool processPerWindow = numberOfFirefoxProcessesAfterLaunch > numberBeforeLaunch; diff --git a/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj b/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj index 55879537..2ed62459 100644 --- a/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj +++ b/src/TestStack.White.WebBrowser/TestStack.White.WebBrowser.csproj @@ -83,6 +83,7 @@ + From adc8500287576f23cfdae589b050b6bf73da4658 Mon Sep 17 00:00:00 2001 From: Roemer Date: Mon, 11 Jan 2016 17:09:08 +0100 Subject: [PATCH 3/3] Alternative configuration example in RepositoryConfiguration --- .../Configuration/IRepositoryConfiguration.cs | 12 ---- .../RepositoryAppXmlConfiguration.cs | 48 ------------- .../Configuration/RepositoryConfiguration.cs | 44 ++++++++++++ .../RepositoryConfigurationLocator.cs | 24 +++++++ .../Services/ServiceExecution.cs | 2 +- .../Services/WhiteExecution.cs | 2 +- .../TestStack.White.ScreenObjects.csproj | 4 +- .../Configuration/AppConfigReader.cs | 47 ++++++++++++ .../Configuration/ConfigurationBase.cs | 71 +++++++++++++++++++ .../Configuration/ConfigurationReaderBase.cs | 23 ++++++ src/TestStack.White/TestStack.White.csproj | 3 + 11 files changed, 216 insertions(+), 64 deletions(-) delete mode 100644 src/TestStack.White.ScreenObjects/Configuration/IRepositoryConfiguration.cs delete mode 100644 src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs create mode 100644 src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs create mode 100644 src/TestStack.White.ScreenObjects/Configuration/RepositoryConfigurationLocator.cs create mode 100644 src/TestStack.White/Configuration/AppConfigReader.cs create mode 100644 src/TestStack.White/Configuration/ConfigurationBase.cs create mode 100644 src/TestStack.White/Configuration/ConfigurationReaderBase.cs diff --git a/src/TestStack.White.ScreenObjects/Configuration/IRepositoryConfiguration.cs b/src/TestStack.White.ScreenObjects/Configuration/IRepositoryConfiguration.cs deleted file mode 100644 index 07acf29c..00000000 --- a/src/TestStack.White.ScreenObjects/Configuration/IRepositoryConfiguration.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.IO; - -namespace TestStack.White.ScreenObjects.Configuration -{ - //TODO: Power management - public interface IRepositoryConfiguration - { - bool RecordFlow { get; } - DirectoryInfo ServiceCallHistoryLocation { get; } - bool UseHistory { get; } - } -} \ No newline at end of file diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs b/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs deleted file mode 100644 index 5c2e605b..00000000 --- a/src/TestStack.White.ScreenObjects/Configuration/RepositoryAppXmlConfiguration.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using TestStack.White.Bricks; -using TestStack.White.Configuration; - -namespace TestStack.White.ScreenObjects.Configuration -{ - public class RepositoryAppXmlConfiguration : AssemblyConfiguration, IRepositoryConfiguration - { - public static IRepositoryConfiguration instance; - - private static readonly Dictionary DefaultValues = new Dictionary(); - private const string UseHistoryKey = "UseHistory"; - private const string ServiceCallHistoryLocationKey = "ServiceCallHistoryLocation"; - private const string RecordFlowKey = "RecordFlow"; - - static RepositoryAppXmlConfiguration() - { - DefaultValues[RecordFlowKey] = false; - DefaultValues[ServiceCallHistoryLocationKey] = "."; - DefaultValues[UseHistoryKey] = false; - } - - public static IRepositoryConfiguration Instance - { - get { return instance ?? (instance = new RepositoryAppXmlConfiguration()); } - } - - private RepositoryAppXmlConfiguration() : base("White", "Repository", DefaultValues, - CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(RepositoryAppXmlConfiguration))) {} - - public virtual bool RecordFlow - { - get { return Convert.ToBoolean(UsedValues[RecordFlowKey]); } - } - - public virtual DirectoryInfo ServiceCallHistoryLocation - { - get { return new DirectoryInfo(UsedValues[ServiceCallHistoryLocationKey]); } - } - - public virtual bool UseHistory - { - get { return Convert.ToBoolean(UsedValues[UseHistoryKey]); } - } - } -} \ No newline at end of file diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs new file mode 100644 index 00000000..cea22cfd --- /dev/null +++ b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfiguration.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.IO; +using TestStack.White.Configuration; + +namespace TestStack.White.ScreenObjects.Configuration +{ + public class RepositoryConfiguration : ConfigurationBase + { + private const string RecordFlowKey = "RecordFlow"; + private const string ServiceCallHistoryLocationKey = "ServiceCallHistoryLocation"; + private const string UseHistoryKey = "UseHistory"; + + public bool RecordFlow + { + get { return GetValueBoolean(RecordFlowKey); } + set { SetValue(RecordFlowKey, value); } + } + + public DirectoryInfo ServiceCallHistoryLocation + { + get { return new DirectoryInfo(GetValue(ServiceCallHistoryLocationKey)); } + set { SetValue(ServiceCallHistoryLocationKey, value); } + } + + public bool UseHistory + { + get { return GetValueBoolean(UseHistoryKey); } + set { SetValue(UseHistoryKey, value); } + } + + public override Dictionary DefaultValues + { + get + { + return new Dictionary + { + {RecordFlowKey, false}, + {ServiceCallHistoryLocationKey, "."}, + {UseHistoryKey, false} + }; + } + } + } +} diff --git a/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfigurationLocator.cs b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfigurationLocator.cs new file mode 100644 index 00000000..1408abc1 --- /dev/null +++ b/src/TestStack.White.ScreenObjects/Configuration/RepositoryConfigurationLocator.cs @@ -0,0 +1,24 @@ +using TestStack.White.Configuration; + +namespace TestStack.White.ScreenObjects.Configuration +{ + public static class RepositoryConfigurationLocator + { + private static RepositoryConfiguration repositoryConfiguration; + + public static RepositoryConfiguration Get() + { + if (repositoryConfiguration == null) + { + Register(new AppConfigReader("White", "Repository")); + } + return repositoryConfiguration; + } + + public static void Register(ConfigurationReaderBase configurationReader) + { + repositoryConfiguration = new RepositoryConfiguration(); + configurationReader.FillConfigurationFromReader(repositoryConfiguration); + } + } +} diff --git a/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs b/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs index 17a59b0e..871acfbe 100644 --- a/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs +++ b/src/TestStack.White.ScreenObjects/Services/ServiceExecution.cs @@ -91,7 +91,7 @@ public virtual void RevertToSnapshot() public static ServiceExecution Create(IWorkEnvironment workEnvironment) { - if (!RepositoryAppXmlConfiguration.Instance.UseHistory) + if (!RepositoryConfigurationLocator.Get().UseHistory) return new NullServiceExecution(); ExecutionHistory executionHistory = ExecutionHistory.Create(); return new ServiceExecution(executionHistory, workEnvironment ?? new NullWorkEnvironment()); diff --git a/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs b/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs index 88f0db16..2efef6ce 100644 --- a/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs +++ b/src/TestStack.White.ScreenObjects/Services/WhiteExecution.cs @@ -37,7 +37,7 @@ public virtual T GetService(params object[] objs) where T : Service if (services.TryGetValue(typeof (T), out service)) return (T) service; service = (T) Activator.CreateInstance(typeof(T), objs); - if (RepositoryAppXmlConfiguration.Instance.UseHistory || ReportingAppXmlConfiguration.Instance.PublishTestReports) + if (RepositoryConfigurationLocator.Get().UseHistory || ReportingAppXmlConfiguration.Instance.PublishTestReports) { service = (Service) DynamicProxyGenerator.Instance.CreateProxy(typeof (T), new ServiceInterceptor(service, serviceExecution, sessionReport)); services.Add(typeof (T), service); diff --git a/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj b/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj index 12f43d8a..3e227a0d 100644 --- a/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj +++ b/src/TestStack.White.ScreenObjects/TestStack.White.ScreenObjects.csproj @@ -77,12 +77,12 @@ + + - - diff --git a/src/TestStack.White/Configuration/AppConfigReader.cs b/src/TestStack.White/Configuration/AppConfigReader.cs new file mode 100644 index 00000000..a04f670d --- /dev/null +++ b/src/TestStack.White/Configuration/AppConfigReader.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Configuration; + +namespace TestStack.White.Configuration +{ + /// + /// Configuration reader which reads from the app.config file + /// + public class AppConfigReader : ConfigurationReaderBase + { + private NameValueCollection nameValues; + + public AppConfigReader(string sectionGroup, string sectionName) + { + nameValues = (NameValueCollection)ConfigurationManager.GetSection(sectionGroup + "/" + sectionName); + if (nameValues == null) + { + nameValues = new NameValueCollection(); + } + } + + protected override void FillConfigurationFromReaderInternal(ConfigurationBase configuration) + { + // Overwrite the values contained in the section + foreach (var key in nameValues.AllKeys) + { + var value = nameValues.Get(key); + if (value != null) + { + configuration.SetValue(key, value); + } + } + // Overwrite the values from the AppSettings + var allKeys = new List(nameValues.AllKeys); + allKeys.AddRange(configuration.DefaultValues.Keys); + foreach (var key in allKeys) + { + var value = ConfigurationManager.AppSettings[key]; + if (value != null) + { + configuration.SetValue(key, value); + } + } + } + } +} diff --git a/src/TestStack.White/Configuration/ConfigurationBase.cs b/src/TestStack.White/Configuration/ConfigurationBase.cs new file mode 100644 index 00000000..124582a8 --- /dev/null +++ b/src/TestStack.White/Configuration/ConfigurationBase.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using TestStack.White.Utility; + +namespace TestStack.White.Configuration +{ + /// + /// Base class for configurations + /// + public abstract class ConfigurationBase + { + /// + /// Dictionary which holds the current values + /// + protected readonly Dictionary ConfigurationValues = new Dictionary(); + + /// + /// Temporarily override some configuration values + /// + public virtual IDisposable ApplyTemporarySetting(Action changes) + { + var existing = new Dictionary(ConfigurationValues); + changes(this); + + return new DelegateDisposable(() => + { + foreach (var value in existing) + { + SetValue(value.Key, value.Value); + } + }); + } + + /// + /// Set the value for the given key + /// + public void SetValue(string key, object value) + { + ConfigurationValues[key] = value.ToString(); + } + + /// + /// Get the value for the given key + /// + public string GetValue(string key) + { + return ConfigurationValues[key]; + } + + /// + /// Get the value converted to int32 for the given key + /// + public int GetValueInt32(string key) + { + return Convert.ToInt32(GetValue(key)); + } + + /// + /// Get the value converted to boolean for the given key + /// + public bool GetValueBoolean(string key) + { + return Convert.ToBoolean(GetValue(key)); + } + + /// + /// Dictionary with the default values + /// + public abstract Dictionary DefaultValues { get; } + } +} diff --git a/src/TestStack.White/Configuration/ConfigurationReaderBase.cs b/src/TestStack.White/Configuration/ConfigurationReaderBase.cs new file mode 100644 index 00000000..27976423 --- /dev/null +++ b/src/TestStack.White/Configuration/ConfigurationReaderBase.cs @@ -0,0 +1,23 @@ +using System; + +namespace TestStack.White.Configuration +{ + /// + /// Base class for a configuration reader + /// + public abstract class ConfigurationReaderBase + { + public void FillConfigurationFromReader(ConfigurationBase configuration) + { + // Initialize with default Values + foreach (var kvp in configuration.DefaultValues) + { + configuration.SetValue(kvp.Key, kvp.Value); + } + // Custom initialization + FillConfigurationFromReaderInternal(configuration); + } + + protected abstract void FillConfigurationFromReaderInternal(ConfigurationBase configuration); + } +} diff --git a/src/TestStack.White/TestStack.White.csproj b/src/TestStack.White/TestStack.White.csproj index 65b07908..a3cf28dc 100644 --- a/src/TestStack.White/TestStack.White.csproj +++ b/src/TestStack.White/TestStack.White.csproj @@ -91,7 +91,10 @@ + + +