Updated castle common build to accomodate framework extension libraries/overrides...
[castle.git] / AspectSharp / AspectSharp.Tests / AspectEngineBuilderTestCase.cs
blob62bac19eb395ce7d65165667779ef67cb0c9a1ab
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
17 using System;
19 using AspectSharp.Builder;
20 using AspectSharp.Lang.AST;
21 using AspectSharp.Tests.Classes;
23 using NUnit.Framework;
25 /// <summary>
26 /// Summary description for AspectEngineBuilderTestCase.
27 /// </summary>
28 [TestFixture]
29 public class AspectEngineBuilderTestCase
31 [Test]
32 public void BuildUsingLanguage()
34 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
35 " interceptors [" +
36 " \"key\" : DummyInterceptor " +
37 " ]" +
38 " mixins [" +
39 " \"key\" : DummyMixin " +
40 " ]" +
41 " " +
42 " aspect McBrother for DummyCustomer " +
43 " include \"key\"" +
44 " " +
45 " pointcut method(*)" +
46 " advice(\"key\")" +
47 " end" +
48 " " +
49 " end ";
51 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
53 AspectEngine engine = builder.Build();
54 AssertEngineConfiguration(engine);
57 [Test]
58 public void BuildUsingLanguageWithDifferingKeys()
60 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
61 " interceptors [" +
62 " \"interceptor\" : DummyInterceptor " +
63 " ]" +
64 " mixins [" +
65 " \"mixin\" : DummyMixin " +
66 " ]" +
67 " " +
68 " aspect McBrother for DummyCustomer " +
69 " include \"mixin\"" +
70 " " +
71 " pointcut method(*)" +
72 " advice(\"interceptor\")" +
73 " end" +
74 " " +
75 " end ";
77 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
79 AspectEngine engine = builder.Build();
80 AssertEngineConfiguration(engine);
83 [Test]
84 public void BuildUsingXmlWithLanguageInCData()
86 String xmlContents = "<configuration>" +
87 " <![CDATA[" +
88 " import AspectSharp.Tests.Classes in AspectSharp.Tests " +
89 " interceptors [" +
90 " \"key\" : DummyInterceptor " +
91 " ]" +
92 " mixins [" +
93 " \"key\" : DummyMixin " +
94 " ]" +
95 " " +
96 " aspect McBrother for DummyCustomer " +
97 " include \"key\"" +
98 " " +
99 " pointcut method(*)" +
100 " advice(\"key\")" +
101 " end" +
102 " " +
103 " end " +
104 " ]]>" +
105 "</configuration>";
106 XmlEngineBuilder builder = new XmlEngineBuilder(xmlContents);
107 AspectEngine engine = builder.Build();
108 AssertEngineConfiguration(engine);
111 [Test]
112 public void BuildUsingXmlWithLanguageInConfigurationNode()
114 String xmlContents = "<configuration>" +
115 " import AspectSharp.Tests.Classes in AspectSharp.Tests " +
116 " interceptors [" +
117 " \"key\" : DummyInterceptor " +
118 " ]" +
119 " mixins [" +
120 " \"key\" : DummyMixin " +
121 " ]" +
122 " " +
123 " aspect McBrother for DummyCustomer " +
124 " include \"key\"" +
125 " " +
126 " pointcut method(*)" +
127 " advice(\"key\")" +
128 " end" +
129 " " +
130 " end " +
131 "</configuration>";
132 XmlEngineBuilder builder = new XmlEngineBuilder(xmlContents);
133 AspectEngine engine = builder.Build();
134 AssertEngineConfiguration(engine);
137 [Test]
138 public void BuildUsingAppDomainConfiguration()
140 AppDomainConfigurationBuilder builder = new AppDomainConfigurationBuilder();
141 AspectEngine engine = builder.Build();
142 AssertEngineConfiguration(engine);
145 [Test]
146 public void BuildUsingXml()
148 String xmlContents = "<configuration>" +
149 "<import namespace=\"AspectSharp.Tests.Classes\" assembly=\"AspectSharp.Tests\" />" +
150 "<mixins>" +
151 "<mixin key=\"key\" type=\"DummyMixin\" refTypeEnum=\"Type\" />" +
152 "</mixins><interceptors>" +
153 "<interceptor key=\"key\" type=\"DummyInterceptor\" refTypeEnum=\"Type\" />" +
154 "</interceptors>" +
155 "<aspect name=\"McBrother\"><for>" +
156 "<singletype type=\"DummyCustomer\" refTypeEnum=\"Type\" />" +
157 "</for>" +
158 "<mixin type=\"key\" refTypeEnum=\"Link\" />" +
159 "<pointcut symbol=\"Method\"><signature>(*)</signature>" +
160 "<interceptor type=\"key\" refTypeEnum=\"Link\" />" +
161 "</pointcut>" +
162 "</aspect>" +
163 "</configuration>";
164 XmlEngineBuilder builder = new XmlEngineBuilder(xmlContents);
165 AspectEngine engine = builder.Build();
166 AssertEngineConfiguration(engine);
169 [Test]
170 public void XmlWithMoreComplexMethodSignature()
172 String xmlContents = "<configuration>" +
173 "<import namespace=\"AspectSharp.Tests.Classes\" assembly=\"AspectSharp.Tests\" />" +
174 "<mixins>" +
175 "<mixin key=\"key\" type=\"DummyMixin\" refTypeEnum=\"Type\" />" +
176 "</mixins><interceptors>" +
177 "<interceptor key=\"key\" type=\"DummyInterceptor\" refTypeEnum=\"Type\" />" +
178 "</interceptors>" +
179 "<aspect name=\"McBrother\"><for>" +
180 "<singletype type=\"DummyCustomer\" refTypeEnum=\"Type\" />" +
181 "</for>" +
182 "<mixin type=\"key\" refTypeEnum=\"Link\" />" +
183 "<pointcut symbol=\"Method\"><signature>(void Name(*))</signature>" +
184 "<interceptor type=\"key\" refTypeEnum=\"Link\" />" +
185 "</pointcut>" +
186 "</aspect>" +
187 "</configuration>";
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 );
207 [Test]
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 );