public void CommitMethodCanHandleEmptyValue() { var json = @"{ ""key1"": """", ""key2"": { ""key3"": """" } }"; var expectedJson = @"{ ""key1"": ""value1"", ""key2"": { ""key3"": ""value2"" } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var outputCacheStream = new MemoryStream(); jsonConfigSrc.Load(StringToStream(json)); jsonConfigSrc.Set("key1", "value1"); jsonConfigSrc.Set("key2:key3", "value2"); jsonConfigSrc.Commit(StringToStream(json), outputCacheStream); var newContents = StreamToString(outputCacheStream); Assert.Equal(expectedJson, newContents); }
public void CommitOperationThrowsExceptionWhenFindInvalidModificationAfterLoadOperation() { var json = @"{ ""name"": ""test"", ""address"": { ""street"": ""Something street"", ""zipcode"": ""12345"" } }"; var modifiedJson = @" { ""name"": [""first"", ""last""], ""address"": { ""street"": ""Something street"", ""zipcode"": ""12345"" } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var outputCacheStream = new MemoryStream(); jsonConfigSrc.Load(StringToStream(json)); var exception = Assert.Throws <FormatException>( () => jsonConfigSrc.Commit(StringToStream(modifiedJson), outputCacheStream)); Assert.Equal(Resources.FormatError_UnsupportedJSONToken("StartArray", "name", 3, 12), exception.Message); }
public void CommitOperationThrowsExceptionWhenFindNewlyAddedKeyAfterLoadOperation() { var json = @"{ ""name"": ""test"", ""address"": { ""street"": ""Something street"", ""zipcode"": ""12345"" } }"; var newJson = @"{ ""name"": ""test"", ""address"": { ""street"": ""Something street"", ""zipcode"": ""12345"" }, ""NewKey"": ""NewValue"" }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var outputCacheStream = new MemoryStream(); jsonConfigSrc.Load(StringToStream(json)); var exception = Assert.Throws <InvalidOperationException>( () => jsonConfigSrc.Commit(StringToStream(newJson), outputCacheStream)); Assert.Equal(Resources.FormatError_CommitWhenNewKeyFound("NewKey"), exception.Message); }
public void NonObjectRootIsInvalid() { var json = @"'test'"; var jsonConfigSource = new JsonConfigurationSource(ArbitraryFilePath); var expectedMsg = Resources.FormatError_RootMustBeAnObject(string.Empty, 1, 6); var exception = Assert.Throws <FormatException>(() => jsonConfigSource.Load(StringToStream(json))); Assert.Equal(expectedMsg, exception.Message); }
public void LoadMethodCanHandleEmptyValue() { var json = @" { 'name': '' }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); jsonConfigSrc.Load(StringToStream(json)); Assert.Equal(1, jsonConfigSrc.Data.Count); Assert.Equal(string.Empty, jsonConfigSrc.Data["name"]); }
public void ArraysAreNotSupported() { var json = @"{ 'name': 'test', 'address': ['Something street', '12345'] }"; var jsonConfigSource = new JsonConfigurationSource(ArbitraryFilePath); var expectedMsg = Resources.FormatError_UnsupportedJSONToken("StartArray", "address", 3, 29); var exception = Assert.Throws <FormatException>(() => jsonConfigSource.Load(StringToStream(json))); Assert.Equal(expectedMsg, exception.Message); }
public void ArraysAreNotSupported() { var json = @"{ 'name': 'test', 'address': ['Something street', '12345'] }"; var jsonConfigSource = new JsonConfigurationSource(ArbitraryFilePath); var expectedMsg = Resources.FormatError_UnsupportedJSONToken("StartArray", "address", 3, 29); var exception = Assert.Throws<FormatException>(() => jsonConfigSource.Load(StringToStream(json))); Assert.Equal(expectedMsg, exception.Message); }
public void ThrowExceptionWhenUnexpectedEndFoundBeforeFinishParsing() { var json = @"{ 'name': 'test', 'address': { 'street': 'Something street', 'zipcode': '12345' } /* Missing a right brace here*/"; var jsonConfigSource = new JsonConfigurationSource(ArbitraryFilePath); var expectedMsg = Resources.FormatError_UnexpectedEnd("address", 7, 44); var exception = Assert.Throws <FormatException>(() => jsonConfigSource.Load(StringToStream(json))); Assert.Equal(expectedMsg, exception.Message); }
public void ThrowExceptionWhenKeyIsDuplicated() { var json = @"{ 'name': 'test', 'address': { 'street': 'Something street', 'zipcode': '12345' }, 'name': 'new name' }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var exception = Assert.Throws <FormatException>(() => jsonConfigSrc.Load(StringToStream(json))); Assert.Equal(Resources.FormatError_KeyIsDuplicated("name"), exception.Message); }
public void CanCreateNewConfig() { var targetJson = @"{ ""name"": ""test"", ""address:street"": ""Something street"", ""address:zipcode"": ""12345"" }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var outputCacheStream = new MemoryStream(); jsonConfigSrc.Data["name"] = "test"; jsonConfigSrc.Data["address:street"] = "Something street"; jsonConfigSrc.Data["address:zipcode"] = "12345"; jsonConfigSrc.GenerateNewConfig(outputCacheStream); Assert.Equal(targetJson, StreamToString(outputCacheStream)); }
public void JsonConfiguration_Throws_On_Missing_Configuration_File() { var configSource = new JsonConfigurationSource("NotExistingConfig.json", optional: false); Assert.Throws<FileNotFoundException>(() => { try { configSource.Load(); } catch (FileNotFoundException exception) { Assert.Equal( string.Format(Resources.Error_FileNotFound, Path.Combine(Directory.GetCurrentDirectory(), "NotExistingConfig.json")), exception.Message); throw; } }); }
public void SupportAndIgnoreComments() { var json = @"/* Comments */ {/* Comments */ ""name"": /* Comments */ ""test"", ""address"": { ""street"": ""Something street"", /* Comments */ ""zipcode"": ""12345"" } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); jsonConfigSrc.Load(StringToStream(json)); Assert.Equal(3, jsonConfigSrc.Data.Count); Assert.Equal("test", jsonConfigSrc.Data["name"]); Assert.Equal("Something street", jsonConfigSrc.Data["address:street"]); Assert.Equal("12345", jsonConfigSrc.Data["address:zipcode"]); }
public void LoadKeyValuePairsFromValidJson() { var json = @" { 'firstname': 'test', 'test.last.name': 'last.name', 'residential.address': { 'street.name': 'Something street', 'zipcode': '12345' } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); jsonConfigSrc.Load(StringToStream(json)); Assert.Equal("test", jsonConfigSrc.Get("firstname")); Assert.Equal("last.name", jsonConfigSrc.Get("test.last.name")); Assert.Equal("Something street", jsonConfigSrc.Get("residential.address:STREET.name")); Assert.Equal("12345", jsonConfigSrc.Get("residential.address:zipcode")); }
public void CommitMethodPreservesCommments() { var json = @"{ ""name"": ""test"", ""address"": { ""street"": ""Something street"", ""zipcode"": ""12345"" } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var outputCacheStream = new MemoryStream(); jsonConfigSrc.Load(StringToStream(json)); jsonConfigSrc.Commit(StringToStream(json), outputCacheStream); var newContents = StreamToString(outputCacheStream); Assert.Equal(json, newContents); }
public void CommitOperationThrowsExceptionWhenKeysAreMissingInConfigFile() { var json = @"{ ""name"": ""test"", ""address"": { ""street"": ""Something street"", ""zipcode"": ""12345"" } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var outputCacheStream = new MemoryStream(); jsonConfigSrc.Load(StringToStream(json)); json = json.Replace(@"""name"": ""test"",", string.Empty); var exception = Assert.Throws <InvalidOperationException>( () => jsonConfigSrc.Commit(StringToStream(json), outputCacheStream)); Assert.Equal(Resources.FormatError_CommitWhenKeyMissing("name"), exception.Message); }
public void LoadKeyValuePairsFromValidJson() { var json = @" { 'firstname': 'test', 'test.last.name': 'last.name', 'residential.address': { 'street.name': 'Something street', 'zipcode': '12345' } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); jsonConfigSrc.Load(StringToStream(json)); Assert.Equal(4, jsonConfigSrc.Data.Count); Assert.Equal("test", jsonConfigSrc.Data["firstname"]); Assert.Equal("last.name", jsonConfigSrc.Data["test.last.name"]); Assert.Equal("Something street", jsonConfigSrc.Data["residential.address:STREET.name"]); Assert.Equal("12345", jsonConfigSrc.Data["residential.address:zipcode"]); }
public void CommitMethodUpdatesValues() { var json = @"{ ""name"": ""test"", ""address"": { ""street"": ""Something street"", ""zipcode"": ""12345"" } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var outputCacheStream = new MemoryStream(); jsonConfigSrc.Load(StringToStream(json)); jsonConfigSrc.Set("name", "new_name"); jsonConfigSrc.Set("address:zipcode", "67890"); jsonConfigSrc.Commit(StringToStream(json), outputCacheStream); var newContents = StreamToString(outputCacheStream); Assert.Equal(json.Replace("test", "new_name").Replace("12345", "67890"), newContents); }
public void LoadMethodCanHandleEmptyValue() { var json = @" { 'name': '' }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); jsonConfigSrc.Load(StringToStream(json)); Assert.Equal(string.Empty, jsonConfigSrc.Get("name")); }
public void JsonConfiguration_Does_Not_Throw_On_Optional_Configuration() { var configSource = new JsonConfigurationSource("NotExistingConfig.json", optional: true); configSource.Load(); Assert.Throws<InvalidOperationException>(() => configSource.Get("key")); }
public void ThrowExceptionWhenUnexpectedEndFoundBeforeFinishParsing() { var json = @"{ 'name': 'test', 'address': { 'street': 'Something street', 'zipcode': '12345' } /* Missing a right brace here*/"; var jsonConfigSource = new JsonConfigurationSource(ArbitraryFilePath); var expectedMsg = Resources.FormatError_UnexpectedEnd("address", 7, 44); var exception = Assert.Throws<FormatException>(() => jsonConfigSource.Load(StringToStream(json))); Assert.Equal(expectedMsg, exception.Message); }
public void ThrowExceptionWhenKeyIsDuplicated() { var json = @"{ 'name': 'test', 'address': { 'street': 'Something street', 'zipcode': '12345' }, 'name': 'new name' }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); var exception = Assert.Throws<FormatException>(() => jsonConfigSrc.Load(StringToStream(json))); Assert.Equal(Resources.FormatError_KeyIsDuplicated("name"), exception.Message); }
public void SupportAndIgnoreComments() { var json = @"/* Comments */ {/* Comments */ ""name"": /* Comments */ ""test"", ""address"": { ""street"": ""Something street"", /* Comments */ ""zipcode"": ""12345"" } }"; var jsonConfigSrc = new JsonConfigurationSource(ArbitraryFilePath); jsonConfigSrc.Load(StringToStream(json)); Assert.Equal("test", jsonConfigSrc.Get("name")); Assert.Equal("Something street", jsonConfigSrc.Get("address:street")); Assert.Equal("12345", jsonConfigSrc.Get("address:zipcode")); }
public void NonObjectRootIsInvalid() { var json = @"'test'"; var jsonConfigSource = new JsonConfigurationSource(ArbitraryFilePath); var expectedMsg = Resources.FormatError_RootMustBeAnObject(string.Empty, 1, 6); var exception = Assert.Throws<FormatException>(() => jsonConfigSource.Load(StringToStream(json))); Assert.Equal(expectedMsg, exception.Message); }