Microsoft.Protocols.TestSuites.Common.Common.MergeConfigurationFile C# (CSharp) 메소드

MergeConfigurationFile() 공개 정적인 메소드

Merge the properties from the specified ptfconfig file.
public static MergeConfigurationFile ( string configFilename, ITestSite site ) : void
configFilename string ptfconfig filename.
site ITestSite An instance of interface ITestSite which provides logging, assertions, /// and adapters for test code onto its execution context.
리턴 void
        public static void MergeConfigurationFile(string configFilename, ITestSite site)
        {
            if (!File.Exists(configFilename))
            {
                throw new FileNotFoundException(string.Format("The ptfconfig file '{0}' could not be found.", configFilename));
            }

            XmlNodeList properties = null;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(configFilename);
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("tc", "http://schemas.microsoft.com/windows/ProtocolsTest/2007/07/TestConfig");
                properties = doc.DocumentElement.SelectNodes("//tc:Property", nsmgr);
                if (properties == null)
                {
                    return;
                }
            }
            catch (XmlException exception)
            {
                throw new InvalidOperationException(
                    string.Format("Merging the ptfconfig file '{0}' failed. It is an invalid XML file." + exception.Message, configFilename),
                    exception);
            }

            foreach (XmlNode property in properties)
            {
                string propertyName;
                string propertyValue;

                if (property.Attributes["name"] == null || string.IsNullOrEmpty(property.Attributes["name"].Value))
                {
                    throw new PtfConfigLoadException(
                        string.Format(
                            "A property defined in the ptfconfig file '{0}' has a missing or an empty 'name' attribute.",
                            configFilename));
                }
                else
                {
                    propertyName = property.Attributes["name"].Value;
                }

                if (property.Attributes["value"] == null)
                {
                    throw new PtfConfigLoadException(
                        string.Format(
                            "Property '{0}' defined in the ptfconfig file '{1}' has a missing 'value' attribute.",
                            propertyName,
                            configFilename));
                }
                else
                {
                    propertyValue = property.Attributes["value"].Value;
                }

                if (site.Properties[propertyName] == null)
                {
                    site.Properties.Add(propertyName, propertyValue);
                }
                else if (configFilename.Contains("SHOULDMAY"))
                {
                    // Same property should not exist in both the test suite specific ptfconfig file and the SHOULD/MAY ptfconfig file.
                    throw new PtfConfigLoadException(
                        string.Format(
                            "Same property '{0}' was found in both the local ptfconfig file and the SHOULD/MAY ptfconfig file '{1}'. " +
                            "It cannot exist in the local ptfconfig file.",
                            propertyName,
                            configFilename));
                }
                else
                {
                    // Since the test suite specific ptfconfig file should take precedence over the global ptfconfig file, 
                    // when the same property exists in both, the global ptfconfig property is ignored.
                    site.Log.Add(
                        LogEntryKind.Warning,
                        string.Format(
                            "Same property '{0}' exists in both the local ptfconfig file and the global ptfconfig file '{1}'. " +
                            "Test suite is using the one from the local ptfconfig file.",
                            propertyName,
                            configFilename));

                    continue;
                }
            }
        }

Usage Example

예제 #1
0
        /// <summary>
        /// Merge the properties from the SHOULD/MAY ptfconfig file according to the supported products and specified protocol short name.
        /// </summary>
        /// <param name="supportedProducts">A parameter represents the all supported products.</param>
        /// <param name="supportedProductsRelatedRequirementId">A parameter represents the id of the requirement which specifies the supported products.</param>
        /// <param name="site">An instance of interface ITestSite which provides logging, assertions, and adapters for test code onto its execution context.</param>
        /// <param name="shortName">A parameter represents the current protocol short name. If this parameter is string.empty or null, this method will not use the parameter.</param>
        public static void MergeSHOULDMAYConfig(string[] supportedProducts, int supportedProductsRelatedRequirementId, ITestSite site, string shortName)
        {
            if (null == supportedProducts)
            {
                throw new ArgumentNullException("supportedProducts");
            }

            if (supportedProductsRelatedRequirementId <= 0)
            {
                throw new ArgumentException("The value should be greater than Zero.", "supportedProductsRelatedRequirementId");
            }

            SutVersion currentSutVersion       = Common.GetConfigurationPropertyValue <SutVersion>("SutVersion", site);
            string     currentSutVersionValue  = currentSutVersion.ToString();
            bool       isCurrentSutSupported   = supportedProducts.Any(supportedProduct => supportedProduct.Equals(currentSutVersionValue, StringComparison.OrdinalIgnoreCase));
            string     requirementPropertyName = string.Empty;
            string     actualShortNameValue    = string.Empty;

            if (string.IsNullOrEmpty(shortName))
            {
                requirementPropertyName = string.Format("R{0}Enabled", supportedProductsRelatedRequirementId);
                actualShortNameValue    = site.DefaultProtocolDocShortName;
            }
            else
            {
                requirementPropertyName = string.Format("R{0}Enabled_{1}", supportedProductsRelatedRequirementId, shortName);
                actualShortNameValue    = shortName;
            }

            if (isCurrentSutSupported)
            {
                string shouldMayConfigFilename = string.Format("{0}_{1}_SHOULDMAY.deployment.ptfconfig", actualShortNameValue, currentSutVersionValue);
                Common.MergeConfigurationFile(shouldMayConfigFilename, site);
                site.Log.Add(LogEntryKind.Debug, "Use {0} file for optional requirements configuration", shouldMayConfigFilename);
                bool isExpectedPropertyExist = site.Properties.AllKeys.Any(property => property.Equals(requirementPropertyName, StringComparison.OrdinalIgnoreCase));
                if (!isExpectedPropertyExist)
                {
                    site.Assert.Fail("There should be a property [{0}].", requirementPropertyName);
                }
            }
            else
            {
                // If the current SUT version does not support the protocol, this method add a R***enable property into the properties collection.
                bool isExpectedPropertyExist = site.Properties.AllKeys.Any(property => property.Equals(requirementPropertyName, StringComparison.OrdinalIgnoreCase));
                if (isExpectedPropertyExist)
                {
                    site.Properties[requirementPropertyName] = bool.FalseString;
                }
                else
                {
                    site.Properties.Add(requirementPropertyName, bool.FalseString);
                }
            }
        }
Common