示例#1
0
        /// <summary>
        /// Gets the adapter specific settings from the xml.
        /// </summary>
        /// <param name="runSettingsXml"> The xml with the settings passed from the test platform. </param>
        /// <param name="settingName"> The name of the adapter settings to fetch - Its either MSTest or MSTestV2 </param>
        /// <returns> The settings if found. Null otherwise. </returns>
        internal static MSTestSettings GetSettings(string runSettingsXml, string settingName)
        {
            if (string.IsNullOrWhiteSpace(runSettingsXml))
            {
                return(null);
            }

            using (var stringReader = new StringReader(runSettingsXml))
            {
                XmlReader reader = XmlReader.Create(stringReader, XmlRunSettingsUtilities.ReaderSettings);

                // read to the fist child
                XmlReaderUtilities.ReadToRootNode(reader);
                reader.ReadToNextElement();

                // Read till we reach nodeName element or reach EOF
                while (!string.Equals(reader.Name, settingName, StringComparison.OrdinalIgnoreCase)
                       &&
                       !reader.EOF)
                {
                    reader.SkipToNextElement();
                }

                if (!reader.EOF)
                {
                    // read nodeName element.
                    return(ToSettings(reader.ReadSubtree()));
                }
            }

            return(null);
        }
        private static T GetNodeValue <T>(string settingsXml, string nodeName, Func <XmlReader, T> nodeParser)
        {
            // use XmlReader to avoid loading of the plugins in client code (mainly from VS).
            if (!string.IsNullOrWhiteSpace(settingsXml))
            {
                using (StringReader stringReader = new StringReader(settingsXml))
                {
                    XmlReader reader = XmlReader.Create(stringReader, ReaderSettings);

                    // read to the fist child
                    XmlReaderUtilities.ReadToRootNode(reader);
                    reader.ReadToNextElement();

                    // Read till we reach nodeName element or reach EOF
                    while (!string.Equals(reader.Name, nodeName, StringComparison.OrdinalIgnoreCase)
                           &&
                           !reader.EOF)
                    {
                        reader.SkipToNextElement();
                    }

                    if (!reader.EOF)
                    {
                        // read nodeName element.
                        return(nodeParser(reader));
                    }
                }
            }

            return(default(T));
        }
示例#3
0
        /// <summary>
        /// Reads test run settings from XmlReader
        /// </summary>
        /// <param name="reader"></param>
        private void ReadRunSettings(XmlReader reader)
        {
            // If settings have already been loaded, throw.
            if (this.isSettingsLoaded)
            {
                throw new InvalidOperationException(CommonResources.RunSettingsAlreadyLoaded);
            }

            this.isSettingsLoaded = true;

            try
            {
                // Read to the root element.
                XmlReaderUtilities.ReadToRootNode(reader);

                // Read to the first section.
                reader.ReadToNextElement();

                // Lookup the settings provider for each of the elements.
                var settingsExtensionManager = SettingsProviderExtensionManager.Create();
                while (!reader.EOF)
                {
                    this.LoadSection(reader, settingsExtensionManager);
                    reader.SkipToNextElement();
                }
            }
            catch (SettingsException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new SettingsException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              CommonResources.RunSettingsParseError,
                              e.Message),
                          e);
            }
        }