#region CPL License /* Nuclex Framework Copyright (C) 2002-2014 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion #if UNITTEST using System; using NUnit.Framework; using System.IO; using Microsoft.Win32; using System.Globalization; using System.Collections.Generic; namespace Nuclex.Support.Settings { /// Unit tests for the windows registry settings store [TestFixture] internal class WindowsRegistryStoreTest { #region class TestContext /// Sets up a temporary registry key for the unit test private class TestContext : IDisposable { /// Initializes a new test context public TestContext() { this.keyName = Guid.NewGuid().ToString(); this.registryKey = Registry.CurrentUser.CreateSubKey(this.keyName); this.store = new WindowsRegistryStore(this.registryKey, writable: true); } /// Immediately frees all resources owned by the test context public void Dispose() { if(this.store != null) { this.store.Dispose(); this.store = null; this.registryKey = null; } else if(this.registryKey != null) { this.registryKey.Dispose(); this.registryKey = null; } if(this.keyName != null) { Registry.CurrentUser.DeleteSubKeyTree(this.keyName); this.keyName = null; } } /// Store created on a temporary registry key public WindowsRegistryStore Store { get { return this.store; } } /// Name of the temporary registry key private string keyName; /// Registry key (ownership transfered to the store) private RegistryKey registryKey; /// Store that is accessing the registry key private WindowsRegistryStore store; } #endregion // class TestContext /// Verifies that new instances of the registry store can be created [Test] public void CanBeCreated() { Assert.That( () => { using(var context = new TestContext()) { } }, Throws.Nothing ); } /// Verifies that booleans can be stored in the registry [Test] public void BooleansCanBeStored() { using(var context = new TestContext()) { context.Store.Set(null, "test", true); Assert.That(context.Store.Get(null, "test"), Is.True); context.Store.Set(null, "test", false); Assert.That(context.Store.Get(null, "test"), Is.False); } } /// Verifies that integers can be stored in the registry [Test] public void IntegersCanBeStored() { using(var context = new TestContext()) { context.Store.Set(null, "test", 123); Assert.That(context.Store.Get(null, "test"), Is.EqualTo(123)); context.Store.Set(null, "test", 456); Assert.That(context.Store.Get(null, "test"), Is.EqualTo(456)); } } /// Verifies that floats can be stored in the registry [Test] public void FloatsCanBeStored() { float testValue = float.Parse("123.456", CultureInfo.InvariantCulture); using(var context = new TestContext()) { context.Store.Set(null, "test", testValue); Assert.That(context.Store.Get(null, "test"), Is.EqualTo(testValue)); testValue = float.Parse("654.321", CultureInfo.InvariantCulture); context.Store.Set(null, "test", testValue); Assert.That(context.Store.Get(null, "test"), Is.EqualTo(testValue)); } } /// Verifies that strings can be stored in the registry [Test] public void StringsCanBeStored() { using(var context = new TestContext()) { context.Store.Set(null, "test", "hello world"); Assert.That(context.Store.Get(null, "test"), Is.EqualTo("hello world")); context.Store.Set(null, "test", "world hello"); Assert.That(context.Store.Get(null, "test"), Is.EqualTo("world hello")); } } /// Verifies that the subkeys of a registry key can be enumerated [Test] public void CategoriesCanBeEnumerated() { string[] names = new string[] { "one", "two", "three" }; using(var context = new TestContext()) { context.Store.Set(names[0], "sol", 21); context.Store.Set(names[1], "sol", 42); context.Store.Set(names[2], "sol", 84); Assert.That(context.Store.EnumerateCategories(), Is.EquivalentTo(names)); } } /// Verifies that the values under a registry subkey can be enumerated [Test] public void OptionsInCategoryCanBeEnumerated() { string[] names = new string[] { "one", "two", "three" }; using(var context = new TestContext()) { context.Store.Set("test", names[0], 1); context.Store.Set("test", names[1], 2); context.Store.Set("test", names[2], 3); var optionInfos = new List(context.Store.EnumerateOptions("test")); Assert.That(optionInfos.Count, Is.EqualTo(3)); } } /// Verifies that the values under a registry key can be enumerated [Test] public void RootOptionsCanBeEnumerated() { string[] names = new string[] { "one", "two", "three" }; using(var context = new TestContext()) { context.Store.Set(null, names[0], 1); context.Store.Set(null, names[1], 2); context.Store.Set(null, names[2], 3); var optionInfos = new List(context.Store.EnumerateOptions(null)); Assert.That(optionInfos.Count, Is.EqualTo(3)); } } } } // namespace Nuclex.Support.Settings #endif // UNITTEST