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 MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Unit() { GoodCommand target = new GoodCommand(); NopInterceptor advice = new NopInterceptor(); IObjectFactory factory = A.Fake <IObjectFactory>(); ProxyFactoryObject fac = new ProxyFactoryObject(); fac.ProxyInterfaces = new[] { typeof(ICommand).FullName }; fac.IsSingleton = false; fac.InterceptorNames = new[] { "advice", "prototype" }; fac.ObjectFactory = factory; A.CallTo(() => factory.IsSingleton("advice")).Returns(true); A.CallTo(() => factory.GetObject("advice")).Returns(advice); A.CallTo(() => factory.GetType("prototype")).Returns(target.GetType()); A.CallTo(() => factory.GetObject("prototype")).Returns(target); fac.GetObject(); }
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 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."); } }
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 SupportsTransparentProxyAsTarget() { AppDomain domain = null; try { AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = Environment.CurrentDirectory; domain = AppDomain.CreateDomain("Spring", new Evidence(AppDomain.CurrentDomain.Evidence), setup); 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 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 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 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 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 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 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(); }