Example #1
0
        /// <summary>
        /// Create an instance of the managed adapter.
        /// </summary>
        /// <typeparam name="T">The type of the adapter.</typeparam>
        /// <param name="adapterType">The type of the implementation.</param>
        /// <param name="typeToProxy">The type of the adapter.</param>
        /// <returns>The managed adapter</returns>
        public static T Wrap <T>(Type adapterType, Type typeToProxy) where T : IAdapter
        {
            object proxy             = Create <T, ManagedAdapterProxy>();
            ManagedAdapterProxy self = (ManagedAdapterProxy)proxy;

            AdapterProxyBase.SetParameters((ManagedAdapterProxy)proxy, typeToProxy);
            self.trueType = adapterType;

            try
            {
                self.instance = TestToolHelpers.CreateInstanceFromTypeName(adapterType.FullName) as ManagedAdapterBase;
            }
            catch (InvalidOperationException)
            {
            }

            if (self.instance == null)
            {
                throw new InvalidOperationException(
                          String.Format("Adapter {0} instance creation failed",
                                        typeToProxy.FullName));
            }
            else if (!typeToProxy.IsAssignableFrom(self.instance.GetType()))
            {
                throw new InvalidOperationException(
                          String.Format("Adapter {0} does not implement {1}",
                                        adapterType.Name, typeToProxy.FullName));
            }

            return((T)proxy);
        }
Example #2
0
        /// <summary>
        /// Constructs a new managed adapter proxy.
        /// </summary>
        /// <param name="adapterType">The managed adapter type</param>
        /// <param name="typeToProxy">The type of adapter which the proxy works for.</param>
        public ManagedAdapterProxy(Type adapterType, Type typeToProxy)
            : base(typeToProxy)
        {
            trueType = adapterType;
            try
            {
                instance = TestToolHelpers.CreateInstanceFromTypeName(adapterType.FullName) as ManagedAdapterBase;
            }
            catch (InvalidOperationException)
            {
            }

            if (instance == null)
            {
                throw new InvalidOperationException(
                          String.Format("Adapter {0} instance creation failed",
                                        typeToProxy.FullName));
            }
            else if (!typeToProxy.IsAssignableFrom(instance.GetType()))
            {
                throw new InvalidOperationException(
                          String.Format("Adapter {0} does not implement {1}",
                                        adapterType.Name, typeToProxy.FullName));
            }
        }
Example #3
0
        private void InvokeActions(Type attribute, object testClass)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException("attribute");
            }
            if (testClass == null)
            {
                throw new ArgumentNullException("testClass");
            }
            IList <MethodInfo> actions = new List <MethodInfo>();
            Type testClassType         = testClass.GetType();

            if (attribute == typeof(ProtocolTestCleanupAttribute))
            {
                if (!this.cleanupActions.ContainsKey(testClassType))
                {
                    this.cleanupActions[testClassType] =
                        TestToolHelpers.GetMethodsByAttribute(attribute, testClassType, true);
                }
                actions = this.cleanupActions[testClassType];
            }
            else if (attribute == typeof(ProtocolTestInitializeAttribute))
            {
                if (!this.initializeActions.ContainsKey(testClassType))
                {
                    this.initializeActions[testClassType] =
                        TestToolHelpers.GetMethodsByAttribute(attribute, testClassType, true);
                }
                actions = this.initializeActions[testClassType];
            }
            else
            {
                throw new InvalidOperationException(
                          "Unexpected attribute found while invoking protocol testing actions.");
            }
            //invoke all actions
            foreach (MethodInfo mi in actions)
            {
                // we don't use MethodInfo.Invoke() because
                // that will wrap any exceptions thrown from the method
                // being invoked into TargetInvocationException.
                Action action = (Action)Delegate.CreateDelegate(
                    typeof(Action),
                    testClass,
                    mi);

                action();
            }
        }
Example #4
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);
        }