Ejemplo n.º 1
0
        public void IncludedChange()
        {
            string cfgMainFile = Path.GetTempFileName();
            string cfgAdditionalFile = Path.GetTempFileName();

            File.WriteAllText(cfgAdditionalFile, _xmlCfgAutoOrigin);
            File.WriteAllText(cfgMainFile, string.Format(_xmlCfgMain, cfgAdditionalFile));

            var loader = new SettingsLoader();
            loader.XmlFileBySection();
            var loadResult = loader.LoadSettings(new XmlFileSettings(cfgMainFile));

            var wait = new ManualResetEvent(false);
            var fileCheckers = FileChecker.TryCreate(loadResult.Sources).ToArray();
            new FirstChange(fileCheckers).Changed += (s, e) =>
            {
                wait.Set();
                foreach (var fileChecker in fileCheckers)
                    fileChecker.Dispose();
            };

            var t = Task.Factory.StartNew(() =>
            {
                File.WriteAllText(cfgAdditionalFile, _xmlCfgAutoModify);
            }, TaskCreationOptions.LongRunning);

            Task.WaitAll(t);

            Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed");
        }
        public void MultiChange()
        {
            var xmlFileLoader = new XmlFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);

            string cfgMainFile = Path.GetTempFileName();
            string cfgAdditionalFile = Path.GetTempFileName();

            File.WriteAllText(cfgAdditionalFile, _xmlCfgAutoOrigin);
            File.WriteAllText(cfgMainFile, string.Format(_xmlCfgMain, cfgAdditionalFile));

            var loader = new SettingsLoader(xmlFileLoader);
            loader.LoadSettings(xmlFileLoader.LoadFile(cfgMainFile));

            IAppSettings settings = loader.Settings;

            var wait = new ManualResetEvent(false);
            ((IChangeable)settings).Changed += (s, e) => { wait.Set(); };

            var t = Task.Factory.StartNew(() =>
            {
                File.WriteAllText(cfgAdditionalFile, _xmlCfgAutoModify);
            }, TaskCreationOptions.LongRunning);

            Task.WaitAll(t);

            Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed");
        }
Ejemplo n.º 3
0
        private static IAppSettings loadSettings(string file)
        {
            var loader = new SettingsLoader();
            loader.XmlFileBySection();
            loader.JsonFileBySection();
            loader.IniFileBySection();

            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            if (Path.GetExtension(file) == ".xml")
                return loader.LoadSettings(new XmlFileSettings(Path.Combine("Including".ResolveTestPath(), file))).Joined.ToAppSettings();
            else
                return loader.LoadSettings(new JsonFileSettings(Path.Combine("Including".ResolveTestPath(), file))).Joined.ToAppSettings();
        }
        private static IAppSettings LoadSettings(string file)
        {
            var xmlFileLoader = new XmlFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);
            var jsonFileLoader = new JsonFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);
            var iniFileLoader = new IniFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);

            var loader = new SettingsLoader(xmlFileLoader, jsonFileLoader, iniFileLoader);
            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            if (Path.GetExtension(file) == ".xml")
                loader.LoadSettings(xmlFileLoader.LoadFile(Path.Combine("Including", file)));
            else
                loader.LoadSettings(jsonFileLoader.LoadFile(Path.Combine("Including", file)));

            return loader.Settings;
        }
Ejemplo n.º 5
0
        public void IncludeInMiddle()
        {
            var loader = new SettingsLoader();
            loader.XmlFileBySection();

            var settings = loader
                .LoadSettings(XmlFileSettings.Create("Joining/AppDirectory/IncludeInMiddle.config".ResolveTestPath()))
                .Joined.ToAppSettings();

            Assert.That(settings.LoadSections<AdditionalConfig>().Select(_ => _.F), Is.EquivalentTo(new[] { "InMainPre", "InAdditional", "InMainPost" }));
        }
Ejemplo n.º 6
0
        public void RelativeInclude()
        {
            var loader = new SettingsLoader();
            loader.XmlFileBySection();

            var settings = loader
                .LoadSettings(XmlFileSettings.Create("Joining/AppDirectory/Deeper/RelativeInclude.config".ResolveTestPath()))
                .Joined.ToAppSettings();

            Assert.That(settings.LoadSections<AdditionalConfig>().Select(_ => _.F), Is.EquivalentTo(
                new[] { "BeginMain", "BeginUpper", "InAdditional", "EndUpper", "EndMain" }));
        }
Ejemplo n.º 7
0
        public void IncludeByRelativePath()
        {
            var loader = new SettingsLoader();
            loader.XmlFileByExtension();
            var systemSettings = new XmlSystemSettings("ExtConfigure");
            var settings = loader.LoadSettings(systemSettings).Joined.ToAppSettings();

            var cfg = settings.Get<MyXmlConfig>("MyCfg2");

            Assert.AreEqual("2", cfg.AttrField);
            Assert.IsNull(cfg.ElemField);
        }
Ejemplo n.º 8
0
        public void IncludeByFixedPathOwner()
        {
            var localPath = "".ResolveTestPath();
            var systemSettings = new XmlSystemSettings("ExtConfigure", localPath); // fix for R# test runner

            var loader = new SettingsLoader();
            loader.XmlFileByExtension();
            var settings = loader.LoadSettings(systemSettings).Joined.ToAppSettings();

            var cfg = settings.Get<MyXmlConfig>("MyCfg2");

            Assert.AreEqual("2", cfg.AttrField);
            Assert.IsNull(cfg.ElemField);
        }
Ejemplo n.º 9
0
        public void SettingsLoader()
        {
            var storage = new VariableStorage();
            storage["machineName"] = "TestServer";

            var loader = new SettingsLoader(storage.CfgNodeConverter);

            var xmlCfg = @"<?xml version='1.0' encoding='utf-8' ?>
            <configuration>
            <Before Field='${machineName} ${var1}' />
            <variable name='var1' value='value1' />
            <After Field='${machineName} ${var1}' />
            </configuration>".ToXmlSettings();

            var settings = loader.LoadSettings(xmlCfg).Joined.ToAppSettings();

            Assert.AreEqual("TestServer value1", settings.Get<TestConfig>("Before").Field);
            Assert.AreEqual("TestServer value1", settings.Get<TestConfig>("After").Field);
        }
Ejemplo n.º 10
0
        public void AutoCombineLoad()
        {
            var loader = new SettingsLoader();
            loader.XmlFileBySection();
            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            var settings = loader
                .LoadSettings(new XmlFileSettings("Examples/AppDirectory/autoMain.config".ResolveTestPath()))
                .Joined
                .ToAppSettings();

            var cfg = settings.TryGet<ChildAutoCombinableConnectionConfig>();

            Assert.IsNotNull(cfg);
            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;Trusted_Connection=True;Connection Timeout=60", cfg.ConnectionString);
        }
        public void Load()
        {
            var xmlFileLoader = new XmlFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);

            var loader = new SettingsLoader(xmlFileLoader);
            loader.Loaded += (s,e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            loader.LoadSettings(xmlFileLoader.LoadFile("Examples/AppDirectory/main.config"));

            IAppSettings settings = loader.Settings;

            var addCfg = settings.TryCombine<ExampleCombineConfig>("AdditionalConfig");

            Assert.IsNotNull(addCfg);
            Assert.AreEqual("InAppDirectory", addCfg.F);
        }
Ejemplo n.º 12
0
        public void Load()
        {
            var loader = new SettingsLoader();
            loader.XmlFileBySection();

            loader.Loaded += (s,e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            var settings = loader
                .LoadSettings(new XmlFileSettings("Examples/AppDirectory/main.config".ResolveTestPath()))
                .Joined
                .ToAppSettings();

            var addCfg = settings.TryGet<ExampleCombineConfig>("AdditionalConfig");

            Assert.IsNotNull(addCfg);
            Assert.AreEqual("InAppDirectory", addCfg.F);
        }
        public void AutoCombineLoad()
        {
            var xmlFileLoader = new XmlFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);

            var loader = new SettingsLoader(xmlFileLoader);
            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            loader.LoadSettings(xmlFileLoader.LoadFile("Examples/AppDirectory/autoMain.config"));

            var settings = new CombinableAppSettings(loader.Settings, Global.GenericCombiner);

            var cfg = settings.TryGet<ChildAutoCombinableConnectionConfig>();

            var cfgs = settings.Settings.LoadCollection<ChildAutoCombinableConnectionConfig>().ToArray();

            Assert.IsNotNull(cfg);
            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;Trusted_Connection=True;Connection Timeout=60", cfg.ConnectionString);
        }
        public void GetFiles()
        {
            var settings = @"<?xml version='1.0' encoding='utf-8' ?>
            <Config>
            <Include FinalSearch='true'>
            <XmlFile Path='file1'/>
            <XmlFile Path='file2' Search='Exact' Include='All' Required='false'/>
            <XmlFile Path='file3' Search='All' Include='First' Required='true'/>
            </Include>
            </Config>".ToXmlSettings();

            var mock = new MockFactory();

            SettingsLoader loader = new SettingsLoader(mock);
            loader.LoadSettings(settings);

            var files = mock.Configs;

            CollectionAssert.IsNotEmpty(files);

            Assert.AreEqual(3, files.Count);

            Assert.AreEqual("file1", files[0].Path);
            Assert.AreEqual(SearchMode.All, files[0].Search);
            Assert.AreEqual(IncludeMode.All, files[0].Include);
            Assert.IsFalse(files[0].Required);

            Assert.AreEqual("file2", files[1].Path);
            Assert.AreEqual(SearchMode.Exact, files[1].Search);
            Assert.AreEqual(IncludeMode.All, files[1].Include);
            Assert.IsFalse(files[1].Required);

            Assert.AreEqual("file3", files[2].Path);
            Assert.AreEqual(SearchMode.All, files[2].Search);
            Assert.AreEqual(IncludeMode.First, files[2].Include);
            Assert.IsTrue(files[2].Required);
        }
Ejemplo n.º 15
0
        public void SecureLoad()
        {
            KeyManager.Create();

            var providerLoader = new ProviderLoader();

            var loader = new SettingsLoader();
            loader.XmlFileBySection();
            loader.Loaded += providerLoader.TryExtractConfigProtectedData;

            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            var settings = loader
                .LoadSettings(new XmlFileSettings("Examples/AppDirectory/secureMain.config".ResolveTestPath()))
                .Joined.ToAppSettings();

            var addCfg = settings.TryGet<ExampleCombineConfig>("AdditionalConfig");

            Assert.IsNotNull(addCfg);
            Assert.AreEqual("InUpDirectory", addCfg.F);

            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryGet<ConnectionConfig>("MyExtConnection").ConnectionString);
            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryGet<ConnectionConfig>("MySecuredConnection").ConnectionString);

            KeyManager.Delete();
        }
        public void SecureLoad()
        {
            KeyManager.Create();

            var providerLoader = new ProviderLoader();
            var xmlFileLoader = new XmlFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);

            var loader = new SettingsLoader(xmlFileLoader);
            loader.Loaded += providerLoader.TryExtractConfigProtectedData;

            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            loader.LoadSettings(xmlFileLoader.LoadFile("Examples/AppDirectory/secureMain.config"));

            IAppSettings settings = loader.Settings;

            var addCfg = settings.TryCombine<ExampleCombineConfig>("AdditionalConfig");

            Assert.IsNotNull(addCfg);
            Assert.AreEqual("InUpDirectory", addCfg.F);

            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryFirst<ConnectionConfig>("MyExtConnection").ConnectionString);
            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryFirst<ConnectionConfig>("MySecuredConnection").ConnectionString);

            KeyManager.Delete();
        }