1 // Copyright 2004-2008 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
.Registration
18 using System
.Reflection
;
20 using Castle
.MicroKernel
.Registration
;
21 using Castle
.MicroKernel
.Tests
.ClassComponents
;
22 using NUnit
.Framework
;
29 public class AllTypesOfTestCase
31 private IKernel kernel
;
36 kernel
= new DefaultKernel();
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
);
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
);
69 public void RegisterAssemblyTypes_DefaultService_RegisteredInContainer()
71 kernel
.Register(AllTypesOf
<ICommon
>
72 .FromAssembly(Assembly
.GetExecutingAssembly())
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
);
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
);
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
>>();
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"));
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
));
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)
157 IHandler
[] handlers
= kernel
.GetAssignableHandlers(typeof(CustomerChain1
));
158 Assert
.AreEqual(2, handlers
.Length
);
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
);