示例#1
0
        /// <summary>
        /// Loads RunConfiguration from XmlReader.
        /// </summary>
        /// <param name="reader">XmlReader having run configuration node.</param>
        /// <returns></returns>
        public static RunConfiguration FromXml(XmlReader reader)
        {
            ValidateArg.NotNull <XmlReader>(reader, "reader");
            var runConfiguration = new RunConfiguration();
            var empty            = reader.IsEmptyElement;

            XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

            // Process the fields in Xml elements
            reader.Read();
            if (!empty)
            {
                while (reader.NodeType == XmlNodeType.Element)
                {
                    string elementName = reader.Name;
                    switch (elementName)
                    {
                    case "ResultsDirectory":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
                        runConfiguration.ResultsDirectory = reader.ReadElementContentAsString();
                        break;

                    case "MaxCpuCount":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

                        string cpuCount = reader.ReadElementContentAsString();
                        int    count;
                        if (!int.TryParse(cpuCount, out count) || count < 0)
                        {
                            throw new SettingsException(
                                      string.Format(
                                          CultureInfo.CurrentCulture,
                                          Resources.Resources.InvalidSettingsIncorrectValue,
                                          Constants.RunConfigurationSettingsName,
                                          cpuCount,
                                          elementName));
                        }

                        runConfiguration.MaxCpuCount = count;
                        break;

                    case "BatchSize":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

                        string batchSize = reader.ReadElementContentAsString();
                        long   size;
                        if (!long.TryParse(batchSize, out size) || size < 0)
                        {
                            throw new SettingsException(
                                      string.Format(
                                          CultureInfo.CurrentCulture,
                                          Resources.Resources.InvalidSettingsIncorrectValue,
                                          Constants.RunConfigurationSettingsName,
                                          batchSize,
                                          elementName));
                        }

                        runConfiguration.BatchSize = size;
                        break;

                    case "DesignMode":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

                        string designModeValueString = reader.ReadElementContentAsString();
                        bool   designMode;
                        if (!bool.TryParse(designModeValueString, out designMode))
                        {
                            throw new SettingsException(String.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, designModeValueString, elementName));
                        }
                        runConfiguration.DesignMode = designMode;
                        break;

                    case "DisableAppDomain":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

                        string disableAppDomainValueString = reader.ReadElementContentAsString();
                        bool   disableAppDomainCheck;
                        if (!bool.TryParse(disableAppDomainValueString, out disableAppDomainCheck))
                        {
                            throw new SettingsException(String.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, disableAppDomainValueString, elementName));
                        }
                        runConfiguration.DisableAppDomain = disableAppDomainCheck;
                        break;

                    case "DisableParallelization":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

                        string disableParallelizationValueString = reader.ReadElementContentAsString();
                        bool   disableParallelizationCheck;
                        if (!bool.TryParse(disableParallelizationValueString, out disableParallelizationCheck))
                        {
                            throw new SettingsException(String.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, disableParallelizationValueString, elementName));
                        }
                        runConfiguration.DisableParallelization = disableParallelizationCheck;
                        break;

                    case "TargetPlatform":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
                        Architecture archType;
                        string       value = reader.ReadElementContentAsString();
                        try
                        {
                            archType = (Architecture)Enum.Parse(typeof(Architecture), value, true);
                            if (archType != Architecture.X64 && archType != Architecture.X86 && archType != Architecture.ARM)
                            {
                                throw new SettingsException(
                                          string.Format(
                                              CultureInfo.CurrentCulture,
                                              Resources.Resources.InvalidSettingsIncorrectValue,
                                              Constants.RunConfigurationSettingsName,
                                              value,
                                              elementName));
                            }
                        }
                        catch (ArgumentException)
                        {
                            throw new SettingsException(string.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, value, elementName));
                        }

                        runConfiguration.TargetPlatform = archType;
                        break;

                    case "TargetFrameworkVersion":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
                        Framework frameworkType;
                        value = reader.ReadElementContentAsString();
                        try
                        {
                            frameworkType = Framework.FromString(value);

                            if (frameworkType == null)
                            {
                                throw new SettingsException(
                                          string.Format(
                                              CultureInfo.CurrentCulture,
                                              Resources.Resources.InvalidSettingsIncorrectValue,
                                              Constants.RunConfigurationSettingsName,
                                              value,
                                              elementName));
                            }
                        }
                        catch (ArgumentException)
                        {
                            throw new SettingsException(string.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, value, elementName));
                        }

                        runConfiguration.TargetFrameworkVersion = frameworkType;
                        break;

                    case "TestAdaptersPaths":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
                        runConfiguration.TestAdaptersPaths = reader.ReadElementContentAsString();
                        break;

                    case "TreatTestAdapterErrorsAsWarnings":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
                        bool treatTestAdapterErrorsAsWarnings = false;

                        value = reader.ReadElementContentAsString();

                        try
                        {
                            treatTestAdapterErrorsAsWarnings = bool.Parse(value);
                        }
                        catch (ArgumentException)
                        {
                            throw new SettingsException(string.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, value, elementName));
                        }
                        catch (FormatException)
                        {
                            throw new SettingsException(string.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, value, elementName));
                        }

                        runConfiguration.TreatTestAdapterErrorsAsWarnings = treatTestAdapterErrorsAsWarnings;
                        break;

                    case "SolutionDirectory":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
                        string solutionDirectory = reader.ReadElementContentAsString();
                        solutionDirectory = Environment.ExpandEnvironmentVariables(solutionDirectory);
                        if (string.IsNullOrEmpty(solutionDirectory) || !Directory.Exists(solutionDirectory))
                        {
                            if (EqtTrace.IsErrorEnabled)
                            {
                                EqtTrace.Error(string.Format(CultureInfo.CurrentCulture, Resources.Resources.SolutionDirectoryNotExists, solutionDirectory));
                            }

                            solutionDirectory = null;
                        }

                        runConfiguration.SolutionDirectory = solutionDirectory;

                        break;

                    case "BinariesRoot":
                        XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
                        runConfiguration.BinariesRoot = reader.ReadElementContentAsString();
                        break;

                    default:
                        // Ignore a runsettings element that we don't understand. It could occur in the case
                        // the test runner is of a newer version, but the test host is of an earlier version.
                        if (EqtTrace.IsErrorEnabled)
                        {
                            EqtTrace.Warning(
                                string.Format(
                                    CultureInfo.CurrentCulture,
                                    Resources.Resources.InvalidSettingsXmlElement,
                                    Constants.RunConfigurationSettingsName,
                                    reader.Name));
                        }
                        reader.Skip();
                        break;
                    }
                }

                reader.ReadEndElement();
            }

            return(runConfiguration);
        }