Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Registration / AllTypesOfTestCase.cs
blobc0680606515636eac72ba49f87062066e3c9e0df
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 #if DOTNET35
104 [Test]
105 public void RegisterAssemblyTypes_IfCondition_RegisteredInContainer()
107 kernel.Register(AllTypesOf<ICustomer>
108 .FromAssembly(Assembly.GetExecutingAssembly())
109 .If(t => t.FullName.Contains("Chain"))
112 IHandler[] handlers = kernel.GetAssignableHandlers(typeof(ICustomer));
113 Assert.AreNotEqual(0, handlers.Length);
115 foreach (IHandler handler in handlers)
117 Assert.IsTrue(handler.ComponentModel.Implementation.FullName.Contains("Chain"));
121 [Test]
122 public void RegisterAssemblyTypes_UnlessCondition_RegisteredInContainer()
124 kernel.Register(AllTypesOf<ICustomer>
125 .FromAssembly(Assembly.GetExecutingAssembly())
126 .Unless(t => typeof(CustomerChain1).IsAssignableFrom(t))
129 foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(ICustomer)))
131 Assert.IsFalse(typeof(CustomerChain1).IsAssignableFrom(handler.ComponentModel.Implementation));
135 [Test]
136 public void RegisterTypes_WithLinq_RegisteredInContainer()
138 kernel.Register(AllTypesOf<CustomerChain1>
139 .Pick(from type in Assembly.GetExecutingAssembly().GetExportedTypes()
140 where type.IsDefined(typeof(SerializableAttribute), true)
141 select type
144 IHandler[] handlers = kernel.GetAssignableHandlers(typeof(CustomerChain1));
145 Assert.AreEqual(2, handlers.Length);
148 [Test]
149 public void RegisterAssemblyTypes_WithKLinqConfiguration_RegisteredInContainer()
151 kernel.Register( AllTypesOf<ICommon>
152 .FromAssembly( Assembly.GetExecutingAssembly() )
153 .Configure( component => component.LifeStyle.Transient
154 .Named( component.Implementation.FullName + "XYZ" )
158 foreach ( IHandler handler in kernel.GetAssignableHandlers( typeof( ICommon ) ) )
160 Assert.AreEqual( LifestyleType.Transient, handler.ComponentModel.LifestyleType );
161 Assert.AreEqual( handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name );
165 #endif