1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
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
;
31 kernel
= new DefaultKernel();
35 public void TearDown()
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
;
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
106 public void Dispose()
112 public class DisposableTemplateEngine
: DefaultTemplateEngine
, IDisposable
114 private bool isDisposed
= false;
116 public bool IsDisposed
118 get { return isDisposed; }
121 public void Dispose()