public void CanBindObject()
        {
            MockNamedConfigurationSetting setting = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");

            setting.Changed      += this.Changed;
            setting.SourceElement = new TestConfigurationSection();             // just so Commit works

            setting.Publish();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Baz", resultEnumerator.Current.Properties["Name"].Value);
                Assert.AreEqual("MockNamedConfigurationSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);

                ManagementObject managementObject = resultEnumerator.Current as ManagementObject;
                Assert.IsNotNull(managementObject);

                Assert.IsNull(this.changedObject);
                managementObject.Put();
                Assert.IsNotNull(this.changedObject);

                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void RevokedInstanceIsNotReturnedInQuery()
        {
            MockNamedConfigurationSetting setting = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");

            setting.Publish();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Baz", resultEnumerator.Current.Properties["Name"].Value);
                Assert.AreEqual("MockNamedConfigurationSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }

            setting.Revoke();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        [Ignore] // not sure why this is not working.
        public void BindOnRevokedInstanceThrows()
        {
            MockNamedConfigurationSetting setting = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");

            setting.Changed += this.Changed;

            setting.Publish();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Baz", resultEnumerator.Current.Properties["Name"].Value);
                Assert.AreEqual("MockNamedConfigurationSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);

                ManagementObject managementObject = resultEnumerator.Current as ManagementObject;
                Assert.IsNotNull(managementObject);

                Assert.IsNull(this.changedObject);
                setting.Revoke();
                try
                {
                    managementObject.Put();
                    Assert.Fail("should have thrown");
                }
                catch (ManagementException e)
                {
                    Assert.AreEqual(ManagementStatus.ProviderNotCapable, e.ErrorCode);
                }
            }
        }
        public void SecondPublishIsNoOp()
        {
            MockNamedConfigurationSetting setting = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");

            setting.Publish();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Baz", resultEnumerator.Current.Properties["Name"].Value);
                Assert.AreEqual("MockNamedConfigurationSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }

            // second publish, should not throw but should return single instance
            setting.Publish();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Baz", resultEnumerator.Current.Properties["Name"].Value);
                Assert.AreEqual("MockNamedConfigurationSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void CanPublishInstancesOfSiblingClassesWithTheSameKey()
        {
            MockNamedConfigurationSetting  setting  = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");
            MockNamedConfigurationSettingB setting2 = CreateMockNamedConfigurationSettingB("Baz", "Foo", "Bar");

            setting.Publish();
            setting2.Publish();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Baz", resultEnumerator.Current.Properties["Name"].Value);
                Assert.AreEqual("MockNamedConfigurationSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSettingB")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Baz", resultEnumerator.Current.Properties["Name"].Value);
                Assert.AreEqual("MockNamedConfigurationSettingB", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void RevokedInstanceIsNotReturnedWhenMultipleInstancesArePublished()
        {
            MockNamedConfigurationSetting setting  = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");
            MockNamedConfigurationSetting setting2 = CreateMockNamedConfigurationSetting("Baz2", "Foo", "Bar");

            setting.Publish();
            setting2.Publish();

            setting.Revoke();

            IDictionary <string, MockNamedConfigurationSetting> nameMapping = new Dictionary <string, MockNamedConfigurationSetting>();

            nameMapping.Add(setting2.Name, setting2);

            using (System.Management.ManagementObjectCollection resultCollection
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSetting")
                         .Get())
            {
                foreach (ManagementObject current in resultCollection)
                {
                    Assert.AreEqual("MockNamedConfigurationSetting", current.SystemProperties["__CLASS"].Value);
                    Assert.IsTrue(nameMapping.Remove((string)current.Properties["Name"].Value), "unknown name");
                }
            }

            Assert.AreEqual(0, nameMapping.Count, "Not all elements were retrieved");
        }
        public void RevokeForNonPublishedInstanceIsIgnored()
        {
            MockNamedConfigurationSetting setting  = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");
            MockNamedConfigurationSetting setting2 = CreateMockNamedConfigurationSetting("Baz2", "Foo", "Bar");

            setting.Publish();
            setting2.Revoke();
        }
        private static MockNamedConfigurationSetting CreateMockNamedConfigurationSetting(string name, string applicationName, string sectionName)
        {
            MockNamedConfigurationSetting setting = new MockNamedConfigurationSetting(name);

            setting.ApplicationName = applicationName;
            setting.SectionName     = sectionName;
            return(setting);
        }
        public void ReturnsEmptyResultForNonPublishedClassWithPublishedSibling()
        {
            MockNamedConfigurationSetting setting = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");

            setting.Publish();

            using (System.Management.ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockNamedConfigurationSettingB")
                         .Get().GetEnumerator())
            {
                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void PublishInstanceWithDuplicateKeyThrows()
        {
            MockNamedConfigurationSetting setting  = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");
            MockNamedConfigurationSetting setting2 = CreateMockNamedConfigurationSetting("Baz", "Foo", "Bar");

            setting.Publish();
            try
            {
                setting2.Publish();
                Assert.Fail("should have thrown");
            }
            catch (ArgumentException) { }
        }