public static void Invoke_Receives_Correct_Arguments() { object[] actualArgs = null; TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); ((TestDispatchProxy)proxy).CallOnInvoke = (method, args) => { actualArgs = args; return(String.Empty); }; proxy.Hello("testInput"); object[] expectedArgs = new object[] { "testInput" }; Assert.True(actualArgs != null && actualArgs.Length == expectedArgs.Length, String.Format("Invoked expected object[] of length {0} but actual was {1}", expectedArgs.Length, (actualArgs == null ? "null" : actualArgs.Length.ToString()))); for (int i = 0; i < expectedArgs.Length; ++i) { Assert.True(expectedArgs[i].Equals(actualArgs[i]), String.Format("Expected arg[{0}] = '{1}' but actual was '{2}'", i, expectedArgs[i], actualArgs[i])); } }
public static void Create_Proxy_Derives_From_DispatchProxy_BaseType() { TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); Assert.NotNull(proxy); Assert.IsAssignableFrom <TestDispatchProxy>(proxy); }
public static void Create_Proxy_Derives_From_DispatchProxy_BaseType() { TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); Assert.NotNull(proxy); Assert.True(typeof(TestDispatchProxy).GetTypeInfo().IsAssignableFrom(proxy.GetType().GetTypeInfo()), String.Format("Proxy type {0} did not derive from {1}", proxy.GetType().Name, typeof(TestDispatchProxy).Name)); }
public static void Create_Same_Proxy_Type_And_Base_Type_Reuses_Same_Generated_Type() { TestType_IHelloService proxy1 = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); TestType_IHelloService proxy2 = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); Assert.NotNull(proxy1); Assert.NotNull(proxy2); Assert.IsType(proxy1.GetType(), proxy2); }
public static void Created_Proxy_With_Different_Proxy_Type_Use_Different_Generated_Type() { TestType_IHelloService proxy1 = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); TestType_IGoodbyeService proxy2 = DispatchProxy.Create <TestType_IGoodbyeService, TestDispatchProxy>(); Assert.NotNull(proxy1); Assert.NotNull(proxy2); Assert.False(proxy1.GetType() == proxy2.GetType(), String.Format("Proxy generated for type {0} used same for type {1}", typeof(TestType_IHelloService).Name, typeof(TestType_IGoodbyeService).Name)); }
public static void Create_Proxy_Instances_Of_Same_Proxy_And_Base_Type_Are_Unique() { TestType_IHelloService proxy1 = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); TestType_IHelloService proxy2 = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); Assert.NotNull(proxy1); Assert.NotNull(proxy2); Assert.False(object.ReferenceEquals(proxy1, proxy2), String.Format("First and second instance of proxy type {0} were the same instance", proxy1.GetType().ToString())); }
public static void Create_Same_Proxy_Type_And_Base_Type_Reuses_Same_Generated_Type() { TestType_IHelloService proxy1 = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); TestType_IHelloService proxy2 = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); Assert.NotNull(proxy1); Assert.NotNull(proxy2); Assert.True(proxy1.GetType() == proxy2.GetType(), String.Format("First instance of proxy type was {0} but second was {1}", proxy1.GetType().Name, proxy2.GetType().Name)); }
public static void Create_Same_Proxy_Type_With_Different_BaseType_Uses_Different_Generated_Type() { TestType_IHelloService proxy1 = DispatchProxyAsync.Create <TestType_IHelloService, TestDispatchProxyAsync>(); TestType_IHelloService proxy2 = DispatchProxyAsync.Create <TestType_IHelloService, TestDispatchProxyAsync2>(); Assert.NotNull(proxy1); Assert.NotNull(proxy2); Assert.False(proxy1.GetType() == proxy2.GetType(), String.Format("Proxy generated for base type {0} used same for base type {1}", typeof(TestDispatchProxyAsync).Name, typeof(TestDispatchProxyAsync).Name)); }
public static void Invoke_Returns_Correct_Value() { TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); ((TestDispatchProxy)proxy).CallOnInvoke = (method, args) => { return("testReturn"); }; string expectedResult = "testReturn"; string actualResult = proxy.Hello(expectedResult); Assert.Equal(expectedResult, actualResult); }
public static void Invoke_Receives_Correct_MethodInfo_And_Arguments() { bool wasInvoked = false; StringBuilder errorBuilder = new StringBuilder(); // This Func is called whenever we call a method on the proxy. // This is where we validate it received the correct arguments and methods Func <MethodInfo, object[], object> invokeCallback = (method, args) => { wasInvoked = true; if (method == null) { string error = String.Format("Proxy for {0} was called with null method", typeof(TestType_IHelloService).Name); errorBuilder.AppendLine(error); return(null); } else { MethodInfo expectedMethod = typeof(TestType_IHelloService).GetTypeInfo().GetDeclaredMethod("Hello"); if (expectedMethod != method) { string error = String.Format("Proxy for {0} was called with incorrect method. Expected = {1}, Actual = {2}", typeof(TestType_IHelloService).Name, expectedMethod, method); errorBuilder.AppendLine(error); return(null); } } return("success"); }; TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); Assert.NotNull(proxy); TestDispatchProxy dispatchProxy = proxy as TestDispatchProxy; Assert.NotNull(dispatchProxy); // Redirect Invoke to our own Func above dispatchProxy.CallOnInvoke = invokeCallback; // Calling this method now will invoke the Func above which validates correct method proxy.Hello("testInput"); Assert.True(wasInvoked, "The invoke method was not called"); Assert.True(errorBuilder.Length == 0, errorBuilder.ToString()); }
public static void Invoke_Receives_Correct_MethodInfo() { MethodInfo invokedMethod = null; TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); ((TestDispatchProxy)proxy).CallOnInvoke = (method, args) => { invokedMethod = method; return(String.Empty); }; proxy.Hello("testInput"); MethodInfo expectedMethod = typeof(TestType_IHelloService).GetTypeInfo().GetDeclaredMethod("Hello"); Assert.True(invokedMethod != null && expectedMethod == invokedMethod, String.Format("Invoke expected method {0} but actual was {1}", expectedMethod, invokedMethod)); }
public static void Create_Using_BaseType_Without_Default_Ctor_Throws_ArgumentException() { ArgumentException caughtException = null; try { TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, NoDefaultCtor_TestDispatchProxy>(); } catch (ArgumentException ex) { caughtException = ex; } catch (Exception e) { Assert.True(false, String.Format("Caught unexpected exception {0}", e.ToString())); } Assert.True(caughtException != null, "Expected ArgumentException to be thrown"); Assert.True(String.Equals(caughtException.ParamName, "TProxy"), String.Format("Expected paramName 'TProxy' but received '{0}'", caughtException.ParamName)); }
public static void Invoke_Thrown_Exception_Rethrown_To_Caller() { Exception actualException = null; InvalidOperationException expectedException = new InvalidOperationException("testException"); TestType_IHelloService proxy = DispatchProxy.Create <TestType_IHelloService, TestDispatchProxy>(); ((TestDispatchProxy)proxy).CallOnInvoke = (method, args) => { throw expectedException; }; try { proxy.Hello("testCall"); } catch (Exception e) { actualException = e; } Assert.Equal(expectedException, actualException); }