/// <summary> /// Determines, whether the given object shall be proxied. /// </summary> protected override bool IsEligibleForProxying(Type targetType, string targetName) { AssertUtils.ArgumentNotNull(_pointcut, "Pointcut"); bool shallProxy = AopUtils.CanApply(_pointcut, targetType, null); return(shallProxy); }
public void CanApplyWithTrueIntroductionAdvisor() { IIntroductionAdvisor mockIntroAdvisor = A.Fake <IIntroductionAdvisor>(); A.CallTo(() => mockIntroAdvisor.TypeFilter).Returns(TrueTypeFilter.True); Assert.IsTrue(AopUtils.CanApply(mockIntroAdvisor, typeof(TestObject), null)); }
public void PointcutAppliesToOneInterfaceOfSeveral() { IPointcut pointcut = new OneInterfaceTestPointcut(); // Will return true if we're proxying interfaces including ITestObject Assert.IsTrue(AopUtils.CanApply(pointcut, typeof(TestObject), new Type[] { typeof(ITestObject), typeof(IComparable) })); // Will return true if we're proxying interfaces including ITestObject Assert.IsFalse(AopUtils.CanApply(pointcut, typeof(TestObject), new Type[] { typeof(IComparable) })); }
public void PointcutAppliesToOneMethodOnObject() { IPointcut pointcut = new OneMethodTestPointcut(); Assert.IsTrue(AopUtils.CanApply(pointcut, typeof(object), null), "Must return true if we're not proxying interfaces."); Assert.IsFalse(AopUtils.CanApply(pointcut, typeof(object), new Type[] { typeof(ITestObject) }), "Must return false if we're proxying interfaces."); }
public void CanApplyWithTrueIntroductionAdvisor() { IIntroductionAdvisor mockIntroAdvisor = (IIntroductionAdvisor)mocks.CreateMock(typeof(IIntroductionAdvisor)); Expect.Call(mockIntroAdvisor.TypeFilter).Return(TrueTypeFilter.True); mocks.ReplayAll(); Assert.IsTrue(AopUtils.CanApply(mockIntroAdvisor, typeof(TestObject), null)); mocks.VerifyAll(); }
/// <summary> /// From the given list of candidate advisors, select the ones that are applicable /// to the given target specified by targetType and name. /// </summary> /// <param name="candidateAdvisors">the list of candidate advisors to date</param> /// <param name="targetType">the target object's type</param> /// <param name="targetName">the target object's name</param> /// <returns>the list of applicable advisors</returns> protected virtual IList <IAdvisor> FindAdvisorsThatCanApply(IList <IAdvisor> candidateAdvisors, Type targetType, string targetName) { if (candidateAdvisors.Count == 0) { return(candidateAdvisors); } List <IAdvisor> eligibleAdvisors = new List <IAdvisor>(); foreach (IAdvisor candidate in candidateAdvisors) { if (candidate is IIntroductionAdvisor && AopUtils.CanApply(candidate, targetType, null)) { if (logger.IsInfoEnabled) { logger.Info(string.Format("Candidate advisor [{0}] accepted for targetType [{1}]", candidate, targetType)); } eligibleAdvisors.Add(candidate); } } bool hasIntroductions = eligibleAdvisors.Count > 0; foreach (IAdvisor candidate in candidateAdvisors) { if (candidate is IIntroductionAdvisor) { continue; } if (AopUtils.CanApply(candidate, targetType, null, hasIntroductions)) { if (logger.IsInfoEnabled) { logger.Info(string.Format("Candidate advisor [{0}] accepted for targetType [{1}]", candidate, targetType)); } eligibleAdvisors.Add(candidate); } else { if (logger.IsInfoEnabled) { logger.Info(string.Format("Candidate advisor [{0}] rejected for targetType [{1}]", candidate, targetType)); } } } return(eligibleAdvisors); }
/// <summary> /// Add PersistenceExceptionTranslationAdvice to candidate object if it is a match. /// Create AOP proxy if necessary or add advice to existing advice chain. /// </summary> /// <param name="instance">The new object instance.</param> /// <param name="objectName">The name of the object.</param> /// <returns> /// The object instance to use, wrapped with either the original or a wrapped one. /// </returns> /// <exception cref="Spring.Objects.ObjectsException"> /// In case of errors. /// </exception> public object PostProcessAfterInitialization(object instance, string objectName) { IAdvised advised = instance as IAdvised; Type targetType; if (advised != null) { targetType = advised.TargetSource.TargetType; } else { targetType = instance.GetType(); } if (targetType == null) { // Can't do much here return(instance); } if (AopUtils.CanApply(this.persistenceExceptionTranslationAdvisor, targetType, ReflectionUtils.GetInterfaces(targetType))) { if (advised != null) { advised.AddAdvisor(this.persistenceExceptionTranslationAdvisor); return(instance); } else { ProxyFactory proxyFactory = new ProxyFactory(instance); // copy our properties inherited from ProxyConfig proxyFactory.CopyFrom(this); proxyFactory.AddAdvisor(this.persistenceExceptionTranslationAdvisor); return(proxyFactory.GetProxy()); } } else { return(instance); } }
public void CanApplyWithAdvisorYieldsTrueIfAdvisorIsNotKnownAdvisorType() { IAdvisor advisor = (IAdvisor)mocks.CreateMock(typeof(IAdvisor)); Assert.IsTrue(AopUtils.CanApply(advisor, typeof(TestObject), null)); }
public void PointcutAlwaysApplies() { Assert.IsTrue(AopUtils.CanApply(new DefaultPointcutAdvisor(new NopInterceptor()), typeof(Object), null)); Assert.IsTrue(AopUtils.CanApply(new DefaultPointcutAdvisor(new NopInterceptor()), typeof(TestObject), new Type[] { typeof(ITestObject) })); }
public void PointcutCanNeverApply() { IPointcut pointcut = new NeverMatchesPointcut(); Assert.IsFalse(AopUtils.CanApply(pointcut, typeof(Object), null)); }
public void CanApplyWithAdvisorYieldsTrueIfAdvisorIsNull() { Assert.IsTrue(AopUtils.CanApply((IAdvisor)null, typeof(TestObject), null)); }
public void CanApplyWithAdvisorYieldsTrueIfAdvisorIsNotKnownAdvisorType() { IAdvisor advisor = A.Fake <IAdvisor>(); Assert.IsTrue(AopUtils.CanApply(advisor, typeof(TestObject), null)); }