Пример #1
0
        public static void Run(string[] args)
        {
            //load version 1.0.0.6
            Settings = JsonSettings.Configure <VersioningSettings>("versioning.jsn")
                       .WithVersioning("1.0.0.6", VersioningResultAction.RenameAndLoadDefault)
                       .LoadNow()
                       .EnableAutosave();

            Settings.AutoProperty = "Hello"; //Boom! saves.

            //after some changes and development, you decide to upgrade to 1.0.0.7
            Settings = JsonSettings.Configure <VersioningSettings>("versioning.jsn")
                       .WithVersioning("1.0.0.7", VersioningResultAction.RenameAndLoadDefault)
                       .LoadNow()
                       .EnableAutosave();

            Console.WriteLine(Settings.AutoProperty); // == "Hi";

            // AutoProperty is now the default value.
            // because the versions mismatch and the
            // handling VersioningResultAction method is
            // RenameAndReload therefore there should be a file
            // named versioning.1.0.0.6.jsn holding the old
            // values and a new and filled with default values
            // versioning.jsn is created.

            // other handling techniques can be
            // VersioningResultAction.Throw: throws InvalidVersionException
            // VersioningResultAction.OverrideDefault: Incase of invalid version, default settings will be loaded and saved to disk immediately.
            // VersioningResultAction.LoadDefaultSilently: Incase of invalid version, default settings will be loaded without touching the existing file until next save.
            // VersioningResultAction.RenameAndReload
        }
Пример #2
0
        public void RenameAndLoadDefault_Case1()
        {
            using (var f = new TempfileLife(false)) {
                //load
                var cfg = JsonSettings.Configure <RecoverySettings>(f)
                          .WithRecovery(RecoveryAction.Throw)
                          .WithVersioning(new Version(1, 0, 0, 0), VersioningResultAction.Throw)
                          .LoadNow();
                cfg.Value = 1;
                cfg.Save();
                cfg.Version.Should().Be(new Version(1, 0, 0, 0));
                //assert

                //load
                var cfg2 = JsonSettings.Configure <ThrowingRecoverySettings>(f)
                           .WithRecovery(RecoveryAction.RenameAndLoadDefault)
                           .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.RenameAndLoadDefault)
                           .LoadNow();

                cfg2.Version.Should().Be(new Version(1, 2, 0, 0));
                using var _1_0_0_0 = FindFile(f, new Version(1, 0, 0, 0));

                /*
                 * new Action(() => {
                 *  cfg = JsonSettings.Configure<RecoverySettings>(f)
                 *                    .WithVersioning(new Version(1, 1, 0, 0), VersioningResultAction.Throw)
                 *                    .LoadNow();
                 * }).ShouldThrow<InvalidVersionException>();*/
            }
        }
        public void LoadDefault_Case1()
        {
            using (var f = new TempfileLife(false)) {
                //load
                var cfg = JsonSettings.Configure <VersionedSettings>(f)
                          .WithVersioning(new Version(1, 0, 0, 0), VersioningResultAction.Throw)
                          .LoadNow();

                //assert
                cfg.Version.Should().Be(new Version(1, 0, 0, 0));

                //load
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.LoadDefault)
                      .LoadNow();

                cfg.Version.Should().Be(new Version(1, 2, 0, 0));

                new Action(() => {
                    cfg = JsonSettings.Configure <VersionedSettings>(f)
                          .WithVersioning(new Version(1, 1, 0, 0), VersioningResultAction.Throw)
                          .LoadNow();
                }).ShouldThrow <InvalidVersionException>();
            }
        }
Пример #4
0
 public void OnConfigure_AddSingleConfig_PriorToLoadNow()
 {
     using (var f = new TempfileLife()) {
         //used for autodelete file after test ends
         var o = JsonSettings.Configure <CasualConfiguredSettings>(f.FileName);
         o.Modulation.Modules.Should().ContainItemsAssignableTo <RijndaelModule>();
         Assert.True(o.Modulation.Modules.Count == 1, "o.Modules.Count == 1");
     }
 }
Пример #5
0
        public void Fluent_ConstructorFileNameComparison()
        {
            using (var f = new TempfileLife()) {
                var o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();

                //validate
                o.FileName.Should().EndWith(f.FileName).And.Contain("\\");
                TestContext.Out.WriteLine($"{f.FileName} -> {o.FileName}");
            }
        }
Пример #6
0
        public void Fluent_WithFileName()
        {
            using (var f = new TempfileLife()) {
                var o = JsonSettings.Configure <CasualExampleSettings>().WithFileName(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();

                //validate
                o.FileName.Should().EndWith(f.FileName);
                Console.WriteLine($"{f.FileName} -> {o.FileName}");
            }
        }
Пример #7
0
        public void Fluent_ConstructorFileNameVsWithFilenameComparison()
        {
            using (var f = new TempfileLife()) {
                var o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();
                var n = JsonSettings.Configure <CasualExampleSettings>().WithFileName(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();

                //validate
                o.FileName.Should().Be(n.FileName);
                Console.WriteLine($"{o.FileName} <-> {n.FileName}");
            }
        }
Пример #8
0
 public void SettingsBag_InvalidPassword()
 {
     using (var f = new TempfileLife()) {
         var o = JsonSettings.Configure <SettingsBag>().WithEncryption("yoyo").WithFileName(f.FileName).LoadNow();
         o["lol"]  = "xoxo";
         o["loly"] = 2;
         o.Save();
         Action func = () => JsonSettings.Configure <SettingsBag>().WithEncryption("invalidpass").WithFileName(f.FileName).LoadNow();
         func.ShouldThrow <JsonSettingsException>("Password is invalid").Where(e => e.Message.StartsWith("Password", StringComparison.OrdinalIgnoreCase));
     }
 }
Пример #9
0
 public void SettingsBag_WithEncryption_Autosave()
 {
     using (var f = new TempfileLife()) {
         var o = JsonSettings.Configure <SettingsBag>().WithEncryption("swag").WithFileName(f.FileName).LoadNow().EnableAutosave();
         o.Autosave = true;
         o["lol"]   = "xoxo";
         o["loly"]  = 2;
         var x = JsonSettings.Configure <SettingsBag>().WithEncryption("swag").WithFileName(f.FileName).LoadNow();
         x["lol"].ShouldBeEquivalentTo("xoxo");
         x["loly"].ShouldBeEquivalentTo(2);
     }
 }
Пример #10
0
 public void SettingsBag_Passless()
 {
     using (var f = new TempfileLife()) {
         var o = JsonSettings.Configure <SettingsBag>().WithEncryption((string)null).WithFileName(f.FileName).LoadNow();
         ((RijndaelModule)o.Modulation.Modules[0]).Password.ShouldBeEquivalentTo(SecureStringExt.EmptyString);
         o.Autosave = false;
         o["lol"]   = "xoxo";
         o["loly"]  = 2;
         o.Save();
         var x = JsonSettings.Configure <SettingsBag>().WithEncryption((string)null).WithFileName(f.FileName).LoadNow();
         x["lol"].ShouldBeEquivalentTo("xoxo");
         x["loly"].ShouldBeEquivalentTo(2);
     }
 }
Пример #11
0
        public void Throw_Case1()
        {
            using (var f = new TempfileLife(false)) {
                //assert
                var cfg = JsonSettings.Configure <RecoverySettings>(f)
                          .WithVersioning(new Version(1, 0, 0, 2), VersioningResultAction.DoNothing)
                          .LoadNow();

                cfg.Version.Should().Be(new Version(1, 0, 0, 2));

                new Action(() => JsonSettings.Configure <ThrowingRecoverySettings>(f)
                           .WithRecovery(RecoveryAction.Throw)
                           .WithVersioning(new Version(1, 0, 0, 2), VersioningResultAction.RenameAndLoadDefault)
                           .LoadNow()).ShouldThrow <JsonSettingsRecoveryException>();
            }
        }
Пример #12
0
        public void Fluent_PostSaveFilenameComparison()
        {
            using (var f = new TempfileLife()) {
                var o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();
                var n = JsonSettings.Configure <CasualExampleSettings>().WithFileName(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();
                //validate
                o.FileName.Should().Be(n.FileName);
                o.Save();
                o.FileName.Should().Be(n.FileName);
                n.Save();
                o.FileName.Should().Be(n.FileName);

                o.FileName.Should().EndWith(f.FileName);
                n.FileName.Should().EndWith(f.FileName);
                TestContext.Out.WriteLine($"{o.FileName} <-> {n.FileName}");
            }
        }
Пример #13
0
        public void Fluent_SavingWithBase64_LoadingWithout()
        {
            using (var f = new TempfileLife()) {
                //used for autodelete file after test ends
                var o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().LoadNow();
                o.SomeNumeralProperty = 1;
                o.SomeProperty        = "with some value";
                o.SomeClassProperty   = new SmallClass()
                {
                    Name = "Small", Value = "Class"
                };
                o.Save();

                //validate
                Assert.Throws <JsonReaderException>(() => { o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).LoadNow(); });
            }
        }
Пример #14
0
        public void LoadDefault_ReloadOnCorruption()
        {
            using (var f = new TempfileLife(false)) {
                //load
                var cfg = JsonSettings.Configure <RecoverySettings>(f)
                          .WithRecovery(RecoveryAction.LoadDefault)
                          .LoadNow();

                cfg.Value = 5;
                cfg.Save();

                //load
                var cfgg = JsonSettings.Configure <ThrowingRecoverySettings>(f)
                           .WithRecovery(RecoveryAction.LoadDefault)
                           .LoadNow();

                cfgg.Value.Should().BeNullOrEmpty();
            }
        }
Пример #15
0
        public void Use_Configure_CasualSettingsExample()
        {
            using (var f = new TempfileLife()) {
                //load using Configure
                var o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();
                o.SomeNumeralProperty = 1;
                o.SomeProperty        = "with some value";
                o.SomeClassProperty   = new SmallClass()
                {
                    Name = "Small", Value = "Class"
                };
                o.Save();

                //validate
                o = JsonSettings.Configure <CasualExampleSettings>(f.FileName).WithBase64().WithEncryption("SuperPassword").LoadNow();
                o.SomeProperty.Should().Be("with some value");
                o.SomeNumeralProperty.ShouldBeEquivalentTo(1);
                o.SomeClassProperty.Should().BeOfType(typeof(SmallClass)).And.Match(obj => (obj as SmallClass).Name == "Small");
            }
        }
Пример #16
0
        public void SuspendAutosaving_Case1()
        {
            using (var f = new TempfileLife()) {
                var o = JsonSettings.Load <SettingsBag>(f);

                dynamic d = o.AsDynamic();

                d.SomeProp = "Works";
                d.Num      = 1;
                Assert.IsTrue(d["SomeProp"] == "Works");
                Assert.IsTrue(d.Num == 1);

                o.Save();
                o = JsonSettings.Configure <SettingsBag>(f)
                    .LoadNow()
                    .EnableAutosave();

                o["SomeProp"].Should().Be("Works");
                o["Num"].Should().Be(1L); //newtonsoft deserializes numbers as long.
                StrongBox <int> a = new StrongBox <int>();
                o.AfterSave += (sender, destinition) => { a.Value++; };

                using (o.SuspendAutosave()) {
                    o["SomeProp"] = "Works2";
                    o["Num"]      = 2;
                    a.Value.Should().Be(0);
                    var k = JsonSettings.Load <SettingsBag>(f);
                    k["SomeProp"].Should().Be("Works");
                    k["Num"].Should().Be(1L); //newtonsoft deserializes numbers as long.
                    a.Value.Should().Be(0);
                }

                a.Value.Should().Be(1);

                var kk = JsonSettings.Load <SettingsBag>(f);
                kk["SomeProp"].Should().Be("Works2");
                kk["Num"].Should().Be(2L); //newtonsoft deserializes numbers as long.
            }
        }
Пример #17
0
        public void RenameAndLoadDefault_Case1()
        {
            using (var f = new TempfileLife(false)) {
                //load
                var cfg = JsonSettings.Configure <VersionedSettings>(f)
                          .WithVersioning(new Version(1, 0, 0, 0), VersioningResultAction.RenameAndLoadDefault)
                          .LoadNow();

                //assert
                cfg.Version.Should().Be(new Version(1, 0, 0, 0));

                //load
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.RenameAndLoadDefault)
                      .LoadNow();

                using var _1_0_0_0 = FindFile(f, new Version(1, 0, 0, 0));

                //change version and save
                cfg.Version = new Version("1.0.0.1");
                cfg.Save();

                //assert
                cfg.Version.Should().Be(new Version(1, 0, 0, 1));
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 0, 0, 1), VersioningResultAction.RenameAndLoadDefault)
                      .LoadNow();
                cfg.Version.Should().Be(new Version(1, 0, 0, 1));

                //assert
                cfg = JsonSettings.Configure <VersionedSettings>(f)
                      .WithVersioning(new Version(1, 0, 0, 2), VersioningResultAction.RenameAndLoadDefault)
                      .LoadNow();
                using var _1_0_0_1 = FindFile(f, new Version(1, 0, 0, 1));

                cfg.Version.Should().Be(new Version(1, 0, 0, 2));
            }
        }
Пример #18
0
        public void OnConfigure_Only_WithEncyption_CheckBeforeLoadNow()
        {
            using (var f = new TempfileLife()) {
                //used for autodelete file after test ends
                var o = JsonSettings.Configure <CasualConfiguredSettings>(f.FileName);
                o.Modulation.Modules.Should().ContainItemsAssignableTo <RijndaelModule>();
                Assert.True(o.Modulation.Modules.Count == 1, "o.Modules.Count == 1");
                o.LoadNow();
                o.SomeNumeralProperty = 1;
                o.SomeProperty        = "with some value";
                o.SomeClassProperty   = new SmallClass()
                {
                    Name = "Small", Value = "Class"
                };
                o.Save();

                //validate
                o = JsonSettings.Configure <CasualConfiguredSettings>(f.FileName).LoadNow();
                o.Modulation.Modules.Should().ContainItemsAssignableTo <RijndaelModule>();
                o.SomeProperty.Should().Be("with some value");
                o.SomeNumeralProperty.ShouldBeEquivalentTo(1);
                o.SomeClassProperty.Should().BeOfType(typeof(SmallClass)).And.Match(obj => (obj as SmallClass).Name == "Small");
            }
        }
Пример #19
0
        public void LoadDefaultAndSave_Case1()
        {
            using (var f = new TempfileLife(false)) {
                //load
                var cfg = JsonSettings.Configure <RecoverySettings>(f)
                          .WithVersioning(new Version(1, 0, 0, 0), VersioningResultAction.Throw)
                          .LoadNow();

                //assert
                cfg.Version.Should().Be(new Version(1, 0, 0, 0));

                //load
                cfg = JsonSettings.Configure <RecoverySettings>(f)
                      .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.LoadDefaultAndSave)
                      .LoadNow();
                cfg.Version.Should().Be(new Version(1, 2, 0, 0));

                new Action(() => {
                    cfg = JsonSettings.Configure <RecoverySettings>(f)
                          .WithVersioning(new Version(1, 2, 0, 0), VersioningResultAction.Throw)
                          .LoadNow();
                }).ShouldNotThrow();
            }
        }