How to use ProxyTypeInfo class of Telerik.JustMock.Core package

Best JustMockLite code snippet using Telerik.JustMock.Core.ProxyTypeInfo

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

ProxyTypeInfo.cs

Source: ProxyTypeInfo.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015,2108 Progress Software Corporation
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9	 http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16*/
17
18using System;
19using System.Collections.Generic;
20
21namespace Telerik.JustMock.Core
22{
23    internal class ProxyTypeInfo
24	{
25#if !PORTABLE
26        public Type ProxyType;
27#endif
28		public Dictionary<Type, object> Mixins = new Dictionary<Type, object>();
29	}
30}
31
Full Screen

DynamicProxyMockFactory.cs

Source: DynamicProxyMockFactory.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015,2018 Progress Software Corporation
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16*/
17
18using System;
19using System.Collections.Generic;
20using System.Linq;
21using System.Linq.Expressions;
22using System.Reflection;
23using System.Text;
24using Telerik.JustMock.Core.Behaviors;
25using Telerik.JustMock.Core.Castle.DynamicProxy;
26using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
27using Telerik.JustMock.Core.Expressions;
28
29namespace Telerik.JustMock.Core
30{
31	internal class DynamicProxyMockFactory : IMockFactory
32	{
33		private static readonly ProxyGenerator generator;
34
35		static DynamicProxyMockFactory()
36		{
37#if DEBUG
38			generator = new ProxyGenerator(new DefaultProxyBuilder(new ModuleScope(savePhysicalAssembly: true)));
39#else
40			generator = new ProxyGenerator();
41#endif
42		}
43
44#if (DEBUG && !SILVERLIGHT && !NETCORE)
45		internal static void SaveAssembly()
46		{
47			generator.ProxyBuilder.ModuleScope.SaveAssembly();
48		}
49#endif
50
51		public bool IsAccessible(Type type)
52		{
53            return ProxyUtil.IsAccessibleType(type);
54        }
55
56		public object Create(Type type, MocksRepository repository, IMockMixin mockMixinImpl, MockCreationSettings settings, bool createTransparentProxy)
57		{
58			var options = new ProxyGenerationOptions();
59			options.AddMixinInstance(mockMixinImpl);
60			foreach (var mixin in settings.Mixins)
61				options.AddMixinInstance(mixin);
62
63			if (settings.AdditionalProxyTypeAttributes != null)
64			{
65				foreach (var attributeBuilder in settings.AdditionalProxyTypeAttributes)
66				{
67					options.AdditionalAttributes.Add(new CustomAttributeInfo(attributeBuilder));
68				}
69			}
70
71			var interceptor = repository.Interceptor;
72#if (SILVERLIGHT)
73			options.Hook = new ProxyGenerationHook(false, settings.InterceptorFilter);
74#else
75			options.Hook = new ProxyGenerationHook(settings.MockConstructorCall, settings.InterceptorFilter);
76#endif
77
78			object instance = null;
79			Exception proxyFailure = null;
80
81			if (type.IsInterface)
82			{
83				if (settings.Args != null && settings.Args.Length > 0)
84					throw new ArgumentException("Do not supply contructor arguments when mocking an interface or delegate.");
85				try
86				{
87					instance = generator.CreateInterfaceProxyWithoutTarget(type, settings.AdditionalMockedInterfaces, options, interceptor);
88				}
89				catch (TypeLoadException ex)
90				{
91					proxyFailure = ex;
92				}
93				catch (GeneratorException ex)
94				{
95					proxyFailure = ex;
96				}
97			}
98			else
99			{
100				try
101				{
102#if (SILVERLIGHT || NETCORE)
103					if (settings.Args == null || settings.Args.Length == 0)
104					{
105						ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
106
107						if (!constructors.Any(constr => constr.GetParameters().Length == 0))
108						{
109							var constructorToCall = constructors.FirstOrDefault();
110							if (constructorToCall != null)
111							{
112								var @params = constructorToCall.GetParameters();
113								settings.Args = new object[@params.Length];
114
115								for (int i = 0; i < @params.Length; ++i)
116								{
117									var p = @params[i];
118									settings.Args[i] = Convert.IsDBNull(p.DefaultValue)
119										? p.ParameterType.GetDefaultValue()
120										: p.DefaultValue;
121								}
122							}
123						}
124					}
125#endif
126					instance = generator.CreateClassProxy(type, settings.AdditionalMockedInterfaces, options, settings.Args, interceptor);
127				}
128				catch (TypeLoadException ex)
129				{
130					proxyFailure = ex;
131				}
132				catch (GeneratorException ex)
133				{
134					proxyFailure = ex;
135				}
136				catch (InvalidProxyConstructorArgumentsException ex)
137				{
138					proxyFailure = ex;
139					if (!settings.MockConstructorCall)
140						throw new MockException(ex.Message);
141				}
142			}
143			if (proxyFailure != null)
144			{
145				throw new ProxyFailureException(proxyFailure);
146			}
147			return instance;
148		}
149
150		public Type CreateDelegateBackend(Type delegateType)
151		{
152			var moduleScope = generator.ProxyBuilder.ModuleScope;
153			var moduleBuilder = moduleScope.ObtainDynamicModuleWithStrongName();
154
155			var targetIntfName =
156				"Castle.Proxies.Delegates." +
157				delegateType.ToString()
158				.Replace('.', '_')
159				.Replace(',', '`')
160				.Replace("+", "__")
161				.Replace("[", "``")
162				.Replace("]", "``");
163
164			var typeName = moduleScope.NamingScope.GetUniqueName(targetIntfName);
165			var typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Interface);
166
167			var delegateInvoke = delegateType.GetMethod("Invoke");
168			typeBuilder.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
169				delegateInvoke.ReturnType, delegateInvoke.GetParameters().Select(p => p.ParameterType).ToArray());
170
171			return typeBuilder.CreateType();
172		}
173
174		public IMockMixin CreateExternalMockMixin(IMockMixin mockMixin, IEnumerable<object> mixins)
175		{
176			var options = new ProxyGenerationOptions();
177			options.AddMixinInstance(mockMixin);
178			foreach (var mixin in mixins)
179				options.AddMixinInstance(mixin);
180
181			var compoundMockMixin = (IMockMixin)generator.CreateClassProxy(typeof(MocksRepository.ExternalMockMixin), options);
182			return compoundMockMixin;
183		}
184
185		public ProxyTypeInfo CreateClassProxyType(Type classToProxy, MocksRepository repository, MockCreationSettings settings, MockMixin mockMixinImpl)
186		{
187			var pgo = CreateProxyGenerationOptions(classToProxy, settings, mockMixinImpl);
188			var typeInfo = new ProxyTypeInfo
189			{
190				ProxyType = generator.ProxyBuilder.CreateClassProxyType(classToProxy, Type.EmptyTypes, pgo)
191			};
192			typeInfo.Mixins.Add(typeof(IInterceptor), repository.Interceptor);
193			foreach (var mixin in pgo.MixinData.MixinInterfaces)
194			{
195				typeInfo.Mixins.Add(mixin, pgo.MixinData.GetMixinInstance(mixin));
196			}
197			return typeInfo;
198		}
199
200		private ProxyGenerationOptions CreateProxyGenerationOptions(Type type, MockCreationSettings settings, MockMixin mockMixinImpl = null)
201		{
202			var options = new ProxyGenerationOptions();
203			if (mockMixinImpl != null)
204				options.AddMixinInstance(mockMixinImpl);
205			foreach (var mixin in settings.Mixins)
206				options.AddMixinInstance(mixin);
207
208			if (settings.AdditionalProxyTypeAttributes != null)
209			{
210				foreach (var attributeBuilder in settings.AdditionalProxyTypeAttributes)
211				{
212					options.AdditionalAttributes.Add(new CustomAttributeInfo(attributeBuilder));
213				}
214			}
215
216			return options;
217		}
218
219		private class ProxyGenerationHook : IProxyGenerationHook, IConstructorGenerationHook
220		{
221			private readonly bool myMockConstructors;
222			private readonly Expression<Predicate<MethodInfo>> myInterceptorFilter;
223			private readonly Predicate<MethodInfo> myInterceptorFilterImpl;
224
225			public ProxyGenerationHook(bool mockConstructors, Expression<Predicate<MethodInfo>> interceptorFilter)
226			{
227				myMockConstructors = mockConstructors;
228				if (interceptorFilter != null)
229				{
230					myInterceptorFilter = interceptorFilter;
231					myInterceptorFilterImpl = myInterceptorFilter.Compile();
232				}
233			}
234
235			public void MethodsInspected()
236			{
237			}
238
239			public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo)
240			{
241			}
242
243            public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo)
244            {
245                if (Attribute.IsDefined(methodInfo.DeclaringType, typeof(MixinAttribute)))
246                {
247                    return false;
248                }
249
250                bool profilerCannotIntercept = methodInfo.IsAbstract || methodInfo.IsExtern() || !ProfilerInterceptor.TypeSupportsInstrumentation(methodInfo.DeclaringType);
251
252                if (ProfilerInterceptor.IsProfilerAttached && !profilerCannotIntercept)
253                {
254                    bool isDefaultMethodImplementation = !methodInfo.IsAbstract && methodInfo.DeclaringType.IsInterface;
255                    if (type == methodInfo.DeclaringType && !isDefaultMethodImplementation)
256                    {
257                        return false;
258                    }
259                }
260
261				return myInterceptorFilterImpl != null ? myInterceptorFilterImpl(methodInfo) : true;
262			}
263
264			public ProxyConstructorImplementation GetConstructorImplementation(ConstructorInfo constructorInfo, ConstructorImplementationAnalysis analysis)
265			{
266				return myMockConstructors ? ProxyConstructorImplementation.DoNotCallBase
267					: analysis.IsBaseVisible ? ProxyConstructorImplementation.CallBase
268					: ProxyConstructorImplementation.SkipConstructor;
269			}
270
271			public ProxyConstructorImplementation DefaultConstructorImplementation
272			{
273				get
274				{
275#if (SILVERLIGHT || NETCORE)
276					return ProxyConstructorImplementation.SkipConstructor;
277#else
278					return myMockConstructors ? ProxyConstructorImplementation.DoNotCallBase : ProxyConstructorImplementation.SkipConstructor;
279#endif
280				}
281			}
282
283			#region Equality members
284
285			public override bool Equals(object obj)
286			{
287				if (ReferenceEquals(null, obj)) return false;
288				if (ReferenceEquals(this, obj)) return true;
289				if (obj.GetType() != this.GetType()) return false;
290
291				var other = (ProxyGenerationHook)obj;
292				return this.myMockConstructors == other.myMockConstructors
293					&& ((this.myInterceptorFilter == null && other.myInterceptorFilter == null)
294						|| ExpressionComparer.AreEqual(this.myInterceptorFilter, other.myInterceptorFilter));
295			}
296
297			public override int GetHashCode()
298			{
299				return this.myMockConstructors.GetHashCode();
300			}
301
302			#endregion
303		}
304	}
305}
306
Full Screen

IMockFactory.cs

Source: IMockFactory.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015 Progress Software Corporation
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16*/
17
18using System;
19using System.Collections.Generic;
20using System.Linq;
21using System.Text;
22
23namespace Telerik.JustMock.Core
24{
25	internal interface IMockFactory
26	{
27		bool IsAccessible(Type type);
28		object Create(Type type, MocksRepository repository, IMockMixin mockMixinImpl, MockCreationSettings settings, bool createTransparentProxy);
29		Type CreateDelegateBackend(Type delegateType);
30		IMockMixin CreateExternalMockMixin(IMockMixin mockMixin, IEnumerable<object> mixins);
31		ProxyTypeInfo CreateClassProxyType(Type classToProxy, MocksRepository repository, MockCreationSettings settings, MockMixin mockMixinImpl);
32	}
33}
34
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in ProxyTypeInfo

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)