private static Contact GetProxy() { Contact target = new Contact(); target.FirstName = "Aleksandar"; target.LastName = "Seovic"; ProxyFactory pf = new ProxyFactory(target); pf.AddAdvisor(new ModificationAdvisor(target.GetType())); pf.AddIntroduction(new IsModifiedAdvisor()); pf.ProxyTargetType = true; return (Contact)pf.GetProxy(); }
public void IndexOfMethods() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); IAdvisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice()); IAdvised advised = (IAdvised)pf.GetProxy(); // Can use advised and ProxyFactory interchangeably advised.AddAdvice(nop); pf.AddAdvisor(advisor); Assert.AreEqual(-1, pf.IndexOf((IInterceptor)null)); Assert.AreEqual(-1, pf.IndexOf(new NopInterceptor())); Assert.AreEqual(0, pf.IndexOf(nop)); Assert.AreEqual(-1, advised.IndexOf((IAdvisor)null)); Assert.AreEqual(1, pf.IndexOf(advisor)); Assert.AreEqual(-1, advised.IndexOf(new DefaultPointcutAdvisor(null))); }
public void CreateProxyFactoryWithoutTargetThenSetTarget() { TestObject target = new TestObject(); target.Name = "Adam"; NopInterceptor nopInterceptor = new NopInterceptor(); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.Target = target; pf.AddAdvice(nopInterceptor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = (ITestObject)proxy; Assert.AreEqual("Adam", to.Name); Assert.AreEqual(1, countingBeforeAdvice.GetCalls()); }
public void RemoveAdvisorByReference() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); IAdvisor advisor = new DefaultPointcutAdvisor(cba); pf.AddAdvice(nop); pf.AddAdvisor(advisor); ITestObject proxied = (ITestObject)pf.GetProxy(); proxied.Age = 5; Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.IsFalse(pf.RemoveAdvisor(null)); Assert.IsTrue(pf.RemoveAdvisor(advisor)); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.IsFalse(pf.RemoveAdvisor(new DefaultPointcutAdvisor(null))); }
public void Matches() { SerializablePerson target = new SerializablePerson(); target.SetAge(27); ControlFlowPointcut cflow = new ControlFlowPointcut(typeof(One), "GetAge"); ProxyFactory factory = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); IPerson proxied = (IPerson) factory.GetProxy(); factory.AddAdvisor(new DefaultPointcutAdvisor(cflow, nop)); // not advised, not under One... Assert.AreEqual(target.GetAge(), proxied.GetAge()); Assert.AreEqual(0, nop.Count, "Whoops, appear to be advising when not under One's cflow."); // will be advised... One one = new One(); Assert.AreEqual(27, one.GetAge(proxied)); Assert.AreEqual(1, nop.Count, "Not advising when under One's cflow (must be)."); // won't be advised... Assert.AreEqual(target.GetAge(), new One().NoMatch(proxied)); Assert.AreEqual(1, nop.Count, "Whoops, appear to be advising when under One's cflow scope, BUT NOT under a target method's cflow scope."); Assert.AreEqual(3, cflow.EvaluationCount, "Pointcut not invoked the correct number of times."); }
public void NestedProxiesDontInvokeSameAdviceOrIntroductionTwice() { MultiProxyingTestClass testObj = new MultiProxyingTestClass(); ProxyFactory pf1 = new ProxyFactory(); pf1.Target = testObj; NopInterceptor di = new NopInterceptor(); NopInterceptor diUnused = new NopInterceptor(1); // // make instance unique (see SPRNET-847) TestCountingIntroduction countingMixin = new TestCountingIntroduction(); pf1.AddAdvice(diUnused); pf1.AddAdvisor(new DefaultPointcutAdvisor(di)); pf1.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin)); object innerProxy = pf1.GetProxy(); ProxyFactory pf2 = new ProxyFactory(); pf2.Target = innerProxy; pf2.AddAdvice(diUnused); pf2.AddAdvisor(new DefaultPointcutAdvisor(di)); pf2.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin)); object outerProxy = pf2.GetProxy(); // any advice instance is invoked once only string result = ((IMultiProxyingTestInterface)outerProxy).TestMethod("arg"); Assert.AreEqual(1, testObj.InvocationCounter); Assert.AreEqual("arg|arg", result); Assert.AreEqual(1, di.Count); // any introduction instance is invoked once only ((ICountingIntroduction)outerProxy).Inc(); Assert.AreEqual(1, countingMixin.Counter); }
public void RemoveAdvisorByIndex() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); IAdvisor advisor = new DefaultPointcutAdvisor(cba); pf.AddAdvice(nop); pf.AddAdvisor(advisor); NopInterceptor nop2 = new NopInterceptor(2); // make instance unique (see SPRNET-847) pf.AddAdvice(nop2); ITestObject proxied = (ITestObject)pf.GetProxy(); proxied.Age = 5; Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.AreEqual(1, nop2.Count); // Removes counting before advisor pf.RemoveAdvisor(1); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(2, nop2.Count); // Removes Nop1 pf.RemoveAdvisor(0); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(3, nop2.Count); // Check out of bounds try { pf.RemoveAdvisor(-1); Assert.Fail("Supposed to throw exception"); } catch (AopConfigException) { // Ok } try { pf.RemoveAdvisor(2); Assert.Fail("Supposed to throw exception"); } catch (AopConfigException) { // Ok } Assert.AreEqual(5, proxied.Age); Assert.AreEqual(4, nop2.Count); }
public void CacheTest() { for (int i = 0; i < 2; i++) { TestObject target = new TestObject(); NopInterceptor nopInterceptor = new NopInterceptor(); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.Target = target; pf.AddAdvice(nopInterceptor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); } // fails when running in resharper/testdriven.net // DynamicProxyManager.SaveAssembly(); }
public void AddAndRemoveEventHandlerThroughInterceptor() { TestObject target = new TestObject(); NopInterceptor nopInterceptor = new NopInterceptor(); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); target.Name = "SOME-NAME"; ProxyFactory pf = new ProxyFactory(target); pf.AddAdvice(nopInterceptor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = proxy as ITestObject; // add event handler through proxy to.Click += new EventHandler(OnClick); OnClickWasCalled = false; to.OnClick(); Assert.IsTrue(OnClickWasCalled); Assert.AreEqual(2, countingBeforeAdvice.GetCalls()); // remove event handler through proxy to.Click -= new EventHandler(OnClick); OnClickWasCalled = false; to.OnClick(); Assert.IsFalse(OnClickWasCalled); Assert.AreEqual(4, countingBeforeAdvice.GetCalls()); }
public void AddAndRemoveEventHandlerThroughIntroduction() { TestObject target = new TestObject(); DoubleClickableIntroduction dci = new DoubleClickableIntroduction(); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(dci); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); target.Name = "SOME-NAME"; ProxyFactory pf = new ProxyFactory(target); pf.AddIntroduction(advisor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = proxy as ITestObject; Assert.IsNotNull(to); Assert.AreEqual("SOME-NAME", to.Name); IDoubleClickable doubleClickable = proxy as IDoubleClickable; // add event handler through introduction doubleClickable.DoubleClick += new EventHandler(OnClick); OnClickWasCalled = false; doubleClickable.FireDoubleClickEvent(); Assert.IsTrue(OnClickWasCalled); Assert.AreEqual(3, countingBeforeAdvice.GetCalls()); // remove event handler through introduction doubleClickable.DoubleClick -= new EventHandler(OnClick); OnClickWasCalled = false; doubleClickable.FireDoubleClickEvent(); Assert.IsFalse(OnClickWasCalled); Assert.AreEqual(5, countingBeforeAdvice.GetCalls()); }
/// <summary> /// Create an AOP proxy for the given object. /// </summary> /// <param name="objectType">Type of the object.</param> /// <param name="objectName">The name of the object.</param> /// <returns>The AOP Proxy for the object.</returns> protected virtual ProxyFactory CreateProxyFactory(Type objectType, string objectName) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.ProxyTargetAttributes = this.ProxyTargetAttributes; proxyFactory.TargetSource = new InheritanceBasedAopTargetSource(objectType); if (!ProxyInterfaces) { proxyFactory.Interfaces = Type.EmptyTypes; } IAdvisor[] advisors = ResolveInterceptorNames(); foreach (IAdvisor advisor in advisors) { if (advisor is IIntroductionAdvisor) { proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor); } else { proxyFactory.AddAdvisor(advisor); } } return proxyFactory; }
public void ReplaceAdvisor() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba1 = new CountingBeforeAdvice(); CountingBeforeAdvice cba2 = new CountingBeforeAdvice(); IAdvisor advisor1 = new DefaultPointcutAdvisor(cba1); IAdvisor advisor2 = new DefaultPointcutAdvisor(cba2); pf.AddAdvisor(advisor1); pf.AddAdvice(nop); ITestObject proxied = (ITestObject)pf.GetProxy(); // Use the type cast feature // Replace etc methods on advised should be same as on ProxyFactory IAdvised advised = (IAdvised)proxied; proxied.Age = 5; Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(0, cba2.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.IsFalse(advised.ReplaceAdvisor(null, null)); Assert.IsFalse(advised.ReplaceAdvisor(null, advisor2)); Assert.IsFalse(advised.ReplaceAdvisor(advisor1, null)); Assert.IsTrue(advised.ReplaceAdvisor(advisor1, advisor2)); Assert.AreEqual(advisor2, pf.Advisors[0]); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(1, cba2.GetCalls()); Assert.IsFalse(pf.ReplaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)); }
public void SelectiveApplication() { SerializablePerson target = new SerializablePerson(); target.SetAge(27); NopInterceptor nop = new NopInterceptor(); ControlFlowPointcut cflow = new ControlFlowPointcut(typeof (One)); IPointcut settersUnderOne = Pointcuts.Intersection(SetterPointcut.Instance, cflow); ProxyFactory pf = new ProxyFactory(target); IPerson proxied = (IPerson) pf.GetProxy(); pf.AddAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop)); // Not advised, not under One target.SetAge(16); Assert.AreEqual(0, nop.Count); // Not advised; under One but not a setter Assert.AreEqual(16, new One().GetAge(proxied)); Assert.AreEqual(0, nop.Count); // Won't be advised new One().Set(proxied); Assert.AreEqual(1, nop.Count); // We saved most evaluations Assert.AreEqual(1, cflow.EvaluationCount); }
protected virtual void AddPersistenceExceptionTranslation(ProxyFactory pf, IPersistenceExceptionTranslator pet) { pf.AddAdvisor(new PersistenceExceptionTranslationAdvisor(pet, typeof(RepositoryAttribute))); }
/// <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; } }
/// <summary> /// Method run after all the properties have been set for this object. /// Responsible for actual proxy creation. /// </summary> public void AfterPropertiesSet() { _transactionInterceptor.AfterPropertiesSet(); if ( _target == null ) { throw new ArgumentException("'target' is required."); } ProxyFactory proxyFactory = new ProxyFactory(); if ( _preInterceptors != null ) { for ( int i = 0; i < _preInterceptors.Length; i++ ) { proxyFactory.AddAdvisor(_advisorAdapterRegistry.Wrap(_preInterceptors[i])); } } if ( _pointcut != null ) { IAdvisor advice = new DefaultPointcutAdvisor(_pointcut, _transactionInterceptor); proxyFactory.AddAdvisor(advice); } else { proxyFactory.AddAdvisor( new TransactionAttributeSourceAdvisor( _transactionInterceptor ) ); } if ( _postInterceptors != null ) { for ( int i = 0; i < _postInterceptors.Length; i++ ) { proxyFactory.AddAdvisor(_advisorAdapterRegistry.Wrap(_postInterceptors[i])); } } proxyFactory.CopyFrom(this); proxyFactory.TargetSource = createTargetSource(_target); if ( _proxyInterfaces != null ) { proxyFactory.Interfaces = _proxyInterfaces; } else if ( !ProxyTargetType ) { if ( _target is ITargetSource ) { throw new AopConfigException("Either 'ProxyInterfaces' or 'ProxyTargetType' is required " + "when using an ITargetSource as 'target'"); } proxyFactory.Interfaces = AopUtils.GetAllInterfaces(_target); } _proxy = proxyFactory.GetProxy(); }
/// <summary> /// Initialize the proxy. /// </summary> private void InitializeProxy() { if (this.adviceChain.Length == 0) { return; } var factory = new ProxyFactory(); foreach (var advice in this.adviceChain) { factory.AddAdvisor(new DefaultPointcutAdvisor(TruePointcut.True, advice)); } factory.ProxyTargetType = false; factory.AddInterface(typeof(IContainerDelegate)); // TODO: Is this really the right target??? Doesn't seem right. factory.Target = this; this.proxy = (IContainerDelegate)factory.GetProxy(); }