Repository: reef Updated Branches: refs/heads/master ee1b6bbe6 -> 0d3b46f51
[REEF-1308] Enhance Tang duplicated named parameter entries error message Update ConfigurationBuilderImpl Add test case JIRA: [REEF-1308](https://issues.apache.org/jira/browse/REEF-1308) This closes #920 Project: http://git-wip-us.apache.org/repos/asf/reef/repo Commit: http://git-wip-us.apache.org/repos/asf/reef/commit/0d3b46f5 Tree: http://git-wip-us.apache.org/repos/asf/reef/tree/0d3b46f5 Diff: http://git-wip-us.apache.org/repos/asf/reef/diff/0d3b46f5 Branch: refs/heads/master Commit: 0d3b46f5129b6fde48f7685083dcf25d87f58604 Parents: ee1b6bb Author: Julia Wang <[email protected]> Authored: Wed Apr 6 13:14:49 2016 -0700 Committer: Andrew Chung <[email protected]> Committed: Thu Apr 7 11:34:41 2016 -0700 ---------------------------------------------------------------------- .../Configuration/TestNegativeCases.cs | 52 ++++++++++++++++++++ .../Org.Apache.REEF.Tang.Tests.csproj | 1 + .../Configuration/ConfigurationBuilderImpl.cs | 12 ++++- 3 files changed, 63 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/reef/blob/0d3b46f5/lang/cs/Org.Apache.REEF.Tang.Tests/Configuration/TestNegativeCases.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Configuration/TestNegativeCases.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Configuration/TestNegativeCases.cs new file mode 100644 index 0000000..310d8d0 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Configuration/TestNegativeCases.cs @@ -0,0 +1,52 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Tang.Implementations.Configuration; +using Org.Apache.REEF.Tang.Implementations.Tang; +using Xunit; + +namespace Org.Apache.REEF.Tang.Tests.Configuration +{ + public class TestNegativeCases + { + [Fact] + public void TestDuplicatedNamedParameterbinding() + { + string msg = null; + try + { + var c = TangFactory.GetTang().NewConfigurationBuilder() + .BindStringNamedParam<NamedString>("abc") + .BindStringNamedParam<NamedString>("def") + .Build(); + } + catch (ArgumentException e) + { + msg = e.Message; + } + Assert.NotNull(msg); + Assert.True(msg.Contains(ConfigurationBuilderImpl.DuplicatedEntryForNamedParamater)); + } + + [NamedParameter(Documentation = "test", ShortName = "NamedString")] + class NamedString : Name<string> + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/reef/blob/0d3b46f5/lang/cs/Org.Apache.REEF.Tang.Tests/Org.Apache.REEF.Tang.Tests.csproj ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Org.Apache.REEF.Tang.Tests.csproj b/lang/cs/Org.Apache.REEF.Tang.Tests/Org.Apache.REEF.Tang.Tests.csproj index 51b5eb0..4d0117a 100644 --- a/lang/cs/Org.Apache.REEF.Tang.Tests/Org.Apache.REEF.Tang.Tests.csproj +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Org.Apache.REEF.Tang.Tests.csproj @@ -59,6 +59,7 @@ under the License. <Compile Include="Configuration\TestAvroSerializerRoundTrip.cs" /> <Compile Include="Configuration\TestConfiguration.cs" /> <Compile Include="Configuration\TestCsConfigurationBuilderExtension.cs" /> + <Compile Include="Configuration\TestNegativeCases.cs" /> <Compile Include="Format\TestConfigurationModule.cs" /> <Compile Include="Format\TestConfigurationModuleForList.cs" /> <Compile Include="Format\TestTaskConfiguration.cs" /> http://git-wip-us.apache.org/repos/asf/reef/blob/0d3b46f5/lang/cs/Org.Apache.REEF.Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs b/lang/cs/Org.Apache.REEF.Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs index ab850fb..fe3a099 100644 --- a/lang/cs/Org.Apache.REEF.Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs +++ b/lang/cs/Org.Apache.REEF.Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs @@ -41,7 +41,7 @@ namespace Org.Apache.REEF.Tang.Implementations.Configuration public readonly IDictionary<INamedParameterNode, IList<object>> BoundLists = new MonotonicTreeMap<INamedParameterNode, IList<object>>(); public readonly static string INIT = "<init>"; - + public const string DuplicatedEntryForNamedParamater = "Duplicated entries: "; private static readonly Logger LOGGER = Logger.GetLogger(typeof(ConfigurationBuilderImpl)); protected ConfigurationBuilderImpl() @@ -272,7 +272,15 @@ namespace Org.Apache.REEF.Tang.Implementations.Configuration } else { - NamedParameters.Add(name, value); + try + { + NamedParameters.Add(name, value); + } + catch (ArgumentException e) + { + var msg = string.Format(CultureInfo.InvariantCulture, DuplicatedEntryForNamedParamater + "try to bind [{0}] to [{1}], but the configuration has been set for it.", value, name.GetFullName()); + Utilities.Diagnostics.Exceptions.Throw(new ArgumentException(msg + e.Message), LOGGER); + } } }
