1 // Copyright 2004-2007 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
.MixinTests
19 using NUnit
.Framework
;
21 using AspectSharp
.Builder
;
22 using AspectSharp
.Tests
.Classes
;
25 /// Summary description for MixinTestCase.
28 public class MixinTestCase
31 public void MakeAuthorAPersonAndACustomer()
33 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
35 " aspect McBrother for Author " +
37 " include DummyCustomer" +
38 " include DummyPerson" +
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
);
60 Assert
.AreEqual( 1, author
.BooksPublished
);
64 public void ImplementLoggerProperty()
66 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
68 " aspect McBrother for Author " +
70 " include Loggeable" +
72 " pointcut method|property(*)" +
73 " advice(LogInvocationsInterceptor)" +
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
);
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
);
96 public void MixinMethodsMustBeIntercepted()
98 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
100 " aspect McBrother for Author " +
102 " include Loggeable" +
103 " include DummyPerson" +
105 " pointcut method|property(*)" +
106 " advice(LogInvocationsInterceptor)" +
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
;
125 String messages
= log
.GetLogMessages();
126 Assert
.AreEqual( "Invoking set_Name;Invoking get_Name;Test;", messages
);
130 public void MixinProxyAware()
132 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
134 " aspect McBrother for LogEnabledAuthor " +
136 " include LogFactoryMixin" +
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
);