public void SingletonProxyWithPrototypeTargetCreatesTargetOnlyOnce() { try { RootObjectDefinition advice = new RootObjectDefinition(typeof(NopInterceptor)); // prototype target... RootObjectDefinition target = new RootObjectDefinition(typeof(InstantiationCountingCommand), false); DefaultListableObjectFactory ctx = new DefaultListableObjectFactory(); ctx.RegisterObjectDefinition("advice", advice); ctx.RegisterObjectDefinition("prototype", target); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName }; fac.IsSingleton = true; fac.InterceptorNames = new string[] { "advice", "prototype" }; fac.ObjectFactory = ctx; Assert.AreEqual(0, InstantiationCountingCommand.NumberOfInstantiations, "First Call"); fac.GetObject(); Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Second Call"); fac.GetObject(); Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Third Call"); } finally { InstantiationCountingCommand.NumberOfInstantiations = 0; } }
public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Unit() { GoodCommand target = new GoodCommand(); NopInterceptor advice = new NopInterceptor(); MockRepository mocks = new MockRepository(); IObjectFactory factory = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory)); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName }; fac.IsSingleton = false; fac.InterceptorNames = new string[] { "advice", "prototype" }; fac.ObjectFactory = factory; // using (mocks.Record()) { using (mocks.Unordered()) { Expect.Call(factory.IsSingleton("advice")).Return(true); Expect.Call(factory.GetObject("advice")).Return(advice); Expect.Call(factory.GetType("prototype")).Return(target.GetType()); Expect.Call(factory.GetObject("prototype")).Return(target); } } mocks.ReplayAll(); // using(mocks.Playback()) { fac.GetObject(); } mocks.VerifyAll(); }
public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Integration() { try { RootObjectDefinition advice = new RootObjectDefinition(typeof(NopInterceptor)); // prototype target... RootObjectDefinition target = new RootObjectDefinition(typeof(InstantiationCountingCommand), false); DefaultListableObjectFactory ctx = new DefaultListableObjectFactory(); ctx.RegisterObjectDefinition("advice", advice); ctx.RegisterObjectDefinition("prototype", target); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName }; fac.IsSingleton = false; fac.InterceptorNames = new string[] { "advice", "prototype" }; fac.ObjectFactory = ctx; Assert.AreEqual(0, InstantiationCountingCommand.NumberOfInstantiations, "Prototype target instance is being (needlessly) created during PFO initialization."); fac.GetObject(); Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Expected 1 inst"); fac.GetObject(); Assert.AreEqual(2, InstantiationCountingCommand.NumberOfInstantiations); } finally { InstantiationCountingCommand.NumberOfInstantiations = 0; } }
public void ReplaceAdvisorWhenConfigIsFrozen() { ProxyFactoryObject fac = CreateFrozenProxyFactory(); fac.IsFrozen = true; Assert.Throws <AopConfigException>(() => fac.ReplaceAdvisor(new PointcutForVoid(), new PointcutForVoid())); }
public void TargetAtEndOfInterceptorList() { GoodCommand target = new GoodCommand(); NopInterceptor advice = new NopInterceptor(); IObjectFactory mock = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory)); Expect.Call(mock.GetObject("advice")).Return(advice); Expect.Call(mock.GetObject("singleton")).Return(target); Expect.Call(mock.GetType("singleton")).Return(typeof(GoodCommand)); mocks.ReplayAll(); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName }; fac.IsSingleton = true; // default, just being explicit... fac.InterceptorNames = new string[] { "advice", "singleton" }; fac.ObjectFactory = mock; ICommand one = (ICommand)fac.GetObject(); ICommand two = (ICommand)fac.GetObject(); Assert.IsTrue(ReferenceEquals(one, two)); one.Execute(); Assert.AreEqual(1, advice.Count); two.Execute(); Assert.AreEqual(2, advice.Count); mocks.VerifyAll(); }
public void SupportsTransparentProxyAsTarget() { AppDomain domain = null; try { domain = AppDomain.CreateDomain("Spring"); object command = domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName, typeof(RemotableCommand).FullName); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.AddInterface(typeof(ICommand)); fac.AddAdvice(new NopInterceptor()); fac.Target = command; IAdvised advised = fac.GetObject() as IAdvised; Assert.IsNotNull(advised); ICommand cmd = fac.GetObject() as ICommand; Assert.IsNotNull(cmd); cmd.Execute(); } finally { AppDomain.Unload(domain); } }
public void ProxyTypeDoesntChangeIfSameConfig() { ProxyFactoryObject factoryObject = (ProxyFactoryObject)this.factory.GetObject("&concurrentPrototype"); Type testObjectType1 = factoryObject.GetObject().GetType(); Type testObjectType2 = factoryObject.GetObject().GetType(); Assert.AreSame(testObjectType1, testObjectType2); }
private ProxyFactoryObject CreateFrozenProxyFactory() { ProxyFactoryObject fac = new ProxyFactoryObject(); fac.AddInterface(typeof(ITestObject)); fac.IsFrozen = true; fac.AddAdvisor(new PointcutForVoid()); // this is ok, no proxy created yet fac.GetObject(); return(fac); }
public void ProxyTypeChangesIfConfigChanges() { ProxyFactoryObject factoryObject = (ProxyFactoryObject)this.factory.GetObject("&concurrentPrototype"); Type testObjectType1 = factoryObject.GetObject().GetType(); factoryObject.Interfaces = new Type[] {}; Type testObjectType2 = factoryObject.GetObject().GetType(); Assert.AreNotSame(testObjectType1, testObjectType2); }
public void GetObjectTypeWithTargetViaTargetSource() { IObjectFactory bf = new XmlObjectFactory( new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml", GetType())); ITestObject tb = (ITestObject)bf.GetObject("viaTargetSource"); Assert.IsTrue(tb.Name.Equals("Adam")); ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&viaTargetSource"); Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type"); }
public void AddAdvisorWhenConfigIsFrozen() { ProxyFactoryObject fac = CreateFrozenProxyFactory(); try { fac.AddAdvisor(new PointcutForVoid()); // not ok Assert.Fail("changing a frozen config must throw AopConfigException"); } catch (AopConfigException) {} }
public void FactoryWrapsObjectInSingletonTargetSource() { IObjectFactory bf = new XmlObjectFactory( new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml", GetType())); ITestObject tb = (ITestObject)bf.GetObject("viaTargetSource"); Assert.IsTrue(tb.Name.Equals("Adam")); ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&viaTargetSource"); Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type"); Assert.AreEqual(typeof(SingletonTargetSource), pfb.TargetSource.GetType(), "Incorrect target source, expected singleton"); }
private void CallGetObject() { ProxyFactoryObject proxyFactory = (ProxyFactoryObject)this.factory.GetObject("&concurrentPrototype"); ITestObject prevTestObject = null; for (int i = 0; i < 20; i++) { ITestObject o = proxyFactory.GetObject() as ITestObject; Assert.IsNotNull(o); Assert.AreNotSame(prevTestObject, o); prevTestObject = o; Thread.Sleep(0); } }
public void NullNameInInterceptorNamesArrayThrowAopConfigException() { IObjectFactory factory = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory)); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName }; fac.IsSingleton = false; fac.InterceptorNames = new string[] { null, null }; fac.ObjectFactory = factory; try { fac.GetObject(); Assert.Fail(); } catch (AopConfigException) {} }
public void GetObjectTypeWithDirectTarget() { IObjectFactory bf = new XmlObjectFactory( new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml", GetType())); // We have a counting before advice here CountingBeforeAdvice cba = (CountingBeforeAdvice)bf.GetObject("countingBeforeAdvice"); Assert.AreEqual(0, cba.GetCalls()); ITestObject tb = (ITestObject)bf.GetObject("directTarget"); Assert.IsTrue(tb.Name.Equals("Adam")); Assert.AreEqual(1, cba.GetCalls()); ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&directTarget"); Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type"); }
public void GlobalsCanAddAspectInterfaces() { IAddedGlobalInterface agi = (IAddedGlobalInterface)factory.GetObject("autoInvoker"); Assert.IsTrue(agi.GlobalsAdded == -1); ProxyFactoryObject pfb = (ProxyFactoryObject)factory.GetObject("&validGlobals"); pfb.GetObject(); // for creation Assert.AreEqual(2, pfb.Advisors.Count, "Proxy should have 1 global and 1 explicit advisor"); Assert.AreEqual(1, pfb.Introductions.Count, "Proxy should have 1 global introduction"); agi.GlobalsAdded = ((IAdvised)agi).Introductions.Count; Assert.IsTrue(agi.GlobalsAdded == 1); IApplicationEventListener l = (IApplicationEventListener)factory.GetObject("validGlobals"); agi = (IAddedGlobalInterface)l; Assert.IsTrue(agi.GlobalsAdded == -1); Assert.Throws <InvalidCastException>(() => factory.GetObject <IAddedGlobalInterface>("test1")); }
public void PassEmptyInterceptorNamesArray_WithTargetThatImplementsAnInterfaceCanBeCastToSaidInterface() { IObjectFactory factory = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory)); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { }; fac.Target = new GoodCommand(); fac.ObjectFactory = factory; IAdvised advised = fac.GetObject() as IAdvised; Assert.IsNotNull(advised); ICommand cmd = fac.GetObject() as ICommand; Assert.IsNotNull(cmd); DoesntImplementAnyInterfaces obj = fac.GetObject() as DoesntImplementAnyInterfaces; Assert.IsNull(obj); }
public void IsSingletonTrueReturnsNew_ProxyInstance_NotNewProxyTargetSource() { GoodCommand target = new GoodCommand(); IObjectFactory mock = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory)); Expect.Call(mock.GetObject("singleton")).Return(target); mocks.ReplayAll(); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName }; fac.IsSingleton = true; // default, just being explicit... fac.TargetName = "singleton"; fac.ObjectFactory = mock; fac.AddAdvice(new NopInterceptor()); ICommand one = (ICommand)fac.GetObject(); ICommand two = (ICommand)fac.GetObject(); Assert.IsTrue(ReferenceEquals(one, two)); mocks.VerifyAll(); }
public void ProxiedObjectUnwrapsTargetInvocationException() { ProxyFactoryObject fac = new ProxyFactoryObject(); fac.AddInterface(typeof(ICommand)); fac.AddAdvice(new NopInterceptor()); fac.Target = new BadCommand(); ICommand cmd = (ICommand)fac.GetObject(); try { cmd.Execute(); } catch (NotImplementedException) { // this is good, we want this exception to bubble up... } catch (TargetInvocationException) { Assert.Fail("Must have unwrapped this."); } }
public void CanAddAndRemoveIntroductionsOnSingleton() { try { ITimeStamped ts = (ITimeStamped)factory.GetObject("test1"); Assert.Fail("Shouldn't implement ITimeStamped before manipulation"); } catch (InvalidCastException) { } ProxyFactoryObject config = (ProxyFactoryObject)factory.GetObject("&test1"); long time = 666L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.TimeStamp = new DateTime(time); IIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped)); // add to front of introduction chain int oldCount = config.Introductions.Count; config.AddIntroduction(0, advisor); Assert.IsTrue(config.Introductions.Count == oldCount + 1); ITimeStamped ts2 = (ITimeStamped)factory.GetObject("test1"); Assert.IsTrue(ts2.TimeStamp == new DateTime(time)); // Can remove config.RemoveIntroduction(advisor); Assert.IsTrue(config.Introductions.Count == oldCount); // Existing reference will still work object o = ts2.TimeStamp; // But new proxies should not implement ITimeStamped try { ts2 = (ITimeStamped)factory.GetObject("test1"); Assert.Fail("Should no longer implement ITimeStamped"); } catch (InvalidCastException) { // expected... } // Now check non-effect of removing interceptor that isn't there oldCount = config.Advisors.Count; config.RemoveAdvice(new DebugAdvice()); Assert.IsTrue(config.Advisors.Count == oldCount); ITestObject it = (ITestObject)ts2; DebugAdvice debugInterceptor = new DebugAdvice(); config.AddAdvice(0, debugInterceptor); object foo = it.Spouse; Assert.AreEqual(1, debugInterceptor.Count); config.RemoveAdvice(debugInterceptor); foo = it.Spouse; // not invoked again Assert.IsTrue(debugInterceptor.Count == 1); }
public void PassNullElementListToTheProxyInterfacesProperty() { ProxyFactoryObject fac = new ProxyFactoryObject(); Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = new string[] { null }); }
public void PassRubbishNameToTheProxyInterfacesProperty() { ProxyFactoryObject fac = new ProxyFactoryObject(); Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = new string[] { "Hey" }); }
public void PassClassNotInterfaceNameToTheProxyInterfacesProperty() { ProxyFactoryObject fac = new ProxyFactoryObject(); Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = new string[] { typeof(GoodCommand).FullName }); }
public void PassNullToTheProxyInterfacesProperty() { ProxyFactoryObject fac = new ProxyFactoryObject(); Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = null); }