public void CoordinatorDeclarativeWithAttributes() { ITestCoord coord = ctx["testCoordinator"] as ITestCoord; Assert.IsNotNull(coord); CallCountingTransactionManager ccm = ctx["transactionManager"] as CallCountingTransactionManager; Assert.IsNotNull(ccm); LoggingAroundAdvice advice = (LoggingAroundAdvice)ctx["consoleLoggingAroundAdvice"]; Assert.IsNotNull(advice); ITestObjectMgr testObjectMgr = ctx["testObjectManager"] as ITestObjectMgr; Assert.IsNotNull(testObjectMgr); //Proxied due to NameMatchMethodPointcutAdvisor Assert.IsTrue(AopUtils.IsAopProxy(coord)); //Proxied due to DefaultAdvisorAutoProxyCreator Assert.IsTrue(AopUtils.IsAopProxy(testObjectMgr)); TestObject to1 = new TestObject("Jack", 7); TestObject to2 = new TestObject("Jill", 6); Assert.AreEqual(0, ccm.begun); Assert.AreEqual(0, ccm.commits); Assert.AreEqual(0, advice.numInvoked); coord.WorkOn(to1, to2); Assert.AreEqual(1, ccm.begun); Assert.AreEqual(1, ccm.commits); Assert.AreEqual(1, advice.numInvoked); }
public void ProxyTransparentProxy() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues(); ctorArgs.AddNamedArgumentValue("objectType", typeof(ITestObject)); of.RegisterObjectDefinition("bar", new RootObjectDefinition(typeof(TransparentProxyFactory), ctorArgs, null)); TestAutoProxyCreator apc = new TestAutoProxyCreator(of); of.AddObjectPostProcessor(apc); ITestObject o = of.GetObject("bar") as ITestObject; Assert.IsTrue(AopUtils.IsAopProxy(o)); // ensure interceptors get called o.Foo(); Assert.AreEqual(1, apc.NopInterceptor.Count); IAdvised advised = (IAdvised)o; // ensure target was called object target = advised.TargetSource.GetTarget(); Assert.AreEqual(1, TransparentProxyFactory.GetRealProxy(target).Count); }
public void HasProxy() { IObjectFactory of = ObjectFactory; object o = of.GetObject("testObject"); Assert.IsTrue(AopUtils.IsAopProxy(o), "Expected TestObject to be proxied"); }
public void GetSaoWithSingletonModeAndAop() { IApplicationContext ctx = new XmlApplicationContext("assembly://Spring.Services.Tests/Spring.Data.Spring.Remoting/saoSingleton-aop.xml"); ContextRegistry.RegisterContext(ctx); //object saoFactory = ctx.GetObject("&remoteCounter"); //Assert.IsNotNull(saoFactory); object obj = ctx.GetObject("remoteCounter"); Assert.IsNotNull(obj, "Object is null even though a object has been exported."); Assert.IsTrue(AopUtils.IsAopProxy(obj)); Assert.IsTrue((obj is ISimpleCounter), "Object should implement 'ISimpleCounter' interface."); MethodCounter aopCounter = ctx.GetObject("countingBeforeAdvice") as MethodCounter; Assert.IsNotNull(aopCounter); int aopCount = aopCounter.GetCalls("Count"); Assert.AreEqual(0, aopCount); ISimpleCounter sc = (ISimpleCounter)obj; Assert.AreEqual(1, sc.Counter, "Remote object hasn't been activated by the server."); sc.Count(); Assert.AreEqual(2, sc.Counter, "Remote object doesn't work in a 'Singleton' mode."); Assert.AreEqual(1, aopCounter.GetCalls("Count")); }
public void NoProxy() { IObjectFactory of = ObjectFactory; object o = of.GetObject("noSetterPropertiesObject"); Assert.IsFalse(AopUtils.IsAopProxy(o)); }
public void ProxyDataAccessAndServiceLayer() { Assert.IsFalse(AopUtils.IsAopProxy(ctx["DbProvider"])); Assert.IsFalse(AopUtils.IsAopProxy(ctx["SessionFactory"])); Assert.IsFalse(AopUtils.IsAopProxy(ctx["hibernateTransactionManager"])); Assert.IsFalse(AopUtils.IsAopProxy(ctx["transactionManager"])); //Assert.IsTrue(AopUtils.IsAopProxy(ctx["testObjectDaoTransProxy"])); Assert.IsTrue(AopUtils.IsAopProxy(ctx["TestObjectDao"])); Assert.IsTrue(AopUtils.IsAopProxy(ctx["SimpleService"])); CallCountingTransactionManager ccm = ctx["transactionManager"] as CallCountingTransactionManager; Assert.IsNotNull(ccm); Assert.AreEqual(0, ccm.begun); Assert.AreEqual(0, ccm.commits); LoggingAroundAdvice caa = ctx["loggingAroundAdvice"] as LoggingAroundAdvice; Assert.IsNotNull(caa); Assert.AreEqual(0, caa.numInvoked); ISimpleService simpleService = ctx["SimpleService"] as ISimpleService; Assert.IsNotNull(simpleService); simpleService.DoWork(new TestObject()); Assert.AreEqual(1, ccm.begun); Assert.AreEqual(1, ccm.commits); Assert.AreEqual(1, caa.numInvoked); }
public void NoProxy() { ITestObject testObject = (ITestObject)ctx.GetObject("noproxy"); Assert.IsFalse(AopUtils.IsAopProxy(testObject), testObject + " is not an AOP proxy"); Assert.AreEqual("noproxy", testObject.Name); }
protected override object GetTargetObject(object obj) { var target = obj; return(AopUtils.IsAopProxy(target) ? ((IAdvised)target).TargetSource.GetTarget() : target); }
public void DoesNotCreateProxyIfNoPointcutMatch() { PointcutFilteringAutoProxyCreator apc = new PointcutFilteringAutoProxyCreator(); apc.Pointcut = new SdkRegularExpressionMethodPointcut(".*\\.DOEsNOTExist"); object result = apc.PostProcessAfterInitialization(new TestObject(), "testObject"); Assert.IsFalse(AopUtils.IsAopProxy(result)); }
public void CreatesProxyOnPointcutMatch() { PointcutFilteringAutoProxyCreator apc = new PointcutFilteringAutoProxyCreator(); apc.Pointcut = new SdkRegularExpressionMethodPointcut(".*\\.GetHashCode"); object result = apc.PostProcessAfterInitialization(new TestObject(), "testObject"); Assert.IsTrue(AopUtils.IsAopProxy(result)); }
public void DoesNotCreateProxyOnInheritedAttributeMatchWhenNotCheckInherited() { AttributeAutoProxyCreator apc = new AttributeAutoProxyCreator(); apc.AttributeTypes = new Type[] { typeof(ApcTestAttribute) }; apc.CheckInherited = false; object result = apc.PostProcessAfterInitialization(new DerivedAttributedApcTestObject(), "testObject"); Assert.IsFalse(AopUtils.IsAopProxy(result)); }
public void DoesNotCreateProxyIfEmptyTypeNameList() { TypeNameAutoProxyCreator apc = new TypeNameAutoProxyCreator(); apc.TypeNames = new string[] {}; object result = apc.PostProcessAfterInitialization(new TestObject(), "testObject"); Assert.IsFalse(AopUtils.IsAopProxy(result)); }
public void DoesNotCreateProxyIfEmptyAtributeList() { AttributeAutoProxyCreator apc = new AttributeAutoProxyCreator(); apc.AttributeTypes = new Type[0]; object result = apc.PostProcessAfterInitialization(new ApcTestObject(), "testObject"); Assert.IsFalse(AopUtils.IsAopProxy(result)); }
public void CreatesProxyOnAttributeMatch() { AttributeAutoProxyCreator apc = new AttributeAutoProxyCreator(); apc.AttributeTypes = new Type[] { typeof(ApcTestAttribute) }; object result = apc.PostProcessAfterInitialization(new AttributedApcTestObject(), "testObject"); Assert.IsTrue(AopUtils.IsAopProxy(result)); }
public void CreatesProxyOnTypeNameMatch() { TypeNameAutoProxyCreator apc = new TypeNameAutoProxyCreator(); apc.TypeNames = new string[] { "Spring.Objects.Test*", "*MyLocal*" }; object result = apc.PostProcessAfterInitialization(new TestObject(), "testObject"); Assert.IsTrue(AopUtils.IsAopProxy(result)); result = apc.PostProcessAfterInitialization(new MyLocalTestObject(), "myLocalTestObject"); Assert.IsTrue(AopUtils.IsAopProxy(result)); }
public void DoesNotCheckMethodLevelAttributes() { AttributeAutoProxyCreator apc = new AttributeAutoProxyCreator(); apc.AttributeTypes = new Type[] { typeof(ApcTestAttribute) }; apc.CheckInherited = false; // (!) // does not check method level attributes! object result = apc.PostProcessAfterInitialization(new DerivedAttributedApcTestObject(), "testObject"); Assert.IsFalse(AopUtils.IsAopProxy(result)); }
public void IsDecoratorProxy() { var pf = new ProxyFactory(new DebugAdvice()); pf.Target = _target; pf.ProxyTargetType = true; var proxy = (TestObject)pf.GetProxy(); Assert.True(AopUtils.IsDecoratorAopProxy(proxy)); Assert.True(AopUtils.IsAopProxy(proxy)); }
public void IsCompositionProxy() { var pf = new ProxyFactory(typeof(ITestObject), new DebugAdvice()); pf.Target = _target; Assert.False(pf.ProxyTargetType); var proxy = (ITestObject)pf.GetProxy(); Assert.True(AopUtils.IsCompositionAopProxy(proxy)); Assert.True(AopUtils.IsAopProxy(proxy)); Assert.IsNotInstanceOf <TestObject>(proxy); }
public void IsInheritanceBasedProxy() { using (var ctx = new XmlApplicationContext(ReadOnlyXmlTestResource.GetFilePath("IsAopProxyTests.xml", this.GetType()))) { var proxy = (TestObject)ctx["michael"]; Assert.AreEqual("Michael", proxy.Name); Assert.True(AopUtils.IsInheritanceAopProxyType(proxy.GetType())); Assert.True(AopUtils.IsAopProxyType(proxy.GetType())); Assert.True(AopUtils.IsInheritanceAopProxy(proxy)); Assert.True(AopUtils.IsAopProxy(proxy)); } }
public void ProxyObjectWithoutInterface() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); of.RegisterObjectDefinition("bar", new RootObjectDefinition(typeof(ObjectWithoutInterface))); TestAutoProxyCreator apc = new TestAutoProxyCreator(of); of.AddObjectPostProcessor(apc); ObjectWithoutInterface o = of.GetObject("bar") as ObjectWithoutInterface; Assert.IsTrue(AopUtils.IsAopProxy(o)); o.Foo(); Assert.AreEqual(1, apc.NopInterceptor.Count); }
public void ProxiesCorrectly() { GenericApplicationContext gac = new GenericApplicationContext(); gac.RegisterObjectDefinition("translator", new RootObjectDefinition(typeof(PersistenceExceptionTranslationPostProcessor))); gac.RegisterObjectDefinition("notProxied", new RootObjectDefinition(typeof(RepositoryInterfaceImpl))); gac.RegisterObjectDefinition("proxied", new RootObjectDefinition(typeof(StereotypedRepositoryInterfaceImpl))); gac.RegisterObjectDefinition("chainedTranslator", new RootObjectDefinition(typeof(ChainedPersistenceExceptionTranslator))); gac.Refresh(); IRepositoryInterface shouldNotBeProxied = (IRepositoryInterface)gac.GetObject("notProxied"); Assert.IsFalse(AopUtils.IsAopProxy(shouldNotBeProxied)); IRepositoryInterface shouldBeProxied = (IRepositoryInterface)gac.GetObject("proxied"); Assert.IsTrue(AopUtils.IsAopProxy(shouldBeProxied)); CheckWillTranslateExceptions(shouldBeProxied); }
public void Registered() { Assert.IsNotNull(NamespaceParserRegistry.GetParser("http://www.springframework.net/aop")); IPointcut pointcut = ctx["getDescriptionCalls"] as IPointcut; Assert.IsNotNull(pointcut); Assert.IsFalse(AopUtils.IsAopProxy(pointcut)); ITestObject testObject = ctx["testObject"] as ITestObject; Assert.IsNotNull(testObject); Assert.IsTrue(AopUtils.IsAopProxy(testObject), "Object should be an AOP proxy"); IAdvised advised = testObject as IAdvised; Assert.IsNotNull(advised); IAdvisor[] advisors = advised.Advisors; Assert.IsTrue(advisors.Length > 0, "Advisors should not be empty"); }
public void TestAutoProxyCreation() { XmlApplicationContext context = new XmlApplicationContext(ReadOnlyXmlTestResource.GetFilePath("advisorAutoProxyCreatorCircularReferencesTests.xml", typeof(AdvisorAutoProxyCreatorCircularReferencesTests))); CountingAfterReturningAdvisor countingAdvisor = (CountingAfterReturningAdvisor)context.GetObject("testAdvisor"); // direct deps of AutoProxyCreator are not eligable for proxying Assert.IsFalse(AopUtils.IsAopProxy(context.GetObject("aapc"))); Assert.IsFalse(AopUtils.IsAopProxy(countingAdvisor)); TestObjectFactoryObject testObjectFactory = (TestObjectFactoryObject)context.GetObject("&testObjectFactory"); Assert.IsFalse(AopUtils.IsAopProxy(testObjectFactory)); Assert.IsFalse(AopUtils.IsAopProxy(context.GetObject("someOtherObject"))); // this one is completely independent Assert.IsTrue(AopUtils.IsAopProxy(context.GetObject("independentObject"))); // Asserts SPRNET-1225 - advisor dependencies most not be auto-proxied object testObject = context.GetObject("testObjectFactory"); Assert.IsFalse(AopUtils.IsAopProxy(testObject)); // Asserts SPRNET-1224 - factory product most be cached context.GetObject("testObjectFactory"); testObjectFactory.GetObjectCounter = 0; context.GetObject("testObjectFactory"); Assert.AreEqual(0, testObjectFactory.GetObjectCounter); ICloneable someOtherObject = (ICloneable)context.GetObject("someOtherObject"); someOtherObject.Clone(); ICloneable independentObject = (ICloneable)context.GetObject("independentObject"); independentObject.Clone(); Assert.AreEqual(1, countingAdvisor.GetCalls()); }
public void CanProxyFactoryMethodProducts() { GenericApplicationContext ctx = new GenericApplicationContext(); ctx.ObjectFactory.AddObjectPostProcessor(new DefaultAdvisorAutoProxyCreator()); CapturingAdvice capturingAdvice = new CapturingAdvice(); ctx.ObjectFactory.RegisterSingleton("logging", new DefaultPointcutAdvisor(TruePointcut.True, capturingAdvice)); // register "factory" object RootObjectDefinition rod; rod = new RootObjectDefinition(typeof(TestObjectFactoryObject)); ctx.ObjectFactory.RegisterObjectDefinition("test", rod); // register product, referencing the factory object rod = new RootObjectDefinition(typeof(ITestObject)); rod.FactoryObjectName = "test"; rod.FactoryMethodName = "CreateTestObject"; ctx.ObjectFactory.RegisterObjectDefinition("testProduct", rod); ctx.Refresh(); ITestObjectFactoryObject fo = (ITestObjectFactoryObject)ctx.GetObject("test"); Assert.IsTrue(AopUtils.IsAopProxy(fo)); Assert.AreEqual("CreateTestObject", ((IMethodInvocation)capturingAdvice.CapturedCalls[0]).Method.Name); capturingAdvice.CapturedCalls.Clear(); ITestObject to = (ITestObject)ctx.GetObject("testProduct"); Assert.IsTrue(AopUtils.IsAopProxy(to)); Assert.AreEqual("TheName", to.Name); Assert.AreEqual("get_Name", ((IMethodInvocation)capturingAdvice.CapturedCalls[0]).Method.Name); }
protected override object GetObjectFromField(FieldInfo fieldInfo, object o) { var target = fieldInfo.GetValue(o); if (TypesToSpySubrogateSet.Contains(fieldInfo.FieldType)) { var names = GetAppContextObjectNameListFromType(fieldInfo.FieldType); if (names.Any()) { // TODO: Deal with the case that there are several names. Using always the First // may not be okay in all cases. var spyObjectDef = _applicationContext.GetFactoryObject(names.First()); if (spyObjectDef is SurrogateFactoryObject) { target = (spyObjectDef as SurrogateFactoryObject).ObjectToFake; } } } while (AopUtils.IsAopProxy(target)) { target = ((IAdvised)target).TargetSource.GetTarget(); } return(target); }
public void TargetIsNotAProxy() { Assert.False(AopUtils.IsAopProxy(_target)); Assert.False(AopUtils.IsInheritanceAopProxy(_target)); Assert.False(AopUtils.IsInheritanceAopProxyType(_target.GetType())); }