/// <summary>
        /// Creates a new instance of InteractiveAdapterDialog class.
        /// </summary>
        /// <param name="methodCall">An IMessage that contains a IDictionary of information about the method call.</param>
        /// <param name="ptfProp">A NameValueCollection of settings from the test configuration file.</param>
        /// <param name="args">args.</param>
        public InteractiveAdapterConsole(MethodInfo methodCall, NameValueCollection ptfProp, object[] args)
        {
            if (methodCall == null)
            {
                throw new ArgumentNullException("methodCall");
            }

            if (ptfProp == null)
            {
                throw new ArgumentNullException("ptfProp");
            }

            // Stores all arguments for generating the 'OutArgs' property,
            // since ReturnMessage needs all arguments including in args.
            outArgs = methodCall.GetGenericArguments();

            // Change caption
            this.Text = methodCall.Name;

            // Set the help message
            this.HelperMessage = AdapterProxyHelpers.GetHelpMessage(methodCall);

            // Set the properties
            this.properties = ptfProp;

            // Set data grid views
            builder      = BuildInputParamsCommand(methodCall, args);
            paraCommands = new List <ParaCommandInfo>();
        }
        private int GetValueFromConsole(string prompt, Type type)
        {
            string keyVaule = ConsoleHelper.ReadFromConsole(prompt);

            try
            {
                var result = "" + AdapterProxyHelpers.ParseResult(type, keyVaule);
                if (string.IsNullOrEmpty(result) || result.Equals("0"))
                {
                    return(0);
                }
                else
                {
                    return(1);
                }
            }
            catch (FormatException)
            {
                var originalColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("The return value is invalid, please input a valid value.");
                Console.ForegroundColor = originalColor;
                return(GetValueFromConsole(prompt, type));
            }
        }
示例#3
0
        /// <summary>
        /// Creates a new instance of InteractiveAdapterDialog class.
        /// </summary>
        /// <param name="methodCall">An IMessage that contains a IDictionary of information about the method call.</param>
        /// <param name="ptfProp">A NameValueCollection of settings from the test configuration file.</param>
        public InteractiveAdapterDialog(IMethodCallMessage methodCall, NameValueCollection ptfProp)
        {
            InitializeComponent();

            if (methodCall == null)
            {
                throw new ArgumentNullException("methodCall");
            }

            if (ptfProp == null)
            {
                throw new ArgumentNullException("ptfProp");
            }

            // Stores all arguments for generating the 'OutArgs' property,
            // since ReturnMessage needs all arguments including in args.
            outArgs = methodCall.Args;

            // Change caption
            this.Text = methodCall.MethodName;

            // Set the help message
            this.textHelpMessage.Text = AdapterProxyHelpers.GetHelpMessage(methodCall);

            // Set the properties
            this.properties = ptfProp;

            // Set data grid views
            builder = BindGridToDataTable(
                methodCall,
                dataGridViewActionPara);

            // Adjust the controls
            if (!builder.HasInArg)
            {
                labelActionParameters.Visible         = false;
                dataGridViewActionPara.Visible        = false;
                tableLayoutPanel1.RowStyles[2].Height = 5;
                tableLayoutPanel1.RowStyles[3].Height = 5;
            }

            if ((!builder.HasReturnVal) && (!builder.HasOutArg))
            {
                labelActionResult.Visible             = false;
                tableActionResult.Visible             = false;
                tableLayoutPanel1.RowStyles[4].Height = 5;
                tableLayoutPanel1.RowStyles[5].Height = 5;
            }
        }
示例#4
0
        private object GetReturnValue()
        {
            string    strValue; // The string value can be parsed by 'Parse' method.
            DataTable dt     = builder.OutArgDataTable;
            object    retVal = null;

            if (builder.HasReturnVal)
            {
                if (dt.Rows[0][1] is DBNull)
                {
                    throw new InvalidOperationException(
                              string.Format("The {0} return value is expected.", builder.RetValType));
                }
                else
                {
                    strValue = (string)dt.Rows[0]["Value"];
                }
                retVal = AdapterProxyHelpers.ParseResult(builder.RetValType, strValue);
            }
            return(retVal);
        }
示例#5
0
        private void GetOutArgumentsValues()
        {
            string    strValue; // The string value can be parsed by 'Parse' method.
            DataTable dt    = builder.OutArgDataTable;
            int       count = dt.Rows.Count;
            int       i     = builder.HasReturnVal ? 1 : 0;
            int       j     = 0; // Indexing out argument position of the passed-in arguments.

            foreach (Type t in builder.OutArgTypes)
            {
                object o = (object)dt.Rows[i++][1];
                if (o is DBNull)
                {
                    strValue = String.Empty;
                }
                else
                {
                    strValue = (string)o;
                }
                outArgs[builder.OutArgIndexes[j]] = AdapterProxyHelpers.ParseResult(t, strValue);
                j++;
            }
        }
示例#6
0
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="targetMethod">The method the caller invoked.</param>
        /// <param name="args">The arguments the caller passed to the method.</param>
        /// <returns>The return value of the ExecuteMethod implementation.</returns>
        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            object retVal = null;

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName) &&
                (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "Managed adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 targetMethod.Name);

                try
                {
                    int           timeout    = AdapterProxyHelpers.GetTimeout(targetMethod, int.Parse(TestSite.Properties["AdapterInvokeTimeout"]));
                    Task <object> invokeTask = Task.Run <object>(() =>
                    {
                        TestSite.Log.Add(LogEntryKind.Debug, $"Start to invoke target method {targetMethod.Name}, timeout: {timeout}");
                        object invokeResult;
                        if (targetMethod.IsStatic)
                        {
                            invokeResult = targetMethod.Invoke(null, args);
                        }
                        else
                        {
                            invokeResult = targetMethod.Invoke(instance, args);
                        }
                        TestSite.Log.Add(LogEntryKind.Debug, $"Complete invoke target method {targetMethod.Name}.");
                        return(invokeResult);
                    });

                    TimeSpan waiter = TimeSpan.FromMinutes(timeout);

                    if (invokeTask.Wait(waiter))
                    {
                        retVal = invokeTask.Result;
                    }
                    else
                    {
                        throw new TimeoutException($"Invoke adapater method timeout after wait {timeout} minutes.");
                    }
                }
                catch (Exception ex)
                {
                    if (ex is TargetInvocationException)
                    {
                        // thrown by methods invoked through reflection
                        // InnerException contains the actual exception thrown by methods
                        TestSite.Log.Add(LogEntryKind.Debug, ex.InnerException.ToString());
                        throw ex.InnerException;
                    }
                    else
                    {
                        TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                        throw;
                    }
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "Managed adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     targetMethod.Name);
                }
            }

            return(retVal);
        }
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="targetMethod">The method the caller invoked.</param>
        /// <param name="args">The arguments the caller passed to the method.</param>
        /// <returns>The return value of the ExecuteMethod implementation.</returns>
        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            //get help message from attribute
            string methodhelp = AdapterProxyHelpers.GetHelpMessage(targetMethod);

            bool compactMode = ((targetMethod.Name == "Initialize" || targetMethod.Name == "Reset") &&
                                AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName)
                                );

            if (compactMode)
            {
                return(ExecuteMethodCompact(targetMethod, methodhelp));
            }

            object retVal = null;

            object[] outArgs = args;

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName) &&
                (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "PowerShell adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 targetMethod.Name);

                try
                {
                    string path = LookupScript(targetMethod.Name);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "PowerShell script file ({0}.ps1) can not be found.",
                            targetMethod.Name);
                    }
                    else
                    {
                        PSParameterBuilder builder = InvokeScript(path, targetMethod, args, methodhelp);
                        if (builder != null)
                        {
                            retVal = builder.RetValue;

                            if (builder.OutArguments != null)
                            {
                                int argsIndex    = 0;
                                int outArgsIndex = 0;
                                foreach (ParameterInfo pi in targetMethod.GetParameters())
                                {
                                    if (pi.ParameterType.IsByRef)
                                    {
                                        outArgs[argsIndex] = builder.OutArguments[outArgsIndex++];
                                    }
                                    argsIndex++;
                                }
                            }

                            //clear builder
                            builder = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "PowerShell adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     targetMethod.Name);
                }
            }

            return(retVal);
        }
示例#8
0
        protected override IMessage Invoke(IMethodCallMessage methodCall)
        {
            //get help message from attribute
            string methodhelp = AdapterProxyHelpers.GetHelpMessage(methodCall);

            bool compactMode = ((methodCall.MethodName == "Initialize" || methodCall.MethodName == "Reset") &&
                                AdapterType.IsAdapterTypeFullName(methodCall.MethodBase.DeclaringType.FullName)
                                );

            if (compactMode)
            {
                return(InvokeCompact(methodCall, methodhelp));
            }

            object retVal = null;

            object[] outArgs = methodCall.Args;

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(methodCall.MethodBase.DeclaringType.FullName) &&
                (methodCall.MethodBase.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "Power Shell adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 methodCall.MethodName);

                try
                {
                    string path = LookupScript(methodCall.MethodName);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "PowerShell script file ({0}.ps1) can not be found.",
                            methodCall.MethodName);
                    }
                    else
                    {
                        PSParameterBuilder builder = InvokeScript(path, methodCall, methodhelp);
                        if (builder != null)
                        {
                            retVal = builder.RetValue;

                            if (builder.OutArguments != null)
                            {
                                int argsIndex    = 0;
                                int outArgsIndex = 0;
                                foreach (ParameterInfo pi in methodCall.MethodBase.GetParameters())
                                {
                                    if (pi.ParameterType.IsByRef)
                                    {
                                        outArgs[argsIndex] = builder.OutArguments[outArgsIndex++];
                                    }
                                    argsIndex++;
                                }
                            }

                            //clear builder
                            builder = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "Power Shell adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     methodCall.MethodName);
                }
            }

            ReturnMessage mret = new ReturnMessage(
                retVal,
                (outArgs != null && outArgs.Length > 0) ? outArgs : null,
                (outArgs != null) ? outArgs.Length : 0,
                methodCall.LogicalCallContext,
                methodCall);

            return(mret);
        }
示例#9
0
        private void GetOutArguments()
        {
            int       firstOutArgIndex = 0;
            string    strValue; // The string value can be parsed by 'Parse' method.
            DataTable dt    = builder.OutArgDataTable;
            int       count = dt.Rows.Count;

            if (builder.HasReturnVal)
            {
                if (dt.Rows[0][1] is DBNull)
                {
                    strValue = String.Empty;
                }
                else
                {
                    strValue = (string)dt.Rows[0][1];
                }

                try
                {
                    returnValue      = AdapterProxyHelpers.ParseResult(builder.RetValType, strValue);
                    firstOutArgIndex = 1;
                }
                catch (FormatException)
                {
                    MessageBox.Show("The return value is invalid, please input a valid value.",
                                    "Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error,
                                    MessageBoxDefaultButton.Button1,
                                    0);
                    return;
                }
            }

            int i = firstOutArgIndex;
            int j = 0; // Indexing out argument position of the passed-in arguments.

            foreach (Type t in builder.OutArgTypes)
            {
                object o = (object)dt.Rows[i++][1];
                if (o is DBNull)
                {
                    strValue = String.Empty;
                }
                else
                {
                    strValue = (string)o;
                }

                try
                {
                    outArgs[builder.OutArgIndexes[j]] = AdapterProxyHelpers.ParseResult(t, strValue);
                    j++;
                }
                catch (FormatException)
                {
                    MessageBox.Show(String.Format("The input value for out argument \"{0}\" is not valid, please input a valid value.",
                                                  (string)builder.OutArgDataTable.Rows[firstOutArgIndex + j].ItemArray[0]),
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                    return;
                }
            }

            // The btnSucceed.DialogResult is not set in InteractiveAdapterUI.designer.cs,
            // so this Modal Dialog would not be closed automatically when click the Succeed button.
            // We need to set the form's DialogResult property to DialogResult.OK to close this form.
            this.DialogResult = DialogResult.OK;
        }
示例#10
0
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="mcall">The IMethodCallMessage containing method invoking data.</param>
        /// <returns>The IMessage containing method return data.</returns>
        protected override IMessage Invoke(IMethodCallMessage mcall)
        {
            // set to compat Mode to allow running of Initialize.cmd or Reset.cmd if call from IAdapter context
            compatMode = ((mcall.MethodName == "Initialize" || mcall.MethodName == "Reset") &&
                          AdapterType.IsAdapterTypeFullName(mcall.MethodBase.DeclaringType.FullName)
                          );
            if (compatMode)
            {
                return(InvokeCompat(mcall));
            }

            // Build parameter on each invoke
            builder = new ParameterDataBuilder(mcall);
            builder.Build();

            // Initial error message on each invoke
            ptfAdFailureMessage = null;

            object retVal = null;

            outArgs = mcall.Args;
            string methodhelp = AdapterProxyHelpers.GetHelpMessage(mcall);
            string arguments  = BuildScriptArguments(methodhelp);

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(mcall.MethodBase.DeclaringType.FullName) &&
                (mcall.MethodBase.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "Script adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 mcall.MethodName);

                try
                {
                    string path = LookupScript(mcall.MethodName);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "The invoking script file ({0}.cmd) can not be found.",
                            mcall.MethodName);
                    }
                    else
                    {
                        int scriptRet = InvokeScript(path, arguments);
                        if (scriptRet != 0)
                        {
                            TestSite.Assume.Fail(
                                "Script {0}.cmd exited with non-zero code. Error code: {1}. Failure message: {2}",
                                mcall.MethodName,
                                scriptRet,
                                ptfAdFailureMessage);
                        }
                        retVal = GetReturnValue();
                        GetOutArgumentsValues();
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "Script adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     mcall.MethodName);
                }
            }

            ReturnMessage mret = new ReturnMessage(
                retVal,
                (outArgs != null && outArgs.Length > 0) ? outArgs : null,
                (outArgs != null) ? outArgs.Length : 0,
                mcall.LogicalCallContext,
                mcall);

            return(mret);
        }
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="targetMethod">The method the caller invoked.</param>
        /// <param name="args">The arguments the caller passed to the method.</param>
        /// <returns>The return value of the ExecuteMethod implementation.</returns>
        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            //get help message from attribute
            string methodhelp = AdapterProxyHelpers.GetHelpMessage(targetMethod);

            bool compactMode = ((targetMethod.Name == "Initialize" || targetMethod.Name == "Reset") &&
                                AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName)
                                );

            if (compactMode)
            {
                return(ExecuteMethodCompact(targetMethod, methodhelp));
            }

            object retVal = null;

            object[] outArgs = args;

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName) &&
                (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "PowerShell adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 targetMethod.Name);

                try
                {
                    string path = LookupScript(targetMethod.Name);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "PowerShell script file ({0}.ps1) can not be found.",
                            targetMethod.Name);
                    }
                    else
                    {
                        int timeout = AdapterProxyHelpers.GetTimeout(targetMethod, int.Parse(TestSite.Properties["AdapterInvokeTimeout"]));
                        Task <PSParameterBuilder> invokeTask = Task.Run <PSParameterBuilder>(() =>
                        {
                            TestSite.Log.Add(LogEntryKind.Debug, $"Start to invoke Script {targetMethod.Name}.ps1, timeout: {timeout}");
                            var invokeResult = InvokeScript(path, targetMethod, args, methodhelp);
                            TestSite.Log.Add(LogEntryKind.Debug, $"Complete execute Script {targetMethod.Name}.ps1");
                            return(invokeResult);
                        });

                        TimeSpan waiter = TimeSpan.FromMinutes(timeout);

                        if (invokeTask.Wait(waiter))
                        {
                            PSParameterBuilder resultBuilder = invokeTask.Result;
                            if (resultBuilder != null)
                            {
                                retVal = resultBuilder.RetValue;

                                if (resultBuilder.OutArguments != null)
                                {
                                    int argsIndex    = 0;
                                    int outArgsIndex = 0;
                                    foreach (ParameterInfo pi in targetMethod.GetParameters())
                                    {
                                        if (pi.ParameterType.IsByRef)
                                        {
                                            outArgs[argsIndex] = resultBuilder.OutArguments[outArgsIndex++];
                                        }
                                        argsIndex++;
                                    }
                                }

                                //clear builder
                                resultBuilder = null;
                            }
                        }
                        else
                        {
                            throw new TimeoutException($"Invoke adapater method timeout after wait {timeout} minutes.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "PowerShell adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     targetMethod.Name);
                }
            }

            return(retVal);
        }
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="mcall">The IMethodCallMessage containing method invoking data.</param>
        /// <returns>The IMessage containing method return data.</returns>
        protected override IMessage Invoke(IMethodCallMessage mcall)
        {
            // set to compat Mode to allow running of Initialize.sh or Reset.sh if call from IAdapter context
            compatMode = ((mcall.MethodName == "Initialize" || mcall.MethodName == "Reset") &&
                          AdapterType.IsAdapterTypeFullName(mcall.MethodBase.DeclaringType.FullName)
                          );
            if (compatMode)
            {
                return(InvokeCompat(mcall));
            }

            // Build parameter on each invoke
            builder = new ParameterDataBuilder(mcall);
            builder.Build();

            outArgs    = mcall.Args;
            lastOutput = null;
            errorMsg   = new StringBuilder();

            object retVal    = null;
            string arguments = BuildScriptArguments();

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(mcall.MethodBase.DeclaringType.FullName) &&
                (mcall.MethodBase.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "Shell adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 mcall.MethodName);

                try
                {
                    string path = LookupScript(mcall.MethodName);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "Shell script file ({0}.sh) can not be found.",
                            mcall.MethodName);
                    }
                    else
                    {
                        int scriptRet = InvokeScript(path, arguments);

                        if (scriptRet != 0)
                        {
                            string exceptionMessage = string.Format("Exception thrown when executing {0}.sh.\nExit code: {1}\nError message: {2}\n", mcall.MethodName, scriptRet, errorMsg);
                            throw new InvalidOperationException(exceptionMessage);
                        }

                        if (builder.HasReturnVal)
                        {
                            retVal = AdapterProxyHelpers.ParseResult(builder.RetValType, lastOutput);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "Shell adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     mcall.MethodName);
                }
            }

            ReturnMessage mret = new ReturnMessage(
                retVal,
                (outArgs != null && outArgs.Length > 0) ? outArgs : null,
                (outArgs != null) ? outArgs.Length : 0,
                mcall.LogicalCallContext,
                mcall);

            return(mret);
        }
示例#13
0
        /// <summary>
        /// Proxy method for substitution of executing methods in adapter interface.
        /// </summary>
        /// <param name="targetMethod">The method the caller invoked.</param>
        /// <param name="args">The arguments the caller passed to the method.</param>
        /// <returns>The return value of the ExecuteMethod implementation.</returns>
        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            //get help message from attribute
            string methodhelp = AdapterProxyHelpers.GetHelpMessage(targetMethod);

            bool compactMode = ((targetMethod.Name == "Initialize" || targetMethod.Name == "Reset") &&
                                AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName)
                                );

            if (compactMode)
            {
                return(ExecuteMethodCompact(targetMethod, methodhelp));
            }

            // Build parameter on each invoke
            builder = new ParameterDataBuilder(targetMethod);
            builder.Build(args);

            lastOutput = null;
            errorMsg   = new StringBuilder();

            object retVal    = null;
            string arguments = BuildScriptArguments();

            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName) &&
                (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                                 "Shell adapter: {0}, method: {1}",
                                 ProxyType.Name,
                                 targetMethod.Name);

                try
                {
                    string path = LookupScript(targetMethod.Name);
                    if (path == null)
                    {
                        TestSite.Assume.Fail(
                            "Shell script file ({0}.sh) can not be found.",
                            targetMethod.Name);
                    }
                    else
                    {
                        int timeout = AdapterProxyHelpers.GetTimeout(targetMethod, int.Parse(TestSite.Properties["AdapterInvokeTimeout"]));

                        Task <int> invokeTask = Task.Run <int>(() =>
                        {
                            TestSite.Log.Add(LogEntryKind.Debug, $"Start to invoke shell {targetMethod.Name}.sh, timeout: {timeout}");
                            int invokeResult = InvokeScript(path, arguments);
                            TestSite.Log.Add(LogEntryKind.Debug, $"Complete execute shell {targetMethod.Name}.sh");

                            return(invokeResult);
                        });

                        TimeSpan waiter = TimeSpan.FromMinutes(timeout);
                        if (invokeTask.Wait(waiter))
                        {
                            if (invokeTask.Result != 0)
                            {
                                string exceptionMessage = string.Format("Exception thrown when executing {0}.sh.\nExit code: {1}\nError message: {2}\n", targetMethod.Name, invokeTask.Result, errorMsg);
                                throw new InvalidOperationException(exceptionMessage);
                            }

                            if (builder.HasReturnVal)
                            {
                                retVal = AdapterProxyHelpers.ParseResult(builder.RetValType, lastOutput);
                            }
                        }
                        else
                        {
                            throw new TimeoutException($"Invoke adapater method timeout after wait {timeout} minutes.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                    throw;
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                                     "Shell adapter: {0}, method: {1}",
                                     ProxyType.Name,
                                     targetMethod.Name);
                }
            }
            return(retVal);
        }