public void InvokeWithStaticMethod() { MethodInvoker mi = new MethodInvoker(); mi.TargetType = typeof(Int32); mi.TargetMethod = "Parse"; mi.Arguments = new object[] { "27" }; mi.Prepare(); object actual = mi.Invoke(); Assert.IsNotNull(actual); Assert.AreEqual(typeof(int), actual.GetType()); Assert.AreEqual(27, (int)actual); }
public OperationResponse Post(OperationRequest request) { var st = new Stopwatch(); st.Start(); OperationResponse response = new OperationResponse() { TargetOperation = request.TargetOperation, TargetMethod = request.TargetMethod }; try { MethodInvoker methodInvoker = new MethodInvoker(); object targetOperation = SpringContext.GetObject(request.TargetOperation); if (targetOperation == null) throw new ObjectDefinitionStoreException(string.Format("The operation id named '{0}' could not be found in spring context.", request.TargetOperation)); methodInvoker.TargetObject = targetOperation; methodInvoker.TargetMethod = request.TargetMethod; methodInvoker.Arguments = SerializerHelper.DeserializeBinary(request.Arguments); methodInvoker.Prepare(); object result = methodInvoker.Invoke(); response.Result.Value = SerializerHelper.SerializeBinary(new object[] { result }); response.Result.Status = OperationResultType.Success; } catch (Exception ex) { response.Result.Exception = new OperationException(ex); response.Result.Status = OperationResultType.Error; } finally { st.Stop(); response.ElapsedTotalSeconds = st.Elapsed.TotalSeconds; logger.DebugFormat("Invoking method [{0}] of operation [{1}] done in {2} seconds [status={3}].", response.TargetMethod, response.TargetOperation, response.ElapsedTotalSeconds, response.Result.Status); if (response.Result.Status == OperationResultType.Error) { logger.Error("---------------------------------------------------------------------------------------------------------------------------------------"); logger.ErrorFormat("[Message]: {0} ", response.Result.Exception.Message); logger.ErrorFormat("[Source]: {0}", response.Result.Exception.Source); logger.ErrorFormat("[StackTrace]: {0}", response.Result.Exception.StackTrace); logger.Error("---------------------------------------------------------------------------------------------------------------------------------------"); } } return response; }
/// <summary>Invokes the specified listener method.</summary> /// <param name="methodName">The name of the listener method.</param> /// <param name="arguments">The message arguments to be passed in.</param> /// <returns>The result returned from the listener method.</returns> protected object InvokeListenerMethod(string methodName, object[] arguments) { try { var methodInvoker = new MethodInvoker(); methodInvoker.TargetObject = this.handlerObject; methodInvoker.TargetMethod = methodName; methodInvoker.Arguments = arguments; methodInvoker.Prepare(); var result = methodInvoker.Invoke(); if (result == MethodInvoker.Void) { return null; } return result; } catch (TargetInvocationException ex) { var targetEx = ex.InnerException; if (targetEx is IOException) { throw new AmqpIOException(targetEx); } else { throw new ListenerExecutionFailedException("Listener method '" + methodName + "' threw exception", targetEx); } } catch (Exception ex) { throw new ListenerExecutionFailedException(this.BuildInvocationFailureMessage(methodName, arguments), ex); } }
public void InvokeWithNullArgument() { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.TargetType = GetType(); methodInvoker.TargetMethod = "NullArgument"; methodInvoker.Arguments = new object[] {null}; methodInvoker.Prepare(); methodInvoker.Invoke(); }
private static object InvokeOperation(MethodInvoker methodInvoker) { try { return methodInvoker.Invoke(); } catch (Exception ex) { Log.Error("Exception -------------- " + ex.GetType()); Log.Error(" Invoke operation error: " + ex.Message); throw ex.InnerException; } }
public void InvokeWithGenericStaticMethod() { MethodInvoker mi = new MethodInvoker(); mi.TargetType = typeof(Activator); mi.TargetMethod = "CreateInstance<Spring.Objects.TestObject>"; mi.Prepare(); object actual = mi.Invoke(); Assert.IsNotNull(actual); Assert.AreEqual(typeof(TestObject), actual.GetType()); }
public void NamedArgumentsOverwriteEachOther() { Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "growolder"; vkr.AddNamedArgument("years", 10); // this second setting must overwrite the first... vkr.AddNamedArgument("years", 200); vkr.Prepare(); object actual = vkr.Invoke(); Assert.IsFalse(98.Equals(actual), "The first named argument setter is sticking; must allow itslf to be overwritten."); Assert.AreEqual(288, actual, "The second named argument was not applied (it must be)."); }
public void InvokeWithWeIRdLyCasedNamedArgument() { Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "gROwOldeR"; vkr.AddNamedArgument("YEarS", 10); vkr.Prepare(); object actual = vkr.Invoke(); Assert.AreEqual(98, actual); }
public void InvokeWithArgumentOfWrongType() { Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "growolder"; vkr.AddNamedArgument("years", "Bingo"); vkr.Prepare(); Assert.Throws<MethodInvocationException>(() => vkr.Invoke(), "At least one of the arguments passed to this MethodInvoker was incompatible with the signature of the invoked method."); }
public void InvokeWithMixOfNamedAndPlainVanillaArgumentsOfDifferingTypes() { int maximumAge = 95; string expectedStatus = "Old Age Pensioner"; Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "GrowOlderUntilMaximumAgeReachedAndSetStatusName"; vkr.AddNamedArgument("years", 10); vkr.AddNamedArgument("status", expectedStatus); vkr.Arguments = new object[] {maximumAge}; vkr.Prepare(); object actual = vkr.Invoke(); Assert.AreEqual(maximumAge, actual); Assert.AreEqual(expectedStatus, foo.Status); }
public void InvokeWithMixOfNamedAndPlainVanillaArguments() { int maximumAge = 95; Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "GrowOlderUntilMaximumAgeReached"; vkr.AddNamedArgument("years", 10); vkr.Arguments = new object[] {maximumAge}; vkr.Prepare(); object actual = vkr.Invoke(); Assert.AreEqual(maximumAge, actual); }
public void InvokeWithNamedArgument() { Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "growolder"; vkr.AddNamedArgument("years", 10); vkr.Prepare(); object actual = vkr.Invoke(); Assert.AreEqual(98, actual); }
public void InvokeWithOKArgumentsAndMixedCaseMethodName() { Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "growolder"; vkr.Arguments = new object[] {10}; vkr.Prepare(); object actual = vkr.Invoke(); Assert.AreEqual(98, actual); }
public void InvokeWithArgumentOfWrongType() { Foo foo = new Foo(); foo.Age = 88; MethodInvoker vkr = new MethodInvoker(); vkr.TargetObject = foo; vkr.TargetMethod = "growolder"; vkr.AddNamedArgument("years", "Bingo"); vkr.Prepare(); vkr.Invoke(); }