PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/src/UnitTests/IOC/Factories/FactoryTests.cs

http://github.com/philiplaureano/LinFu
C# | 185 lines | 135 code | 36 blank | 14 comment | 1 complexity | 14414d2457b89956b54806fdc46f5c38 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Runtime.Serialization;
  4using LinFu.IoC;
  5using LinFu.IoC.Factories;
  6using LinFu.IoC.Interfaces;
  7using Moq;
  8using Xunit;
  9using SampleLibrary;
 10using SampleLibrary.IOC.BugFixes;
 11
 12namespace LinFu.UnitTests.IOC.Factories
 13{
 14    public class FactoryTests : BaseTestFixture
 15    {
 16        private Func<IFactoryRequest, ISerializable> _createInstance;
 17
 18        protected override void Init()
 19        {
 20            // Create a new mock service instance on each
 21            // factory method call
 22            _createInstance = request => new Mock<ISerializable>().Object;
 23        }
 24
 25        protected override void Term()
 26        {
 27            _createInstance = null;
 28        }
 29
 30        [Fact]
 31        public void GenericFactoryAdapterShouldCallUntypedFactoryInstance()
 32        {
 33            var container = new ServiceContainer();
 34            var mockFactory = new Mock<IFactory<ISerializable>>();
 35            var mockService = new Mock<ISerializable>();
 36            var adapter = new FactoryAdapter<ISerializable>(mockFactory.Object);
 37
 38            // The adapter itself should call the container on creation
 39            mockFactory.Expect(f => f.CreateInstance(It.Is<IFactoryRequest>(request => request.Container == container)))
 40                .Returns(mockService.Object);
 41
 42            Assert.NotNull(adapter);
 43
 44            var factoryRequest = new FactoryRequest
 45            {
 46                ServiceName = null,
 47                ServiceType = typeof(ISerializable),
 48                Container = container
 49            };
 50
 51            adapter.CreateInstance(factoryRequest);
 52
 53            mockFactory.VerifyAll();
 54        }
 55
 56        [Fact]
 57        public void OncePerRequestFactoryShouldCreateUniqueInstances()
 58        {
 59            var factory = new OncePerRequestFactory<ISerializable>(_createInstance);
 60
 61            var first = factory.CreateInstance(null);
 62            var second = factory.CreateInstance(null);
 63
 64            // Both instances must be unique
 65            Assert.NotSame(first, second);
 66            Assert.NotNull(first);
 67            Assert.NotNull(second);
 68        }
 69
 70        [Fact]
 71        public void OncePerThreadFactoryShouldCreateTheSameInstanceFromWithinTheSameThread()
 72        {
 73            IFactory<ISerializable> localFactory = new OncePerThreadFactory<ISerializable>(_createInstance);
 74
 75            var first = localFactory.CreateInstance(null);
 76            var second = localFactory.CreateInstance(null);
 77
 78            // The two instances should be the same
 79            // since they were created from the same thread
 80            Assert.NotNull(first);
 81            Assert.Same(first, second);
 82        }
 83
 84        [Fact]
 85        public void OncePerThreadFactoryShouldCreateUniqueInstancesFromDifferentThreads()
 86        {
 87            IFactory<ISerializable> localFactory = new OncePerThreadFactory<ISerializable>(_createInstance);
 88            var resultList = new List<ISerializable>();
 89
 90            Action<IFactory<ISerializable>> doCreate = factory =>
 91            {
 92                var instance = factory.CreateInstance(null);
 93                var otherInstance = factory.CreateInstance(null);
 94
 95                // The two instances 
 96                // within the same thread must match
 97                Assert.Same(instance, otherInstance);
 98                lock (resultList)
 99                {
100                    resultList.Add(instance);
101                }
102            };
103
104
105            // Create the instance in another thread
106            var asyncResult = doCreate.BeginInvoke(localFactory, null, null);
107            var localInstance = localFactory.CreateInstance(null);
108
109            // Wait for the previous thread
110            // to finish executing
111            doCreate.EndInvoke(asyncResult);
112
113            Assert.True(resultList.Count > 0);
114
115            // Collect the results from the other thread
116            var instanceFromOtherThread = resultList[0];
117
118            Assert.NotNull(localInstance);
119            Assert.NotNull(instanceFromOtherThread);
120            Assert.NotSame(localInstance, instanceFromOtherThread);
121        }
122
123        [Fact]
124        public void ShouldBeAbleToCreateClosedGenericTypeUsingACustomFactoryInstance()
125        {
126            var container = new ServiceContainer();
127            container.Initialize();
128            container.LoadFrom(typeof(MyClass<>).Assembly);
129
130            // Get ServiceNotFoundException here instead of a service instance.
131            var serviceName = "frobozz";
132            var service = container.GetService<MyClass<string>>(serviceName);
133
134            Console.WriteLine("foo");
135            Assert.Equal(serviceName, service.Value);
136        }
137
138        [Fact]
139        public void ShouldBeAbleToInstantiateCustomFactoryWithServiceArgumentsInConstructor()
140        {
141            var mock = new Mock<ISampleService>();
142            var container = new ServiceContainer();
143            container.LoadFromBaseDirectory("*.dll");
144
145            container.AddService(mock.Object);
146            var result = container.GetService<string>("SampleFactoryWithConstructorArguments");
147
148            Assert.NotNull(result);
149            Assert.NotEmpty(result);
150        }
151
152        [Fact]
153        public void ShouldLoadStronglyTypedFactoryFromLoadFromExtensionMethod()
154        {
155            var container = new ServiceContainer();
156            container.LoadFrom(typeof(SampleClass).Assembly);
157
158            var serviceInstance = container.GetService<ISampleService>("Test");
159            Assert.NotNull(serviceInstance);
160        }
161
162        [Fact]
163        public void SingletonFactoryShouldCreateTheSameInstanceOnce()
164        {
165            var factory = new SingletonFactory<ISerializable>(_createInstance);
166            var container = new ServiceContainer();
167
168            var request = new FactoryRequest
169            {
170                ServiceName = null,
171                Arguments = new object[0],
172                Container = container,
173                ServiceType = typeof(ISerializable)
174            };
175
176            var first = factory.CreateInstance(request);
177            var second = factory.CreateInstance(request);
178
179            // Both instances must be the same
180            Assert.Same(first, second);
181            Assert.NotNull(first);
182            Assert.NotNull(second);
183        }
184    }
185}