Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Facilities / Startable / StartableFacilityTestCase.cs
blob665d3792ed473fe1181390500dd7f05793fd32fe
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Facilities.Startable.Tests
17 using System.Collections;
18 using Castle.Core;
19 using Castle.Core.Configuration;
20 using Castle.Facilities.Startable.Tests.Components;
21 using Castle.MicroKernel;
22 using Castle.MicroKernel.Registration;
23 using NUnit.Framework;
25 [TestFixture]
26 public class StartableFacilityTestCase
28 private bool startableCreatedBeforeResolved;
30 [SetUp]
31 public void SetUp()
33 startableCreatedBeforeResolved = false;
36 [Test]
37 public void TestInterfaceBasedStartable()
39 IKernel kernel = new DefaultKernel();
40 kernel.ComponentCreated += new ComponentInstanceDelegate(OnStartableComponentStarted);
42 kernel.AddFacility("startable", new StartableFacility());
44 kernel.AddComponent("a", typeof(StartableComponent));
46 Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");
48 StartableComponent component = kernel["a"] as StartableComponent;
50 Assert.IsNotNull(component);
51 Assert.IsTrue(component.Started);
52 Assert.IsFalse(component.Stopped);
54 kernel.ReleaseComponent(component);
55 Assert.IsTrue(component.Stopped);
58 [Test]
59 public void TestComponentWithNoInterface()
61 IKernel kernel = new DefaultKernel();
62 kernel.ComponentCreated += new ComponentInstanceDelegate(OnNoInterfaceStartableComponentStarted);
64 MutableConfiguration compNode = new MutableConfiguration("component");
65 compNode.Attributes["id"] = "b";
66 compNode.Attributes["startable"] = "true";
67 compNode.Attributes["startMethod"] = "Start";
68 compNode.Attributes["stopMethod"] = "Stop";
70 kernel.ConfigurationStore.AddComponentConfiguration("b", compNode);
72 kernel.AddFacility("startable", new StartableFacility());
73 kernel.AddComponent("b", typeof(NoInterfaceStartableComponent));
75 Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");
77 NoInterfaceStartableComponent component = kernel["b"] as NoInterfaceStartableComponent;
79 Assert.IsNotNull(component);
80 Assert.IsTrue(component.Started);
81 Assert.IsFalse(component.Stopped);
83 kernel.ReleaseComponent(component);
84 Assert.IsTrue(component.Stopped);
87 [Test]
88 public void TestStartableWithRegisteredCustomDependencies()
90 IKernel kernel = new DefaultKernel();
91 kernel.ComponentCreated += new ComponentInstanceDelegate(OnStartableComponentStarted);
93 kernel.AddFacility("startable", new StartableFacility());
95 Hashtable dependencies = new Hashtable();
96 dependencies.Add("config", 1);
97 kernel.AddComponent("a", typeof(StartableComponentCustomDependencies));
98 kernel.RegisterCustomDependencies(typeof(StartableComponentCustomDependencies), dependencies);
100 Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");
102 StartableComponentCustomDependencies component = kernel["a"] as StartableComponentCustomDependencies;
104 Assert.IsNotNull(component);
105 Assert.IsTrue(component.Started);
106 Assert.IsFalse(component.Stopped);
108 kernel.ReleaseComponent(component);
109 Assert.IsTrue(component.Stopped);
112 [Test]
113 public void TestStartableCustomDependencies()
115 IKernel kernel = new DefaultKernel();
116 kernel.ComponentCreated += new ComponentInstanceDelegate(OnStartableComponentStarted);
118 kernel.AddFacility("startable", new StartableFacility());
120 kernel.Register(
121 Component.For<StartableComponentCustomDependencies>()
122 .Named("a")
123 .CustomDependencies(Property.ForKey("config").Eq(1))
125 Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");
127 StartableComponentCustomDependencies component = kernel["a"] as StartableComponentCustomDependencies;
129 Assert.IsNotNull(component);
130 Assert.IsTrue(component.Started);
131 Assert.IsFalse(component.Stopped);
133 kernel.ReleaseComponent(component);
134 Assert.IsTrue(component.Stopped);
137 private void OnStartableComponentStarted(ComponentModel mode, object instance)
139 StartableComponent startable = instance as StartableComponent;
141 Assert.IsNotNull(startable);
142 Assert.IsTrue(startable.Started);
143 Assert.IsFalse(startable.Stopped);
145 startableCreatedBeforeResolved = true;
148 private void OnNoInterfaceStartableComponentStarted(ComponentModel mode, object instance)
150 NoInterfaceStartableComponent startable = instance as NoInterfaceStartableComponent;
152 Assert.IsNotNull(startable);
153 Assert.IsTrue(startable.Started);
154 Assert.IsFalse(startable.Stopped);
156 startableCreatedBeforeResolved = true;