Inheritance: ConfigSourceBase
        public void Flattened() {
            RegistryConfigSource source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine,
                              "Software\\NiniTestApp",
                              RegistryRecurse.Flattened);

            IConfig config = source.Configs["NiniTestApp"];
            Assert.AreEqual("Configuration Library", config.Get("Description"));

            config = source.Configs["Pets"];
            Assert.AreEqual("Pets", config.Name);
            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual(source, config.ConfigSource);

            Assert.AreEqual("Chi-chi", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual(5, config.GetInt("count"));

            config.Set("cat", "Muffy");
            config.Set("dog", "Spots");
            config.Set("count", 4);

            source.Save();

            source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            config = source.Configs["Pets"];
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual(4, config.GetInt("count"));
        }
        public void SetAndSaveNormal() {
            RegistryConfigSource source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            IConfig config = source.Configs["Pets"];
            Assert.AreEqual("Chi-chi", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual(5, config.GetInt("count"));

            config.Set("cat", "Muffy");
            config.Set("dog", "Spots");
            config.Set("DoesNotExist", "SomeValue");
            config.Set("count", 4);

            source.Save();

            source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            config = source.Configs["Pets"];
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual("SomeValue", config.Get("DoesNotExist"));
            Assert.AreEqual(4, config.GetInt("count"));
        }
        public void MergeAndSave()
        {
            RegistryConfigSource source = new RegistryConfigSource();

            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            IConfig config = source.Configs["Pets"];

            Assert.AreEqual("Chi-chi", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual(5, config.GetInt("count"));

            StringWriter writer = new StringWriter();

            writer.WriteLine("[Pets]");
            writer.WriteLine("cat = Becky");     // overwrite
            writer.WriteLine("lizard = Saurus"); // new
            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            writer.WriteLine(" man = John");
            IniConfigSource iniSource = new IniConfigSource
                                            (new StringReader(writer.ToString()));

            source.Merge(iniSource);

            config = source.Configs["Pets"];
            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = source.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Jane", config.Get("woman"));
            Assert.AreEqual("John", config.Get("man"));

            config.Set("woman", "Tara");
            config.Set("man", "Quentin");

            source.Save();

            source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            config = source.Configs["Pets"];
            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = source.Configs["People"];
            Assert.IsNull(config); // you cannot merge new sections

            /*
             *          Assert.AreEqual (2, config.GetKeys ().Length);
             *          Assert.AreEqual ("Tara", config.Get ("woman"));
             *          Assert.AreEqual ("Quentin", config.Get ("man"));
             */
        }
        public void AddConfigNoDefaultKey()
        {
            RegistryConfigSource source = new RegistryConfigSource();

            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            IConfig config = source.AddConfig("People");
        }
        public void GetSingleLevel() {
            RegistryConfigSource source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            IConfig config = source.Configs["Pets"];
            Assert.AreEqual("Pets", config.Name);
            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual(source, config.ConfigSource);

            Assert.AreEqual("Chi-chi", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual(5, config.GetInt("count"));
        }
        public void GetSingleLevel()
        {
            RegistryConfigSource source = new RegistryConfigSource();

            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            IConfig config = source.Configs["Pets"];

            Assert.AreEqual("Pets", config.Name);
            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual(source, config.ConfigSource);

            Assert.AreEqual("Chi-chi", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual(5, config.GetInt("count"));
        }
        public void AddConfig()
        {
            RegistryConfigSource source = new RegistryConfigSource();
            RegistryKey          key    =
                Registry.LocalMachine.OpenSubKey("Software\\NiniTestApp", true);

            IConfig config = source.AddConfig("People", key);

            config.Set("woman", "Tara");
            config.Set("man", "Quentin");

            source.Save();

            source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\People");

            Assert.AreEqual(1, source.Configs.Count);
            config = source.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Tara", config.Get("woman"));
            Assert.AreEqual("Quentin", config.Get("man"));
        }
        public void Namespacing()
        {
            RegistryConfigSource source = new RegistryConfigSource();
            RegistryKey          key    = Registry.LocalMachine.OpenSubKey("Software");

            source.AddMapping(key, "NiniTestApp",
                              RegistryRecurse.Namespacing);

            IConfig config = source.Configs["NiniTestApp"];

            Assert.AreEqual("Configuration Library", config.Get("Description"));

            config = source.Configs["NiniTestApp\\Pets"];
            Assert.AreEqual("NiniTestApp\\Pets", config.Name);
            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual(source, config.ConfigSource);

            Assert.AreEqual("Chi-chi", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual(5, config.GetInt("count"));

            config.Set("cat", "Muffy");
            config.Set("dog", "Spots");
            config.Set("count", 4);

            source.Save();

            source = new RegistryConfigSource();
            key    = Registry.LocalMachine.OpenSubKey("Software");
            source.AddMapping(key, "NiniTestApp",
                              RegistryRecurse.Namespacing);

            config = source.Configs["NiniTestApp\\Pets"];
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual(4, config.GetInt("count"));
        }
 public void NonExistantKey() {
     RegistryConfigSource source = new RegistryConfigSource();
     source.AddMapping(Registry.LocalMachine, "Software\\Does\\NotExist");
 }
        public void AddConfigNoDefaultKey() {
            RegistryConfigSource source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            IConfig config = source.AddConfig("People");
        }
        public void AddConfigDefaultKey() {
            RegistryConfigSource source = new RegistryConfigSource();
            source.DefaultKey =
                Registry.LocalMachine.OpenSubKey("Software\\NiniTestApp", true);

            IConfig config = source.AddConfig("People");
            config.Set("woman", "Tara");
            config.Set("man", "Quentin");

            source.Save();

            source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\People");

            Assert.AreEqual(1, source.Configs.Count);
            config = source.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Tara", config.Get("woman"));
            Assert.AreEqual("Quentin", config.Get("man"));
        }
        public void Reload() {
            RegistryConfigSource source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");
            source.Configs["Pets"].Set("cat", "Muffy");
            source.Save();

            Assert.AreEqual(3, source.Configs["Pets"].GetKeys().Length);
            Assert.AreEqual("Muffy", source.Configs["Pets"].Get("cat"));

            RegistryConfigSource newSource = new RegistryConfigSource();
            newSource.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");
            Assert.AreEqual(3, newSource.Configs["Pets"].GetKeys().Length);
            Assert.AreEqual("Muffy", newSource.Configs["Pets"].Get("cat"));

            source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");
            source.Configs["Pets"].Set("cat", "Misha");
            source.Save(); // saves new value

            newSource.Reload();
            Assert.AreEqual(3, newSource.Configs["Pets"].GetKeys().Length);
            Assert.AreEqual("Misha", newSource.Configs["Pets"].Get("cat"));
        }
        public void MergeAndSave() {
            RegistryConfigSource source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            IConfig config = source.Configs["Pets"];
            Assert.AreEqual("Chi-chi", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual(5, config.GetInt("count"));

            StringWriter writer = new StringWriter();
            writer.WriteLine("[Pets]");
            writer.WriteLine("cat = Becky"); // overwrite
            writer.WriteLine("lizard = Saurus"); // new
            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            writer.WriteLine(" man = John");
            IniConfigSource iniSource = new IniConfigSource
                (new StringReader(writer.ToString()));

            source.Merge(iniSource);

            config = source.Configs["Pets"];
            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = source.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Jane", config.Get("woman"));
            Assert.AreEqual("John", config.Get("man"));

            config.Set("woman", "Tara");
            config.Set("man", "Quentin");

            source.Save();

            source = new RegistryConfigSource();
            source.AddMapping(Registry.LocalMachine, "Software\\NiniTestApp\\Pets");

            config = source.Configs["Pets"];
            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = source.Configs["People"];
            Assert.IsNull(config); // you cannot merge new sections
            /*
			Assert.AreEqual (2, config.GetKeys ().Length);
			Assert.AreEqual ("Tara", config.Get ("woman"));
			Assert.AreEqual ("Quentin", config.Get ("man"));
			*/
        }
        public void NonExistantKey()
        {
            RegistryConfigSource source = new RegistryConfigSource();

            source.AddMapping(Registry.LocalMachine, "Software\\Does\\NotExist");
        }