1+ using FluentAssertions ;
2+ using Ninject . Extensions . Interception . Attributes ;
3+ using Ninject . Extensions . Interception . Infrastructure . Language ;
4+ using Ninject . Extensions . Interception . Request ;
5+ using Xunit ;
6+
7+ namespace Ninject . Extensions . Interception
8+ {
9+ public abstract class SystemObjectMethodInterceptionContext : InterceptionTestContext
10+ {
11+ public static bool InterceptionFlag = false ;
12+ private readonly IKernel kernel ;
13+
14+ protected SystemObjectMethodInterceptionContext ( )
15+ {
16+ InterceptionFlag = false ;
17+
18+ kernel = base . CreateDefaultInterceptionKernel ( ) ;
19+ }
20+
21+ [ Fact ]
22+ public void InterceptionUsingAttribute_InterceptsMethods ( )
23+ {
24+ kernel . Bind < IHaveInterceptAttribute > ( ) . To < HaveInterceptAttribute > ( ) ;
25+
26+ kernel . Get < IHaveInterceptAttribute > ( ) . DoSomething ( ) ;
27+
28+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
29+ }
30+
31+ [ Fact ]
32+ public void InterceptionUsingAttribute_DoesNotInterceptSystemObjectMethods ( )
33+ {
34+ kernel . Bind < IHaveInterceptAttribute > ( ) . To < HaveInterceptAttribute > ( ) ;
35+
36+ kernel . Get < IHaveInterceptAttribute > ( ) . GetHashCode ( ) ;
37+
38+ InterceptionFlag . Should ( ) . BeFalse ( ) ;
39+ }
40+
41+ [ Fact ]
42+ public void InterceptionUsingAttribute_DoesInterceptOverridenSystemObjectMethods ( )
43+ {
44+ kernel . Bind < IHaveInterceptAttribute > ( ) . To < HaveInterceptAndOverrideGetHashCodeAttribute > ( ) ;
45+
46+ kernel . Get < IHaveInterceptAttribute > ( ) . GetHashCode ( ) ;
47+
48+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
49+ }
50+
51+ [ Fact ]
52+ public void InterceptionUsingBindingExtension_InterceptsMethods ( )
53+ {
54+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( ) . Intercept ( ) . With < MethodInterceptor > ( ) ;
55+
56+ kernel . Get < IHaveNoInterceptAttribute > ( ) . DoSomething ( ) ;
57+
58+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
59+ }
60+
61+ [ Fact ]
62+ public void InterceptionUsingBindingExtension_DoesNotInterceptSystemObjectMethods ( )
63+ {
64+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( ) . Intercept ( ) . With < MethodInterceptor > ( ) ;
65+
66+ kernel . Get < IHaveNoInterceptAttribute > ( ) . GetHashCode ( ) ;
67+
68+ InterceptionFlag . Should ( ) . BeFalse ( ) ;
69+ }
70+
71+ [ Fact ]
72+ public void InterceptionUsingBindingExtension_DoesInterceptOverriddenSystemObjectMethods ( )
73+ {
74+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttributeButOverrideGetHashCode > ( ) . Intercept ( ) . With < MethodInterceptor > ( ) ;
75+
76+ kernel . Get < IHaveNoInterceptAttribute > ( ) . GetHashCode ( ) ;
77+
78+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
79+ }
80+
81+ [ Fact ]
82+ public void InterceptionUsingBindingExtension_WithInterceptAllMethodsPredicate_DoesInterceptSystemObjectMethods ( )
83+ {
84+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( )
85+ . Intercept ( mi => true ) . With < MethodInterceptor > ( ) ;
86+
87+ kernel . Get < IHaveNoInterceptAttribute > ( ) . GetHashCode ( ) ;
88+
89+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
90+ }
91+
92+ [ Fact ]
93+ public void InterceptionUsingBindingExtension_WithInterceptNothingPredicate_DoesNotInterceptMethods ( )
94+ {
95+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( )
96+ . Intercept ( mi => false ) . With < MethodInterceptor > ( ) ;
97+
98+ kernel . Get < IHaveNoInterceptAttribute > ( ) . DoSomething ( ) ;
99+
100+ InterceptionFlag . Should ( ) . BeFalse ( ) ;
101+ }
102+
103+ [ Fact ]
104+ public void InterceptionUsingKernelExtension_InterceptsMethods ( )
105+ {
106+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( ) ;
107+ kernel
108+ . Intercept ( ctx => typeof ( IHaveNoInterceptAttribute ) . IsAssignableFrom ( ctx . Plan . Type ) )
109+ . With < MethodInterceptor > ( ) ;
110+
111+ kernel . Get < IHaveNoInterceptAttribute > ( ) . DoSomething ( ) ;
112+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
113+ }
114+
115+ [ Fact ]
116+ public void InterceptionUsingKernelExtension_DoesNotInterceptSystemObjectMethods ( )
117+ {
118+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( ) ;
119+ kernel
120+ . Intercept ( ctx => typeof ( IHaveNoInterceptAttribute ) . IsAssignableFrom ( ctx . Plan . Type ) )
121+ . With < MethodInterceptor > ( ) ;
122+
123+ kernel . Get < IHaveNoInterceptAttribute > ( ) . GetHashCode ( ) ;
124+ InterceptionFlag . Should ( ) . BeFalse ( ) ;
125+ }
126+
127+ [ Fact ]
128+ public void InterceptionUsingKernelExtension_DoesInterceptOverriddenSystemObjectMethods ( )
129+ {
130+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttributeButOverrideGetHashCode > ( ) ;
131+ kernel
132+ . Intercept ( ctx => typeof ( IHaveNoInterceptAttribute ) . IsAssignableFrom ( ctx . Plan . Type ) )
133+ . With < MethodInterceptor > ( ) ;
134+
135+ kernel . Get < IHaveNoInterceptAttribute > ( ) . GetHashCode ( ) ;
136+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
137+ }
138+
139+ [ Fact ]
140+ public void InterceptionUsingKernelExtension_WithInterceptAllMethodsPredicate_DoesInterceptSystemObjectMethods ( )
141+ {
142+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( ) ;
143+ kernel
144+ . Intercept (
145+ ctx => typeof ( IHaveNoInterceptAttribute ) . IsAssignableFrom ( ctx . Plan . Type ) ,
146+ mi => true
147+ )
148+ . With < MethodInterceptor > ( ) ;
149+
150+ kernel . Get < IHaveNoInterceptAttribute > ( ) . GetHashCode ( ) ;
151+ InterceptionFlag . Should ( ) . BeTrue ( ) ;
152+ }
153+
154+ [ Fact ]
155+ public void InterceptionUsingKernelExtension_WithInterceptNothingPredicate_DoesNotInterceptMethods ( )
156+ {
157+ kernel . Bind < IHaveNoInterceptAttribute > ( ) . To < HaveNoInterceptAttribute > ( ) ;
158+ kernel
159+ . Intercept (
160+ ctx => typeof ( IHaveNoInterceptAttribute ) . IsAssignableFrom ( ctx . Plan . Type ) ,
161+ mi => false
162+ )
163+ . With < MethodInterceptor > ( ) ;
164+
165+ kernel . Get < IHaveNoInterceptAttribute > ( ) . DoSomething ( ) ;
166+ InterceptionFlag . Should ( ) . BeFalse ( ) ;
167+ }
168+
169+ public interface IHaveInterceptAttribute
170+ {
171+ void DoSomething ( ) ;
172+ }
173+
174+ [ ChangeFlag ]
175+ public class HaveInterceptAttribute : IHaveInterceptAttribute
176+ {
177+ public void DoSomething ( )
178+ {
179+ }
180+ }
181+
182+ public class HaveInterceptAndOverrideGetHashCodeAttribute : HaveInterceptAttribute
183+ {
184+ public override int GetHashCode ( )
185+ {
186+ return base . GetHashCode ( ) + 1 ;
187+ }
188+ }
189+
190+ public interface IHaveNoInterceptAttribute
191+ {
192+ void DoSomething ( ) ;
193+ }
194+
195+ public class HaveNoInterceptAttribute : IHaveNoInterceptAttribute
196+ {
197+ public virtual void DoSomething ( )
198+ {
199+ }
200+ }
201+
202+ public class HaveNoInterceptAttributeButOverrideGetHashCode : HaveNoInterceptAttribute
203+ {
204+ public override int GetHashCode ( )
205+ {
206+ return base . GetHashCode ( ) + 1 ;
207+ }
208+ }
209+
210+ public class ChangeFlagAttribute : InterceptAttribute
211+ {
212+ public override IInterceptor CreateInterceptor ( IProxyRequest request )
213+ {
214+ return new MethodInterceptor ( ) ;
215+ }
216+ }
217+
218+ public class MethodInterceptor : IInterceptor
219+ {
220+ public void Intercept ( IInvocation invocation )
221+ {
222+ InterceptionFlag = true ;
223+ invocation . Proceed ( ) ;
224+ }
225+ }
226+ }
227+ }
0 commit comments