Updated castle common build to accomodate framework extension libraries/overrides...
[castle.git] / AspectSharp / AspectSharp.Tests / MixinTests / MixinTestCase.cs
blob5f16fdafe4e7dfa3268fa90544345ac62b1f8ae1
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.MixinTests
17 using System;
19 using NUnit.Framework;
21 using AspectSharp.Builder;
22 using AspectSharp.Tests.Classes;
24 /// <summary>
25 /// Summary description for MixinTestCase.
26 /// </summary>
27 [TestFixture]
28 public class MixinTestCase
30 [Test]
31 public void MakeAuthorAPersonAndACustomer()
33 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
34 " " +
35 " aspect McBrother for Author " +
36 " " +
37 " include DummyCustomer" +
38 " include DummyPerson" +
39 " " +
40 " end ";
42 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
43 AspectEngine engine = builder.Build();
45 Author author = engine.WrapClass(typeof(Author)) as Author;
46 Assert.IsNotNull(author);
47 Assert.IsNotNull(author as ICustomer);
48 Assert.IsNotNull(author as IPerson);
50 ICustomer customer = author as ICustomer;
51 customer.CustomerId = 10;
52 Assert.AreEqual( 10, customer.CustomerId );
54 IPerson person = author as IPerson;
55 person.Name = "Billy Paul McKinsky";
56 Assert.AreEqual( 10, customer.CustomerId );
58 Assert.AreEqual( 0, author.BooksPublished );
59 author.MoreOneBook();
60 Assert.AreEqual( 1, author.BooksPublished );
63 [Test]
64 public void ImplementLoggerProperty()
66 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
67 " " +
68 " aspect McBrother for Author " +
69 " " +
70 " include Loggeable" +
71 " " +
72 " pointcut method|property(*)" +
73 " advice(LogInvocationsInterceptor)" +
74 " end" +
75 " " +
76 " end ";
78 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
79 AspectEngine engine = builder.Build();
81 Author author = engine.WrapClass(typeof(Author)) as Author;
82 Assert.IsNotNull(author);
83 Assert.IsNotNull(author as ILoggeable);
85 Assert.AreEqual( 0, author.BooksPublished );
86 author.MoreOneBook();
87 Assert.AreEqual( 1, author.BooksPublished );
89 ILoggeable log = author as ILoggeable;
90 String messages = log.GetLogMessages();
91 // TODO: Correct compare
92 Assert.AreEqual( "Invoking get_BooksPublished;Invoking MoreOneBook;Invoking get_BooksPublished;", messages );
95 [Test]
96 public void MixinMethodsMustBeIntercepted()
98 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
99 " " +
100 " aspect McBrother for Author " +
101 " " +
102 " include Loggeable" +
103 " include DummyPerson" +
104 " " +
105 " pointcut method|property(*)" +
106 " advice(LogInvocationsInterceptor)" +
107 " end" +
108 " " +
109 " end ";
111 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
112 AspectEngine engine = builder.Build();
114 Author author = engine.WrapClass(typeof(Author)) as Author;
115 Assert.IsNotNull(author);
116 Assert.IsNotNull(author as ILoggeable);
117 Assert.IsNotNull(author as IPerson);
119 IPerson person = author as IPerson;
120 person.Name = "McBilly";
121 Assert.AreEqual( "McBilly", person.Name );
123 ILoggeable log = author as ILoggeable;
124 log.Log("Test");
125 String messages = log.GetLogMessages();
126 Assert.AreEqual( "Invoking set_Name;Invoking get_Name;Test;", messages );
129 [Test]
130 public void MixinProxyAware()
132 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
133 " " +
134 " aspect McBrother for LogEnabledAuthor " +
135 " " +
136 " include LogFactoryMixin" +
137 " " +
138 " end ";
140 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
141 AspectEngine engine = builder.Build();
143 LogEnabledAuthor author = engine.WrapClass(typeof(LogEnabledAuthor)) as LogEnabledAuthor;
144 Assert.IsNotNull(author);
145 Assert.IsNotNull(author as ILogEnabled);
146 Assert.IsNotNull(author.Logger);