Last active
November 11, 2016 13:39
-
-
Save gaevoy/ff2a1ad743d14495c378c6970fe1ba20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Castle.MicroKernel.Lifestyle; | |
using Castle.MicroKernel.Registration; | |
using Castle.Windsor; | |
using NUnit.Framework; | |
namespace Tests | |
{ | |
[TestFixture(true)] | |
[TestFixture(false)] | |
public class UsingFactoryMethodTests | |
{ | |
private readonly bool _useFactoryMethod; | |
public UsingFactoryMethodTests(bool useFactoryMethod) | |
{ | |
_useFactoryMethod = useFactoryMethod; | |
} | |
[Test] | |
public void ShouldDisposeIfConsumerLifestyleTransient() | |
{ | |
// Given | |
var container = new WindsorContainer(); | |
container.Register( | |
Component.For<IA, A>().LifestyleTransient(), | |
Component.For<IB, B>().LifestyleSingleton(), | |
RegisterConsumer().LifestyleTransient() | |
); | |
// When | |
var actual1 = container.Resolve<Consumer>(); | |
var actual2 = container.Resolve<Consumer>(); | |
container.Dispose(); | |
// Then | |
Assert.IsTrue(actual1.A.IsDisposed); | |
Assert.IsTrue(actual1.B.IsDisposed); | |
Assert.IsTrue(actual2.A.IsDisposed); | |
Assert.IsTrue(actual2.B.IsDisposed); | |
Assert.AreNotSame(actual1, actual2); | |
Assert.AreNotSame(actual1.A, actual2.A); | |
Assert.AreSame(actual1.B, actual2.B); | |
} | |
[Test] | |
public void ShouldDisposeIfConsumerLifestyleSingleton() | |
{ | |
// Given | |
var container = new WindsorContainer(); | |
container.Register( | |
Component.For<IA, A>().LifestyleTransient(), | |
Component.For<IB, B>().LifestyleSingleton(), | |
RegisterConsumer().LifestyleSingleton() | |
); | |
// When | |
var actual1 = container.Resolve<Consumer>(); | |
var actual2 = container.Resolve<Consumer>(); | |
container.Dispose(); | |
// Then | |
Assert.IsTrue(actual1.A.IsDisposed); | |
Assert.IsTrue(actual1.B.IsDisposed); | |
Assert.IsTrue(actual2.A.IsDisposed); | |
Assert.IsTrue(actual2.B.IsDisposed); | |
Assert.AreSame(actual1, actual2); | |
Assert.AreSame(actual1.A, actual2.A); | |
Assert.AreSame(actual1.B, actual2.B); | |
} | |
[Test] | |
public void ShouldDisposeWithinScope() | |
{ | |
// Given | |
var container = new WindsorContainer(); | |
container.Register( | |
Component.For<IA, A>().LifestyleTransient(), | |
Component.For<IB, B>().LifestyleSingleton(), | |
RegisterConsumer().LifestyleTransient() | |
); | |
// When | |
Consumer actual1, actual2; | |
using (container.BeginScope()) | |
{ | |
actual1 = container.Resolve<Consumer>(); | |
container.Release(actual1); | |
} | |
using (container.BeginScope()) | |
{ | |
actual2 = container.Resolve<Consumer>(); | |
container.Release(actual2); | |
} | |
// Then | |
Assert.AreNotSame(actual1, actual2); | |
Assert.AreNotSame(actual1.A, actual2.A); | |
Assert.AreSame(actual1.B, actual2.B); | |
Assert.IsTrue(actual1.A.IsDisposed); | |
Assert.IsFalse(actual1.B.IsDisposed); | |
Assert.IsTrue(actual2.A.IsDisposed); | |
Assert.IsFalse(actual2.B.IsDisposed); | |
container.Dispose(); | |
Assert.IsTrue(actual1.B.IsDisposed); | |
Assert.IsTrue(actual2.B.IsDisposed); | |
} | |
private ComponentRegistration<Consumer> RegisterConsumer() | |
{ | |
if (_useFactoryMethod) | |
return Component.For<Consumer>().UsingFactoryMethod(e => new Consumer(e.Resolve<IA>(), e.Resolve<IB>())); | |
else | |
return Component.For<Consumer>(); | |
} | |
interface ICanDispose : IDisposable { bool IsDisposed { get; } } | |
interface IA : ICanDispose { } | |
interface IB : ICanDispose { } | |
class Impl : IA, IB | |
{ | |
public bool IsDisposed { get; private set; } | |
public void Dispose() | |
{ | |
IsDisposed = true; | |
} | |
} | |
class A : Impl { } | |
class B : Impl { } | |
class Consumer | |
{ | |
public IA A { get; } | |
public IB B { get; } | |
public Consumer(IA a, IB b) | |
{ | |
A = a; | |
B = b; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment