From d81fa347e522a8547c5475b5e8984bc7f8478a57 Mon Sep 17 00:00:00 2001 From: Leila Lali Date: Fri, 24 Mar 2017 10:16:55 -0700 Subject: [PATCH] fixed an issue with setting a boolean option to string (#291) * fixed an issue with setting an option which Boolean to string --- .../Connection/Contracts/ConnectionDetails.cs | 25 ++++++++- .../Connection/ConnectionDetailsTests.cs | 55 +++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectionDetails.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectionDetails.cs index 095c46a4..bdce4fcb 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectionDetails.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectionDetails.cs @@ -3,7 +3,10 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // +using System; using System.Collections.Generic; +using System.Globalization; +using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts { @@ -446,11 +449,27 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts if (Options != null && Options.ContainsKey(name)) { object value = Options[name]; - if(value != null && ( typeof(T) == typeof(int) || typeof(T) == typeof(int?))) + try { - value = System.Convert.ToInt32(value); + if (value != null && (typeof(T) != value.GetType())) + { + if (typeof(T) == typeof(int) || typeof(T) == typeof(int?)) + { + value = Convert.ToInt32(value); + } + else if (typeof(T) == typeof(bool) || typeof(T) == typeof(bool?)) + { + value = Convert.ToBoolean(value); + } + } + result = value != null ? (T)value : default(T); + } + catch + { + result = default(T); + Logger.Write(LogLevel.Warning, string.Format(CultureInfo.InvariantCulture, + "Cannot convert option value {0}:{1} to {2}", name, value ?? "", typeof(T))); } - result = value != null ? (T)value : default(T); } return result; } diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionDetailsTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionDetailsTests.cs index 58b3f924..c44b8d8d 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionDetailsTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Connection/ConnectionDetailsTests.cs @@ -207,5 +207,60 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection int? expectedValue = null; Assert.Equal(details.ConnectTimeout, expectedValue); } + + [Fact] + public void SettingEncryptToStringShouldStillReturnBoolean() + { + ConnectionDetails details = new ConnectionDetails(); + + string encrypt = "True"; + bool? expectedValue = true; + details.Options["encrypt"] = encrypt; + + Assert.Equal(details.Encrypt, expectedValue); + } + + [Fact] + public void SettingEncryptToLowecaseStringShouldStillReturnBoolean() + { + ConnectionDetails details = new ConnectionDetails(); + + string encrypt = "true"; + bool? expectedValue = true; + details.Options["encrypt"] = encrypt; + + Assert.Equal(details.Encrypt, expectedValue); + } + + [Fact] + public void EncryptShouldReturnNullIfNotSet() + { + ConnectionDetails details = new ConnectionDetails(); + bool? expectedValue = null; + Assert.Equal(details.Encrypt, expectedValue); + } + + [Fact] + public void EncryptShouldReturnNullIfSetToNull() + { + ConnectionDetails details = new ConnectionDetails(); + details.Options["encrypt"] = null; + int? expectedValue = null; + Assert.Equal(details.ConnectTimeout, expectedValue); + } + + [Fact] + public void SettingConnectiomTimeoutToLongWhichCannotBeConvertedToIntShouldNotCrash() + { + ConnectionDetails details = new ConnectionDetails(); + + long timeout = long.MaxValue; + int? expectedValue = null; + details.Options["connectTimeout"] = timeout; + details.Options["encrypt"] = true; + + Assert.Equal(details.ConnectTimeout, expectedValue); + Assert.Equal(details.Encrypt, true); + } } }