/// <summary>
        /// Create an instance of the powershell adapter.
        /// </summary>
        /// <typeparam name="T">The type of the adapter.</typeparam>
        /// <param name="scriptDirectory">The folder containing the script files.</param>
        /// <param name="typeToProxy">The type of the adapter.</param>
        /// <returns>The powershell adapter</returns>
        public static T Wrap <T>(string scriptDirectory, Type typeToProxy) where T : IAdapter
        {
            object proxy = Create <T, PowerShellAdapterProxy>();
            PowerShellAdapterProxy self = (PowerShellAdapterProxy)proxy;

            AdapterProxyBase.SetParameters(self, typeToProxy);
            self.scriptDirectory = scriptDirectory.Replace("\\", "/");

            return((T)proxy);
        }
示例#2
0
        /// <summary>
        /// Implements <see cref="ITestSite.GetAdapter"/>
        /// </summary>
        /// <remarks>
        /// For script and interactive adapter, test site provides the default implementations in PTF.
        /// For managed adapter, test site provides the class instances according to the configuration, and if no class type is defined, it returns null.
        /// The <see cref="IAdapter.Initialize"/> method is automatically called before the instances is returned.
        /// </remarks>
        /// <typeparam name="T">The type of the adapter.</typeparam>
        /// <returns>An adapter instance of the given type.</returns>
        public virtual T GetAdapter <T>() where T : IAdapter
        {
            // Set default value for compiling.
            T    adapter     = default(T);
            Type adapterType = typeof(T);

            if (this.configData == null)
            {
                throw new InvalidOperationException("Configuration files is not present");
            }

            // Check if target type adapter is already created.
            if (adapters.ContainsKey(adapterType))
            {
                return((T)adapters[adapterType]);
            }

            // Get target adapter type.
            AdapterConfig adapterConfig = this.configData.GetAdapterConfig(adapterType.Name);

            if (adapterConfig is InteractiveAdapterConfig)
            {
                adapter = InteractiveAdapterProxy.Wrap <T>(adapterType);
            }
            // Create proxy for PowerShell script type adapter
            else if (adapterConfig is PowerShellAdapterConfig)
            {
                string scriptDir = Path.Combine(ptfconfigDirectory, ((PowerShellAdapterConfig)adapterConfig).ScriptDir);
                adapter = PowerShellAdapterProxy.Wrap <T>(
                    scriptDir,
                    adapterType);
            }

            // Create proxy for Shell script type adapter
            else if (adapterConfig is ShellAdapterConfig)
            {
                string scriptDir = ((ShellAdapterConfig)adapterConfig).ScriptDir;
                adapter = ShellAdapterProxy.Wrap <T>(
                    scriptDir,
                    adapterType);
            }

            // Create instance for dot net type adapter.
            if (adapterConfig is ManagedAdapterConfig)
            {
                try
                {
                    string adapterTypeName = ((ManagedAdapterConfig)adapterConfig).AdapterType;
                    if (adapterType.IsGenericType)
                    {
                        IAdapter instance = TestToolHelpers.CreateInstanceFromTypeName(adapterTypeName) as IAdapter;
                        adapter = (T)instance;
                    }
                    else
                    {
                        Type adapterImplType = TestToolHelpers.ResolveTypeFromAssemblies(adapterTypeName, testAssemblyDirectory);
                        if (adapterImplType == null)
                        {
                            throw new InvalidOperationException(
                                      String.Format("Can't find assembly \"{0}\"", adapterTypeName));
                        }

                        adapter = ManagedAdapterProxy.Wrap <T>(adapterImplType, adapterType);
                    }
                    // adapter is null if as operator fails due to an object can't be converted to IAdapter type
                    if (adapter == null)
                    {
                        throw new InvalidOperationException(
                                  String.Format("Adapter {0} does not implement {1}",
                                                adapterTypeName, adapterType.FullName));
                    }
                }
                catch (InvalidOperationException ex)
                {
                    throw new InvalidOperationException(
                              String.Format("Adapter {0} instance creation failed. Reason:{1}",
                                            adapterType.FullName, ex.ToString()));
                }
                catch (FileLoadException e)
                {
                    throw new InvalidOperationException(
                              String.Format("The assembly of the adapter ({0}) could not be loaded.", adapterType.Name), e);
                }
                catch (FileNotFoundException e)
                {
                    throw new InvalidOperationException(
                              String.Format("The assembly of the adapter ({0}) could not be found.", adapterType.Name), e);
                }
                catch (ArgumentException e)
                {
                    throw new InvalidOperationException(
                              String.Format("The type of the adapter ({0}) could not be found.", adapterType.Name), e);
                }
            }

            if (adapter == null)
            {
                throw new InvalidOperationException(String.Format("Failed while creating the adapter: {0}", adapterType.Name));
            }

            adapters.Add(adapterType, adapter);
            adapter.Initialize(this);

            return(adapter);
        }