using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void 単一利用で再利用しない場合()
{
SampleDao dao1 = new SampleDao();
try
{
dao1.execute();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao1.Connection.State);
Assert.AreNotEqual("", dao1.Connection.ConnectionString);
}
[TestMethod]
public void 複数利用で再利用しない場合()
{
SampleDao dao1 = new SampleDao();
SampleDao dao2 = new SampleDao();
// 初回
try
{
dao1.execute();
}
catch (Exception e)
{
Assert.Fail();
}
// 2回目
Assert.AreEqual(ConnectionState.Closed, dao1.Connection.State);
Assert.AreNotEqual("", dao1.Connection.ConnectionString);
try
{
dao2.execute();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao2.Connection.State);
Assert.AreNotEqual("", dao1.Connection.ConnectionString);
}
[TestMethod]
public void 複数利用で再利用する場合()
{
SampleDao dao1 = new SampleDao();
// 初回
try
{
dao1.execute();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao1.Connection.State);
Assert.AreNotEqual("", dao1.Connection.ConnectionString);
// 2回目
try
{
dao1.execute();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao1.Connection.State);
Assert.AreNotEqual("", dao1.Connection.ConnectionString);
}
[TestMethod]
public void using_単一利用で再利用しない場合()
{
SampleDao dao1 = new SampleDao();
try
{
dao1.executeWithUsing();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao1.Connection.State);
Assert.AreEqual("", dao1.Connection.ConnectionString);
}
[TestMethod]
public void using_複数利用で再利用しない場合()
{
SampleDao dao1 = new SampleDao();
SampleDao dao2 = new SampleDao();
// 初回
try
{
dao1.executeWithUsing();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao1.Connection.State);
Assert.AreEqual("", dao1.Connection.ConnectionString);
// 2回目
try
{
dao2.executeWithUsing();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao2.Connection.State);
Assert.AreEqual("", dao1.Connection.ConnectionString);
}
[TestMethod]
public void using_複数利用で再利用する場合()
{
SampleDao dao1 = new SampleDao();
// 初回
try
{
dao1.executeWithUsing();
}
catch (Exception e)
{
Assert.Fail();
}
Assert.AreEqual(ConnectionState.Closed, dao1.Connection.State);
Assert.AreEqual("", dao1.Connection.ConnectionString);
// 2回目
try
{
dao1.executeWithUsing();
Assert.Fail();
}
catch (Exception e)
{
Assert.AreEqual("", dao1.Connection.ConnectionString);
}
}
}
/// <summary>
/// UTで使うためのDao
/// </summary>
public class SampleDao
{
#region データベース接続情報
private static readonly string DB_SERVER = "localhost";
private static readonly string DB_NAME = "TESTDB";
private static readonly string DB_USER = "sa";
private static readonly string DB_PASSWORD = "test";
private static readonly string DB_CONNECTION = string.Format(
"server={0};database={1};User ID={2};Password={3};",
DB_SERVER,
DB_NAME,
DB_USER,
DB_PASSWORD);
#endregion
private SqlConnection connection;
/// <summary>
/// この処理で使用するSqlConnectionオブジェクト
/// </summary>
public SqlConnection Connection
{
get { return this.connection; }
set { this.connection = value; }
}
public SampleDao()
{
this.Connection = new SqlConnection(DB_CONNECTION);
}
/// <summary>
/// try-finallyでCloseする
/// </summary>
public void execute()
{
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT GETDATE()";
command.Connection = this.Connection;
try {
command.Connection.Open();
command.ExecuteReader();
} finally {
command.Connection.Close();
}
}
/// <summary>
/// usingでCloseする
/// </summary>
public void executeWithUsing()
{
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT GETDATE()";
command.Connection = this.Connection;
command.Connection.Open();
using (command.Connection)
{
command.ExecuteReader();
}
}
}
}