Reverted DictionaryComponentAttribute changes.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Lifestyle / DecomissioningResponsibilitiesTestCase.cs
blob4663db44fa4b75500646dd9434d7e85290c44628
1 // Copyright 2004-2007 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.MicroKernel.Tests.Lifestyle
17 using System;
18 using Castle.Core;
19 using Castle.MicroKernel.Tests.ClassComponents;
20 using Castle.MicroKernel.Tests.Pools;
21 using NUnit.Framework;
23 [TestFixture, Explicit]
24 public class DecomissioningResponsibilitiesTestCase
26 private IKernel kernel;
28 [SetUp]
29 public void Setup()
31 kernel = new DefaultKernel();
34 [TearDown]
35 public void TearDown()
37 kernel.Dispose();
40 [Test]
41 public void ReferencedComponentsAreReleased()
43 kernel.AddComponent("spamservice", typeof(DisposableSpamService), LifestyleType.Transient);
44 kernel.AddComponent("mailsender", typeof(DefaultMailSenderService), LifestyleType.Singleton);
45 kernel.AddComponent("templateengine", typeof(DisposableTemplateEngine), LifestyleType.Transient);
46 kernel.AddComponent("poolable", typeof(PoolableComponent1));
48 DisposableSpamService instance1 = (DisposableSpamService) kernel["spamservice"];
49 Assert.IsFalse(instance1.IsDisposed);
50 Assert.IsFalse(instance1.TemplateEngine.IsDisposed);
51 PoolableComponent1 poolable = instance1.Pool;
53 kernel.ReleaseComponent(instance1);
55 Assert.IsTrue(instance1.IsDisposed);
56 Assert.IsTrue(instance1.TemplateEngine.IsDisposed);
58 DisposableSpamService instance2 = (DisposableSpamService) kernel["spamservice"];
59 Assert.IsFalse(instance2.IsDisposed);
60 Assert.IsFalse(instance2.TemplateEngine.IsDisposed);
61 Assert.AreSame(instance2.Pool, poolable);
65 public class DisposableSpamService : IDisposable
67 private bool isDisposed = false;
68 private DefaultMailSenderService mailSender;
69 private DisposableTemplateEngine templateEngine;
70 private PoolableComponent1 pool;
72 public DisposableSpamService(DefaultMailSenderService mailsender, DisposableTemplateEngine templateEngine)
74 mailSender = mailsender;
75 this.templateEngine = templateEngine;
78 public DisposableSpamService(DefaultMailSenderService mailSender, DisposableTemplateEngine templateEngine,
79 PoolableComponent1 pool)
81 this.mailSender = mailSender;
82 this.templateEngine = templateEngine;
83 this.pool = pool;
86 public bool IsDisposed
88 get { return isDisposed; }
91 public DefaultMailSenderService MailSender
93 get { return mailSender; }
96 public DisposableTemplateEngine TemplateEngine
98 get { return templateEngine; }
101 public PoolableComponent1 Pool
103 get { return pool; }
106 public void Dispose()
108 isDisposed = true;
112 public class DisposableTemplateEngine : DefaultTemplateEngine, IDisposable
114 private bool isDisposed = false;
116 public bool IsDisposed
118 get { return isDisposed; }
121 public void Dispose()
123 isDisposed = true;