/
AlternativeConfigurationTester.cs
84 lines (68 loc) · 2.71 KB
/
AlternativeConfigurationTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Xml;
using NUnit.Framework;
using StructureMap.Testing.GenericWidgets;
using StructureMap.Testing.TestData;
using StructureMap.Testing.Widget;
namespace StructureMap.Testing
{
[TestFixture]
public class AlternativeConfigurationTester
{
#region Setup/Teardown
[SetUp]
public void SetUp()
{
DataMother.BackupStructureMapConfig();
DataMother.WriteDocument("Config1.xml");
DataMother.WriteDocument("Config2.xml");
DataMother.WriteDocument("FullTesting.XML");
}
[TearDown]
public void TearDown()
{
DataMother.RestoreStructureMapConfig();
}
#endregion
public void assertTheDefault(string color, Action<ConfigurationExpression> action)
{
var container = new Container(action);
container.GetInstance<IWidget>().ShouldBeOfType<ColorWidget>().Color.ShouldEqual(color);
}
[Test]
public void AddNodeDirectly()
{
string xml = "<StructureMap><Assembly Name=\"StructureMap.Testing.GenericWidgets\"/></StructureMap>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var container = new Container(x =>
{
x.AddConfigurationFromXmlFile("StructureMap.config");
x.AddConfigurationFromNode(doc.DocumentElement);
});
container.GetInstance<IPlug<string>>().ShouldNotBeNull();
}
[Test]
public void Load_configuration_file_after_the_container_has_already_been_initialized()
{
var container = new Container(x => x.AddConfigurationFromXmlFile("Config1.xml"));
container.GetInstance<IWidget>().ShouldBeOfType<ColorWidget>().Color.ShouldEqual("Orange");
container.Configure(x => x.AddConfigurationFromXmlFile("Config2.xml"));
container.GetInstance<IWidget>().ShouldBeOfType<ColorWidget>().Color.ShouldEqual("Green");
}
[Test]
public void NotTheDefault()
{
assertTheDefault("Orange", x => { x.AddConfigurationFromXmlFile("Config1.xml"); });
}
[Test]
public void WithTheDefault()
{
// This code enforces the existence of the StructureMap.config file
// Initialize() will throw an exception if the StructureMap.config file
// cannot be found
ObjectFactory.Initialize(x => { x.UseDefaultStructureMapConfigFile = true; });
ObjectFactory.GetInstance<IWidget>().ShouldBeOfType<ColorWidget>().Color.ShouldEqual("Red");
}
}
}