Added testcase for generic registrations and a workaround for the CLR GetInterfaces...
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Registration / AllTypesOfTestCase.cs
blob5ff758628e18af0f4d8b78e2932f9b2dbd3b310a
1 // Copyright 2004-2008 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.Registration
17 using System;
18 using System.Reflection;
19 using Castle.Core;
20 using Castle.MicroKernel.Registration;
21 using Castle.MicroKernel.Tests.ClassComponents;
22 using NUnit.Framework;
24 #if DOTNET35
25 using System.Linq;
26 #endif
28 [TestFixture]
29 public class AllTypesOfTestCase
31 private IKernel kernel;
33 [SetUp]
34 public void Init()
36 kernel = new DefaultKernel();
39 [Test]
40 public void RegisterAssemblyTypes_NoService_RegisteredInContainer()
42 kernel.Register(AllTypesOf<ICommon>
43 .FromAssembly(Assembly.GetExecutingAssembly())
46 IHandler[] handlers = kernel.GetHandlers(typeof(ICommon));
47 Assert.AreEqual(0, handlers.Length);
49 handlers = kernel.GetAssignableHandlers(typeof(ICommon));
50 Assert.AreNotEqual(0, handlers.Length);
53 [Test]
54 public void RegisterAssemblyTypes_FirstInterfaceService_RegisteredInContainer()
56 kernel.Register(AllTypesOf<ICommon>
57 .FromAssembly(Assembly.GetExecutingAssembly())
58 .WithService.FirstInterface()
61 IHandler[] handlers = kernel.GetHandlers(typeof(ICommon));
62 Assert.AreNotEqual(0, handlers.Length);
64 handlers = kernel.GetAssignableHandlers(typeof(ICommon));
65 Assert.AreNotEqual(0, handlers.Length);
68 [Test]
69 public void RegisterAssemblyTypes_DefaultService_RegisteredInContainer()
71 kernel.Register(AllTypesOf<ICommon>
72 .FromAssembly(Assembly.GetExecutingAssembly())
73 .WithService.Base()
76 IHandler[] handlers = kernel.GetHandlers(typeof(ICommon));
77 Assert.AreNotEqual(0, handlers.Length);
79 handlers = kernel.GetAssignableHandlers(typeof(ICommon));
80 Assert.AreNotEqual(0, handlers.Length);
83 [Test]
84 public void RegisterAssemblyTypes_WithConfiguration_RegisteredInContainer()
86 kernel.Register(AllTypesOf<ICommon>
87 .FromAssembly(Assembly.GetExecutingAssembly())
88 .Configure(delegate(ComponentRegistration component)
90 component.LifeStyle.Transient
91 .Named(component.Implementation.FullName + "XYZ");
95 foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(ICommon)))
97 Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
98 Assert.AreEqual(handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name);
102 [Test]
103 public void RegisterGenericTypes_WithGenericDefinition_RegisteredInContainer()
105 kernel.Register(AllTypes
106 .From(typeof(DefaultRepository<>))
107 .WithService.FirstInterface()
110 Type t = typeof(IRepository<CustomerImpl>);
112 IRepository<CustomerImpl> repository = kernel.Resolve<IRepository<CustomerImpl>>();
115 #if DOTNET35
117 [Test]
118 public void RegisterAssemblyTypes_IfCondition_RegisteredInContainer()
120 kernel.Register(AllTypesOf<ICustomer>
121 .FromAssembly(Assembly.GetExecutingAssembly())
122 .If(t => t.FullName.Contains("Chain"))
125 IHandler[] handlers = kernel.GetAssignableHandlers(typeof(ICustomer));
126 Assert.AreNotEqual(0, handlers.Length);
128 foreach (IHandler handler in handlers)
130 Assert.IsTrue(handler.ComponentModel.Implementation.FullName.Contains("Chain"));
134 [Test]
135 public void RegisterAssemblyTypes_UnlessCondition_RegisteredInContainer()
137 kernel.Register(AllTypesOf<ICustomer>
138 .FromAssembly(Assembly.GetExecutingAssembly())
139 .Unless(t => typeof(CustomerChain1).IsAssignableFrom(t))
142 foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(ICustomer)))
144 Assert.IsFalse(typeof(CustomerChain1).IsAssignableFrom(handler.ComponentModel.Implementation));
148 [Test]
149 public void RegisterTypes_WithLinq_RegisteredInContainer()
151 kernel.Register(AllTypesOf<CustomerChain1>
152 .Pick(from type in Assembly.GetExecutingAssembly().GetExportedTypes()
153 where type.IsDefined(typeof(SerializableAttribute), true)
154 select type
157 IHandler[] handlers = kernel.GetAssignableHandlers(typeof(CustomerChain1));
158 Assert.AreEqual(2, handlers.Length);
161 [Test]
162 public void RegisterAssemblyTypes_WithKLinqConfiguration_RegisteredInContainer()
164 kernel.Register(AllTypesOf<ICommon>
165 .FromAssembly(Assembly.GetExecutingAssembly())
166 .Configure(component => component.LifeStyle.Transient
167 .Named(component.Implementation.FullName + "XYZ")
171 foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(ICommon)))
173 Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
174 Assert.AreEqual(handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name);
178 #endif