Example #1
0
        public void RemovedHandlerReceivesNoFurtherNotifications()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);
            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);

            instance.RemoveSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            valuesForNotifiedSections.Clear();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
Example #2
0
        public void ChangeOnMainConfigurationFileNotifiesHandlersForAllSections()
        {
            // if the main configuration file changes, sections with specified config sources might have changed
            // since the configuration object must be reload entirely, and config sections with specified config sources are
            // rare, notifying all the sections is a sensible trade off

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);
            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
Example #3
0
        public void RegisteredForChangesOnLocalReceivesNotificationsOnMainConfigurationFileUpdateWithUpdatedValuesAvailable()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.GetSection(localSection1);
            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);

            TestsConfigurationSection rwTestConfigurationSection
                = (TestsConfigurationSection)rwConfiguration.GetSection(localSection1);

            rwTestConfigurationSection.Value = "value2";
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("value2", valuesForNotifiedSections[localSection1]);
        }
Example #4
0
        public void CanGetUpdatedConfigurationValuesAfterFileChangeNotification()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper(localSection1, externalSection1);

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);
            manageabilityHelper.updateCalled = false;

            TestsConfigurationSection testConfigurationSection
                = (TestsConfigurationSection)instance.GetSection(localSection1);

            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value1", testConfigurationSection.Value);

            TestsConfigurationSection rwTestConfigurationSection
                = (TestsConfigurationSection)rwConfiguration.GetSection(localSection1);

            rwTestConfigurationSection.Value = "value2";
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            testConfigurationSection
                = (TestsConfigurationSection)instance.GetSection(localSection1);
            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value2", testConfigurationSection.Value);
            Assert.AreNotSame(rwTestConfigurationSection, testConfigurationSection);
        }
Example #5
0
        [Ignore] // TODO temporary ignore
        public void CanCreateConfigurationSourceFromConfigurationElement()
        {
            ManageableConfigurationSourceElement element
                = new ManageableConfigurationSourceElement(
                      "manageable",
                      AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
                      "testapp",
                      true);

            element.ConfigurationManageabilityProviders.Add(new ConfigurationSectionManageabilityProviderData("section1", typeof(MockConfigurationSectionManageabilityProvider)));
            element.ConfigurationManageabilityProviders.Get(0).ManageabilityProviders.Add(new ConfigurationElementManageabilityProviderData("1", typeof(MockConfigurationElementManageabilityProvider), typeof(String)));
            element.ConfigurationManageabilityProviders.Add(new ConfigurationSectionManageabilityProviderData("section2", typeof(MockConfigurationSectionManageabilityProvider)));
            element.ConfigurationManageabilityProviders.Get(1).ManageabilityProviders.Add(new ConfigurationElementManageabilityProviderData("2", typeof(MockConfigurationElementManageabilityProvider), typeof(Boolean)));
            element.ConfigurationManageabilityProviders.Get(1).ManageabilityProviders.Add(new ConfigurationElementManageabilityProviderData("3", typeof(MockConfigurationElementManageabilityProvider), typeof(Int32)));

            IConfigurationSource configurationSource = element.CreateSource();

            Assert.IsNotNull(configurationSource);
            Assert.AreSame(typeof(ManageableConfigurationSource), configurationSource.GetType());

            ManageableConfigurationSourceImplementation implementation
                = ((ManageableConfigurationSource)configurationSource).Implementation;

            Assert.AreSame(typeof(ManageabilityHelper), implementation.ManageabilityHelper.GetType());
            ManageabilityHelper manageabilityHelper = (ManageabilityHelper)implementation.ManageabilityHelper;

            Assert.AreEqual(2, manageabilityHelper.ManageabilityProviders.Count);
        }
Example #6
0
        public void CanGetInstanceFromHelper()
        {
            string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            ManageableConfigurationSourceImplementation configSourceImpl1
                = helper.GetInstance(fullConfigurationFilepath, noProviders, true, true, "app");

            Assert.IsNotNull(configSourceImpl1);
        }
        public void CanCreateInstance()
        {
            Dictionary <string, ConfigurationSectionManageabilityProvider> manageabilityProviders
                = new Dictionary <string, ConfigurationSectionManageabilityProvider>();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       false,
                                                                       manageabilityProviders,
                                                                       true,
                                                                       "TestApplication");
        }
Example #8
0
        public void ConfigurationFilePathsAreResolvedBeforeImplementationIsCreated()
        {
            string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            string configurationFilename     = Path.GetFileName(fullConfigurationFilepath);
            ManageableConfigurationSourceImplementation configSourceImpl1
                = helper.GetInstance(configurationFilename, noProviders, true, true, "app");
            ManageableConfigurationSourceImplementation configSourceImpl2
                = helper.GetInstance(fullConfigurationFilepath, noProviders, true, true, "app");

            Assert.AreSame(configSourceImpl1, configSourceImpl2);
        }
        public void CanCreateInstance()
        {
            Dictionary<string, ConfigurationSectionManageabilityProvider> manageabilityProviders
                = new Dictionary<string, ConfigurationSectionManageabilityProvider>();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       false,
                                                                       manageabilityProviders,
                                                                       true,
                                                                       "TestApplication");
        }
Example #10
0
        public void InstanceCreationAttachesConfigurationSettingChangedEvent()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.addHandlerCalled);
        }
Example #11
0
        public void SecondRequestForDifferentParametersReturnsDifferentInstance()
        {
            string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            ManageableConfigurationSourceImplementation configSourceImpl1
                = helper.GetInstance(fullConfigurationFilepath, noProviders, true, true, "app");
            ManageableConfigurationSourceImplementation configSourceImpl2
                = helper.GetInstance(fullConfigurationFilepath, noProviders, false, true, "app");

            Assert.IsNotNull(configSourceImpl1);
            Assert.IsNotNull(configSourceImpl2);
            Assert.AreNotSame(configSourceImpl1, configSourceImpl2);
        }
        public void CanCreateInstanceWithGivenImplementation()
        {
            ManageableConfigurationSourceImplementation implementation
                = new ManageableConfigurationSourceImplementation("test.config",
                                                                  new MockManageabilityHelper(),
                                                                  new GroupPolicyWatcher(),
                                                                  new ConfigurationChangeWatcherCoordinator("test.config", false),
                                                                  new ConfigurationChangeNotificationCoordinator());
            ManageableConfigurationSource configurationSource
                = new ManageableConfigurationSource(implementation);

            Assert.AreSame(implementation, configurationSource.Implementation);
        }
        public void RequestForNullSectionThrows()
        {
            ManageableConfigurationSourceImplementation implementation
                = new ManageableConfigurationSourceImplementation("test.config",
                                                                  new MockManageabilityHelper(),
                                                                  new GroupPolicyWatcher(),
                                                                  new ConfigurationChangeWatcherCoordinator("test.config", false),
                                                                  new ConfigurationChangeNotificationCoordinator());
            ManageableConfigurationSource configurationSource
                = new ManageableConfigurationSource(implementation);

            configurationSource.GetSection(null);
        }
Example #14
0
        public void ChangeOnMainConfigurationFileNotifiesHandlerForRestoredSection()
        {
            // if the section existed once, then registered handlers must be notified when the removal
            // is detected, but after that they shouldn't be notified while the section is still deleted
            // and they should be notified if the configuration section shows

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            TestsConfigurationSection section = (TestsConfigurationSection)instance.GetSection(localSection1);

            Assert.IsNotNull(section);

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);

            rwConfiguration.Sections.Remove(localSection1);
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual(null, valuesForNotifiedSections[localSection1]);

            valuesForNotifiedSections.Clear();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(0, valuesForNotifiedSections.Count);                        // second time, no notification

            // restore the section and notify again
            rwConfiguration.Sections.Add(localSection1, new TestsConfigurationSection("valuenew"));
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("valuenew", valuesForNotifiedSections[localSection1]);
        }
Example #15
0
        public void InstanceCreationDoesNotAddWatchersForNonExistingManagedSections()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper("external.section.1.nonexisting");

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
        }
Example #16
0
        public void InstanceCreationFiresManageabilityInitialization()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
        }
        public void CanCreateInstanceWithGivenImplementation()
        {
            ManageableConfigurationSourceImplementation implementation
                = new ManageableConfigurationSourceImplementation("test.config",
                                                                  new MockManageabilityHelper(),
                                                                  new GroupPolicyWatcher(),
                                                                  new ConfigurationChangeWatcherCoordinator("test.config", false),
                                                                  new ConfigurationChangeNotificationCoordinator());

            ManageableConfigurationSource configurationSource
                = new ManageableConfigurationSource(implementation);

            Assert.AreSame(implementation, configurationSource.Implementation);
        }
        public void CanReadSectionFromConfigurationSource()
        {
            ManageableConfigurationSourceImplementation implementation
                = new ManageableConfigurationSourceImplementation("test.config",
                                                                  new MockManageabilityHelper(),
                                                                  new GroupPolicyWatcher(),
                                                                  new ConfigurationChangeWatcherCoordinator("test.config", false),
                                                                  new ConfigurationChangeNotificationCoordinator());
            ManageableConfigurationSource configurationSource
                = new ManageableConfigurationSource(implementation);
            TestsConfigurationSection section = (TestsConfigurationSection)configurationSource.GetSection("local.section.1");

            Assert.IsNotNull(section);
            Assert.AreEqual("value1", section.Value);
        }
        public void CanReadSectionFromConfigurationSource()
        {
            ManageableConfigurationSourceImplementation implementation
                = new ManageableConfigurationSourceImplementation("test.config",
                                                                  new MockManageabilityHelper(),
                                                                  new GroupPolicyWatcher(),
                                                                  new ConfigurationChangeWatcherCoordinator("test.config", false),
                                                                  new ConfigurationChangeNotificationCoordinator());

            ManageableConfigurationSource configurationSource
                = new ManageableConfigurationSource(implementation);

            TestsConfigurationSection section = (TestsConfigurationSection)configurationSource.GetSection("local.section.1");
            Assert.IsNotNull(section);
            Assert.AreEqual("value1", section.Value);
        }
Example #20
0
        public void CanGetSection()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper(localSection1, externalSection1);

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Mocks.TestsConfigurationSection testConfigurationSection
                = (Mocks.TestsConfigurationSection)instance.GetSection(externalSection1);

            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value1", testConfigurationSection.Value);
        }
Example #21
0
        public void ChangeInConfigurationFileFiresManageabilityUpdate()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper(localSection1, externalSection1);

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);
            manageabilityHelper.updateCalled = false;

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.IsTrue(manageabilityHelper.updateCalled);
        }
Example #22
0
        public void ChangeOnMainConfigurationFileDoesNotNotifyHandlerForNonExistingSection()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler("non existing", OnConfigurationSectionChange);
            ConfigurationSection section = instance.GetSection("non existing");

            Assert.IsNull(section);

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(0, valuesForNotifiedSections.Count);
        }
Example #23
0
        public void ChangeOnMainConfigurationFileDoesNotNotifyHandlerForExistingSectionNeverRequested()
        {
            // if the section has never been requested, then there's no point in notifying
            // for its (possible) changes.

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();                // make sure it's not requested

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(0, valuesForNotifiedSections.Count);
        }
Example #24
0
        public void RequestForNonExistingSectionDoesNotAddWatcher()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Mocks.TestsConfigurationSection testConfigurationSection
                = (Mocks.TestsConfigurationSection)instance.GetSection("non.existing");

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
        }
Example #25
0
        public void ChangeOnGroupPoliciesNotifiesHandlersForAllSections()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);

            policyRegistrationBuilder.LastRegistration.MachinePolicyEvent.Set();                // fire the notification
            Thread.Sleep(250);

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
Example #26
0
        public void WatcherForNonManagedSectionIsSetOnFirstRequest()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Mocks.TestsConfigurationSection testConfigurationSection
                = (Mocks.TestsConfigurationSection)instance.GetSection(externalSection1);

            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value1", testConfigurationSection.Value);
            Assert.AreEqual(2, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains("test.external.config"));
        }
Example #27
0
        public void ChangeOnExternalConfigurationFileNotifiesHandlersForOtherRestoredSections()
        {
            // if a removed section is detected it should be notified when the external file changes
            // as processing the main change file later will break the logic that deals with deletions

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);

            rwConfiguration.Sections.Remove(localSection1);
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs("test.external.config"));

            rwConfiguration.Sections.Add(localSection1, new TestsConfigurationSection("valuerestored"));
            rwConfiguration.Save();

            valuesForNotifiedSections.Clear();
            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs("test.external.config"));

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("valuerestored", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
        public void RequestForNullSectionThrows()
        {
            ManageableConfigurationSourceImplementation implementation
                = new ManageableConfigurationSourceImplementation("test.config",
                                                                  new MockManageabilityHelper(),
                                                                  new GroupPolicyWatcher(),
                                                                  new ConfigurationChangeWatcherCoordinator("test.config", false),
                                                                  new ConfigurationChangeNotificationCoordinator());

            ManageableConfigurationSource configurationSource
                = new ManageableConfigurationSource(implementation);

            configurationSource.GetSection(null);
        }
        public void ChangeOnMainConfigurationFileNotifiesHandlerForRestoredSection()
        {
            // if the section existed once, then registered handlers must be notified when the removal
            // is detected, but after that they shouldn't be notified while the section is still deleted
            // and they should be notified if the configuration section shows

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            TestsConfigurationSection section = (TestsConfigurationSection)instance.GetSection(localSection1);
            Assert.IsNotNull(section);

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);

            rwConfiguration.Sections.Remove(localSection1);
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual(null, valuesForNotifiedSections[localSection1]);

            valuesForNotifiedSections.Clear();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(0, valuesForNotifiedSections.Count); // second time, no notification

            // restore the section and notify again
            rwConfiguration.Sections.Add(localSection1, new TestsConfigurationSection("valuenew"));
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("valuenew", valuesForNotifiedSections[localSection1]);
        }
        public void ChangeOnMainConfigurationFileForRemovedInternalSectionDoesNotAffectWatchers()
        {
            // the main file watcher is never removed, so removed internal sections don't affect the watchers

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);

            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains("test.external.config"));

            rwConfiguration.Sections.Remove(localSection1);
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(2, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains("test.external.config"));
        }
        public void ChangeOnExternalConfigurationFileNotifiesHandlersForOtherRestoredSections()
        {
            // if a removed section is detected it should be notified when the external file changes
            // as processing the main change file later will break the logic that deals with deletions

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);

            rwConfiguration.Sections.Remove(localSection1);
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs("test.external.config"));

            rwConfiguration.Sections.Add(localSection1, new TestsConfigurationSection("valuerestored"));
            rwConfiguration.Save();

            valuesForNotifiedSections.Clear();
            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs("test.external.config"));

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("valuerestored", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
        public void CanGetUpdatedConfigurationValuesAfterFileChangeNotification()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper(localSection1, externalSection1);

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);
            manageabilityHelper.updateCalled = false;

            TestsConfigurationSection testConfigurationSection
                = (TestsConfigurationSection)instance.GetSection(localSection1);
            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value1", testConfigurationSection.Value);

            TestsConfigurationSection rwTestConfigurationSection
                = (TestsConfigurationSection)rwConfiguration.GetSection(localSection1);
            rwTestConfigurationSection.Value = "value2";
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            testConfigurationSection
                = (TestsConfigurationSection)instance.GetSection(localSection1);
            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value2", testConfigurationSection.Value);
            Assert.AreNotSame(rwTestConfigurationSection, testConfigurationSection);
        }
        public void ChangeOnMainConfigurationFileDoesNotNotifyHandlerForNonExistingSection()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler("non existing", OnConfigurationSectionChange);
            ConfigurationSection section = instance.GetSection("non existing");
            Assert.IsNull(section);

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(0, valuesForNotifiedSections.Count);
        }
        public void RequestForNonExistingSectionDoesNotAddWatcher()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
        }
        public void ChangeOnExternalConfigurationFileNotifiesHandlersForTheExternalSectionOnlyIfNoOtherSectionsWereRemoved()
        {
            // if the file for a section's config source changes only that section can be affected.
            // even though the configuration object is reloaded entirely, changes for other sections are not notified
            // if there were any changes in other sections, then other configuration files would have changed and
            // notifications for these changes would be issued before of after the section's configuration source file
            // i.e. the appropriate notifications will be issued, eventually
            // the only exception is for removed or restored sections, tested elsewhere

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);
            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs("test.external.config"));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
        public void WatcherForNonManagedSectionIsSetOnFirstRequest()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            TestsConfigurationSection testConfigurationSection
                = (TestsConfigurationSection)instance.GetSection(externalSection1);

            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value1", testConfigurationSection.Value);
            Assert.AreEqual(2, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains("test.external.config"));
        }
        public void RemovedHandlerReceivesNoFurtherNotifications()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);
            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);

            instance.RemoveSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            valuesForNotifiedSections.Clear();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
        public void InstanceCreationFiresManageabilityInitialization()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
        }
        public void ChangeOnGroupPoliciesNotifiesHandlersForAllSections()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);

            Thread.Sleep(100); // let the watching thread start
            policyRegistrationBuilder.LastRegistration.MachinePolicyEvent.Set(); // fire the notification
            Thread.Sleep(250);

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
        public void ChangeOnMainConfigurationFileNotifiesHandlersForAllSections()
        {
            // if the main configuration file changes, sections with specified config sources might have changed
            // since the configuration object must be reload entirely, and config sections with specified config sources are
            // rare, notifying all the sections is a sensible trade off

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);
            instance.AddSectionChangeHandler(externalSection1, OnConfigurationSectionChange);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);
            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(2, valuesForNotifiedSections.Count);
            Assert.AreEqual("value1", valuesForNotifiedSections[localSection1]);
            Assert.AreEqual("value1", valuesForNotifiedSections[externalSection1]);
        }
        public void ChangeInConfigurationFileFiresManageabilityUpdate()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper(localSection1, externalSection1);

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);
            manageabilityHelper.updateCalled = false;

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.IsTrue(manageabilityHelper.updateCalled);
        }
        public void RegisteredForChangesOnLocalReceivesNotificationsOnMainConfigurationFileUpdateWithUpdatedValuesAvailable()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.GetSection(localSection1);
            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);

            TestsConfigurationSection rwTestConfigurationSection
                = (TestsConfigurationSection)rwConfiguration.GetSection(localSection1);
            rwTestConfigurationSection.Value = "value2";
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, valuesForNotifiedSections.Count);
            Assert.AreEqual("value2", valuesForNotifiedSections[localSection1]);
        }
        public void InstanceCreationAddsWatchersForManagedSections()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper("external.section.1");

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            Assert.IsTrue(manageabilityHelper.updateCalled);
            Assert.AreEqual(2, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains("test.external.config"));
        }
        public void ChangeOnMainConfigurationFileDoesNotNotifyHandlerForExistingSectionNeverRequested()
        {
            // if the section has never been requested, then there's no point in notifying
            // for its (possible) changes.

            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper(); // make sure it's not requested

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.AddSectionChangeHandler(localSection1, OnConfigurationSectionChange);

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(0, valuesForNotifiedSections.Count);
        }
        public void ChangeOnMainConfigurationFileForRemovedExternalSectionRemovesWatcherOnExternalFile()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper();

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            instance.GetSection(localSection1);
            instance.GetSection(externalSection1);

            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains("test.external.config"));

            rwConfiguration.Sections.Remove(externalSection1);
            rwConfiguration.Save();

            watcherCoordinator.OnConfigurationChanged(null,
                                                      new ConfigurationChangedEventArgs(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));

            Assert.AreEqual(1, watcherCoordinator.WatchedConfigSources.Count);
            Assert.IsTrue(watcherCoordinator.WatchedConfigSources.Contains(ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource));
        }
        public void CanGetSection()
        {
            MockManageabilityHelper manageabilityHelper = new MockManageabilityHelper(localSection1, externalSection1);

            instance = new ManageableConfigurationSourceImplementation(testConfigurationFile,
                                                                       manageabilityHelper,
                                                                       groupPolicyWatcher,
                                                                       watcherCoordinator,
                                                                       notificationCoordinator);

            TestsConfigurationSection testConfigurationSection
                = (TestsConfigurationSection)instance.GetSection(externalSection1);

            Assert.IsNotNull(testConfigurationSection);
            Assert.AreEqual("value1", testConfigurationSection.Value);
        }