Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Experiments / Castle.Igloo / Castle.Igloo.Test / ScopeTest / ScopeTestCase.cs
blobffaa441b17a74fbf817be8e85753395a19d77983
2 using System.Threading;
3 using Castle.Core;
4 using Castle.Core.Resource;
5 using Castle.Igloo.Attributes;
6 using Castle.Igloo.LifestyleManager;
7 using Castle.Igloo.Scopes;
8 using Castle.Igloo.Scopes.Web;
9 using Castle.Igloo.Test.ScopeTest.Components;
10 using Castle.MicroKernel.SubSystems.Configuration;
11 using Castle.Windsor;
12 using Castle.Windsor.Configuration.Interpreters;
13 using NUnit.Framework;
15 using Castle.MicroKernel;
17 namespace Castle.Igloo.Test.ScopeTest
20 /// <summary>
21 /// Summary description for LifestyleManagerTestCase.
22 /// </summary>
23 [TestFixture]
24 public class ScopeTestCase
26 private IWindsorContainer _container = null;
28 private IComponent instance3;
30 [SetUp]
31 public void CreateContainer()
33 DefaultConfigurationStore store = new DefaultConfigurationStore();
34 XmlInterpreter interpreter = new XmlInterpreter(new ConfigResource());
35 interpreter.ProcessResource(interpreter.Source, store);
37 _container = new WindsorContainer(interpreter);
41 [TearDown]
42 public void DisposeContainer()
44 _container.Dispose();
47 /// <summary>
48 /// Test ScopeRegistry
49 /// </summary>
50 [Test]
51 public void TestScopeRegistry()
53 IScopeRegistry registry = _container.Resolve<IScopeRegistry>();
54 Assert.IsNotNull(registry);
56 ISessionScope scope = _container.Resolve<ISessionScope>();
57 Assert.IsNotNull(scope);
59 scope["test"] = new object();
61 Assert.IsTrue(registry.IsInScopes("test"));
63 InjectAttribute attribute = new InjectAttribute();
64 attribute.Name = "test";
65 attribute.Scope = ScopeType.Session;
67 Assert.IsNotNull(registry.GetFromScopes(attribute));
70 [Test]
71 public void ScopeSetThroughAttribute()
73 _container.AddComponent("b", typeof(TransientScopeComponent));
74 IHandler handler = _container.Kernel.GetHandler("b");
75 Assert.AreEqual(LifestyleType.Custom, handler.ComponentModel.LifestyleType);
76 Assert.AreEqual(typeof(ScopeLifestyleManager), handler.ComponentModel.CustomLifestyle);
78 _container.AddComponent("c", typeof(PerScopeThreadComponent));
79 handler = _container.Kernel.GetHandler("c");
80 Assert.AreEqual(LifestyleType.Custom, handler.ComponentModel.LifestyleType);
81 Assert.AreEqual(typeof(ScopeLifestyleManager), handler.ComponentModel.CustomLifestyle);
84 [Test]
85 public void ScopeSetThroughConfig()
87 IHandler handler = _container.Kernel.GetHandler("Singleton.Scope.Component");
88 Assert.AreEqual(LifestyleType.Custom, handler.ComponentModel.LifestyleType);
89 Assert.AreEqual(typeof(ScopeLifestyleManager), handler.ComponentModel.CustomLifestyle);
93 [Test]
94 public void TestSingletonScope()
96 IHandler handler = _container.Kernel.GetHandler("Singleton.Scope.Component");
98 IComponent instance1 = handler.Resolve(CreationContext.Empty) as IComponent;
99 IComponent instance2 = handler.Resolve(CreationContext.Empty) as IComponent;
101 Assert.IsNotNull(instance1);
102 Assert.IsNotNull(instance2);
104 Assert.IsTrue(instance1.Equals(instance2));
105 Assert.IsTrue(instance1.ID == instance2.ID);
107 handler.Release(instance1);
108 handler.Release(instance2);
111 [Test]
112 public void TestTransientScope()
114 _container.AddComponent("a", typeof(IComponent), typeof(TransientScopeComponent));
116 IHandler handler = _container.Kernel.GetHandler("a");
118 IComponent instance1 = handler.Resolve(CreationContext.Empty) as IComponent;
119 IComponent instance2 = handler.Resolve(CreationContext.Empty) as IComponent;
121 Assert.IsNotNull(instance1);
122 Assert.IsNotNull(instance2);
124 Assert.IsTrue(!instance1.Equals(instance2));
125 Assert.IsTrue(instance1.ID != instance2.ID);
127 handler.Release(instance1);
128 handler.Release(instance2);
132 [Test]
133 public void TestThreadScope()
135 _container.AddComponent("a", typeof(IComponent), typeof(PerScopeThreadComponent));
137 IHandler handler = _container.Kernel.GetHandler("a");
139 IComponent instance1 = handler.Resolve(CreationContext.Empty) as IComponent;
140 IComponent instance2 = handler.Resolve(CreationContext.Empty) as IComponent;
142 Assert.IsNotNull(instance1);
143 Assert.IsNotNull(instance2);
145 Assert.IsTrue(instance1.Equals(instance2));
146 Assert.IsTrue(instance1.ID == instance2.ID);
148 Thread thread = new Thread(new ThreadStart(OtherThread));
149 thread.Start();
150 thread.Join();
152 Assert.IsNotNull(this.instance3);
153 Assert.IsTrue(!instance1.Equals(this.instance3));
154 Assert.IsTrue(instance1.ID != this.instance3.ID);
156 handler.Release(instance1);
157 handler.Release(instance2);
160 private void OtherThread()
162 IHandler handler = _container.Kernel.GetHandler("a");
163 this.instance3 = handler.Resolve(CreationContext.Empty) as IComponent;