Inheritance: ConfigSourceBase
Esempio n. 1
0
        public void Merge() {
            StringWriter textWriter = new StringWriter();
            XmlTextWriter xmlWriter = NiniWriter(textWriter);
            WriteSection(xmlWriter, "Pets");
            WriteKey(xmlWriter, "cat", "muffy");
            WriteKey(xmlWriter, "dog", "rover");
            WriteKey(xmlWriter, "bird", "tweety");
            xmlWriter.WriteEndDocument();

            StringReader reader = new StringReader(textWriter.ToString());
            XmlTextReader xmlReader = new XmlTextReader(reader);
            XmlConfigSource xmlSource = new XmlConfigSource(xmlReader);

            StringWriter writer = new StringWriter();
            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            writer.WriteLine(" man = John");
            IniConfigSource iniSource =
                new IniConfigSource(new StringReader(writer.ToString()));

            xmlSource.Merge(iniSource);

            IConfig config = xmlSource.Configs["Pets"];
            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual("muffy", config.Get("cat"));
            Assert.AreEqual("rover", config.Get("dog"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Jane", config.Get("woman"));
            Assert.AreEqual("John", config.Get("man"));
        }
Esempio n. 2
0
        public void SetAndSave() {
            string filePath = "Test.ini";

            StreamWriter writer = new StreamWriter(filePath);
            writer.WriteLine("; some comment");
            writer.WriteLine("[new section]");
            writer.WriteLine(" dog = Rover");
            writer.WriteLine(""); // empty line
            writer.WriteLine("; a comment");
            writer.WriteLine(" cat = Muffy");
            writer.Close();

            IniConfigSource source = new IniConfigSource(filePath);
            IConfig config = source.Configs["new section"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            config.Set("dog", "Spots");
            config.Set("cat", "Misha");
            config.Set("DoesNotExist", "SomeValue");

            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual("Misha", config.Get("cat"));
            Assert.AreEqual("SomeValue", config.Get("DoesNotExist"));
            source.Save();

            source = new IniConfigSource(filePath);
            config = source.Configs["new section"];
            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual("Misha", config.Get("cat"));
            Assert.AreEqual("SomeValue", config.Get("DoesNotExist"));

            File.Delete(filePath);
        }
Esempio n. 3
0
        /// <summary>
        /// Initialize a new instance of InIQueryProviderBase with query file.
        /// </summary>
        /// <param name="queryFilePath">Query String이 정의된 ini 파일의 전체경로</param>
        protected InIQueryProviderBase(string queryFilePath) {
            if(IsDebugEnabled)
                log.Debug("쿼리문을 제공하는 IQueryProvider의 인스턴스를 생성합니다. queryFilePath=[{0}]", queryFilePath);

            try {
                queryFilePath = FileTool.GetPhysicalPath(queryFilePath);

                if(queryFilePath.FileExists() == false)
                    throw new FileNotFoundException("QueryStringFile이 없습니다. queryFilePath=" + queryFilePath, queryFilePath);

                QueryFilePath = queryFilePath;

                // Ini File을 SambaStyle로 해야 다중라인을 이해한다. 다중라인의 구분은 backslash ('\') 로 구분한다.
                //
                QuerySource = new IniConfigSource(new IniDocument(QueryFilePath, IniFileType.SambaStyle));

                // parameter value값들을 계산해둔다.
                //
                QuerySource.ExpandKeyValues();
            }
            catch(Exception ex) {
                if(log.IsErrorEnabled) {
                    log.Error("InIQueryProviderBase를 초기화하는데 실패했습니다!!! queryFilePath=[{0}]" + QueryFilePath);
                    log.Error(ex);
                }

                throw;
            }

            if(IsDebugEnabled)
                log.Debug("Query String 정보를 파싱했습니다. queryFilePath=[{0}]", QueryFilePath);
        }
Esempio n. 4
0
        public void MergeItself() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            writer.WriteLine(" man = John");
            IniConfigSource iniSource =
                new IniConfigSource(new StringReader(writer.ToString()));

            iniSource.Merge(iniSource); // exception
        }
        /// <summary>
        /// 쿼리 문자열을 제공하는 파일을 읽어서 <see cref="IniConfigSource"/> 인스턴스를 빌드합니다.
        /// </summary>
        protected virtual void BuildQuerySource() {
            if(IsDebugEnabled)
                log.Debug("Samba style의 Ini 파일형태의 쿼리정의 정보를 제공하는 QuerySource를 빌드합니다... queryFilePath=[{0}]", QueryFilePath);

            Guard.Assert(QueryFilePath.FileExists(), @"지정된 파일을 찾을 수 없습니다. QueryFilePath=[{0}]", QueryFilePath);

            var source = new IniConfigSource(new IniDocument(QueryFilePath, IniFileType.SambaStyle));
            Thread.MemoryBarrier();
            _querySource = source;
        }
Esempio n. 6
0
        public void MergeAndSave() {
            string fileName = "NiniConfig.ini";

            StreamWriter fileWriter = new StreamWriter(fileName);
            fileWriter.WriteLine("[Pets]");
            fileWriter.WriteLine("cat = Muffy"); // overwrite
            fileWriter.WriteLine("dog = Rover"); // new
            fileWriter.WriteLine("bird = Tweety");
            fileWriter.Close();

            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()));

            IniConfigSource source = new IniConfigSource(fileName);

            source.Merge(iniSource);

            IConfig 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 IniConfigSource(fileName);

            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("Tara", config.Get("woman"));
            Assert.AreEqual("Quentin", config.Get("man"));

            File.Delete(fileName);
        }
Esempio n. 7
0
        public void GetConfig() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Pets]");
            writer.WriteLine(" cat = muffy");
            writer.WriteLine(" dog = rover");
            writer.WriteLine(" bird = tweety");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

            IConfig config = source.Configs["Pets"];
            Assert.AreEqual("Pets", config.Name);
            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual(source, config.ConfigSource);
        }
Esempio n. 8
0
        public void GetLong() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Test]");
            writer.WriteLine(" value 1 = 4000000000");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));
            IConfig config = source.Configs["Test"];

            Assert.AreEqual(4000000000, config.GetLong("value 1"));
            Assert.AreEqual(5000000000, config.GetLong("Not Here", 5000000000));

            try {
                config.GetLong("Not Here Also");
                Assert.Fail();
            }
            catch {}
        }
Esempio n. 9
0
        public void GetString() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Test]");
            writer.WriteLine(" cat = muffy");
            writer.WriteLine(" dog = rover");
            writer.WriteLine(" bird = tweety");
            IniConfigSource source =
                new IniConfigSource(new StringReader(writer.ToString()));
            IConfig config = source.Configs["Test"];

            Assert.AreEqual("muffy", config.Get("cat"));
            Assert.AreEqual("rover", config.Get("dog"));
            Assert.AreEqual("muffy", config.GetString("cat"));
            Assert.AreEqual("rover", config.GetString("dog"));
            Assert.AreEqual("my default", config.Get("Not Here", "my default"));
            Assert.IsNull(config.Get("Not Here 2"));
        }
Esempio n. 10
0
        public void GetLong()
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[Test]");
            writer.WriteLine(" value 1 = 4000000000");
            IniConfigSource source = new IniConfigSource
                                         (new StringReader(writer.ToString()));
            IConfig config = source.Configs["Test"];

            Assert.AreEqual(4000000000, config.GetLong("value 1"));
            Assert.AreEqual(5000000000, config.GetLong("Not Here", 5000000000));

            try {
                config.GetLong("Not Here Also");
                Assert.Fail();
            }
            catch {}
        }
Esempio n. 11
0
        public void GetString()
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[Test]");
            writer.WriteLine(" cat = muffy");
            writer.WriteLine(" dog = rover");
            writer.WriteLine(" bird = tweety");
            IniConfigSource source =
                new IniConfigSource(new StringReader(writer.ToString()));
            IConfig config = source.Configs["Test"];

            Assert.AreEqual("muffy", config.Get("cat"));
            Assert.AreEqual("rover", config.Get("dog"));
            Assert.AreEqual("muffy", config.GetString("cat"));
            Assert.AreEqual("rover", config.GetString("dog"));
            Assert.AreEqual("my default", config.Get("Not Here", "my default"));
            Assert.IsNull(config.Get("Not Here 2"));
        }
Esempio n. 12
0
        public void Rename()
        {
            IniConfigSource source = new IniConfigSource();

            IConfig config = source.AddConfig("Pets");

            config.Set("cat", "Muffy");
            config.Set("dog", "Rover");

            config.Name = "MyPets";
            Assert.AreEqual("MyPets", config.Name);

            Assert.IsNull(source.Configs["Pets"]);
            IConfig newConfig = source.Configs["MyPets"];

            Assert.AreEqual(config, newConfig);

            Assert.AreEqual(2, newConfig.GetKeys().Length);
        }
Esempio n. 13
0
        public void ToStringTest()
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[Test]");
            writer.WriteLine(" cat = muffy");
            writer.WriteLine(" dog = rover");
            writer.WriteLine(" bird = tweety");
            IniConfigSource source =
                new IniConfigSource(new StringReader(writer.ToString()));

            string eol = Environment.NewLine;

            string compare = "[Test]" + eol
                             + "cat = muffy" + eol
                             + "dog = rover" + eol
                             + "bird = tweety" + eol;

            Assert.AreEqual(compare, source.ToString());
        }
Esempio n. 14
0
        public void SaveAfterTextWriter()
        {
            string filePath = "Test.ini";

            StreamWriter writer = new StreamWriter(filePath);

            writer.WriteLine("[new section]");
            writer.WriteLine(" dog = Rover");
            writer.Close();

            IniConfigSource source = new IniConfigSource(filePath);

            Assert.AreEqual(filePath, source.SavePath);
            StringWriter textWriter = new StringWriter();

            source.Save(textWriter);
            Assert.IsNull(source.SavePath);

            File.Delete(filePath);
        }
Esempio n. 15
0
        public void AddConfig()
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[Test]");
            writer.WriteLine(" bool 1 = TrUe");
            IniConfigSource source = new IniConfigSource
                                         (new StringReader(writer.ToString()));

            IConfig newConfig = source.AddConfig("NewConfig");

            newConfig.Set("NewKey", "NewValue");
            newConfig.Set("AnotherKey", "AnotherValue");

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

            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("NewValue", config.Get("NewKey"));
            Assert.AreEqual("AnotherValue", config.Get("AnotherKey"));
        }
Esempio n. 16
0
        public void ConfigBaseGetErrors()
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[web]");
            writer.WriteLine("; No keys");
            IniConfigSource source = new IniConfigSource
                                         (new StringReader(writer.ToString()));
            IConfig config = source.Configs["web"];

            try {
                config.GetInt("not_there");
            }
            catch (Exception ex) {
                Assert.AreEqual("Value not found: not_there", ex.Message);
            }
            try {
                config.GetFloat("not_there");
            }
            catch (Exception ex) {
                Assert.AreEqual("Value not found: not_there", ex.Message);
            }
            try {
                config.GetDouble("not_there");
            }
            catch (Exception ex) {
                Assert.AreEqual("Value not found: not_there", ex.Message);
            }
            try {
                config.GetLong("not_there");
            }
            catch (Exception ex) {
                Assert.AreEqual("Value not found: not_there", ex.Message);
            }
            try {
                config.GetBoolean("not_there");
            }
            catch (Exception ex) {
                Assert.AreEqual("Value not found: not_there", ex.Message);
            }
        }
Esempio n. 17
0
        public void ExpandTextOtherSection()
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[web]");
            writer.WriteLine(" apache = Apache implements ${protocol}");
            writer.WriteLine(" protocol = http");
            writer.WriteLine("[server]");
            writer.WriteLine(" domain = ${web|protocol}://nini.sf.net/");
            IniConfigSource source = new IniConfigSource
                                         (new StringReader(writer.ToString()));

            source.ExpandKeyValues();

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

            Assert.AreEqual("http", config.Get("protocol"));
            Assert.AreEqual("Apache implements http", config.Get("apache"));
            config = source.Configs["server"];
            Assert.AreEqual("http://nini.sf.net/", config.Get("domain"));
        }
Esempio n. 18
0
        public void GetIntAlias()
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[Test]");
            writer.WriteLine(" node type = TEXT");
            writer.WriteLine(" error code = WARN");
            IniConfigSource source = new IniConfigSource(new StringReader(writer.ToString()));

            const int WARN = 100, ERROR = 200;
            IConfig   config = source.Configs["Test"];

            config.Alias.AddAlias("error code", "waRn", WARN);
            config.Alias.AddAlias("error code", "eRRor", ERROR);
            config.Alias.AddAlias("node type", new System.Xml.XmlNodeType());
            config.Alias.AddAlias("default", "age", 31);

            Assert.AreEqual(WARN, config.GetInt("error code", true));
            Assert.AreEqual((int)System.Xml.XmlNodeType.Text,
                            config.GetInt("node type", true));
            Assert.AreEqual(31, config.GetInt("default", 31, true));
        }
Esempio n. 19
0
        public void MergeExisting()
        {
            StringWriter  textWriter = new StringWriter();
            XmlTextWriter xmlWriter  = NiniWriter(textWriter);

            WriteSection(xmlWriter, "Pets");
            WriteKey(xmlWriter, "cat", "muffy");
            xmlWriter.WriteEndDocument();

            StringReader    reader    = new StringReader(xmlWriter.ToString());
            XmlTextReader   xmlReader = new XmlTextReader(reader);
            XmlConfigSource xmlSource = new XmlConfigSource(xmlReader);

            StringWriter writer = new StringWriter();

            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            IniConfigSource iniSource =
                new IniConfigSource(new StringReader(writer.ToString()));

            xmlSource.Merge(iniSource);
            xmlSource.Merge(iniSource); // exception
        }
Esempio n. 20
0
        public void SetAndSave()
        {
            string filePath = "Test.ini";

            StreamWriter writer = new StreamWriter(filePath);

            writer.WriteLine("; some comment");
            writer.WriteLine("[new section]");
            writer.WriteLine(" dog = Rover");
            writer.WriteLine(""); // empty line
            writer.WriteLine("; a comment");
            writer.WriteLine(" cat = Muffy");
            writer.Close();

            IniConfigSource source = new IniConfigSource(filePath);
            IConfig         config = source.Configs["new section"];

            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            config.Set("dog", "Spots");
            config.Set("cat", "Misha");
            config.Set("DoesNotExist", "SomeValue");

            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual("Misha", config.Get("cat"));
            Assert.AreEqual("SomeValue", config.Get("DoesNotExist"));
            source.Save();

            source = new IniConfigSource(filePath);
            config = source.Configs["new section"];
            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual("Misha", config.Get("cat"));
            Assert.AreEqual("SomeValue", config.Get("DoesNotExist"));

            File.Delete(filePath);
        }
Esempio n. 21
0
        public void Merge()
        {
            StringWriter  textWriter = new StringWriter();
            XmlTextWriter xmlWriter  = NiniWriter(textWriter);

            WriteSection(xmlWriter, "Pets");
            WriteKey(xmlWriter, "cat", "muffy");
            WriteKey(xmlWriter, "dog", "rover");
            WriteKey(xmlWriter, "bird", "tweety");
            xmlWriter.WriteEndDocument();

            StringReader    reader    = new StringReader(textWriter.ToString());
            XmlTextReader   xmlReader = new XmlTextReader(reader);
            XmlConfigSource xmlSource = new XmlConfigSource(xmlReader);

            StringWriter writer = new StringWriter();

            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            writer.WriteLine(" man = John");
            IniConfigSource iniSource =
                new IniConfigSource(new StringReader(writer.ToString()));

            xmlSource.Merge(iniSource);

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

            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual("muffy", config.Get("cat"));
            Assert.AreEqual("rover", config.Get("dog"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Jane", config.Get("woman"));
            Assert.AreEqual("John", config.Get("man"));
        }
Esempio n. 22
0
        public void GlobalAlias()
        {
            using (var writer = new StringWriter()) {
                writer.WriteLine("[Test]");
                writer.WriteLine(" TurnOff = true");
                writer.WriteLine(" ErrorCode = WARN");

                IniConfigSource source = new IniConfigSource(new StringReader(writer.ToString()));

                source.Alias.AddAlias("true", true);
                source.Alias.AddAlias("ErrorCode", "warn", 35);

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

                Assert.AreEqual(35, config.GetInt("ErrorCode", true));
                Assert.IsTrue(config.GetBoolean("TurnOff"));

                config.Alias.AddAlias("true", false);
                config.Alias.AddAlias("ErrorCode", "warn", 45);

                Assert.AreEqual(45, config.GetInt("ErrorCode", true));
                Assert.IsFalse(config.GetBoolean("TurnOff"));
            }
        }
Esempio n. 23
0
        public void SaveNewSection() {
            string filePath = "Test.xml";

            StringWriter writer = new StringWriter();
            writer.WriteLine("; some comment");
            writer.WriteLine("[new section]");
            writer.WriteLine(" dog = Rover");
            writer.WriteLine(" cat = Muffy");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

            IConfig config = source.AddConfig("test");
            Assert.IsNotNull(source.Configs["test"]);
            source.Save(filePath);

            source = new IniConfigSource(filePath);
            config = source.Configs["new section"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.IsNotNull(source.Configs["test"]);

            File.Delete(filePath);
        }
Esempio n. 24
0
        public void SaveAfterTextWriter() {
            string filePath = "Test.ini";

            StreamWriter writer = new StreamWriter(filePath);
            writer.WriteLine("[new section]");
            writer.WriteLine(" dog = Rover");
            writer.Close();

            IniConfigSource source = new IniConfigSource(filePath);
            Assert.AreEqual(filePath, source.SavePath);
            StringWriter textWriter = new StringWriter();
            source.Save(textWriter);
            Assert.IsNull(source.SavePath);

            File.Delete(filePath);
        }
Esempio n. 25
0
        public void SaveToWriter() {
            string newPath = "TestNew.ini";

            StringWriter writer = new StringWriter();
            writer.WriteLine("; some comment");
            writer.WriteLine("[new section]");
            writer.WriteLine(" dog = Rover");
            writer.WriteLine(" cat = Muffy");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

            Assert.IsNull(source.SavePath);
            IConfig config = source.Configs["new section"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            StreamWriter textWriter = new StreamWriter(newPath);
            source.Save(textWriter);
            textWriter.Close(); // save to disk

            source = new IniConfigSource(newPath);
            Assert.AreEqual(newPath, source.SavePath);
            config = source.Configs["new section"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            File.Delete(newPath);
        }
Esempio n. 26
0
        public void SaveToNewPath() {
            string filePath = "Test.ini";
            string newPath = "TestNew.ini";

            StreamWriter writer = new StreamWriter(filePath);
            writer.WriteLine("; some comment");
            writer.WriteLine("[new section]");
            writer.WriteLine(" dog = Rover");
            writer.WriteLine(" cat = Muffy");
            writer.Close();

            IniConfigSource source = new IniConfigSource(filePath);
            IConfig config = source.Configs["new section"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            source.Save(newPath);

            source = new IniConfigSource(newPath);
            config = source.Configs["new section"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            File.Delete(filePath);
            File.Delete(newPath);
        }
Esempio n. 27
0
        public void Rename() {
            IniConfigSource source = new IniConfigSource();

            IConfig config = source.AddConfig("Pets");
            config.Set("cat", "Muffy");
            config.Set("dog", "Rover");

            config.Name = "MyPets";
            Assert.AreEqual("MyPets", config.Name);

            Assert.IsNull(source.Configs["Pets"]);
            IConfig newConfig = source.Configs["MyPets"];

            Assert.AreEqual(config, newConfig);

            Assert.AreEqual(2, newConfig.GetKeys().Length);
        }
Esempio n. 28
0
        public void EmptyConstructor() {
            string filePath = "EmptyConstructor.ini";
            IniConfigSource source = new IniConfigSource();

            IConfig config = source.AddConfig("Pets");
            config.Set("cat", "Muffy");
            config.Set("dog", "Rover");
            config.Set("bird", "Tweety");
            source.Save(filePath);

            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Tweety", config.Get("bird"));

            source = new IniConfigSource(filePath);
            config = source.Configs["Pets"];

            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Tweety", config.Get("bird"));

            File.Delete(filePath);
        }
Esempio n. 29
0
        public void GetValues() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Test]");
            writer.WriteLine(" key 1 = value 1");
            writer.WriteLine(" key 2 = value 2");
            writer.WriteLine(" key 3 = value 3");
            IniConfigSource source =
                new IniConfigSource(new StringReader(writer.ToString()));

            IConfig config = source.Configs["Test"];
            Assert.AreEqual(3, config.GetValues().Length);
            Assert.AreEqual("value 1", config.GetValues()[0]);
            Assert.AreEqual("value 2", config.GetValues()[1]);
            Assert.AreEqual("value 3", config.GetValues()[2]);
        }
Esempio n. 30
0
        public void CaseInsensitive() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Pets]");
            writer.WriteLine("cat = Becky"); // overwrite
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

            source.CaseSensitive = false;
            Assert.AreEqual("Becky", source.Configs["Pets"].Get("CAT"));

            source.Configs["Pets"].Set("cAT", "New Name");
            Assert.AreEqual("New Name", source.Configs["Pets"].Get("CAt"));

            source.Configs["Pets"].Remove("CAT");
            Assert.IsNull(source.Configs["Pets"].Get("CaT"));
        }
Esempio n. 31
0
        public void FileClosedOnParseError() {
            string filePath = "Reload.ini";

            StreamWriter writer = new StreamWriter(filePath);
            writer.WriteLine(" no section = boom!");
            writer.Close();

            try {
                IniConfigSource source = new IniConfigSource(filePath);
            }
            catch {
                // The file was still opened on a parse error
                File.Delete(filePath);
            }
        }
Esempio n. 32
0
        public void MergeAndSave()
        {
            string xmlFileName = "NiniConfig.xml";

            StreamWriter  textWriter = new StreamWriter(xmlFileName);
            XmlTextWriter xmlWriter  = NiniWriter(textWriter);

            WriteSection(xmlWriter, "Pets");
            WriteKey(xmlWriter, "cat", "Muffy");
            WriteKey(xmlWriter, "dog", "Rover");
            WriteKey(xmlWriter, "bird", "Tweety");
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();

            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()));

            XmlConfigSource xmlSource = new XmlConfigSource(xmlFileName);

            xmlSource.Merge(iniSource);

            IConfig config = xmlSource.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 = xmlSource.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");

            xmlSource.Save();

            xmlSource = new XmlConfigSource(xmlFileName);

            config = xmlSource.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 = xmlSource.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Tara", config.Get("woman"));
            Assert.AreEqual("Quentin", config.Get("man"));

            File.Delete(xmlFileName);
        }
Esempio n. 33
0
        public void RemoveConfigAndKeyFromFile() {
            string filePath = "Test.ini";

            StreamWriter writer = new StreamWriter(filePath);
            writer.WriteLine("[test 1]");
            writer.WriteLine(" dog = Rover");
            writer.WriteLine("[test 2]");
            writer.WriteLine(" cat = Muffy");
            writer.WriteLine(" lizard = Lizzy");
            writer.Close();

            IniConfigSource source = new IniConfigSource(filePath);
            Assert.IsNotNull(source.Configs["test 1"]);
            Assert.IsNotNull(source.Configs["test 2"]);
            Assert.IsNotNull(source.Configs["test 2"].Get("cat"));

            source.Configs.Remove(source.Configs["test 1"]);
            source.Configs["test 2"].Remove("cat");
            source.AddConfig("cause error");
            source.Save();

            source = new IniConfigSource(filePath);
            Assert.IsNull(source.Configs["test 1"]);
            Assert.IsNotNull(source.Configs["test 2"]);
            Assert.IsNull(source.Configs["test 2"].Get("cat"));

            File.Delete(filePath);
        }
Esempio n. 34
0
        public void GetFloat() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Test]");
            writer.WriteLine(" value 1 = 494.59");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));
            IConfig config = source.Configs["Test"];

            Assert.AreEqual(494.59f, config.GetFloat("value 1"));
            Assert.AreEqual((float)5656.2853,
                            config.GetFloat("Not Here", (float)5656.2853));
        }
Esempio n. 35
0
        public void ToStringTest() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Test]");
            writer.WriteLine(" cat = muffy");
            writer.WriteLine(" dog = rover");
            writer.WriteLine(" bird = tweety");
            IniConfigSource source =
                new IniConfigSource(new StringReader(writer.ToString()));

            string eol = Environment.NewLine;

            string compare = "[Test]" + eol
                             + "cat = muffy" + eol
                             + "dog = rover" + eol
                             + "bird = tweety" + eol;
            Assert.AreEqual(compare, source.ToString());
        }
Esempio n. 36
0
        public void GlobalAlias() {
            using(var writer = new StringWriter()) {
                writer.WriteLine("[Test]");
                writer.WriteLine(" TurnOff = true");
                writer.WriteLine(" ErrorCode = WARN");

                IniConfigSource source = new IniConfigSource(new StringReader(writer.ToString()));

                source.Alias.AddAlias("true", true);
                source.Alias.AddAlias("ErrorCode", "warn", 35);

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

                Assert.AreEqual(35, config.GetInt("ErrorCode", true));
                Assert.IsTrue(config.GetBoolean("TurnOff"));

                config.Alias.AddAlias("true", false);
                config.Alias.AddAlias("ErrorCode", "warn", 45);

                Assert.AreEqual(45, config.GetInt("ErrorCode", true));
                Assert.IsFalse(config.GetBoolean("TurnOff"));
            }
        }
Esempio n. 37
0
        public void Reload() {
            string filePath = "Reload.ini";

            // Create the original source file
            IniConfigSource source = new IniConfigSource();

            IConfig petConfig = source.AddConfig("Pets");
            petConfig.Set("cat", "Muffy");
            petConfig.Set("dog", "Rover");
            IConfig weatherConfig = source.AddConfig("Weather");
            weatherConfig.Set("skies", "cloudy");
            weatherConfig.Set("precipitation", "rain");
            source.Save(filePath);

            Assert.AreEqual(2, petConfig.GetKeys().Length);
            Assert.AreEqual("Muffy", petConfig.Get("cat"));
            Assert.AreEqual(2, source.Configs.Count);

            // Create another source file to set values and reload
            IniConfigSource newSource = new IniConfigSource(filePath);

            IConfig compareConfig = newSource.Configs["Pets"];
            Assert.AreEqual(2, compareConfig.GetKeys().Length);
            Assert.AreEqual("Muffy", compareConfig.Get("cat"));
            Assert.IsTrue(compareConfig == newSource.Configs["Pets"],
                          "References before are not equal");

            // Set the new values to source
            source.Configs["Pets"].Set("cat", "Misha");
            source.Configs["Pets"].Set("lizard", "Lizzy");
            source.Configs["Pets"].Set("hampster", "Surly");
            source.Configs["Pets"].Remove("dog");
            source.Configs.Remove(weatherConfig);
            source.Save(); // saves new value

            // Reload the new source and check for changes
            newSource.Reload();
            Assert.IsTrue(compareConfig == newSource.Configs["Pets"],
                          "References after are not equal");
            Assert.AreEqual(1, newSource.Configs.Count);
            Assert.AreEqual(3, newSource.Configs["Pets"].GetKeys().Length);
            Assert.AreEqual("Lizzy", newSource.Configs["Pets"].Get("lizard"));
            Assert.AreEqual("Misha", newSource.Configs["Pets"].Get("cat"));
            Assert.IsNull(newSource.Configs["Pets"].Get("dog"));

            File.Delete(filePath);
        }
Esempio n. 38
0
        public void SetAndRemove() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[Pets]");
            writer.WriteLine(" cat = muffy");
            writer.WriteLine(" dog = rover");
            writer.WriteLine(" bird = tweety");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

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

            config.Set("snake", "cobra");
            Assert.AreEqual(4, config.GetKeys().Length);

            // Test removing			
            Assert.IsNotNull(config.Get("dog"));
            config.Remove("dog");
            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.IsNull(config.Get("dog"));
            Assert.IsNotNull(config.Get("snake"));
        }
Esempio n. 39
0
        public void SaveToStream() {
            string filePath = "SaveToStream.ini";
            FileStream stream = new FileStream(filePath, FileMode.Create);

            // Create a new document and save to stream
            IniConfigSource source = new IniConfigSource();
            IConfig config = source.AddConfig("Pets");
            config.Set("dog", "rover");
            config.Set("cat", "muffy");
            source.Save(stream);
            stream.Close();

            IniConfigSource newSource = new IniConfigSource(filePath);
            config = newSource.Configs["Pets"];
            Assert.IsNotNull(config);
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("rover", config.GetString("dog"));
            Assert.AreEqual("muffy", config.GetString("cat"));

            stream.Close();

            File.Delete(filePath);
        }
Esempio n. 40
0
        public void ExpandWithEndBracket() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[web]");
            writer.WriteLine(" apache = } Apache implements ${protocol}");
            writer.WriteLine(" protocol = http");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

            IConfig config = source.Configs["web"];
            Assert.AreEqual("} Apache implements http", config.GetExpanded("apache"));
        }
Esempio n. 41
0
        public void Contains() {
            IniConfigSource source = new IniConfigSource();

            IConfig config = source.AddConfig("Pets");
            config.Set("cat", "Muffy");
            config.Set("dog", "Rover");

            Assert.IsTrue(config.Contains("cat"));
            Assert.IsTrue(config.Contains("dog"));

            config.Remove("cat");
            Assert.IsFalse(config.Contains("cat"));
            Assert.IsTrue(config.Contains("dog"));
        }
Esempio n. 42
0
        public void ExpandBackToBack() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[web]");
            writer.WriteLine(" apache = Protocol: ${protocol}${version}");
            writer.WriteLine(" protocol = http");
            writer.WriteLine(" version = 1.1");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

            IConfig config = source.Configs["web"];
            Assert.AreEqual("Protocol: http1.1", config.GetExpanded("apache"));
        }
Esempio n. 43
0
        public void ConfigCollectionEvents() {
            IniConfigSource source = new IniConfigSource();
            source.Configs.ConfigAdded +=
                new ConfigEventHandler(source_configAdded);
            source.Configs.ConfigRemoved +=
                new ConfigEventHandler(source_configRemoved);

            Assert.AreEqual(configAddedCount, 0);

            eventCollection = null;
            IConfig config = source.AddConfig("Test");
            Assert.IsTrue(source.Configs == eventCollection);
            Assert.AreEqual(configAddedCount, 1);
            Assert.AreEqual("Test", eventConfig.Name);

            eventCollection = null;
            config = source.Configs.Add("Test 2");
            Assert.IsTrue(source.Configs == eventCollection);
            Assert.AreEqual(configAddedCount, 2);
            Assert.AreEqual("Test 2", eventConfig.Name);

            eventCollection = null;
            source.Configs.RemoveAt(0);
            Assert.IsTrue(source.Configs == eventCollection);
            Assert.AreEqual(configAddedCount, 2);
            Assert.AreEqual("Test", eventConfig.Name);
        }
Esempio n. 44
0
        public void ExpandString() {
            StringWriter writer = new StringWriter();
            writer.WriteLine("[web]");
            writer.WriteLine(" apache = Apache implements ${protocol}");
            writer.WriteLine(" protocol = http");
            writer.WriteLine("[server]");
            writer.WriteLine(" domain = ${web|protocol}://nini.sf.net/");
            IniConfigSource source = new IniConfigSource
                (new StringReader(writer.ToString()));

            IConfig config = source.Configs["web"];
            Assert.AreEqual("http", config.Get("protocol"));
            Assert.AreEqual("Apache implements ${protocol}", config.Get("apache"));
            Assert.AreEqual("Apache implements http", config.GetExpanded("apache"));
            Assert.AreEqual("Apache implements ${protocol}", config.Get("apache"));
            config = source.Configs["server"];
            Assert.AreEqual("http://nini.sf.net/", config.GetExpanded("domain"));
            Assert.AreEqual("${web|protocol}://nini.sf.net/", config.Get("domain"));
        }