Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args">Named arguments used in "Where" field of WQL query</param>
        /// <returns></returns>
        public override Object CollectData(IDictionary <String, Object> args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            if (this.conf.PerfCounters == null)
            {
                return(null);
            }

            String condition = String.Empty;

            condition = NamedArgumentHelper.Resolve(this.conf.Where, this.conf.WhereArgs, args);

            if (this.conf.PerfCounters == null || this.conf.PerfCounters.Count == 0)
            {
                return(null);
            }

            using (var result = WMIHelper.QueryInstacnes(this.conf.Namespace, this.conf.From, condition, this.selectedProperties))
            {
                var mgmtObjs = result.Cast <ManagementObject>();
                if (this.conf.ReturnValueType == MgmtObjectReturnValueType.Single)
                {
                    var mgmtObj = mgmtObjs.FirstOrDefault();
                    return(SelectPerfCounter(mgmtObj, args));
                }
                else
                {
                    var list = new List <Object>();
                    foreach (var mgmtObj in mgmtObjs)
                    {
                        list.Add(SelectPerfCounter(mgmtObj, args));
                    }
                    return(list);
                }
            }
        }
        public void TestResolveNamedArgument()
        {
            var str      = "MaxClockSpeed_{0}_{1}";
            var expected = "MaxClockSpeed_ubuntu_0";
            var argsName = new List <WhereArgConfiguration>()
            {
                new WhereArgConfiguration()
                {
                    Name = "VirtualMachine"
                },
                new WhereArgConfiguration()
                {
                    Name = "ProcessorId"
                }
            };
            var args = new Dictionary <String, Object>()
            {
                { "VirtualMachine", "ubuntu" },
                { "ProcessorId", 0 }
            };

            Assert.AreEqual(expected, NamedArgumentHelper.Resolve(str, argsName, args));
        }
        public void TestResolveNamedArgumentNagativeCase()
        {
            var str      = "MaxClockSpeed_{0}_{1}";
            var argsName = new List <WhereArgConfiguration>()
            {
                new WhereArgConfiguration()
                {
                    Name = "VirtualMachine"
                },
                new WhereArgConfiguration()
                {
                    Name = "ProcessorId"
                }
            };
            var args = new Dictionary <String, Object>()
            {
                { "VirtualMachine", "ubuntu" }
            };

            //Argument name is null
            try
            {
                NamedArgumentHelper.Resolve(str, null, args);
                Assert.Fail("FormatException is expected");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
            }

            //Argument names are less than cells
            try
            {
                NamedArgumentHelper.Resolve(str, new List <WhereArgConfiguration>()
                {
                    new WhereArgConfiguration()
                    {
                        Name = "VirtualMachine"
                    }
                }, args);
                Assert.Fail("FormatException is expected");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
            }

            //Name argument map is null
            try
            {
                NamedArgumentHelper.Resolve(str, argsName, null);
                Assert.Fail("ArgumentException is expected");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
            }

            //Named argument not found
            try
            {
                NamedArgumentHelper.Resolve(str, argsName, args);
                Assert.Fail("ArgumentException is expected");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
            }
        }