-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#39 Added JsonConfig to replace Config
The class "JsonConfig" will be renamed to "Config" once refactoring is completed
- Loading branch information
Geoff Green
committed
Jan 18, 2014
1 parent
5f743ae
commit 64f1f75
Showing
13 changed files
with
700 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Procon.Core.Shared.Test/TestConfig/Mocks/MockSimpleConcrete.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Procon.Core.Shared.Test.TestConfig.Mocks { | ||
internal class MockSimpleConcrete { | ||
public String Name { get; set; } | ||
public int Age { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using Newtonsoft.Json.Linq; | ||
using Procon.Core.Shared.Test.TestConfig.Mocks; | ||
|
||
namespace Procon.Core.Shared.Test.TestConfig { | ||
[TestFixture] | ||
public class TestAppend : TestConfigBase { | ||
/// <summary> | ||
/// Tests data can be appended to the root | ||
/// </summary> | ||
[Test] | ||
public void TestAppendedToRoot() { | ||
IConfig config = new JsonConfig().Create<MockSimpleConcrete>(); | ||
config.Append(new MockSimpleConcrete() { | ||
Name = "Phogue", | ||
Age = 100 | ||
}); | ||
|
||
Assert.AreEqual("Phogue", config.Root.First["Name"].Value<String>()); | ||
Assert.AreEqual(100, config.Root.First["Age"].Value<int>()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests an argument null exception is raised if null is passed into union. | ||
/// </summary> | ||
[Test, ExpectedException(typeof(ArgumentNullException))] | ||
public void TestNullException() { | ||
new JsonConfig().Append<MockSimpleConcrete>(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.IO; | ||
using NUnit.Framework; | ||
|
||
namespace Procon.Core.Shared.Test.TestConfig { | ||
public abstract class TestConfigBase { | ||
/// <summary> | ||
/// Test file for us to save/load from. | ||
/// </summary> | ||
public FileInfo ConfigFileA = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete.json")); | ||
|
||
/// <summary> | ||
/// Test file for us to save/load from. | ||
/// </summary> | ||
public FileInfo ConfigFileB = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "Procon.Core.Shared.Test.TestConfig.Mocks.AlternativeName.json")); | ||
|
||
[SetUp] | ||
public void ClearConfigDirectory() { | ||
this.ConfigFileA.Refresh(); | ||
|
||
if (this.ConfigFileA.Directory != null) { | ||
if (this.ConfigFileA.Directory.Exists == true) { | ||
this.ConfigFileA.Directory.Delete(true); | ||
} | ||
|
||
this.ConfigFileA.Directory.Create(); | ||
} | ||
|
||
this.ConfigFileB.Refresh(); | ||
|
||
if (this.ConfigFileB.Directory != null) { | ||
if (this.ConfigFileB.Directory.Exists == true) { | ||
this.ConfigFileB.Directory.Delete(true); | ||
} | ||
|
||
this.ConfigFileB.Directory.Create(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using Newtonsoft.Json.Linq; | ||
using Procon.Core.Shared.Test.TestConfig.Mocks; | ||
|
||
namespace Procon.Core.Shared.Test.TestConfig { | ||
[TestFixture] | ||
public class TestCreate : TestConfigBase { | ||
/// <summary> | ||
/// Tests that the namespace is setup correctly. | ||
/// </summary> | ||
[Test] | ||
public void TestStructureCreated() { | ||
IConfig config = new JsonConfig().Create<MockSimpleConcrete>(); | ||
|
||
Assert.IsNotNull(config.Document); | ||
Assert.IsNotNull(config.Document["Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"]); | ||
Assert.IsNotNull(config.Document["Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"].Value<JArray>()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the root is set to the maximum document value. | ||
/// </summary> | ||
[Test] | ||
public void TestRootMatchesNamespace() { | ||
IConfig config = new JsonConfig().Create<MockSimpleConcrete>(); | ||
|
||
Assert.AreEqual(config.Document["Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"], config.Root); | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/Procon.Core.Shared.Test/TestConfig/TestLoadDirectoryInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.IO; | ||
using NUnit.Framework; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Procon.Core.Shared.Test.TestConfig { | ||
[TestFixture] | ||
public class TestLoadDirectoryInfo : TestConfigBase { | ||
/// <summary> | ||
/// Tests that the document is populated after loading | ||
/// </summary> | ||
[Test] | ||
public void TestSingleDocumentNotNull() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.IsNotNull(config.Document); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root is populated after loading | ||
/// </summary> | ||
[Test] | ||
public void TestSingleRootNotNull() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.IsNotNull(config.Root); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root element is set if a property exists with the same filename. | ||
/// </summary> | ||
[Test] | ||
public void TestSingleRootSetToFilePath() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.AreEqual("Phogue", config.Root.First["Name"].Value<String>()); | ||
Assert.AreEqual(100, config.Root.First["Age"].Value<int>()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the document is populated after loading | ||
/// </summary> | ||
[Test] | ||
public void TestUnionedDocumentNotNull() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
File.WriteAllText(this.ConfigFileB.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.AlternativeName"": [ { ""Name"":""Ike"", ""Age"": 10 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.IsNotNull(config.Document); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root is populated after loading | ||
/// </summary> | ||
[Test] | ||
public void TestUnionedRootNotNull() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
File.WriteAllText(this.ConfigFileB.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.AlternativeName"": [ { ""Name"":""Ike"", ""Age"": 10 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.IsNotNull(config.Root); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root element is set if a property exists with the same filename. | ||
/// Files are loaded in alphabetical order. | ||
/// </summary> | ||
[Test] | ||
public void TestUnionedRootSetToFirstFilePath() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
File.WriteAllText(this.ConfigFileB.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.AlternativeName"": [ { ""Name"":""Ike"", ""Age"": 10 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.AreEqual("Ike", config.Root.First["Name"].Value<String>()); | ||
Assert.AreEqual(10, config.Root.First["Age"].Value<int>()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the document contains the alternative data | ||
/// </summary> | ||
[Test] | ||
public void TestUnionedDocumentContainsAlternativeToRoot() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
File.WriteAllText(this.ConfigFileB.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.AlternativeName"": [ { ""Name"":""Ike"", ""Age"": 10 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.AreEqual("Phogue", config.Document["Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"].First["Name"].Value<String>()); | ||
Assert.AreEqual(100, config.Document["Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"].First["Age"].Value<int>()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root element is set if a property exists with the same filename. | ||
/// </summary> | ||
[Test] | ||
public void TestUnionedWithIdenticalNamespaces() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
File.WriteAllText(this.ConfigFileB.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Ike"", ""Age"": 10 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA.Directory); | ||
|
||
Assert.AreEqual("Ike", config.Root.First["Name"].Value<String>()); | ||
Assert.AreEqual(10, config.Root.First["Age"].Value<int>()); | ||
|
||
Assert.AreEqual("Phogue", config.Root.Last["Name"].Value<String>()); | ||
Assert.AreEqual(100, config.Root.Last["Age"].Value<int>()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests an argument null exception is raised if null is passed into union. | ||
/// </summary> | ||
[Test, ExpectedException(typeof(ArgumentNullException))] | ||
public void TestNullException() { | ||
new JsonConfig().Load((DirectoryInfo)null); | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
src/Procon.Core.Shared.Test/TestConfig/TestLoadFileInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
using System.IO; | ||
using NUnit.Framework; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Procon.Core.Shared.Test.TestConfig { | ||
[TestFixture] | ||
public class TestLoadFileInfo : TestConfigBase { | ||
/// <summary> | ||
/// Tests that the document is populated after loading | ||
/// </summary> | ||
[Test] | ||
public void TestDocumentNotNull() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.IsNotNull(config.Document); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root is populated after loading | ||
/// </summary> | ||
[Test] | ||
public void TestRootNotNullWithMatchingProperty() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.IsNotNull(config.Root); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root is still populated even if no matching property is found. It | ||
/// will then get the first property in the document. | ||
/// </summary> | ||
[Test] | ||
public void TestRootNotNullWithoutMatchingProperty() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""This.Type.Does.Not.Exist"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.IsNotNull(config.Root); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root is still populated even if the resulting document is empty. | ||
/// </summary> | ||
[Test] | ||
public void TestRootNotNullEmptyFile() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.IsNotNull(config.Root); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root is still populated even if the first property is not an array | ||
/// </summary> | ||
[Test] | ||
public void TestRootNotNullNotArray() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""This.Type.Does.Not.Exist"": { ""Name"":""Phogue"", ""Age"": 100 } }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.IsNotNull(config.Root); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the root element is set if a property exists with the same filename. | ||
/// </summary> | ||
[Test] | ||
public void TestRootSetToFilePath() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""Name"":""Phogue"", ""Age"": 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.AreEqual("Phogue", config.Root.First["Name"].Value<String>()); | ||
Assert.AreEqual(100, config.Root.First["Age"].Value<int>()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that invalid json will result in an empty document/root | ||
/// </summary> | ||
[Test] | ||
public void TestInvalidJsonParsingNotNullRoot() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""N 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.IsNotNull(config.Root); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that invalid json will result in an empty document/root | ||
/// </summary> | ||
[Test] | ||
public void TestInvalidJsonParsingNotNullDocument() { | ||
File.WriteAllText(this.ConfigFileA.FullName, @"{ ""Procon.Core.Shared.Test.TestConfig.Mocks.MockSimpleConcrete"": [ { ""N 100 } ] }"); | ||
|
||
IConfig config = new JsonConfig().Load(this.ConfigFileA); | ||
|
||
Assert.IsNotNull(config.Document); | ||
} | ||
|
||
/// <summary> | ||
/// Tests an argument null exception is raised if null is passed into union. | ||
/// </summary> | ||
[Test, ExpectedException(typeof(ArgumentNullException))] | ||
public void TestNullException() { | ||
new JsonConfig().Load((FileInfo) null); | ||
} | ||
} | ||
} |
Oops, something went wrong.