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 AspectSharp
.Tests
.InterceptorTests
19 using NUnit
.Framework
;
21 using AspectSharp
.Builder
;
22 using AspectSharp
.Tests
.Classes
;
25 /// Summary description for InterceptorTestCase.
28 public class InterceptorTestCase
33 LogInvocationInterceptor
.Clear();
37 public void InterceptAll()
39 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
41 " aspect MyAspect for ComplexClass " +
43 " pointcut method|property(*)" +
44 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
49 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
50 AspectEngine engine
= builder
.Build();
52 WrapAndInvokeEverything(engine
);
54 String
[] messages
= LogInvocationInterceptor
.Messages
;
55 Assert
.AreEqual( 8, messages
.Length
);
56 // TODO: Assert messages in correct order
60 public void InterceptAllMethods()
62 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
64 " aspect MyAspect for ComplexClass " +
66 " pointcut method(*)" +
67 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
72 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
73 AspectEngine engine
= builder
.Build();
75 WrapAndInvokeEverything(engine
);
77 String
[] messages
= LogInvocationInterceptor
.Messages
;
78 Assert
.AreEqual( 4, messages
.Length
);
79 // TODO: Assert messages in correct order
83 public void InterceptAllProperties()
85 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
87 " aspect MyAspect for ComplexClass " +
89 " pointcut property(*)" +
90 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
95 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
96 AspectEngine engine
= builder
.Build();
98 WrapAndInvokeEverything(engine
);
100 String
[] messages
= LogInvocationInterceptor
.Messages
;
101 Assert
.AreEqual( 4, messages
.Length
);
102 // TODO: Assert messages in correct order
106 public void InterceptAllPropertyRead()
108 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
110 " aspect MyAspect for ComplexClass " +
112 " pointcut propertyread(*)" +
113 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
118 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
119 AspectEngine engine
= builder
.Build();
121 WrapAndInvokeEverything(engine
);
123 String
[] messages
= LogInvocationInterceptor
.Messages
;
124 Assert
.AreEqual( 2, messages
.Length
);
125 // TODO: Assert messages in correct order
129 public void InterceptAllPropertyReadReturningString()
131 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
133 " aspect MyAspect for ComplexClass " +
135 " pointcut propertyread(String)" +
136 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
141 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
142 AspectEngine engine
= builder
.Build();
144 WrapAndInvokeEverything(engine
);
146 String
[] messages
= LogInvocationInterceptor
.Messages
;
147 Assert
.AreEqual( 1, messages
.Length
);
148 // TODO: Assert messages in correct order
152 public void InterceptAllPropertyWrite()
154 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
156 " aspect MyAspect for ComplexClass " +
158 " pointcut propertyread(*)" +
159 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
164 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
165 AspectEngine engine
= builder
.Build();
167 WrapAndInvokeEverything(engine
);
169 String
[] messages
= LogInvocationInterceptor
.Messages
;
170 Assert
.AreEqual( 2, messages
.Length
);
171 // TODO: Assert messages in correct order
175 public void InterceptAllMethodsPropertyWrite()
177 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
179 " aspect MyAspect for ComplexClass " +
181 " pointcut method|propertyread(*)" +
182 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
187 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
188 AspectEngine engine
= builder
.Build();
190 WrapAndInvokeEverything(engine
);
192 String
[] messages
= LogInvocationInterceptor
.Messages
;
193 Assert
.AreEqual( 6, messages
.Length
);
194 // TODO: Assert messages in correct order
198 public void InterceptAllDoSomethingMethods()
200 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
202 " aspect MyAspect for ComplexClass " +
204 " pointcut method(* DoSomething)" +
205 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
210 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
211 AspectEngine engine
= builder
.Build();
213 WrapAndInvokeEverything(engine
);
215 String
[] messages
= LogInvocationInterceptor
.Messages
;
216 Assert
.AreEqual( 3, messages
.Length
);
220 public void InterceptAllDoSomethingMethodsWithAnIntArgument()
222 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
224 " aspect MyAspect for ComplexClass " +
226 " pointcut method(* DoSomething(int))" +
227 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
232 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
233 AspectEngine engine
= builder
.Build();
235 WrapAndInvokeEverything(engine
);
237 String
[] messages
= LogInvocationInterceptor
.Messages
;
238 Assert
.AreEqual( 1, messages
.Length
);
242 public void InterceptAllDoSomethingMethodsWithAnIntArgumentAndAnyOther()
244 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
246 " aspect MyAspect for ComplexClass " +
248 " pointcut method(* DoSomething(int, *))" +
249 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
254 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
255 AspectEngine engine
= builder
.Build();
257 WrapAndInvokeEverything(engine
);
259 String
[] messages
= LogInvocationInterceptor
.Messages
;
260 Assert
.AreEqual( 2, messages
.Length
);
264 public void InterceptAllDoSomethingMethodsWithAnIntAndAnIntArguments()
266 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
268 " aspect MyAspect for ComplexClass " +
270 " pointcut method(* DoSomething(int, int))" +
271 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
276 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
277 AspectEngine engine
= builder
.Build();
279 WrapAndInvokeEverything(engine
);
281 String
[] messages
= LogInvocationInterceptor
.Messages
;
282 Assert
.AreEqual( 0, messages
.Length
);
286 public void InterceptAllDoSomethingMethodReturningVoid()
288 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
290 " aspect MyAspect for ComplexClass " +
292 " pointcut method(void DoSomething(*))" +
293 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
298 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
299 AspectEngine engine
= builder
.Build();
301 WrapAndInvokeEverything(engine
);
303 String
[] messages
= LogInvocationInterceptor
.Messages
;
304 Assert
.AreEqual( 0, messages
.Length
);
308 public void InterceptAllDoSomethingMethodReturningInt()
310 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
312 " aspect MyAspect for ComplexClass " +
314 " pointcut method(int DoSomething(*))" +
315 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
320 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
321 AspectEngine engine
= builder
.Build();
323 WrapAndInvokeEverything(engine
);
325 String
[] messages
= LogInvocationInterceptor
.Messages
;
326 Assert
.AreEqual( 3, messages
.Length
);
330 public void InterceptAllDoSMethods()
332 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
334 " aspect MyAspect for ComplexClass " +
336 " pointcut method(* DoS.*(*))" +
337 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
342 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
343 AspectEngine engine
= builder
.Build();
345 WrapAndInvokeEverything(engine
);
347 String
[] messages
= LogInvocationInterceptor
.Messages
;
348 Assert
.AreEqual( 3, messages
.Length
);
352 public void InterceptPropertyName()
354 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
356 " aspect MyAspect for ComplexClass " +
358 " pointcut property(* Na.*)" +
359 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
364 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
365 AspectEngine engine
= builder
.Build();
367 WrapAndInvokeEverything(engine
);
369 String
[] messages
= LogInvocationInterceptor
.Messages
;
370 Assert
.AreEqual( 2, messages
.Length
);
371 // TODO: Assert messages in correct order
375 public void PointcutsCombined()
377 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
379 " aspect MyAspect for ComplexClass " +
381 " pointcut property(*)" +
382 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
384 " pointcut method(*)" +
385 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
390 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
391 AspectEngine engine
= builder
.Build();
393 WrapAndInvokeEverything(engine
);
395 String
[] messages
= LogInvocationInterceptor
.Messages
;
396 Assert
.AreEqual( 8, messages
.Length
);
397 // TODO: Assert messages in correct order
400 private static void WrapAndInvokeEverything(AspectEngine engine
)
402 ComplexClass instance
= engine
.WrapClass(typeof(ComplexClass
)) as ComplexClass
;
403 instance
.DoNothing();
404 instance
.DoSomething();
407 instance
.DoSomething(arg
);
408 instance
.DoSomething(arg
, "hiya");
410 //TODO: Intercept by ref calls.
411 //Assert.AreEqual(arg, instance.Pong(ref arg));
413 instance
.Name
= "John Johnson";
414 Assert
.AreEqual( "John Johnson", instance
.Name
);
415 instance
.Started
= true;
416 Assert
.IsTrue( instance
.Started
);