public void ShortCircuit() { Recorder.Records.Clear(); SpyException rawObject = new SpyException(); MethodBase method = typeof(SpyException).GetMethod("InterceptedMethod"); Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >(); List <IInterceptionHandler> handlers = new List <IInterceptionHandler>(); handlers.Add(new ShortCircuitHandler()); dictionary.Add(method, handlers); SpyException wrapped = RemotingInterceptor.Wrap(rawObject, dictionary); wrapped.InterceptedMethod(); // Does not throw because it was short circuited }
public void PassesParameters() { Recorder.Records.Clear(); SpyWithParameters rawObject = new SpyWithParameters(); MethodBase method = typeof(SpyWithParameters).GetMethod("InterceptedMethod"); Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >(); List <IInterceptionHandler> handlers = new List <IInterceptionHandler>(); dictionary.Add(method, handlers); int i = 9; string s; SpyWithParameters wrapped = RemotingInterceptor.Wrap(rawObject, dictionary); int result = wrapped.InterceptedMethod(4.2, ref i, out s); Assert.Equal(42, result); Assert.Equal(18, i); Assert.Equal("MyString", s); Assert.Equal("d = 4.2", Recorder.Records[0]); }
public void CanInterceptInterface() { Recorder.Records.Clear(); SpyInterfaceClass rawObject = new SpyInterfaceClass(); RecordingHandler handler = new RecordingHandler(); MethodBase method = typeof(ISpyInterface).GetMethod("InterceptedMethod"); Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >(); List <IInterceptionHandler> handlers = new List <IInterceptionHandler>(); handlers.Add(handler); dictionary.Add(method, handlers); ISpyInterface result = RemotingInterceptor.Wrap <ISpyInterface>(rawObject, dictionary); result.InterceptedMethod(); Assert.Equal(3, Recorder.Records.Count); Assert.Equal("Before Method", Recorder.Records[0]); Assert.Equal("In Method", Recorder.Records[1]); Assert.Equal("After Method", Recorder.Records[2]); }
public void Exceptions() { Recorder.Records.Clear(); SpyException rawObject = new SpyException(); MethodBase method = typeof(SpyException).GetMethod("InterceptedMethod"); Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >(); List <IInterceptionHandler> handlers = new List <IInterceptionHandler>(); handlers.Add(new RecordingHandler()); dictionary.Add(method, handlers); SpyException wrapped = RemotingInterceptor.Wrap(rawObject, dictionary); Assert.Throws <NotImplementedException>(delegate { wrapped.InterceptedMethod(); }); Assert.Equal(2, Recorder.Records.Count); Assert.Equal("Before Method", Recorder.Records[0]); Assert.Equal("After Method", Recorder.Records[1]); }
public override object BuildUp(IBuilderContext context, object buildKey, object existing) { Type typeToBuild; if (TryGetTypeFromBuildKey(buildKey, out typeToBuild)) { Type originalType; if (!TryGetTypeFromBuildKey(context.OriginalBuildKey, out originalType)) { originalType = typeToBuild; } IRemotingInterceptionPolicy policy = context.Policies.Get <IRemotingInterceptionPolicy>(buildKey); if (existing != null && policy != null) { existing = RemotingInterceptor.Wrap(existing, originalType, policy); } } return(base.BuildUp(context, buildKey, existing)); }