Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Castle.Igloo / Castle.Igloo.Test / ScopeTest / ComponentActivatorInspectorTests.cs
blob864d9a6ea7d6b3a946c6f3e9877083444c5e468f
2 using System;
3 using Castle.Core;
4 using Castle.Core.Internal;
5 using Castle.Igloo.Attributes;
6 using Castle.Igloo.ComponentActivator;
7 using Castle.Igloo.Inspectors;
8 using Castle.Igloo.Interceptors;
9 using Castle.Igloo.LifestyleManager;
10 using Castle.Igloo.Test.ScopeTest.Components;
11 using Castle.MicroKernel;
12 using Castle.MicroKernel.ModelBuilder;
13 using Castle.Windsor;
14 using NUnit.Framework;
16 namespace Castle.Igloo.Test.ScopeTest
18 [TestFixture]
19 public class ComponentActivatorTests
21 private IContributeComponentModelConstruction _inspector = null;
22 private IKernel _kernel = null;
23 private IWindsorContainer _container = null;
25 [SetUp]
26 public void Setup()
28 _kernel = new DefaultKernel();
29 _inspector = new IglooInspector();
30 _container = new WindsorContainer();
31 _container.AddFacility("Igloo.Facility", new IglooFacility());
34 [Test]
35 public void ProcessModel_NoScopeAttribute()
37 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(SingletonScopeComponent));
38 _inspector.ProcessModel(_kernel, model);
39 Assert.IsNull(model.CustomComponentActivator);
42 [Test]
43 public void ProcessModel_ScopeAttributeWithProxy()
45 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(SimpleComponent));
46 _inspector.ProcessModel(_kernel, model);
48 Assert.IsNotNull(model.CustomComponentActivator);
49 Assert.AreEqual(typeof(ScopeComponentActivator), model.CustomComponentActivator);
51 ScopeAttribute scopeAttribute = (ScopeAttribute)model.ExtendedProperties[ScopeInspector.SCOPE_ATTRIBUTE];
53 Assert.IsTrue(scopeAttribute.UseProxy);
54 Assert.AreEqual(ScopeType.Singleton ,scopeAttribute.Scope);
57 [Test]
58 public void ProcessModel_ScopeAttributeWithoutProxy()
60 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(TransientScopeComponent));
61 _inspector.ProcessModel(_kernel, model);
63 Assert.IsNull(model.CustomComponentActivator);
65 ScopeAttribute scopeAttribute = (ScopeAttribute)model.ExtendedProperties[ScopeInspector.SCOPE_ATTRIBUTE];
67 Assert.IsFalse(scopeAttribute.UseProxy);
68 Assert.AreEqual(ScopeType.Transient, scopeAttribute.Scope);
70 Assert.AreEqual(LifestyleType.Custom, model.LifestyleType);
71 Assert.AreEqual(typeof(ScopeLifestyleManager), model.CustomLifestyle);
74 [Test]
75 public void ProcessModel_ScopeConfigWithProxy()
77 _container.AddComponent("Simple.Component", typeof(IComponent), typeof(SimpleComponent));
79 IVertex[] vertices = TopologicalSortAlgo.Sort(_container.Kernel.GraphNodes);
81 for (int i = 0; i < vertices.Length; i++)
83 ComponentModel model = (ComponentModel)vertices[i];
84 if (model.Name == "Simple.Component")
86 Assert.IsNotNull(model.CustomComponentActivator);
87 Assert.AreEqual(typeof(ScopeComponentActivator), model.CustomComponentActivator);
89 ScopeAttribute scopeAttribute = (ScopeAttribute)model.ExtendedProperties[ScopeInspector.SCOPE_ATTRIBUTE];
91 Assert.IsTrue(scopeAttribute.UseProxy);
92 Assert.AreEqual(ScopeType.Singleton, scopeAttribute.Scope);
97 [Test]
98 public void ProcessModel_ScopeConfigWithoutProxy()
100 _container.AddComponent("Transient.Scope.Component", typeof(IComponent), typeof(TransientScopeComponent));
102 IVertex[] vertices = TopologicalSortAlgo.Sort(_container.Kernel.GraphNodes);
104 for (int i = 0; i < vertices.Length; i++)
106 ComponentModel model = (ComponentModel)vertices[i];
107 if (model.Name == "Transient.Scope.Component")
109 Assert.IsNull(model.CustomComponentActivator);
111 ScopeAttribute scopeAttribute = (ScopeAttribute)model.ExtendedProperties[ScopeInspector.SCOPE_ATTRIBUTE];
113 Assert.IsFalse(scopeAttribute.UseProxy);
114 Assert.AreEqual(ScopeType.Transient, scopeAttribute.Scope);
116 Assert.AreEqual(LifestyleType.Custom, model.LifestyleType);
117 Assert.AreEqual(typeof(ScopeLifestyleManager), model.CustomLifestyle);
118 Assert.AreEqual(0, model.Interceptors.Count);
120 foreach (InterceptorReference interceptorRef in model.Interceptors)
122 Assert.AreEqual(interceptorRef.ReferenceType, InterceptorReferenceType.Interface);
123 Assert.AreEqual(interceptorRef.ServiceType, typeof(BijectionInterceptor));
129 [Test]
130 public void ProcessModel_WithoutInjectProperties()
132 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(TransientScopeComponent));
133 _inspector.ProcessModel(_kernel, model);
135 Assert.AreEqual(0, model.Interceptors.Count);
138 [Test]
139 public void ProcessModel_WithInjectProperties()
141 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(ScopeComponentWithInject));
142 _inspector.ProcessModel(_kernel, model);
144 Assert.AreEqual(1, model.Interceptors.Count);
146 foreach (InterceptorReference interceptorRef in model.Interceptors)
148 Assert.AreEqual(interceptorRef.ReferenceType, InterceptorReferenceType.Interface);
149 Assert.AreEqual(interceptorRef.ServiceType, typeof(BijectionInterceptor));
153 [Test]
154 public void ProcessModel_WithOutjectProperties()
156 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(TransientScopeComponent));
157 _inspector.ProcessModel(_kernel, model);
159 Assert.AreEqual(0, model.Interceptors.Count);
162 [Test]
163 public void ProcessModel_WithoutOutjectProperties()
165 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(ScopeComponentWithOutject));
166 _inspector.ProcessModel(_kernel, model);
168 Assert.AreEqual(1, model.Interceptors.Count);
170 foreach (InterceptorReference interceptorRef in model.Interceptors)
172 Assert.AreEqual(interceptorRef.ReferenceType, InterceptorReferenceType.Interface);
173 Assert.AreEqual(interceptorRef.ServiceType, typeof(BijectionInterceptor));
177 [Test]
178 public void ProcessModel_WithInjectOutjectProperties()
180 ComponentModel model = new ComponentModel("test", typeof(IComponent), typeof(ScopeComponentWithInjectOutject));
181 _inspector.ProcessModel(_kernel, model);
183 Assert.AreEqual(1, model.Interceptors.Count);
185 foreach (InterceptorReference interceptorRef in model.Interceptors)
187 Assert.AreEqual(interceptorRef.ReferenceType, InterceptorReferenceType.Interface);
188 Assert.AreEqual(interceptorRef.ServiceType, typeof(BijectionInterceptor));
192 [Scope(Scope = ScopeType.Transient)]
193 private class ScopeComponentWithInject : IComponent
195 [Inject(Name="Test")]
196 public string Name
200 throw new NotImplementedException("The method or operation is not implemented.");
203 #region IComponent Members
205 public int ID
207 get { throw new NotImplementedException("The method or operation is not implemented."); }
210 #endregion
213 [Scope(Scope = ScopeType.Transient)]
214 private class ScopeComponentWithOutject : IComponent
216 [Outject(Name = "Test")]
217 public string Name
221 throw new NotImplementedException("The method or operation is not implemented.");
224 #region IComponent Members
226 public int ID
228 get { throw new NotImplementedException("The method or operation is not implemented."); }
231 #endregion
234 [Scope(Scope = ScopeType.Transient)]
235 private class ScopeComponentWithInjectOutject : IComponent
237 [Outject(Name = "Test")]
238 [Inject(Name = "Test")]
239 public string Name
243 throw new NotImplementedException("The method or operation is not implemented.");
247 throw new NotImplementedException("The method or operation is not implemented.");
250 #region IComponent Members
252 public int ID
254 get { throw new NotImplementedException("The method or operation is not implemented."); }
257 #endregion