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 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); }