public void SettingsManagerShouldCollectSetPropertyExceptions() { var inMemoryRepository = new InMemoryRepository(); inMemoryRepository.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Decimal", Value = "test" }); inMemoryRepository.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Text", Value = "test" }); var manager = new SettingsManagerPeriodic(inMemoryRepository); bool propertyErrorOccurred = false; manager.PropertyError += (sender, eventArgs) => { propertyErrorOccurred = eventArgs.IsRepeating; }; var settings = manager.Get<Settings>(true); Assert.IsTrue(propertyErrorOccurred); }
public void SavingAndLoadingMustBeSame() { var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<Settings>(); var custom = new CustomSetting() { Text = "1", Ref = new CustomSetting() { Text = "2" } }; settings.Decimal = 0.01m; settings.Text = "test"; settings.BigInteger = UInt64.MaxValue; settings.Boolean = true; settings.Character = 'X'; settings.Enum = ConsoleColor.Green; settings.Float = float.MaxValue; settings.Integer = int.MinValue; settings.Time = DateTime.MaxValue; settings.TimeSpan = TimeSpan.MaxValue; settings.TimeSpan2 = new TimeSpan(0, 1, 2, 3); settings.Custom = custom; manager.Save(settings); settings = manager.Get<Settings>(true); Assert.AreEqual(0.01m, settings.Decimal); Assert.AreEqual("test", settings.Text); Assert.AreEqual(UInt64.MaxValue, settings.BigInteger); Assert.AreEqual(true, settings.Boolean); Assert.AreEqual('X', settings.Character); Assert.AreEqual(ConsoleColor.Green, settings.Enum); Assert.AreEqual(float.MaxValue, settings.Float); Assert.AreEqual(int.MinValue, settings.Integer); Assert.AreEqual(0, (int)(DateTime.MaxValue - settings.Time).TotalSeconds); //compare only to seconds precision Assert.AreEqual(TimeSpan.MaxValue, settings.TimeSpan); Assert.AreEqual(new TimeSpan(0, 1, 2, 3), settings.TimeSpan2); Assert.AreEqual(custom.Text, settings.Custom.Text); Assert.AreEqual(custom.Ref.Text, settings.Custom.Ref.Text); }
public void SavingMustWriteNewObjectToRepository() { var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo){AutoPersistOnCreate = false}; var settings = manager.Get<Settings>(); var custom = new CustomSetting() { Text = "abc" }; var value = repo.Content.Where(i => i.Name == "Custom").Select(i=>i.Value).FirstOrDefault(); Assert.IsNull(settings.Custom); Assert.IsNull(value); settings.Custom = custom; manager.Save(settings); value = repo.Content.First(i => i.Name == "Custom").Value; Assert.IsNotNull(value); Assert.IsTrue(value.Contains("abc")); }
public void PropertyChangedEventMustNotFireWhenPropertyValueNotChanged() { var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<Settings>(); settings.Text = "old"; var fired = false; manager.PropertyValueChanged += (sender, args) => { fired = true; }; manager.ChangeAndSave(s => s.Text = "old", settings); manager.Get<Settings>(true); Assert.IsFalse(fired); }
public void ReadOnlyPropertyMustBeRespected() { var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<ReadOnlySettings>(); settings.Text = "a"; manager.Save(settings); settings = manager.Get<ReadOnlySettings>(true); Assert.AreNotEqual("a", settings.Text); manager.ChangeAndSave(s => s.Text = "b", settings); settings = manager.Get<ReadOnlySettings>(true); Assert.AreNotEqual("b", settings.Text); }
public void PropertyChangedEventMustFire() { var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<Settings>(); settings.Text = "old"; string newValue = ""; string oldValue = ""; manager.PropertyValueChanged += (sender, args) => { args.When<Settings, string>((s) => s.Text, (ov, nv) => { oldValue = ov; newValue = nv; }); }; repo.Content.Single(s => s.Name == "Text").Value = "new"; //simulate value change in repository (db) manager.Get<Settings>(true); Assert.AreEqual("new", newValue); Assert.AreEqual("old", oldValue); }
public void PreriodicReaderMustRaiseError() { var cts = new CancellationTokenSource(); var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<ReadOnlySettings>(); settings.Text = "a"; manager.Save(settings); var error = false; manager.PeriodicReaderError += (s, a) => { error = true; }; manager.StartReadingTask(TimeSpan.FromMilliseconds(10), cts.Token); Thread.Sleep(200); Assert.AreEqual(false, error); }
public void PeriodicReaderCancel() { var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<Settings>(); var cts = new CancellationTokenSource(); var task = manager.StartReadingTask(TimeSpan.FromMilliseconds(1), cts.Token); int cnt = 0; bool cancelEvent = false; manager.ReaderExecuting += (sender, args) => cnt++; manager.PeriodicReaderCanceled += (sender, args) => cancelEvent = true; Thread.Sleep(200); cts.Cancel(); Thread.Sleep(200); Assert.IsTrue(cancelEvent); Assert.IsTrue(task.IsCanceled); Assert.IsTrue(cnt > 0); }
public void EmptyStringsMustWork() { var repo = new InMemoryRepository(); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<Settings>(); settings.Text = ""; manager.Save(settings); settings = manager.Get<Settings>(); Assert.AreEqual("", settings.Text); }
public void BasicReading() { var repo = new InMemoryRepository(); repo.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Decimal", Value = "1.5" }); repo.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Text", Value = "test" }); var manager = new SettingsManagerPeriodic(repo); var settings = manager.Get<Settings>(); Assert.AreEqual(1.5m, settings.Decimal); Assert.AreEqual("test", settings.Text); }