public void CanBindObject()
        {
            MockConfigurationSectionSetting setting = CreateMockConfigurationSectionSetting("Foo", "Bar");

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

            setting.Publish();

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

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

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

                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void RevokedInstanceIsNotReturnedInQuery()
        {
            MockConfigurationSectionSetting setting = CreateMockConfigurationSectionSetting("Foo", "Bar");

            setting.Publish();

            using (ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockConfigurationSectionSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Foo", resultEnumerator.Current.Properties["ApplicationName"].Value);
                Assert.AreEqual("Bar", resultEnumerator.Current.Properties["SectionName"].Value);
                Assert.AreEqual("MockConfigurationSectionSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }

            setting.Revoke();

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

            setting.Changed += this.Changed;

            setting.Publish();

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

                ManagementObject managementObject = (ManagementObject)resultEnumerator.Current;
                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()
        {
            MockConfigurationSectionSetting setting = CreateMockConfigurationSectionSetting("Foo", "Bar");

            setting.Publish();

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

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

            using (ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockConfigurationSectionSetting")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Bar", resultEnumerator.Current.Properties["SectionName"].Value);
                Assert.AreEqual("MockConfigurationSectionSetting", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void CanPublishInstancesOfSiblingClassesWithTheSameKey()
        {
            MockConfigurationSectionSetting  setting  = CreateMockConfigurationSectionSetting("Foo", "Bar");
            MockConfigurationSectionSettingB setting2 = CreateMockConfigurationSectionSettingB("Foo", "Bar");

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

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

            using (ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockConfigurationSectionSettingB")
                         .Get().GetEnumerator())
            {
                Assert.IsTrue(resultEnumerator.MoveNext());
                Assert.AreEqual("Bar", resultEnumerator.Current.Properties["SectionName"].Value);
                Assert.AreEqual("MockConfigurationSectionSettingB", resultEnumerator.Current.SystemProperties["__CLASS"].Value);
                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void RevokedInstanceIsNotReturnedWhenMultipleInstancesArePublished()
        {
            MockConfigurationSectionSetting setting  = CreateMockConfigurationSectionSetting("Foo", "Bar");
            MockConfigurationSectionSetting setting2 = CreateMockConfigurationSectionSetting("Foo", "Bar2");

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

            setting.Revoke();

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

            nameMapping.Add(setting2.SectionName, setting2);

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

            Assert.AreEqual(0, nameMapping.Count, "Not all elements were retrieved");
        }
        public void RevokeForNonPublishedInstanceIsIgnored()
        {
            MockConfigurationSectionSetting setting  = CreateMockConfigurationSectionSetting("Foo", "Bar");
            MockConfigurationSectionSetting setting2 = CreateMockConfigurationSectionSetting("Foo", "Bar2");

            setting.Publish();
            setting2.Revoke();
        }
        private static MockConfigurationSectionSetting CreateMockConfigurationSectionSetting(string applicationName,
                                                                                             string sectionName)
        {
            MockConfigurationSectionSetting setting = new MockConfigurationSectionSetting();

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

            setting.Publish();

            using (ManagementObjectCollection.ManagementObjectEnumerator resultEnumerator
                       = new ManagementObjectSearcher("root\\enterpriselibrary", "SELECT * FROM MockConfigurationSectionSettingB")
                         .Get().GetEnumerator())
            {
                Assert.IsFalse(resultEnumerator.MoveNext());
            }
        }
        public void PublishInstanceWithDuplicateKeyThrows()
        {
            MockConfigurationSectionSetting setting  = CreateMockConfigurationSectionSetting("Foo", "Bar");
            MockConfigurationSectionSetting setting2 = CreateMockConfigurationSectionSetting("Foo", "Bar");

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