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
);
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"));
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
));
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)
144 IHandler
[] handlers
= kernel
.GetAssignableHandlers(typeof(CustomerChain1
));
145 Assert
.AreEqual(2, handlers
.Length
);
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
);