public void SettingNamedArgumentsToNullJustClearsOutAnyNamedArguments()
		{
			MethodInvoker vkr = new MethodInvoker();
			vkr.AddNamedArgument("age", 10);
			vkr.NamedArguments = null;
			Assert.IsNotNull(vkr.NamedArguments);
			Assert.AreEqual(0, vkr.NamedArguments.Count);
		}
		public void ArgumentsProperty()
		{
			MethodInvoker vkr = new MethodInvoker();
			vkr.Arguments = null;
			Assert.IsNotNull(vkr.Arguments); // should always be the empty object array, never null
			Assert.AreEqual(0, vkr.Arguments.Length);
			vkr.Arguments = new string[] {"Chank Pop"};
			Assert.AreEqual(1, vkr.Arguments.Length);
		}
 public void TestMethodInvoker_MethodSetCorrectly()
 {
     InvocationCountingJob job = new InvocationCountingJob();
     MethodInvoker mi = new MethodInvoker();
     mi.TargetObject = job;
     mi.TargetMethod = "Invoke";
     mi.Prepare();
     methodInvokingJob.MethodInvoker = mi;
     methodInvokingJob.Execute(CreateMinimalJobExecutionContext());
     Assert.AreEqual(1, job.CounterValue, "Job was not invoked once");
 }
 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);
 }
Example #5
0
        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>
        /// Constructor for JobMethodInvocationFailedException.
        /// </summary>
        /// <param name="methodInvoker">the MethodInvoker used for reflective invocation</param>
        /// <param name="cause">the root cause (as thrown from the target method)</param>
        public JobMethodInvocationFailedException(MethodInvoker methodInvoker, Exception cause) :
                base("Invocation of method '" + methodInvoker.TargetMethod +
                    "' on target class [" + methodInvoker.TargetType + "] failed", cause)
        {

        }
 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 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 InvokeWithNullArgument()
		{
			MethodInvoker methodInvoker = new MethodInvoker();
			methodInvoker.TargetType = GetType();
			methodInvoker.TargetMethod = "NullArgument";
			methodInvoker.Arguments = new object[] {null};
			methodInvoker.Prepare();
			methodInvoker.Invoke();
		}
 public void TestMethodInvoker_PrivateMethod()
 {
     InvocationCountingJob job = new InvocationCountingJob();
     MethodInvoker mi = new MethodInvoker();
     mi.TargetObject = job;
     mi.TargetMethod = "PrivateMethod";
     mi.Prepare();
     methodInvokingJob.MethodInvoker = mi;
     methodInvokingJob.Execute(CreateMinimalJobExecutionContext());
 }
		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 Instantiation()
		{
			MethodInvoker vkr = new MethodInvoker();
			Assert.IsNotNull(vkr.Arguments);
		}
		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 InvokeWithNamedArgumentThatDoesNotExist()
		{
			Foo foo = new Foo();
			foo.Age = 88;
			MethodInvoker vkr = new MethodInvoker();
			vkr.TargetObject = foo;
			vkr.TargetMethod = "growolder";
			vkr.AddNamedArgument("southpaw", 10);
            Assert.Throws<ArgumentException>(() => vkr.Prepare(), "The named argument 'southpaw' could not be found on the 'GrowOlder' method of class [Spring.Objects.Support.MethodInvokerTests+Foo].");
		}
		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);
		}
Example #18
0
        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 TestMethodInvoker_ShouldSetResultToExecutionContext()
        {
            InvocationCountingJob job = new InvocationCountingJob();
            MethodInvoker mi = new MethodInvoker();
            mi.TargetObject = job;
            mi.TargetMethod = "InvokeWithReturnValue";
            mi.Prepare();
            methodInvokingJob.MethodInvoker = mi;
            IJobExecutionContext context = CreateMinimalJobExecutionContext();
            methodInvokingJob.Execute(context);

            Assert.AreEqual(InvocationCountingJob.DefaultReturnValue, context.Result, "result value was not set to context");
        }
Example #20
0
        public static OperationResponse ProcessRequest(OperationRequest request)
        {
            var st = new Stopwatch();
            st.Start();

            OperationResponse response = new OperationResponse()
            {
                Request = new OperationRequest()
                {
                    TargetOperationId = request.TargetOperationId,
                    TargetMethod = request.TargetMethod,
                    Url = request.Url,
                    InvokerType = request.InvokerType
                }
            };

            try
            {
                object targetOperation = SpringContext.GetObject(request.TargetOperationId);
                if (Log.IsDebugEnabled)
                    Log.DebugFormat("Looking up operation object by id '{0}'", request.TargetOperationId);

                if (targetOperation == null)
                    throw new ObjectDefinitionStoreException(string.Format("The operation id named '{0}' could not be found in spring context.", request.TargetOperationId));

                MethodInvoker methodInvoker = new MethodInvoker();
                methodInvoker.TargetObject = targetOperation;
                methodInvoker.TargetMethod = request.TargetMethod;

                object[] deserializedArguments = null;

                if (request.InvokerType == InvokerType.WEB)
                    deserializedArguments = SerializerUtils.DeserializeBinary((Byte[])request.OperationArgs);
                else
                    deserializedArguments = (object[])request.OperationArgs;

                methodInvoker.Arguments = deserializedArguments;
                methodInvoker.Prepare();

                object result = InvokeOperation(methodInvoker);

                if (request.InvokerType == InvokerType.WEB)
                {
                    byte[] serializedResult = SerializerUtils.SerializeBinary(new object[] { result });
                    byte[] compressedResult = SevenZipUtils.CompressBytes(serializedResult);
                    response.Result.Value = compressedResult;
                }
                else
                {
                    response.Result.Value = result;
                }

                response.Result.Type = OperationResultType.Ok;
            }
            catch (NoSuchObjectDefinitionException ex)
            {
                response.Result.Message = ex.Message;
            }
            catch (ObjectDefinitionStoreException ex)
            {
                response.Result.Message = ex.Message;
            }
            catch (System.MissingMethodException ex)
            {
                response.Result.Message = ex.Message;
            }
            catch (Spring.Core.MethodInvocationException ex)
            {
                response.Result.Message = ex.Message;
            }
            catch (OperationAccessException ex)
            {
                response.Result.Message = ex.Message;
            }
            catch (Exception ex)
            {
                response.Result.Message = ex.Message;
                Log.Error(ex);
            }
            finally
            {
                st.Stop();
                response.ElapsedTotalSeconds = st.Elapsed.TotalSeconds;

                string messageFormat = "Invoking method [{0}] of operation [{1}] done in {2} seconds.";
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat
                        ( messageFormat
                        , response.Request.TargetMethod
                        , response.Request.TargetOperationId
                        , response.ElapsedTotalSeconds
                        );
                }
            }

            return response;
        }
Example #21
0
		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();
		}
 public void TestMethodInvoker_MethodSetCorrectlyThrowsException()
 {
     InvocationCountingJob job = new InvocationCountingJob();
     MethodInvoker mi = new MethodInvoker();
     mi.TargetObject = job;
     mi.TargetMethod = "InvokeAndThrowException";
     mi.Prepare();
     methodInvokingJob.MethodInvoker = mi;
     try
     {
         methodInvokingJob.Execute(CreateMinimalJobExecutionContext());
         Assert.Fail("Successful invoke when method threw exception");
     }
     catch (JobMethodInvocationFailedException)
     {
         // ok
     }
     Assert.AreEqual(1, job.CounterValue, "Job was not invoked once");
 }
Example #23
0
		public void PrepareWithOnlyTargetMethodSet()
		{
			MethodInvoker vkr = new MethodInvoker();
			vkr.TargetMethod = "Foo";
			vkr.Prepare();
		}
		public void PrepareWithOnlyTargetMethodSet()
		{
			MethodInvoker vkr = new MethodInvoker();
			vkr.TargetMethod = "Foo";
            Assert.Throws<ArgumentException>(() => vkr.Prepare(), "One of either the 'TargetType' or 'TargetObject' properties is required.");
		}
		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);
		}
        /// <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);
            }
        }
Example #27
0
        public void Instantiation()
        {
            MethodInvoker vkr = new MethodInvoker();

            Assert.IsNotNull(vkr.Arguments);
        }