private IMethodReturnMessage InvokeOnServer(MockingProxy proxy, MockableCall call) { IMessage request = call.OriginalCall; IMethodCallMessage mcm = request as IMethodCallMessage; IConstructionCallMessage ccm = request as IConstructionCallMessage; if(ccm != null) { try { // Attach server instance: // (we do it only for MarshalByRefObjects as it's a requirement, don't really // know however what's the added value of attachingToServer... it also works // well without doing this) if (this.serverInstance is MarshalByRefObject) proxy.AttachServer((MarshalByRefObject)this.serverInstance); // Call instance constructor: RemotingServices.GetRealProxy(serverInstance).InitializeServerObject(ccm); // Build response: return MockingTools.BuildReturnMessage(proxy.GetTransparentProxy(), ccm.Args, mcm); } catch (Exception ex) { // Build response: return MockingTools.BuildReturnMessage(ex, mcm); } } else { try { // Invoke instance method: object[] args = new object[mcm.ArgCount]; mcm.Args.CopyTo(args, 0); object result = mcm.MethodBase.Invoke(serverInstance, args); // Build response: return MockingTools.BuildReturnMessage(result, args, mcm); } catch (TargetInvocationException ex) { // Build response: return MockingTools.BuildReturnMessage(ex.InnerException, mcm); } } }
/// <summary> /// Set the result of construction call. /// </summary> /// <param name="instanceName">Name to be assigned to the created instance.</param> /// <param name="outArgs">The values of output arguments returned by the call.</param> public virtual void SetConstructionResult(string instanceName, object[] outArgs) { // Set return values: this.callee.InstanceName = instanceName; this.returnValue = callee.GetTransparentProxy(); this.outArgs = outArgs; this.callDuration = Environment.TickCount - callCreationTime; this.isCompleted = true; }
/// <summary> /// Creates an eventually mocked instance for the given serverType. /// </summary> /// <param name="serverType">The type for which to create an instance. /// Should be the type decorated by this attribute.</param> protected override MarshalByRefObject CreateMockedInstance(Type serverType) { RealProxy rp; switch (RecorderManager.Action) { case RecorderState.Recording: // Create a recording proxy for the object: MarshalByRefObject target = CreateUnmockedInstance(serverType); rp = new MockingProxy(serverType, new RecordingMocker(target), RecorderManager.GetNextInstanceName(serverType)); return (MarshalByRefObject)rp.GetTransparentProxy(); case RecorderState.PlayBack: // Create a playback proxy: rp = new MockingProxy(serverType, new PlayBackMocker(), null); return (MarshalByRefObject)rp.GetTransparentProxy(); default: // Create an instance without proxy: return CreateUnmockedInstance(serverType); } }
private IMethodReturnMessage InvokeOnServer(MockingProxy proxy, MockableCall call) { IMessage request = call.OriginalCall; IMethodCallMessage mcm = request as IMethodCallMessage; IConstructionCallMessage ccm = request as IConstructionCallMessage; if (ccm != null) { try { // Attach server instance: // (we do it only for MarshalByRefObjects as it's a requirement, don't really // know however what's the added value of attachingToServer... it also works // well without doing this) if (this.serverInstance is MarshalByRefObject) { proxy.AttachServer((MarshalByRefObject)this.serverInstance); } // Call instance constructor: RemotingServices.GetRealProxy(serverInstance).InitializeServerObject(ccm); // Build response: return(MockingTools.BuildReturnMessage(proxy.GetTransparentProxy(), ccm.Args, mcm)); } catch (Exception ex) { // Build response: return(MockingTools.BuildReturnMessage(ex, mcm)); } } else { try { // Invoke instance method: object[] args = new object[mcm.ArgCount]; mcm.Args.CopyTo(args, 0); object result = mcm.MethodBase.Invoke(serverInstance, args); // Build response: return(MockingTools.BuildReturnMessage(result, args, mcm)); } catch (TargetInvocationException ex) { // Build response: return(MockingTools.BuildReturnMessage(ex.InnerException, mcm)); } } }
/// <summary> /// Creates an eventually mocked instance for the given serverType. /// </summary> /// <param name="serverType">The type for which to create an instance. /// Should be the type decorated by this attribute.</param> protected override MarshalByRefObject CreateMockedInstance(Type serverType) { RealProxy rp; switch (RecorderManager.Action) { case RecorderState.Recording: // Create a recording proxy for the object: MarshalByRefObject target = CreateUnmockedInstance(serverType); rp = new MockingProxy(serverType, new RecordingMocker(target), RecorderManager.GetNextInstanceName(serverType)); return((MarshalByRefObject)rp.GetTransparentProxy()); case RecorderState.PlayBack: // Create a playback proxy: rp = new MockingProxy(serverType, new PlayBackMocker(), null); return((MarshalByRefObject)rp.GetTransparentProxy()); default: // Create an instance without proxy: return(CreateUnmockedInstance(serverType)); } }
private MockableCall(SerializationInfo info, StreamingContext context) { // Retrieve serialization version: string assemblyVersion = info.GetString("assemblyVersion"); decimal serializationVersion = info.GetDecimal("serializationVersion"); // Retrieve callee: this.callee = new MockingProxy(Type.GetType(info.GetString("calleeType")), new PlayBackMocker(), info.GetString("calleeInstanceName")); // Retrieve isConstructorCall: this.isConstructorCall = info.GetBoolean("isConstructorCall"); // Retrieve method: Type methodType = Type.GetType(info.GetString("methodType")); string[] methodSignatureStr = (string[])info.GetValue("methodSignature", typeof(string[])); Type[] methodSignature = new Type[methodSignatureStr.Length]; for(int i=0; i<methodSignatureStr.Length; i++) methodSignature[i] = Type.GetType(methodSignatureStr[i]); if (this.isConstructorCall) { this.method = methodType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, methodSignature, null); } else { this.method = methodType.GetMethod(info.GetString("methodName"), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, methodSignature, null); } // Retrieve inArgs: this.inArgs = (object[])info.GetValue("inArgs", typeof(object[])); object[] inArgsSubstitutions = (object[])info.GetValue("inArgsMockers", typeof(object[])); foreach(object[] subst in inArgsSubstitutions) { MockingProxy proxy = new MockingProxy(Type.GetType((string)subst[1]), new PlayBackMocker(), (string)subst[2]); this.inArgs[(int)subst[0]] = proxy.GetTransparentProxy(); } // Retrieve outArgs: this.outArgs = (object[])info.GetValue("outArgs", typeof(object[])); object[] outArgsSubstitutions = (object[])info.GetValue("outArgsMockers", typeof(object[])); if (outArgs != null) { foreach(object[] subst in outArgsSubstitutions) { MockingProxy proxy = new MockingProxy(Type.GetType((string)subst[1]), new PlayBackMocker(), (string)subst[2]); this.outArgs[(int)subst[0]] = proxy.GetTransparentProxy(); } } // Retrieve returnValue: bool returnValueMocked = info.GetBoolean("returnValueMocked"); Type returnValueType = Type.GetType(info.GetString("returnValueType")); if (returnValueMocked) { MockingProxy proxy = new MockingProxy(Type.GetType(info.GetString("returnValueType")), new PlayBackMocker(), info.GetString("returnValueName")); this.returnValue = proxy.GetTransparentProxy(); } else { this.returnValue = info.GetValue("returnValue", returnValueType); } // Retrieve exception: this.exception = (Exception)info.GetValue("exception", typeof(Exception)); if (exception == null) { string exceptionType = info.GetString("exceptionType"); if (exceptionType != null) { this.exception = (Exception)Type.GetType(exceptionType).GetConstructor(new Type[] {}).Invoke(new object[] {}); } } // Retrieve iscompleted & duration: this.isCompleted = info.GetBoolean("isCompleted"); this.callDuration = info.GetInt32("callDuration"); }
private MockableCall(SerializationInfo info, StreamingContext context) { // Retrieve serialization version: string assemblyVersion = info.GetString("assemblyVersion"); decimal serializationVersion = info.GetDecimal("serializationVersion"); // Retrieve callee: this.callee = new MockingProxy(Type.GetType(info.GetString("calleeType")), new PlayBackMocker(), info.GetString("calleeInstanceName")); // Retrieve isConstructorCall: this.isConstructorCall = info.GetBoolean("isConstructorCall"); // Retrieve method: Type methodType = Type.GetType(info.GetString("methodType")); string[] methodSignatureStr = (string[])info.GetValue("methodSignature", typeof(string[])); Type[] methodSignature = new Type[methodSignatureStr.Length]; for (int i = 0; i < methodSignatureStr.Length; i++) { methodSignature[i] = Type.GetType(methodSignatureStr[i]); } if (this.isConstructorCall) { this.method = methodType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, methodSignature, null); } else { this.method = methodType.GetMethod(info.GetString("methodName"), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, methodSignature, null); } // Retrieve inArgs: this.inArgs = (object[])info.GetValue("inArgs", typeof(object[])); object[] inArgsSubstitutions = (object[])info.GetValue("inArgsMockers", typeof(object[])); foreach (object[] subst in inArgsSubstitutions) { MockingProxy proxy = new MockingProxy(Type.GetType((string)subst[1]), new PlayBackMocker(), (string)subst[2]); this.inArgs[(int)subst[0]] = proxy.GetTransparentProxy(); } // Retrieve outArgs: this.outArgs = (object[])info.GetValue("outArgs", typeof(object[])); object[] outArgsSubstitutions = (object[])info.GetValue("outArgsMockers", typeof(object[])); if (outArgs != null) { foreach (object[] subst in outArgsSubstitutions) { MockingProxy proxy = new MockingProxy(Type.GetType((string)subst[1]), new PlayBackMocker(), (string)subst[2]); this.outArgs[(int)subst[0]] = proxy.GetTransparentProxy(); } } // Retrieve returnValue: bool returnValueMocked = info.GetBoolean("returnValueMocked"); Type returnValueType = Type.GetType(info.GetString("returnValueType")); if (returnValueMocked) { MockingProxy proxy = new MockingProxy(Type.GetType(info.GetString("returnValueType")), new PlayBackMocker(), info.GetString("returnValueName")); this.returnValue = proxy.GetTransparentProxy(); } else { this.returnValue = info.GetValue("returnValue", returnValueType); } // Retrieve exception: this.exception = (Exception)info.GetValue("exception", typeof(Exception)); if (exception == null) { string exceptionType = info.GetString("exceptionType"); if (exceptionType != null) { this.exception = (Exception)Type.GetType(exceptionType).GetConstructor(new Type[] {}).Invoke(new object[] {}); } } // Retrieve iscompleted & duration: this.isCompleted = info.GetBoolean("isCompleted"); this.callDuration = info.GetInt32("callDuration"); }