Few name changes and expose AdditionalParameters in CreationContext.
[castle.git] / AspectSharp / AspectSharp.Tests / InterceptorTests / InterceptorTestCase.cs
blob134a4031ff808bc439e8077075bbd7ad1e5bfe4d
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 AspectSharp.Tests.InterceptorTests
17 using System;
19 using NUnit.Framework;
21 using AspectSharp.Builder;
22 using AspectSharp.Tests.Classes;
24 /// <summary>
25 /// Summary description for InterceptorTestCase.
26 /// </summary>
27 [TestFixture]
28 public class InterceptorTestCase
30 [SetUp]
31 public void Reset()
33 LogInvocationInterceptor.Clear();
36 [Test]
37 public void InterceptAll()
39 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
40 " " +
41 " aspect MyAspect for ComplexClass " +
42 " " +
43 " pointcut method|property(*)" +
44 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
45 " end" +
46 " " +
47 " end ";
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
59 [Test]
60 public void InterceptAllMethods()
62 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
63 " " +
64 " aspect MyAspect for ComplexClass " +
65 " " +
66 " pointcut method(*)" +
67 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
68 " end" +
69 " " +
70 " end ";
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
82 [Test]
83 public void InterceptAllProperties()
85 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
86 " " +
87 " aspect MyAspect for ComplexClass " +
88 " " +
89 " pointcut property(*)" +
90 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
91 " end" +
92 " " +
93 " end ";
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
105 [Test]
106 public void InterceptAllPropertyRead()
108 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
109 " " +
110 " aspect MyAspect for ComplexClass " +
111 " " +
112 " pointcut propertyread(*)" +
113 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
114 " end" +
115 " " +
116 " end ";
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
128 [Test]
129 public void InterceptAllPropertyReadReturningString()
131 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
132 " " +
133 " aspect MyAspect for ComplexClass " +
134 " " +
135 " pointcut propertyread(String)" +
136 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
137 " end" +
138 " " +
139 " end ";
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
151 [Test]
152 public void InterceptAllPropertyWrite()
154 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
155 " " +
156 " aspect MyAspect for ComplexClass " +
157 " " +
158 " pointcut propertyread(*)" +
159 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
160 " end" +
161 " " +
162 " end ";
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
174 [Test]
175 public void InterceptAllMethodsPropertyWrite()
177 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
178 " " +
179 " aspect MyAspect for ComplexClass " +
180 " " +
181 " pointcut method|propertyread(*)" +
182 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
183 " end" +
184 " " +
185 " end ";
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
197 [Test]
198 public void InterceptAllDoSomethingMethods()
200 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
201 " " +
202 " aspect MyAspect for ComplexClass " +
203 " " +
204 " pointcut method(* DoSomething)" +
205 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
206 " end" +
207 " " +
208 " end ";
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 );
219 [Test]
220 public void InterceptAllDoSomethingMethodsWithAnIntArgument()
222 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
223 " " +
224 " aspect MyAspect for ComplexClass " +
225 " " +
226 " pointcut method(* DoSomething(int))" +
227 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
228 " end" +
229 " " +
230 " end ";
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 );
241 [Test]
242 public void InterceptAllDoSomethingMethodsWithAnIntArgumentAndAnyOther()
244 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
245 " " +
246 " aspect MyAspect for ComplexClass " +
247 " " +
248 " pointcut method(* DoSomething(int, *))" +
249 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
250 " end" +
251 " " +
252 " end ";
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 );
263 [Test]
264 public void InterceptAllDoSomethingMethodsWithAnIntAndAnIntArguments()
266 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
267 " " +
268 " aspect MyAspect for ComplexClass " +
269 " " +
270 " pointcut method(* DoSomething(int, int))" +
271 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
272 " end" +
273 " " +
274 " end ";
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 );
285 [Test]
286 public void InterceptAllDoSomethingMethodReturningVoid()
288 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
289 " " +
290 " aspect MyAspect for ComplexClass " +
291 " " +
292 " pointcut method(void DoSomething(*))" +
293 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
294 " end" +
295 " " +
296 " end ";
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 );
307 [Test]
308 public void InterceptAllDoSomethingMethodReturningInt()
310 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
311 " " +
312 " aspect MyAspect for ComplexClass " +
313 " " +
314 " pointcut method(int DoSomething(*))" +
315 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
316 " end" +
317 " " +
318 " end ";
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 );
329 [Test]
330 public void InterceptAllDoSMethods()
332 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
333 " " +
334 " aspect MyAspect for ComplexClass " +
335 " " +
336 " pointcut method(* DoS.*(*))" +
337 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
338 " end" +
339 " " +
340 " end ";
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 );
351 [Test]
352 public void InterceptPropertyName()
354 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
355 " " +
356 " aspect MyAspect for ComplexClass " +
357 " " +
358 " pointcut property(* Na.*)" +
359 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
360 " end" +
361 " " +
362 " end ";
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
374 [Test]
375 public void PointcutsCombined()
377 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
378 " " +
379 " aspect MyAspect for ComplexClass " +
380 " " +
381 " pointcut property(*)" +
382 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
383 " end" +
384 " pointcut method(*)" +
385 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
386 " end" +
387 " " +
388 " end ";
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();
405 int arg = 1;
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 );