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
19 using AspectSharp
.Builder
;
20 using AspectSharp
.Lang
.AST
;
21 using AspectSharp
.Tests
.Classes
;
23 using NUnit
.Framework
;
26 /// Summary description for AspectEngineBuilderTestCase.
29 public class AspectEngineBuilderTestCase
32 public void BuildUsingLanguage()
34 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
36 " \"key\" : DummyInterceptor " +
39 " \"key\" : DummyMixin " +
42 " aspect McBrother for DummyCustomer " +
45 " pointcut method(*)" +
51 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
53 AspectEngine engine
= builder
.Build();
54 AssertEngineConfiguration(engine
);
58 public void BuildUsingLanguageWithDifferingKeys()
60 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
62 " \"interceptor\" : DummyInterceptor " +
65 " \"mixin\" : DummyMixin " +
68 " aspect McBrother for DummyCustomer " +
69 " include \"mixin\"" +
71 " pointcut method(*)" +
72 " advice(\"interceptor\")" +
77 AspectEngineBuilder builder
= new AspectLanguageEngineBuilder(contents
);
79 AspectEngine engine
= builder
.Build();
80 AssertEngineConfiguration(engine
);
84 public void BuildUsingXmlWithLanguageInCData()
86 String xmlContents
= "<configuration>" +
88 " import AspectSharp.Tests.Classes in AspectSharp.Tests " +
90 " \"key\" : DummyInterceptor " +
93 " \"key\" : DummyMixin " +
96 " aspect McBrother for DummyCustomer " +
99 " pointcut method(*)" +
106 XmlEngineBuilder builder
= new XmlEngineBuilder(xmlContents
);
107 AspectEngine engine
= builder
.Build();
108 AssertEngineConfiguration(engine
);
112 public void BuildUsingXmlWithLanguageInConfigurationNode()
114 String xmlContents
= "<configuration>" +
115 " import AspectSharp.Tests.Classes in AspectSharp.Tests " +
117 " \"key\" : DummyInterceptor " +
120 " \"key\" : DummyMixin " +
123 " aspect McBrother for DummyCustomer " +
126 " pointcut method(*)" +
132 XmlEngineBuilder builder
= new XmlEngineBuilder(xmlContents
);
133 AspectEngine engine
= builder
.Build();
134 AssertEngineConfiguration(engine
);
138 public void BuildUsingAppDomainConfiguration()
140 AppDomainConfigurationBuilder builder
= new AppDomainConfigurationBuilder();
141 AspectEngine engine
= builder
.Build();
142 AssertEngineConfiguration(engine
);
146 public void BuildUsingXml()
148 String xmlContents
= "<configuration>" +
149 "<import namespace=\"AspectSharp.Tests.Classes\" assembly=\"AspectSharp.Tests\" />" +
151 "<mixin key=\"key\" type=\"DummyMixin\" refTypeEnum=\"Type\" />" +
152 "</mixins><interceptors>" +
153 "<interceptor key=\"key\" type=\"DummyInterceptor\" refTypeEnum=\"Type\" />" +
155 "<aspect name=\"McBrother\"><for>" +
156 "<singletype type=\"DummyCustomer\" refTypeEnum=\"Type\" />" +
158 "<mixin type=\"key\" refTypeEnum=\"Link\" />" +
159 "<pointcut symbol=\"Method\"><signature>(*)</signature>" +
160 "<interceptor type=\"key\" refTypeEnum=\"Link\" />" +
164 XmlEngineBuilder builder
= new XmlEngineBuilder(xmlContents
);
165 AspectEngine engine
= builder
.Build();
166 AssertEngineConfiguration(engine
);
170 public void XmlWithMoreComplexMethodSignature()
172 String xmlContents
= "<configuration>" +
173 "<import namespace=\"AspectSharp.Tests.Classes\" assembly=\"AspectSharp.Tests\" />" +
175 "<mixin key=\"key\" type=\"DummyMixin\" refTypeEnum=\"Type\" />" +
176 "</mixins><interceptors>" +
177 "<interceptor key=\"key\" type=\"DummyInterceptor\" refTypeEnum=\"Type\" />" +
179 "<aspect name=\"McBrother\"><for>" +
180 "<singletype type=\"DummyCustomer\" refTypeEnum=\"Type\" />" +
182 "<mixin type=\"key\" refTypeEnum=\"Link\" />" +
183 "<pointcut symbol=\"Method\"><signature>(void Name(*))</signature>" +
184 "<interceptor type=\"key\" refTypeEnum=\"Link\" />" +
188 XmlEngineBuilder builder
= new XmlEngineBuilder(xmlContents
);
189 AspectEngine engine
= builder
.Build();
191 Assert
.IsNotNull(engine
);
192 Assert
.IsNotNull(engine
.Configuration
);
194 AspectDefinition aspect
= engine
.Configuration
.Aspects
[0];
195 Assert
.AreEqual(1, aspect
.Mixins
.Count
);
197 PointCutDefinition pointcut
= aspect
.PointCuts
[0];
198 Assert
.IsTrue( pointcut
.Method
.AllArguments
);
199 Assert
.AreEqual( "void", pointcut
.Method
.RetType
);
200 Assert
.AreEqual("Name", pointcut
.Method
.MethodName
);
202 Assert
.AreEqual(1, pointcut
.Advices
.Count
);
203 InterceptorDefinition advice
= pointcut
.Advices
[0];
204 Assert
.AreEqual( typeof(DummyInterceptor
), advice
.TypeReference
.ResolvedType
);
208 public void BuildUsingCode()
210 CodeEngineBuilder builder
= new CodeEngineBuilder();
211 EngineConfiguration conf
= builder
.GetConfiguration();
213 ImportDirective import
= new ImportDirective(LexicalInfo
.Empty
, "AspectSharp.Tests.Classes");
214 import
.AssemblyReference
= new AssemblyReference(LexicalInfo
.Empty
, "AspectSharp.Tests");
215 conf
.Imports
.Add( import
);
217 conf
.Mixins
.Add( "key", LexicalInfo
.Empty
).TypeReference
= new TypeReference(LexicalInfo
.Empty
, "DummyMixin");
218 conf
.Interceptors
.Add( "key", LexicalInfo
.Empty
).TypeReference
= new TypeReference(LexicalInfo
.Empty
, "DummyInterceptor");
220 AspectDefinition aspect
= new AspectDefinition(LexicalInfo
.Empty
, "McBrother");
221 aspect
.TargetType
= new TargetTypeDefinition();
222 aspect
.TargetType
.SingleType
= new TypeReference(LexicalInfo
.Empty
, "DummyCustomer");
223 conf
.Aspects
.Add(aspect
);
225 MixinDefinition mixin
= new MixinDefinition(LexicalInfo
.Empty
);
226 mixin
.TypeReference
= new TypeReference(LexicalInfo
.Empty
, "key", TargetTypeEnum
.Link
);
227 aspect
.Mixins
.Add( mixin
);
229 PointCutDefinition pointcut
= new PointCutDefinition(LexicalInfo
.Empty
, PointCutFlags
.Method
);
230 pointcut
.Method
= AllMethodSignature
.Instance
;
232 InterceptorDefinition interceptor
= new InterceptorDefinition(LexicalInfo
.Empty
);
233 interceptor
.TypeReference
= new TypeReference(LexicalInfo
.Empty
, "key", TargetTypeEnum
.Link
);
234 pointcut
.Advices
.Add(interceptor
);
236 aspect
.PointCuts
.Add(pointcut
);
238 AspectEngine engine
= builder
.Build();
239 AssertEngineConfiguration(engine
);
242 private static void AssertEngineConfiguration(AspectEngine engine
)
244 Assert
.IsNotNull(engine
);
245 Assert
.IsNotNull(engine
.Configuration
);
246 Assert
.AreEqual(1, engine
.Configuration
.Imports
.Count
);
247 Assert
.AreEqual(1, engine
.Configuration
.Mixins
.Count
);
248 Assert
.AreEqual(1, engine
.Configuration
.Interceptors
.Count
);
249 Assert
.AreEqual(1, engine
.Configuration
.Aspects
.Count
);
251 AspectDefinition aspect
= engine
.Configuration
.Aspects
[0];
252 Assert
.AreEqual("McBrother", aspect
.Name
);
253 Assert
.AreEqual( typeof(DummyCustomer
), aspect
.TargetType
.SingleType
.ResolvedType
);
255 Assert
.AreEqual(1, aspect
.Mixins
.Count
);
256 MixinDefinition mixin
= aspect
.Mixins
[0];
257 Assert
.AreEqual( typeof(DummyMixin
), mixin
.TypeReference
.ResolvedType
);
259 Assert
.AreEqual(1, aspect
.PointCuts
.Count
);
260 PointCutDefinition pointcut
= aspect
.PointCuts
[0];
261 Assert
.AreEqual(AllMethodSignature
.Instance
, pointcut
.Method
);
263 Assert
.AreEqual(1, pointcut
.Advices
.Count
);
264 InterceptorDefinition advice
= pointcut
.Advices
[0];
265 Assert
.AreEqual( typeof(DummyInterceptor
), advice
.TypeReference
.ResolvedType
);