Skip to content

Commit

Permalink
fixed up the AppSettingsProvider so that it actually works -- with UT…
Browse files Browse the repository at this point in the history
…'s too!
  • Loading branch information
jeremydmiller committed Feb 21, 2012
1 parent a49bac7 commit b50b36b
Show file tree
Hide file tree
Showing 19 changed files with 274 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ public void log_for_nested_class_in_array()
");
}).Report;


report.WriteToConsole(true);

var elements = report.For<DeepClass1>(x => x.NestedTargets).Elements;
elements.Count.ShouldEqual(2);

Expand Down
46 changes: 46 additions & 0 deletions src/FubuCore.Testing/Configuration/AppSettingsKeyValuesTester.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AppSettings>();

}

[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<AppFile> Files { get; set; }
}

public class AppFile
{
public string Name { get; set; }
public string Location { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/FubuCore.Testing/FubuCore.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<Compile Include="Binding\ServiceArgumentsTester.cs" />
<Compile Include="Binding\SmartRequestTester.cs" />
<Compile Include="Binding\StandardModelBinderTester.cs" />
<Compile Include="Configuration\AppSettingsKeyValuesTester.cs" />
<Compile Include="Configuration\AppSettingsProviderIntegratedTester.cs" />
<Compile Include="Configuration\AppSettingsRequestDataIntegratedTester.cs" />
<Compile Include="Conversion\descriptions_on_every_converter.cs" />
<Compile Include="Conversion\TimeSpanConverterTester.cs" />
<Compile Include="Conversion\TypeDescripterConverterFamilyTester.cs" />
Expand Down
9 changes: 9 additions & 0 deletions src/FubuCore.Testing/FubuCore.Testing.dll.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
<add key="a" value="1"/>
<add key="b" value="2"/>
<add key="c" value="3"/>
<add key="AppSettings.Flag1" value="f1"/>
<add key="AppSettings1.Flag1" value="different"/>
<add key="AppSettings.Flag2" value="f2"/>
<add key="AppSettings.Nested.Flag3" value="f3"/>
<add key="AppSettings.Nested.Files[0].Name" value="control"/>
<add key="AppSettings.Nested.Files[0].Location" value="control.txt"/>

<add key="AppSettings.Nested.Files[1].Name" value="home"/>
<add key="AppSettings.Nested.Files[1].Location" value="home.txt"/>

</appSettings>
</configuration>
1 change: 0 additions & 1 deletion src/FubuCore/Binding/IRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public interface IRequestData
object Value(string key);
bool Value(string key, Action<BindingValue> callback);
bool HasAnyValuePrefixedWith(string key);
IEnumerable<string> GetKeys();

IRequestData GetSubRequest(string prefixOrChild);
IEnumerable<IRequestData> GetEnumerableRequests(string prefixOrChild);
Expand Down
5 changes: 0 additions & 5 deletions src/FubuCore/Binding/PrefixedRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public bool HasAnyValuePrefixedWith(string key)
return _inner.HasAnyValuePrefixedWith(_prefix + key);
}

public IEnumerable<string> GetKeys()
{
return _inner.GetKeys();
}

public IRequestData GetSubRequest(string prefixOrChild)
{
return new PrefixedRequestData(_inner, _prefix + prefixOrChild);
Expand Down
35 changes: 35 additions & 0 deletions src/FubuCore/Configuration/AppSettingsKeyValues.cs
Original file line number Diff line number Diff line change
@@ -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<string> GetKeys()
{
if (!ConfigurationManager.AppSettings.HasKeys())
{
return Enumerable.Empty<string>();
}

return ConfigurationManager.AppSettings.AllKeys;
}
}
}
15 changes: 4 additions & 11 deletions src/FubuCore/Configuration/AppSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>() where T : class, new()
Expand All @@ -24,18 +22,13 @@ public T SettingsFor<T>() 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;
}


Expand Down
22 changes: 10 additions & 12 deletions src/FubuCore/Configuration/AppSettingsRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(Expression<Func<T, object>> property)
Expand All @@ -26,14 +33,5 @@ public static string GetValueFor<T>(Expression<Func<T, object>> property)
return (ConfigurationManager.AppSettings.AllKeys.Contains(key)) ? ConfigurationManager.AppSettings[key] : string.Empty;
}

public override IEnumerable<string> GetKeys()
{
return ConfigurationManager.AppSettings.AllKeys;
}

protected override string source
{
get { return "AppSettings"; }
}
}
}
1 change: 1 addition & 0 deletions src/FubuCore/Configuration/SettingsRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using FubuCore.Binding;
using FubuCore.Util;

namespace FubuCore.Configuration
{
Expand Down
6 changes: 1 addition & 5 deletions src/FubuCore/Configuration/SubstitutedRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using FubuCore.Binding;
using System.Linq;
using FubuCore.Util;

namespace FubuCore.Configuration
{
Expand Down Expand Up @@ -42,11 +43,6 @@ public bool HasAnyValuePrefixedWith(string key)
return _inner.HasAnyValuePrefixedWith(key);
}

public IEnumerable<string> GetKeys()
{
return _inner.GetKeys();
}

public IRequestData GetSubRequest(string prefixOrChild)
{
var prefixedInner = _inner.GetSubRequest(prefixOrChild);
Expand Down

0 comments on commit b50b36b

Please sign in to comment.