From b50b36b21ed7d139b2ef762848509716b640dafa Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 21 Feb 2012 15:20:08 -0600 Subject: [PATCH] fixed up the AppSettingsProvider so that it actually works -- with UT's too! --- ...cordingBindingLogger_acceptance_testing.cs | 3 + .../AppSettingsKeyValuesTester.cs | 46 ++++++++++++ .../AppSettingsProviderIntegratedTester.cs | 72 +++++++++++++++++++ .../AppSettingsRequestDataIntegratedTester.cs | 43 +++++++++++ .../OverrideSubstitutedRequestDataTester.cs | 3 +- .../SubstitutedRequestDataTester.cs | 1 + src/FubuCore.Testing/FubuCore.Testing.csproj | 3 + .../FubuCore.Testing.dll.config | 9 +++ src/FubuCore/Binding/IRequestData.cs | 1 - src/FubuCore/Binding/PrefixedRequestData.cs | 5 -- .../Configuration/AppSettingsKeyValues.cs | 35 +++++++++ .../Configuration/AppSettingsProvider.cs | 15 ++-- .../Configuration/AppSettingsRequestData.cs | 22 +++--- .../Configuration/SettingsRequestData.cs | 1 + .../Configuration/SubstitutedRequestData.cs | 6 +- src/FubuCore/FubuCore.csproj | 3 + src/FubuCore/TemplateParser.cs | 27 +------ src/FubuCore/Util/DictionaryKeyValues.cs | 29 ++++++++ src/FubuCore/Util/IKeyValues.cs | 11 +++ 19 files changed, 274 insertions(+), 61 deletions(-) create mode 100644 src/FubuCore.Testing/Configuration/AppSettingsKeyValuesTester.cs create mode 100644 src/FubuCore.Testing/Configuration/AppSettingsProviderIntegratedTester.cs create mode 100644 src/FubuCore.Testing/Configuration/AppSettingsRequestDataIntegratedTester.cs create mode 100644 src/FubuCore/Configuration/AppSettingsKeyValues.cs create mode 100644 src/FubuCore/Util/DictionaryKeyValues.cs create mode 100644 src/FubuCore/Util/IKeyValues.cs diff --git a/src/FubuCore.Testing/Binding/RecordingBindingLogger_acceptance_testing.cs b/src/FubuCore.Testing/Binding/RecordingBindingLogger_acceptance_testing.cs index 7554edf..f7c2d43 100644 --- a/src/FubuCore.Testing/Binding/RecordingBindingLogger_acceptance_testing.cs +++ b/src/FubuCore.Testing/Binding/RecordingBindingLogger_acceptance_testing.cs @@ -172,6 +172,9 @@ public void log_for_nested_class_in_array() "); }).Report; + + report.WriteToConsole(true); + var elements = report.For(x => x.NestedTargets).Elements; elements.Count.ShouldEqual(2); diff --git a/src/FubuCore.Testing/Configuration/AppSettingsKeyValuesTester.cs b/src/FubuCore.Testing/Configuration/AppSettingsKeyValuesTester.cs new file mode 100644 index 0000000..11afa56 --- /dev/null +++ b/src/FubuCore.Testing/Configuration/AppSettingsKeyValuesTester.cs @@ -0,0 +1,46 @@ +using FubuCore.Configuration; +using NUnit.Framework; +using FubuTestingSupport; + +namespace FubuCore.Testing.Configuration +{ + [TestFixture] + public class AppSettingsKeyValuesTester + { + private AppSettingsKeyValues theValues; + + [SetUp] + public void SetUp() + { + theValues = new AppSettingsKeyValues(); + } + + [Test] + public void contains_key() + { + theValues.ContainsKey("AppSettings.Nested.Flag3").ShouldBeTrue(); + theValues.ContainsKey("a").ShouldBeTrue(); + theValues.ContainsKey("AppSettings.Nested.Files[1].Location").ShouldBeTrue(); + + + theValues.ContainsKey("not a real value").ShouldBeFalse(); + } + + [Test] + public void get() + { + theValues.Get("a").ShouldEqual("1"); + theValues.Get("AppSettings.Flag1").ShouldEqual("f1"); + } + + [Test] + public void get_all_keys() + { + var keys = theValues.GetKeys(); + keys.ShouldContain("a"); + keys.ShouldContain("b"); + keys.ShouldContain("c"); + keys.ShouldContain("AppSettings.Flag1"); + } + } +} \ No newline at end of file diff --git a/src/FubuCore.Testing/Configuration/AppSettingsProviderIntegratedTester.cs b/src/FubuCore.Testing/Configuration/AppSettingsProviderIntegratedTester.cs new file mode 100644 index 0000000..5c4826c --- /dev/null +++ b/src/FubuCore.Testing/Configuration/AppSettingsProviderIntegratedTester.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using FubuCore.Binding; +using FubuCore.Configuration; +using NUnit.Framework; +using FubuTestingSupport; +using System.Linq; + +namespace FubuCore.Testing.Configuration +{ + + /****************************************** + * + * + * See the FubuCore.Testing.dll.config file for the data + * + * + ******************************************/ + + [TestFixture] + public class AppSettingsProviderIntegratedTester + { + private AppSettings theSettings; + + [SetUp] + public void SetUp() + { + theSettings = new AppSettingsProvider(ObjectResolver.Basic()) + .SettingsFor(); + + } + + [Test] + public void can_get_basic_properties() + { + theSettings.Flag1.ShouldEqual("f1"); + theSettings.Flag2.ShouldEqual("f2"); + } + + [Test] + public void can_get_a_nested_object() + { + theSettings.Nested.ShouldNotBeNull(); + theSettings.Nested.Flag3.ShouldEqual("f3"); + } + + [Test] + public void can_build_enumeration_properties() + { + theSettings.Nested.Files.Select(x => x.Name) + .ShouldHaveTheSameElementsAs("control", "home"); + } + } + + public class AppSettings + { + public string Flag1 { get; set; } + public string Flag2 { get; set; } + public NestedSetting Nested { get; set; } + } + + public class NestedSetting + { + public string Flag3 { get; set; } + public IEnumerable Files { get; set; } + } + + public class AppFile + { + public string Name { get; set; } + public string Location { get; set; } + } +} \ No newline at end of file diff --git a/src/FubuCore.Testing/Configuration/AppSettingsRequestDataIntegratedTester.cs b/src/FubuCore.Testing/Configuration/AppSettingsRequestDataIntegratedTester.cs new file mode 100644 index 0000000..3aaf84f --- /dev/null +++ b/src/FubuCore.Testing/Configuration/AppSettingsRequestDataIntegratedTester.cs @@ -0,0 +1,43 @@ +using FubuCore.Configuration; +using NUnit.Framework; +using FubuTestingSupport; + +namespace FubuCore.Testing.Configuration +{ + [TestFixture] + public class AppSettingsRequestDataIntegratedTester + { + private AppSettingsRequestData theData; + + [SetUp] + public void SetUp() + { + theData = new AppSettingsRequestData(typeof(AppSettings)); + } + + [Test] + public void value() + { + theData.Value("Flag1").ShouldEqual("f1"); + theData.Value("Flag2").ShouldEqual("f2"); + } + + [Test] + public void value_of_complicated_values() + { + theData.Value("NestedFlag3").ShouldEqual("f3"); + } + + [Test] + public void get_subrequest() + { + theData.GetSubRequest("Nested").Value("Flag3").ShouldEqual("f3"); + } + + [Test] + public void get_enumerable_requests() + { + theData.GetEnumerableRequests("NestedFiles").ShouldHaveCount(2); + } + } +} \ No newline at end of file diff --git a/src/FubuCore.Testing/Configuration/OverrideSubstitutedRequestDataTester.cs b/src/FubuCore.Testing/Configuration/OverrideSubstitutedRequestDataTester.cs index 8e88205..d961fa2 100644 --- a/src/FubuCore.Testing/Configuration/OverrideSubstitutedRequestDataTester.cs +++ b/src/FubuCore.Testing/Configuration/OverrideSubstitutedRequestDataTester.cs @@ -1,6 +1,7 @@ -using System.Collections.Generic; +using System.Collections.Generic; using FubuCore.Binding; using FubuCore.Configuration; +using FubuCore.Util; using FubuTestingSupport; using NUnit.Framework; diff --git a/src/FubuCore.Testing/Configuration/SubstitutedRequestDataTester.cs b/src/FubuCore.Testing/Configuration/SubstitutedRequestDataTester.cs index f988c3c..a41bb29 100644 --- a/src/FubuCore.Testing/Configuration/SubstitutedRequestDataTester.cs +++ b/src/FubuCore.Testing/Configuration/SubstitutedRequestDataTester.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using FubuCore.Binding; using FubuCore.Configuration; +using FubuCore.Util; using NUnit.Framework; using FubuTestingSupport; using Rhino.Mocks; diff --git a/src/FubuCore.Testing/FubuCore.Testing.csproj b/src/FubuCore.Testing/FubuCore.Testing.csproj index aa76254..1e24868 100644 --- a/src/FubuCore.Testing/FubuCore.Testing.csproj +++ b/src/FubuCore.Testing/FubuCore.Testing.csproj @@ -96,6 +96,9 @@ + + + diff --git a/src/FubuCore.Testing/FubuCore.Testing.dll.config b/src/FubuCore.Testing/FubuCore.Testing.dll.config index 2010ad3..b273576 100644 --- a/src/FubuCore.Testing/FubuCore.Testing.dll.config +++ b/src/FubuCore.Testing/FubuCore.Testing.dll.config @@ -4,6 +4,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/FubuCore/Binding/IRequestData.cs b/src/FubuCore/Binding/IRequestData.cs index bf0a655..aa7e7cf 100644 --- a/src/FubuCore/Binding/IRequestData.cs +++ b/src/FubuCore/Binding/IRequestData.cs @@ -8,7 +8,6 @@ public interface IRequestData object Value(string key); bool Value(string key, Action callback); bool HasAnyValuePrefixedWith(string key); - IEnumerable GetKeys(); IRequestData GetSubRequest(string prefixOrChild); IEnumerable GetEnumerableRequests(string prefixOrChild); diff --git a/src/FubuCore/Binding/PrefixedRequestData.cs b/src/FubuCore/Binding/PrefixedRequestData.cs index 4338575..d2bfeb4 100644 --- a/src/FubuCore/Binding/PrefixedRequestData.cs +++ b/src/FubuCore/Binding/PrefixedRequestData.cs @@ -29,11 +29,6 @@ public bool HasAnyValuePrefixedWith(string key) return _inner.HasAnyValuePrefixedWith(_prefix + key); } - public IEnumerable GetKeys() - { - return _inner.GetKeys(); - } - public IRequestData GetSubRequest(string prefixOrChild) { return new PrefixedRequestData(_inner, _prefix + prefixOrChild); diff --git a/src/FubuCore/Configuration/AppSettingsKeyValues.cs b/src/FubuCore/Configuration/AppSettingsKeyValues.cs new file mode 100644 index 0000000..14cebb9 --- /dev/null +++ b/src/FubuCore/Configuration/AppSettingsKeyValues.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using FubuCore.Util; + +namespace FubuCore.Configuration +{ + public class AppSettingsKeyValues : IKeyValues + { + public bool ContainsKey(string key) + { + if (!ConfigurationManager.AppSettings.HasKeys()) + { + return false; + } + + return ConfigurationManager.AppSettings.AllKeys.Contains(key); + } + + public string Get(string key) + { + return ConfigurationManager.AppSettings[key]; + } + + public IEnumerable GetKeys() + { + if (!ConfigurationManager.AppSettings.HasKeys()) + { + return Enumerable.Empty(); + } + + return ConfigurationManager.AppSettings.AllKeys; + } + } +} \ No newline at end of file diff --git a/src/FubuCore/Configuration/AppSettingsProvider.cs b/src/FubuCore/Configuration/AppSettingsProvider.cs index 77ecfd8..8a64e2b 100644 --- a/src/FubuCore/Configuration/AppSettingsProvider.cs +++ b/src/FubuCore/Configuration/AppSettingsProvider.cs @@ -6,13 +6,11 @@ namespace FubuCore.Configuration { public class AppSettingsProvider : ISettingsProvider { - private readonly IServiceLocator _locator; private readonly IObjectResolver _resolver; - public AppSettingsProvider(IObjectResolver resolver, IServiceLocator locator) + public AppSettingsProvider(IObjectResolver resolver) { _resolver = resolver; - _locator = locator; } public T SettingsFor() where T : class, new() @@ -24,18 +22,13 @@ public T SettingsFor() where T : class, new() return (T) value; } - // TODO -- this is gross. Get some UT's against this pronto public object SettingsFor(Type settingsType) { - throw new NotImplementedException("NWO"); - //IBindingContext context = new BindingContext(new AppSettingsRequestData(), _locator, new NulloBindingLogger()) - // .PrefixWith(settingsType.Name + "."); + var result = _resolver.BindModel(settingsType, new AppSettingsRequestData(settingsType)); - //BindResult result = _resolver.BindModel(settingsType, context); + result.AssertNoProblems(settingsType); - //result.AssertNoProblems(settingsType); - - //return result.Value; + return result.Value; } diff --git a/src/FubuCore/Configuration/AppSettingsRequestData.cs b/src/FubuCore/Configuration/AppSettingsRequestData.cs index 8524eb5..14dd902 100644 --- a/src/FubuCore/Configuration/AppSettingsRequestData.cs +++ b/src/FubuCore/Configuration/AppSettingsRequestData.cs @@ -8,11 +8,18 @@ namespace FubuCore.Configuration { - public class AppSettingsRequestData : RequestDataBase + public class AppSettingsRequestData : InMemoryRequestData { - protected override object fetch(string key) + public AppSettingsRequestData(Type settingsType) { - return ConfigurationManager.AppSettings[key]; + var values = new AppSettingsKeyValues(); + var prefix = settingsType.Name + "."; + values.GetKeys().Where(x => x.StartsWith(prefix)).Each(key => + { + var propKey = key.Split('.').Skip(1).Join(""); + + this[propKey] = values.Get(key); + }); } public static string KeyFor(Expression> property) @@ -26,14 +33,5 @@ public static string GetValueFor(Expression> property) return (ConfigurationManager.AppSettings.AllKeys.Contains(key)) ? ConfigurationManager.AppSettings[key] : string.Empty; } - public override IEnumerable GetKeys() - { - return ConfigurationManager.AppSettings.AllKeys; - } - - protected override string source - { - get { return "AppSettings"; } - } } } \ No newline at end of file diff --git a/src/FubuCore/Configuration/SettingsRequestData.cs b/src/FubuCore/Configuration/SettingsRequestData.cs index c98487e..82b6115 100644 --- a/src/FubuCore/Configuration/SettingsRequestData.cs +++ b/src/FubuCore/Configuration/SettingsRequestData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using FubuCore.Binding; +using FubuCore.Util; namespace FubuCore.Configuration { diff --git a/src/FubuCore/Configuration/SubstitutedRequestData.cs b/src/FubuCore/Configuration/SubstitutedRequestData.cs index c470173..471f870 100644 --- a/src/FubuCore/Configuration/SubstitutedRequestData.cs +++ b/src/FubuCore/Configuration/SubstitutedRequestData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using FubuCore.Binding; using System.Linq; +using FubuCore.Util; namespace FubuCore.Configuration { @@ -42,11 +43,6 @@ public bool HasAnyValuePrefixedWith(string key) return _inner.HasAnyValuePrefixedWith(key); } - public IEnumerable GetKeys() - { - return _inner.GetKeys(); - } - public IRequestData GetSubRequest(string prefixOrChild) { var prefixedInner = _inner.GetSubRequest(prefixOrChild); diff --git a/src/FubuCore/FubuCore.csproj b/src/FubuCore/FubuCore.csproj index 367480b..d0d71d7 100644 --- a/src/FubuCore/FubuCore.csproj +++ b/src/FubuCore/FubuCore.csproj @@ -113,6 +113,7 @@ + @@ -155,6 +156,7 @@ + @@ -162,6 +164,7 @@ + diff --git a/src/FubuCore/TemplateParser.cs b/src/FubuCore/TemplateParser.cs index f46a184..ced31c7 100644 --- a/src/FubuCore/TemplateParser.cs +++ b/src/FubuCore/TemplateParser.cs @@ -2,35 +2,10 @@ using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; +using FubuCore.Util; namespace FubuCore { - public interface IKeyValues - { - bool ContainsKey(string key); - string Get(string key); - } - - public class DictionaryKeyValues : IKeyValues - { - private readonly IDictionary _dictionary; - - public DictionaryKeyValues(IDictionary dictionary) - { - _dictionary = dictionary; - } - - public bool ContainsKey(string key) - { - return _dictionary.ContainsKey(key); - } - - public string Get(string key) - { - return _dictionary[key]; - } - } - public static class TemplateParser { private static readonly string TemplateGroup; diff --git a/src/FubuCore/Util/DictionaryKeyValues.cs b/src/FubuCore/Util/DictionaryKeyValues.cs new file mode 100644 index 0000000..f8a9742 --- /dev/null +++ b/src/FubuCore/Util/DictionaryKeyValues.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; + +namespace FubuCore.Util +{ + public class DictionaryKeyValues : IKeyValues + { + private readonly IDictionary _dictionary; + + public DictionaryKeyValues(IDictionary dictionary) + { + _dictionary = dictionary; + } + + public bool ContainsKey(string key) + { + return _dictionary.ContainsKey(key); + } + + public string Get(string key) + { + return _dictionary[key]; + } + + public IEnumerable GetKeys() + { + return _dictionary.Keys; + } + } +} \ No newline at end of file diff --git a/src/FubuCore/Util/IKeyValues.cs b/src/FubuCore/Util/IKeyValues.cs new file mode 100644 index 0000000..89a8b4b --- /dev/null +++ b/src/FubuCore/Util/IKeyValues.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace FubuCore.Util +{ + public interface IKeyValues + { + bool ContainsKey(string key); + string Get(string key); + IEnumerable GetKeys(); + } +} \ No newline at end of file