昨天经过 的指出后,又找出了Microsoft Enterprise Library June 2005的源代码,仔细的看了看。
总觉得这个是个bug。
我又找到了src\Data\Tests这个目录下的ConnectionStringFixture.cs测试文件。经过我的测试,我确定这是一个小bug.
这面就是经测试经过:
原来的ConnectionStringFixture.cs文件,没有做任何修改。
源代码如下:
using System;
using NUnit.Framework;
![](/Images/OutliningIndicators/None.gif)
namespace Microsoft.Practices.EnterpriseLibrary.Data.Tests
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
[TestFixture]
public class ConnectionStringFixture
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private static readonly string userName = "User";
private static readonly string password = "Password";
private static readonly string userIdTokens = "user id=,uid=";
private static readonly string passwordTokens = "password=,pwd=";
private ConnectionString connectionString;
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void InvalidOperationExceptionThrownWhenConnectionStringIsNull()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.connectionString = new ConnectionString(null, userIdTokens, passwordTokens);
string password = this.connectionString.Password;
Assert.IsTrue(password != null);
}
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
public void EmptyCredentialsReturnedForEmptyConnectionString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.connectionString = new ConnectionString(string.Empty, userIdTokens, passwordTokens);
Assert.AreEqual(0, this.connectionString.UserName.Length);
Assert.AreEqual(0, this.connectionString.Password.Length);
}
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
public void CanGetCredentialsFromRealSqlDataClass()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string initialConnectionString = String.Format("server=localhost; database=JoeRandom; uid={0}; pwd={1}; ;", userName, password);
this.connectionString = new ConnectionString(initialConnectionString, userIdTokens, passwordTokens);
Assert.AreEqual(userName, this.connectionString.UserName);
Assert.AreEqual(password, this.connectionString.Password);
}
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
public void CreateNewConnectionStringTest()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string initialConnectionString = String.Format("server=localhost; database=JoeRandom; uid={0}; pwd={1}; ;", userName, password);
this.connectionString = new ConnectionString(initialConnectionString, userIdTokens, passwordTokens).CreateNewConnectionString(initialConnectionString);
Assert.AreEqual(userName, this.connectionString.UserName);
Assert.AreEqual(password, this.connectionString.Password);
}
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
public void CanGetCredentialsUsingAlternatePatternsForUidAndPwd()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string initialConnectionString = String.Format("server=localhost; database=JoeRandom; user id={0}; password={1}; ;", userName, password);
this.connectionString = new ConnectionString(initialConnectionString, userIdTokens, passwordTokens);
Assert.AreEqual(userName, this.connectionString.UserName);
Assert.AreEqual(password, this.connectionString.Password);
}
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
public void CanAddCredentialsToConnectionStringThatDoesNotHaveThem()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string initialConnectionString = "server=localhost; database=RandomData; ; ;";
this.connectionString = new ConnectionString(initialConnectionString, userIdTokens, passwordTokens);
this.connectionString.UserName = userName;
this.connectionString.Password = password;
string actualConnectionString = String.Format("server=localhost; database=RandomData; ; ;user id={0};password={1};",
userName, password);
Assert.AreEqual(actualConnectionString, this.connectionString.ToString());
}
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
public void CanSetUserIdAndPasswordInConnectionStringThatAlreadyHasOne()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string initialConnectionString = String.Format("server=localhost; database=JoeRandom; user id={0}; password={1}; ;", "Kill", "Bill");
ConnectionString newConnectionString = new ConnectionString(initialConnectionString, userIdTokens, passwordTokens);
newConnectionString.UserName = userName;
newConnectionString.Password = password;
string actualConnectionString = String.Format("server=localhost; database=JoeRandom; user id={0}; password={1}; ;", userName, password);
Assert.AreEqual(actualConnectionString, newConnectionString.ToString());
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Test to see if ToStringNoCredentials works properly for a connection string
/// without a username and password
/// </summary>
[Test]
public void RemovingCredentialsFromConnectionStringWithoutThemIsOk()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string initialConnectionString = "server=localhost;database=RandomData;";
ConnectionString newConnectionString = new ConnectionString(initialConnectionString, userIdTokens, passwordTokens);
string expectedConnectionString = "server=localhost;database=randomdata;";
string strippedConnectionString = newConnectionString.ToStringNoCredentials();
Assert.AreEqual(expectedConnectionString, strippedConnectionString);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Test to see that ToStringNoCredentials removes the username and password
/// </summary>
[Test]
public void WillRemoveCredentialsFromConnectionString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string initialConnectionString = "server=localhost;database=RandomData;user id=Bill;pwd=goodPassword";
ConnectionString newConnectionString = new ConnectionString(initialConnectionString,
userIdTokens, passwordTokens);
string expectedConnectionString = "server=localhost;database=randomdata;";
string strippedConnectionString = newConnectionString.ToStringNoCredentials();
Assert.AreEqual(expectedConnectionString, strippedConnectionString);
}
![](/Images/OutliningIndicators/InBlock.gif)
[Test]
public void NullTests()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ConnectionString connectionString = new ConnectionString(null, null, null);
Assert.AreEqual(string.Empty, connectionString.ToStringNoCredentials());
}
}
}
![](/Images/OutliningIndicators/None.gif)
测试结果:
![](/images/cnblogs_com/chillsrc/connTestOld.JPG)
测试全部通过。
新的ConnectionStringFixture.cs文件,做了两处修改。其他什么也没修改。
原来的:private static readonly string userIdTokens = "user id=,uid=";
修改后:private static readonly string userIdTokens = "User Id=,Uid="; //只在此处修改
原来的:private static readonly string passwordTokens = "password=,pwd=";
修改后:private static readonly string passwordTokens = "Password=,Pwd=";//只在此处修改
测试结果:
![](/images/cnblogs_com/chillsrc/connTestNew.JPG)
测试有些没有通过。
大家可以自己做个测试。
所以我认为,这是Microsoft Enterprise Library June 2005的一个小bug,不在是疑似。
不对的地方请指正。