+2008-07-28 Gert Driesen <drieseng@users.sourceforge.net>
+
+ * SqlCommandTest.cs: Moved tests that do not require connection to
+ regular test suite. Improved existing tests, and added set of new
+ tests. Reduce failure count.
+ * SqlConnectionTest.cs: Moved tests that do not require connection to
+ regular test suite. Improved existing tests, and added set of new
+ tests. Reduce failure count.
+ * SqlTransactionTest.cs: Added several tests.
+
2008-07-10 Veerapuram Varadhan <vvaradhan@novell.com>
* SqlCommandTest.cs (bug326182_*): Test case for Bug#326182 - StoredProcedure
using NUnit.Framework;
-namespace MonoTests.System.Data.SqlClient
+namespace MonoTests.System.Data.SqlClient
{
[TestFixture]
[Category ("sqlserver")]
- public class SqlCommandTest
+ public class SqlCommandTest
{
-
- public SqlConnection conn = null ;
- SqlCommand cmd = null;
+ SqlConnection conn;
+ SqlCommand cmd;
string connectionString = ConnectionManager.Singleton.ConnectionString;
- [SetUp]
- public void Setup ()
- {
- }
-
[TearDown]
public void TearDown ()
{
- if (conn != null)
+ if (cmd != null) {
+ cmd.Dispose ();
+ cmd = null;
+ }
+
+ if (conn != null) {
conn.Close ();
+ conn = null;
+ }
}
- [Test]
- public void ConstructorTest ()
+ [Test] // ctor (String, SqlConnection, SqlTransaction)
+ public void Constructor4 ()
{
- // Test Default Constructor
- cmd = new SqlCommand ();
- Assert.AreEqual (String.Empty, cmd.CommandText,
- "#1 Command Test should be empty");
- Assert.AreEqual (30, cmd.CommandTimeout,
- "#2 CommandTimeout should be 30");
- Assert.AreEqual (CommandType.Text, cmd.CommandType,
- "#3 CommandType should be text");
- Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
- Assert.AreEqual (0, cmd.Parameters.Count,
- "#5 Parameter shud be empty");
-
- // Test Overloaded Constructor
- String cmdText = "select * from tbl1" ;
- cmd = new SqlCommand (cmdText);
- Assert.AreEqual (cmdText, cmd.CommandText,
- "#5 CommandText should be the same as passed");
- Assert.AreEqual (30, cmd.CommandTimeout,
- "#6 CommandTimeout should be 30");
- Assert.AreEqual (CommandType.Text, cmd.CommandType,
- "#7 CommandType should be text");
- Assert.IsNull (cmd.Connection , "#8 Connection Should be null");
-
- // Test Overloaded Constructor
- SqlConnection conn = new SqlConnection ();
- cmd = new SqlCommand (cmdText , conn);
- Assert.AreEqual (cmdText, cmd.CommandText,
- "#9 CommandText should be the same as passed");
- Assert.AreEqual (30, cmd.CommandTimeout,
- "#10 CommandTimeout should be 30");
- Assert.AreEqual (CommandType.Text, cmd.CommandType,
- "#11 CommandType should be text");
- Assert.AreSame (cmd.Connection, conn, "#12 Connection Should be same");
-
- // Test Overloaded Constructor
- SqlTransaction trans = null ;
+ string cmdText = "select @@version";
+
+ SqlTransaction trans = null;
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+
+ // transaction from same connection
try {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- trans = conn.BeginTransaction ();
- cmd = new SqlCommand (cmdText, conn, trans);
- Assert.AreEqual (cmdText, cmd.CommandText,
- "#9 CommandText should be the same as passed");
- Assert.AreEqual (30, cmd.CommandTimeout,
- "#10 CommandTimeout should be 30");
- Assert.AreEqual (CommandType.Text, cmd.CommandType,
- "#11 CommandType should be text");
- Assert.AreEqual (cmd.Connection, conn,
- "#12 Connection Should be null");
- Assert.AreEqual (cmd.Transaction, trans,
- "#13 Transaction Property should be set");
-
- // Test if parameters are reset to Default Values
- cmd = new SqlCommand ();
- Assert.AreEqual (String.Empty, cmd.CommandText,
- "#1 Command Test should be empty");
- Assert.AreEqual (30, cmd.CommandTimeout,
- "#2 CommandTimeout should be 30");
- Assert.AreEqual (CommandType.Text, cmd.CommandType,
- "#3 CommandType should be text");
- Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
- }finally {
- trans.Rollback ();
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ trans = connA.BeginTransaction ();
+ cmd = new SqlCommand (cmdText, connA, trans);
+
+ Assert.AreEqual (cmdText, cmd.CommandText, "#A1");
+ Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
+ Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
+ Assert.AreSame (connA, cmd.Connection, "#A4");
+ Assert.IsNull (cmd.Container, "#A5");
+ Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
+#if NET_2_0
+ Assert.IsNull (cmd.Notification, "#A7");
+ Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
+#endif
+ Assert.IsNotNull (cmd.Parameters, "#A9");
+ Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
+ Assert.IsNull (cmd.Site, "#A11");
+ Assert.AreSame (trans, cmd.Transaction, "#A12");
+ Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ }
+
+ // transaction from other connection
+ try {
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ trans = connB.BeginTransaction ();
+ cmd = new SqlCommand (cmdText, connA, trans);
+
+ Assert.AreEqual (cmdText, cmd.CommandText, "#B1");
+ Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
+ Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
+ Assert.AreSame (connA, cmd.Connection, "#B4");
+ Assert.IsNull (cmd.Container, "#B5");
+ Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
+#if NET_2_0
+ Assert.IsNull (cmd.Notification, "#B7");
+ Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
+#endif
+ Assert.IsNotNull (cmd.Parameters, "#B9");
+ Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
+ Assert.IsNull (cmd.Site, "#B11");
+ Assert.AreSame (trans, cmd.Transaction, "#B12");
+ Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (connA != null)
+ connA.Close ();
}
}
[Test]
- public void ExecuteScalarTest ()
+ public void ExecuteScalar ()
{
conn = new SqlConnection (connectionString);
cmd = new SqlCommand ("" , conn);
cmd.CommandText = "Select count(*) from numeric_family where id<=4";
- //Check Exception is thrown when executed on a closed connection
- try {
- cmd.ExecuteScalar ();
- Assert.Fail ("#1 InvalidOperation Exception must be thrown");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
-#if NET_2_0
- Assert.AreEqual (typeof (NullReferenceException), e.GetType (),
- "#2 Incorrect Exception : " + e.StackTrace);
-#else
- Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
- "#2 Incorrect Exception : " + e.StackTrace);
-#endif
- }
-
// Check the Return value for a Correct Query
object result = 0;
conn.Open ();
result = cmd.ExecuteScalar ();
- Assert.AreEqual (4, (int)result, "#3 Query Result returned is incorrect");
+ Assert.AreEqual (4, (int)result, "#A1 Query Result returned is incorrect");
- cmd.CommandText = "select id , type_bit from numeric_family order by id asc" ;
+ cmd.CommandText = "select id , type_bit from numeric_family order by id asc";
result = Convert.ToInt32 (cmd.ExecuteScalar ());
Assert.AreEqual (1, result,
- "#4 ExecuteScalar Should return (1,1) the result set" );
+ "#A2 ExecuteScalar Should return (1,1) the result set" );
cmd.CommandText = "select id from numeric_family where id=-1";
result = cmd.ExecuteScalar ();
- Assert.IsNull (result, "#5 Null should be returned if result set is empty");
+ Assert.IsNull (result, "#A3 Null should be returned if result set is empty");
// Check SqlException is thrown for Invalid Query
cmd.CommandText = "select count* from numeric_family";
try {
result = cmd.ExecuteScalar ();
- Assert.Fail ("#6 InCorrect Query should cause an SqlException");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(SqlException), e.GetType(),
- "#7 Incorrect Exception : " + e.StackTrace);
+ Assert.Fail ("#B1");
+ } catch (SqlException ex) {
+ // Incorrect syntax near the keyword 'from'
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
+ Assert.AreEqual ((byte) 15, ex.Class, "#B3");
+ Assert.IsNull (ex.InnerException, "#B4");
+ Assert.IsNotNull (ex.Message, "#B5");
+ Assert.IsTrue (ex.Message.IndexOf ("'from'") != -1, "#B6");
+ Assert.AreEqual (156, ex.Number, "#B7");
+ Assert.AreEqual ((byte) 1, ex.State, "#B8");
}
-
// Parameterized stored procedure calls
int int_value = 20;
cmd.Parameters.Add (p3);
result = cmd.ExecuteScalar ();
- Assert.AreEqual (return_value, result, "#8 ExecuteScalar Should return 'first column of first rowset'");
- Assert.AreEqual (int_value * 2, p2.Value, "#9 ExecuteScalar should fill the parameter collection with the outputted values");
- Assert.AreEqual (string_value, p3.Value, "#10 ExecuteScalar should fill the parameter collection with the outputted values");
+ Assert.AreEqual (return_value, result, "#C1 ExecuteScalar Should return 'first column of first rowset'");
+ Assert.AreEqual (int_value * 2, p2.Value, "#C2 ExecuteScalar should fill the parameter collection with the outputted values");
+ Assert.AreEqual (string_value, p3.Value, "#C3 ExecuteScalar should fill the parameter collection with the outputted values");
p3.Size = 0;
p3.Value = null;
try {
cmd.ExecuteScalar ();
- Assert.Fail ("#11 Query should throw System.InvalidOperationException due to size = 0 and value = null");
+ Assert.Fail ("#D1 Query should throw System.InvalidOperationException due to size = 0 and value = null");
+ } catch (InvalidOperationException ex) {
+ // String[2]: the Size property has an invalid
+ // size of 0
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
+ Assert.IsNull (ex.InnerException, "#D3");
+ Assert.IsNotNull (ex.Message, "#D4");
+ } finally {
+ conn.Close ();
}
- catch (AssertionException e) {
- throw e;
+ }
+
+ [Test]
+ public void ExecuteScalar_Connection_PendingTransaction ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (SqlTransaction trans = conn.BeginTransaction ()) {
+ cmd = new SqlCommand ("select @@version", conn);
+
+ try {
+ cmd.ExecuteScalar ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // Execute requires the command to have a
+ // transaction object when the connection
+ // assigned to the command is in a pending
+ // local transaction. The Transaction
+ // property of the command has not been
+ // initialized
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
}
- catch (Exception e) {
- Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
- "#12 Incorrect Exception : " + e.StackTrace);
+ }
+
+ [Test]
+ public void ExecuteScalar_Query_Invalid ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("InvalidQuery", conn);
+ try {
+ cmd.ExecuteScalar ();
+ Assert.Fail ("#1");
+ } catch (SqlException ex) {
+ // Could not find stored procedure 'InvalidQuery'
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
+ Assert.AreEqual ((byte) 16, ex.Class, "#3");
+ Assert.IsNull (ex.InnerException, "#4");
+ Assert.IsNotNull (ex.Message, "#5");
+ Assert.IsTrue (ex.Message.IndexOf ("'InvalidQuery'") != -1, "#6");
+ Assert.AreEqual (2812, ex.Number, "#7");
+ Assert.AreEqual ((byte) 62, ex.State, "#8");
}
+ }
- conn.Close ();
-
+ [Test]
+ public void ExecuteScalar_Transaction_NotAssociated ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlTransaction trans = null;
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+
+ try {
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ trans = connA.BeginTransaction ();
+
+ cmd = new SqlCommand ("select @@version", connB, trans);
+
+ try {
+ cmd.ExecuteScalar ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // The transaction object is not associated
+ // with the connection object
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ } finally {
+ cmd.Dispose ();
+ }
+
+ cmd = new SqlCommand ("select @@version", connB);
+ cmd.Transaction = trans;
+
+ try {
+ cmd.ExecuteScalar ();
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // The transaction object is not associated
+ // with the connection object
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+ } finally {
+ cmd.Dispose ();
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
+ }
}
[Test]
- public void ExecuteNonQuery ()
+ public void ExecuteScalar_Transaction_Only ()
{
+ Assert.Ignore ("NotWorking");
+
+ SqlTransaction trans = null;
+
conn = new SqlConnection (connectionString);
- cmd = new SqlCommand ("", conn);
- int result = 0;
+ conn.Open ();
+ trans = conn.BeginTransaction ();
+
+ cmd = new SqlCommand ("select @@version");
+ cmd.Transaction = trans;
- // Test for exceptions
- // Test exception is thrown if connection is closed
- cmd.CommandText = "Select id from numeric_family where id=1";
try {
- cmd.ExecuteNonQuery ();
- Assert.Fail ("#1 Connextion shud be open");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
+ cmd.ExecuteScalar ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // ExecuteScalar: Connection property has not
+ // been initialized
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
#if NET_2_0
- Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
- "#2 Incorrect Exception : " + e);
-#else
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
- "#2 Incorrect Exception : " + e);
+ Assert.IsTrue (ex.Message.IndexOf ("ExecuteScalar") != -1, "#5");
#endif
+ } finally {
+ trans.Dispose ();
}
-
- // Test Exception is thrown if Query is incorrect
+ }
+
+ [Test]
+ public void ExecuteNonQuery ()
+ {
+ conn = new SqlConnection (connectionString);
conn.Open ();
- cmd.CommandText = "Select id1 from numeric_family";
- try {
- cmd.ExecuteNonQuery ();
- Assert.Fail ("#1 invalid Query");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(SqlException), e.GetType(),
- "#2 Incorrect Exception : " + e);
- }
- // Test Select/Insert/Update/Delete Statements
SqlTransaction trans = conn.BeginTransaction ();
- cmd.Transaction = trans;
+
+ cmd = conn.CreateCommand ();
+ cmd.Transaction = trans;
+
+ int result = 0;
try {
cmd.CommandText = "Select id from numeric_family where id=1";
result = cmd.ExecuteNonQuery ();
- Assert.AreEqual (-1, result, "#1");
+ Assert.AreEqual (-1, result, "#A1");
cmd.CommandText = "Insert into numeric_family (id,type_int) values (100,200)";
result = cmd.ExecuteNonQuery ();
- Assert.AreEqual (1, result, "#2 One row shud be inserted");
+ Assert.AreEqual (1, result, "#A2 One row shud be inserted");
cmd.CommandText = "Update numeric_family set type_int=300 where id=100";
result = cmd.ExecuteNonQuery ();
- Assert.AreEqual (1, result, "#3 One row shud be updated");
+ Assert.AreEqual (1, result, "#A3 One row shud be updated");
// Test Batch Commands
- cmd.CommandText = "Select id from numeric_family where id=1;";
+ cmd.CommandText = "Select id from numeric_family where id=1;";
cmd.CommandText += "update numeric_family set type_int=10 where id=1000";
cmd.CommandText += "update numeric_family set type_int=10 where id=100";
- result = cmd.ExecuteNonQuery ();
- Assert.AreEqual (1, result, "#4 One row shud be updated");
+ result = cmd.ExecuteNonQuery ();
+ Assert.AreEqual (1, result, "#A4 One row shud be updated");
cmd.CommandText = "Delete from numeric_family where id=100";
result = cmd.ExecuteNonQuery ();
- Assert.AreEqual (1, result, "#5 One row shud be deleted");
-
+ Assert.AreEqual (1, result, "#A5 One row shud be deleted");
}finally {
- trans.Rollback ();
+ trans.Dispose ();
}
-
// Parameterized stored procedure calls
int int_value = 20;
cmd.Parameters.Add (p3);
cmd.ExecuteNonQuery ();
- Assert.AreEqual (int_value * 2, p2.Value, "#6 ExecuteNonQuery should fill the parameter collection with the outputted values");
- Assert.AreEqual (string_value, p3.Value, "#7 ExecuteNonQuery should fill the parameter collection with the outputted values");
+ Assert.AreEqual (int_value * 2, p2.Value, "#B1 ExecuteNonQuery should fill the parameter collection with the outputted values");
+ Assert.AreEqual (string_value, p3.Value, "#B2 ExecuteNonQuery should fill the parameter collection with the outputted values");
+ }
+
+ [Test]
+ public void ExecuteNonQuery_Connection_PendingTransaction ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (SqlTransaction trans = conn.BeginTransaction ()) {
+ cmd = new SqlCommand ("select @@version", conn);
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // Execute requires the command to have a
+ // transaction object when the connection
+ // assigned to the command is in a pending
+ // local transaction. The Transaction
+ // property of the command has not been
+ // initialized
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
+ }
}
[Test]
- public void ExecuteReaderTest ()
+ public void ExecuteNonQuery_Query_Invalid ()
{
- //SqlDataReader reader = null;
conn = new SqlConnection (connectionString);
+ conn.Open ();
+ cmd = new SqlCommand ("select id1 from numeric_family", conn);
- // Test exception is thrown if conn is closed
- cmd = new SqlCommand ("Select count(*) from numeric_family");
try {
- /*reader = */cmd.ExecuteReader ();
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
-#if NET_2_0
- Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
- "#1 Incorrect Exception");
-#else
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
- "#1 Incorrect Exception");
-#endif
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#1");
+ } catch (SqlException ex) {
+ // Invalid column name 'id1'
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
+ Assert.AreEqual ((byte) 16, ex.Class, "#3");
+ Assert.IsNull (ex.InnerException, "#4");
+ Assert.IsNotNull (ex.Message, "#5");
+ Assert.IsTrue (ex.Message.IndexOf ("'id1'") != -1, "#6");
+ Assert.AreEqual (207, ex.Number, "#7");
+ Assert.AreEqual ((byte) 1, ex.State, "#8");
+ } finally {
+ conn.Close ();
}
+ }
+
+ [Test]
+ public void ExecuteNonQuery_Transaction_NotAssociated ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlTransaction trans = null;
+ SqlConnection connA = null;
+ SqlConnection connB = null;
- conn.Open ();
- // Test exception is thrown for Invalid Query
- cmd = new SqlCommand ("InvalidQuery", conn);
try {
- /*reader = */cmd.ExecuteReader ();
- Assert.Fail ("#1 Exception shud be thrown");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(SqlException), e.GetType (),
- "#2 Incorrect Exception : " + e);
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ trans = connA.BeginTransaction ();
+
+ cmd = new SqlCommand ("select @@version", connB, trans);
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // The transaction object is not associated
+ // with the connection object
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ } finally {
+ cmd.Dispose ();
+ }
+
+ cmd = new SqlCommand ("select @@version", connB);
+ cmd.Transaction = trans;
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // The transaction object is not associated
+ // with the connection object
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+ } finally {
+ cmd.Dispose ();
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
}
-
- // NOTE
- // Test SqlException is thrown if a row is locked
- // should lock a particular row and then modify it
- /*
- */
-
- // Test Connection cannot be modified when reader is in use
- // NOTE : msdotnet contradicts documented behavior
- /*
- cmd.CommandText = "select * from numeric_family where id=1";
- reader = cmd.ExecuteReader ();
- reader.Read ();
- conn.Close (); // valid operation
- conn = new SqlConnection (connectionString);
- */
- /*
- // NOTE msdotnet contradcits documented behavior
- // If the above testcase fails, then this shud be tested
- // Test connection can be modified once reader is closed
- conn.Close ();
- reader.Close ();
- conn = new SqlConnection (connectionString); // valid operation
- */
}
[Test]
- public void ExecuteReaderCommandBehaviorTest ()
+ public void ExecuteNonQuery_Transaction_Only ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ SqlTransaction trans = conn.BeginTransaction ();
+
+ cmd = new SqlCommand ("select @@version");
+ cmd.Transaction = trans;
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // ExecuteNonQuery: Connection property has not
+ // been initialized
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsTrue (ex.Message.IndexOf ("ExecuteNonQuery") != -1, "#5");
+ } finally {
+ trans.Dispose ();
+ }
+ }
+
+ [Test] // bug #412569
+ public void ExecuteReader ()
{
- // Test for command behaviors
- DataTable schemaTable = null;
- SqlDataReader reader = null;
+ Assert.Ignore ("bug 412569");
+
+ // Test for command behaviors
+ DataTable schemaTable = null;
+ SqlDataReader reader = null;
conn = new SqlConnection (connectionString);
conn.Open ();
cmd.CommandText = "Select id from numeric_family where id <=4 order by id asc;";
cmd.CommandText += "Select type_bit from numeric_family where id <=4 order by id asc";
- // Test for default command behavior
+ // Test for default command behavior
reader = cmd.ExecuteReader ();
- int rows = 0;
+ int rows = 0;
int results = 0;
do {
while (reader.Read ())
- rows++ ;
+ rows++;
Assert.AreEqual (4, rows, "#1 Multiple rows shud be returned");
- results++;
+ results++;
rows = 0;
- }while (reader.NextResult());
+ } while (reader.NextResult ());
Assert.AreEqual (2, results, "#2 Multiple result sets shud be returned");
reader.Close ();
- // Test if closing reader, closes the connection
+ // Test if closing reader, closes the connection
reader = cmd.ExecuteReader (CommandBehavior.CloseConnection);
reader.Close ();
Assert.AreEqual (ConnectionState.Closed, conn.State,
"#3 Command Behavior is not followed");
- conn.Open();
+ conn.Open ();
// Test if row info and primary Key info is returned
reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
schemaTable = reader.GetSchemaTable ();
- Assert.IsTrue(reader.HasRows, "#4 Data Rows shud also be returned");
- Assert.IsTrue ((bool)schemaTable.Rows[0]["IsKey"],
+ Assert.IsTrue (reader.HasRows, "#4 Data Rows shud also be returned");
+ Assert.IsTrue ((bool) schemaTable.Rows [0] ["IsKey"],
"#5 Primary Key info shud be returned");
- reader.Close ();
+ reader.Close ();
// Test only column information is returned
reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly);
schemaTable = reader.GetSchemaTable ();
Assert.IsFalse (reader.HasRows, "#6 row data shud not be returned");
- Assert.AreEqual(DBNull.Value, schemaTable.Rows[0]["IsKey"],
+ Assert.AreEqual (DBNull.Value, schemaTable.Rows [0] ["IsKey"],
"#7 Primary Key info shud not be returned");
- Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
+ Assert.AreEqual ("id", schemaTable.Rows [0] ["ColumnName"],
"#8 Schema Data is Incorrect");
reader.Close ();
// Test only one result set (first) is returned
reader = cmd.ExecuteReader (CommandBehavior.SingleResult);
schemaTable = reader.GetSchemaTable ();
- Assert.IsFalse (reader.NextResult(),
+ Assert.IsFalse (reader.NextResult (),
"#9 Only one result set shud be returned");
- Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
+ Assert.AreEqual ("id", schemaTable.Rows [0] ["ColumnName"],
"#10 The result set returned shud be the first result set");
reader.Close ();
// Test only one row is returned for all result sets
// msdotnet doesnt work correctly.. returns only one result set
reader = cmd.ExecuteReader (CommandBehavior.SingleRow);
- rows=0;
- results=0;
+ rows = 0;
+ results = 0;
do {
while (reader.Read ())
- rows++ ;
+ rows++;
Assert.AreEqual (1, rows, "#11 Only one row shud be returned");
- results++;
+ results++;
rows = 0;
- }while (reader.NextResult());
- // NOTE msdotnet contradicts documented behavior.
- // Multiple result sets shud be returned , and in this case : 2
- //Assert.AreEqual (2, results, "# Multiple result sets shud be returned");
- Assert.AreEqual (2, results, "#12 Multiple result sets shud be returned");
+ } while (reader.NextResult ());
+
+ // LAMESPEC:
+ // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=357085
+ Assert.AreEqual (1, results, "#12 Multiple result sets shud be returned");
reader.Close ();
}
+ [Test]
+ public void ExecuteReader_Connection_PendingTransaction ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (SqlTransaction trans = conn.BeginTransaction ()) {
+ cmd = new SqlCommand ("select @@version", conn);
+
+ try {
+ cmd.ExecuteReader ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // Execute requires the command to have a
+ // transaction object when the connection
+ // assigned to the command is in a pending
+ // local transaction. The Transaction
+ // property of the command has not been
+ // initialized
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
+ }
+ }
+
+ [Test]
+ public void ExecuteReader_Query_Invalid ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("InvalidQuery", conn);
+ try {
+ cmd.ExecuteReader ();
+ Assert.Fail ("#1");
+ } catch (SqlException ex) {
+ // Could not find stored procedure 'InvalidQuery'
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
+ Assert.AreEqual ((byte) 16, ex.Class, "#3");
+ Assert.IsNull (ex.InnerException, "#4");
+ Assert.IsNotNull (ex.Message, "#5");
+ Assert.IsTrue (ex.Message.IndexOf ("'InvalidQuery'") != -1, "#6");
+ Assert.AreEqual (2812, ex.Number, "#7");
+ Assert.AreEqual ((byte) 62, ex.State, "#8");
+ }
+ }
+
+ [Test]
+ public void ExecuteReader_Transaction_NotAssociated ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlTransaction trans = null;
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+
+ try {
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ trans = connA.BeginTransaction ();
+
+ cmd = new SqlCommand ("select @@version", connB, trans);
+
+ try {
+ cmd.ExecuteReader ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // The transaction object is not associated
+ // with the connection object
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ } finally {
+ cmd.Dispose ();
+ }
+
+ cmd = new SqlCommand ("select @@version", connB);
+ cmd.Transaction = trans;
+
+ try {
+ cmd.ExecuteReader ();
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // The transaction object is not associated
+ // with the connection object
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+ } finally {
+ cmd.Dispose ();
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
+ }
+ }
+
+ [Test]
+ public void ExecuteReader_Transaction_Only ()
+ {
+#if NET_2_0
+ Assert.Ignore ("NotWorking");
+#endif
+
+ SqlTransaction trans = null;
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ trans = conn.BeginTransaction ();
+
+ cmd = new SqlCommand ("select @@version");
+ cmd.Transaction = trans;
+
+ try {
+ cmd.ExecuteReader ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // ExecuteReader: Connection property has not
+ // been initialized
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsTrue (ex.Message.IndexOf ("ExecuteReader") != -1, "#5");
+ } finally {
+ trans.Dispose ();
+ }
+ }
+
+
[Test]
public void PrepareTest_CheckValidStatement ()
{
conn = new SqlConnection (connectionString);
conn.Open ();
- cmd.CommandText = "Select id from numeric_family where id=@ID" ;
- cmd.Connection = conn ;
+ cmd.CommandText = "Select id from numeric_family where id=@ID";
+ cmd.Connection = conn;
// Test if Parameters are correctly populated
cmd.Parameters.Clear ();
cmd.Parameters.Add ("@ID", SqlDbType.TinyInt);
- cmd.Parameters["@ID"].Value = 2 ;
+ cmd.Parameters["@ID"].Value = 2;
cmd.Prepare ();
Assert.AreEqual (2, cmd.ExecuteScalar (), "#3 Prepared Stmt not working");
conn = new SqlConnection (connectionString);
conn.Open ();
- cmd.CommandText = "Select id from numeric_family where id=@ID" ;
- cmd.Connection = conn ;
+ cmd.CommandText = "Select id from numeric_family where id=@ID";
+ cmd.Connection = conn;
// Test InvalidOperation Exception is thrown if Parameter Type
// is not explicitly set
#endif
try {
cmd.Prepare ();
- Assert.Fail ("#1 Parameter Type shud be explicitly Set");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
- "#2 Incorrect Exception : " + e.StackTrace);
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // SqlCommand.Prepare method requires all parameters
+ // to have an explicitly set type
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
}
// Test Exception is thrown for variable size data if precision/scale
cmd.Parameters["@p1"].Value = "afasasadadada";
try {
cmd.Prepare ();
- Assert.Fail ("#5 Exception shud be thrown");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
- "#6 Incorrect Exception " + e.StackTrace);
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // SqlCommand.Prepare method requires all variable
+ // length parameters to have an explicitly set
+ // non-zero Size
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
}
// Test Exception is not thrown for Stored Procs
+ cmd.CommandType = CommandType.StoredProcedure;
+ cmd.CommandText = "ABFSDSFSF";
+ cmd.Prepare ();
+
+ cmd.CommandType = CommandType.Text;
+ conn.Close ();
+ }
+
+ [Test] // bug #412576
+ public void Connection ()
+ {
+ Assert.Ignore ("bug #412576");
+
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+ SqlTransaction trans = null;
+
try {
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.CommandText = "ABFSDSFSF" ;
- cmd.Prepare ();
- }catch {
- Assert.Fail ("#7 Exception shud not be thrown for Stored Procs");
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ cmd = connA.CreateCommand ();
+ cmd.Connection = connB;
+ Assert.AreSame (connB, cmd.Connection, "#A1");
+ Assert.IsNull (cmd.Transaction, "#A2");
+ cmd.Dispose ();
+
+ trans = connA.BeginTransaction ();
+ cmd = new SqlCommand ("select @@version", connA, trans);
+ cmd.Connection = connB;
+ Assert.AreSame (connB, cmd.Connection, "#B1");
+ Assert.AreSame (trans, cmd.Transaction, "#B2");
+ trans.Dispose ();
+
+ trans = connA.BeginTransaction ();
+ cmd = new SqlCommand ("select @@version", connA, trans);
+ trans.Rollback ();
+ Assert.AreSame (connA, cmd.Connection, "#C1");
+ Assert.IsNull (cmd.Transaction, "#C2");
+ cmd.Connection = connB;
+ Assert.AreSame (connB, cmd.Connection, "#C3");
+ Assert.IsNull (cmd.Transaction, "#C4");
+ }finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
}
- cmd.CommandType = CommandType.Text;
- conn.Close ();
+ }
+
+ [Test]
+ public void Connection_Reader_Open ()
+ {
+#if NET_2_0
+ Assert.Ignore ("NotWorking");
+#endif
+
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+ SqlTransaction trans = null;
- //Test InvalidOperation Exception is thrown if connection is not set
- cmd.Connection = null;
try {
- cmd.Prepare ();
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ trans = connA.BeginTransaction ();
+ SqlCommand cmdA = new SqlCommand ("select @@version", connA, trans);
+
+ SqlCommand cmdB = new SqlCommand ("select @@version", connA, trans);
+ using (SqlDataReader reader = cmdB.ExecuteReader ()) {
#if NET_2_0
- Assert.Fail ("#8 NullReferenceException should be thrown");
+ cmdA.Connection = connA;
+ Assert.AreSame (connA, cmdA.Connection, "#A1");
+ Assert.AreSame (trans, cmdA.Transaction, "#A2");
#else
- Assert.Fail ("#8 InvalidOperation Exception should be thrown");
+ try {
+ cmdA.Connection = connA;
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // The SqlCommand is currently busy
+ // Open, Fetching
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+
+ Assert.AreSame (connA, cmdA.Connection, "#A5");
+ Assert.AreSame (trans, cmdA.Transaction, "#A6");
+ }
#endif
- }
- catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
+
#if NET_2_0
- Assert.AreEqual (typeof (NullReferenceException), e.GetType (),
- "#9 Incorrect Exception : " + e.StackTrace);
+ cmdA.Connection = connB;
+ Assert.AreSame (connB, cmdA.Connection, "#B1");
+ Assert.AreSame (trans, cmdA.Transaction, "#B2");
#else
- Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
- "#9 Incorrect Exception : " + e.StackTrace);
+ try {
+ cmdA.Connection = connB;
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // The SqlCommand is currently busy
+ // Open, Fetching
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+
+ Assert.AreSame (connA, cmdA.Connection, "#B5");
+ Assert.AreSame (trans, cmdA.Transaction, "#B6");
+ }
#endif
- }
- //Test InvalidOperation Exception is thrown if connection is closed
- cmd.Connection = conn ;
- try{
- cmd.Prepare ();
- Assert.Fail ("#4 InvalidOperation Exception shud be thrown");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
#if NET_2_0
- Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
- "Incorrect Exception : " + e.StackTrace);
+ cmdA.Connection = null;
+ Assert.IsNull (cmdA.Connection, "#C1");
+ Assert.AreSame (trans, cmdA.Transaction, "#C2");
#else
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
- "Incorrect Exception : " + e.StackTrace);
+ try {
+ cmdA.Connection = null;
+ Assert.Fail ("#C1");
+ } catch (InvalidOperationException ex) {
+ // The SqlCommand is currently busy
+ // Open, Fetching
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
+ Assert.IsNull (ex.InnerException, "#C3");
+ Assert.IsNotNull (ex.Message, "#C4");
+
+ Assert.AreSame (connA, cmdA.Connection, "#C5");
+ Assert.AreSame (trans, cmdA.Transaction, "#C6");
+ }
#endif
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
}
}
[Test]
- public void ResetTimeOut ()
+ public void Transaction ()
{
- SqlCommand cmd = new SqlCommand ();
- cmd.CommandTimeout = 50 ;
- Assert.AreEqual ( cmd.CommandTimeout, 50,
- "#1 CommandTimeout should be modfiable");
- cmd.ResetCommandTimeout ();
- Assert.AreEqual (cmd.CommandTimeout, 30,
- "#2 Reset Should set the Timeout to default value");
- }
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+
+ SqlTransaction transA = null;
+ SqlTransaction transB = null;
- [Test]
- [ExpectedException (typeof(ArgumentException))]
- public void CommandTimeout ()
- {
- cmd = new SqlCommand ();
- cmd.CommandTimeout = 10;
- Assert.AreEqual (10, cmd.CommandTimeout, "#1");
- cmd.CommandTimeout = -1;
- }
-
- [Test]
-#if NET_2_0
- [ExpectedException (typeof(ArgumentOutOfRangeException))]
-#else
- [ExpectedException (typeof(ArgumentException))]
-#endif
- public void CommandTypeTest ()
- {
- cmd = new SqlCommand ();
- Assert.AreEqual (CommandType.Text ,cmd.CommandType,
- "Default CommandType is text");
- cmd.CommandType = (CommandType)(-1);
- }
-
- [Test]
- [Ignore ("msdotnet contradicts documented behavior")]
- [ExpectedException (typeof(InvalidOperationException))]
- public void ConnectionTest ()
- {
- SqlTransaction trans = null;
try {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- trans = conn.BeginTransaction ();
- cmd = new SqlCommand ("", conn,trans);
- cmd.CommandText = "Select id from numeric_family where id=1";
- cmd.Connection = new SqlConnection ();
- }finally {
- trans.Rollback();
- conn.Close ();
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ transA = connA.BeginTransaction ();
+ transB = connB.BeginTransaction ();
+
+ SqlCommand cmd = new SqlCommand ("select @@version", connA, transA);
+ cmd.Transaction = transA;
+ Assert.AreSame (connA, cmd.Connection, "#A1");
+ Assert.AreSame (transA, cmd.Transaction, "#A2");
+ cmd.Transaction = transB;
+ Assert.AreSame (connA, cmd.Connection, "#B1");
+ Assert.AreSame (transB, cmd.Transaction, "#B2");
+ cmd.Transaction = null;
+ Assert.AreSame (connA, cmd.Connection, "#C1");
+ Assert.IsNull (cmd.Transaction, "#C2");
+ } finally {
+ if (transA != null)
+ transA.Dispose ();
+ if (transB != null)
+ transA.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
}
}
-
- [Test]
- public void TransactionTest ()
+
+ [Test] // bug #412579
+ public void Transaction_Reader_Open ()
{
- conn = new SqlConnection (connectionString);
- cmd = new SqlCommand ("", conn);
- Assert.IsNull (cmd.Transaction, "#1 Default value is null");
-
- SqlConnection conn1 = new SqlConnection (connectionString);
- conn1.Open ();
- SqlTransaction trans1 = conn1.BeginTransaction ();
- cmd.Transaction = trans1 ;
+ Assert.Ignore ("bug #412579");
+
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+
+ SqlTransaction transA = null;
+ SqlTransaction transB = null;
+
try {
- cmd.ExecuteNonQuery ();
- Assert.Fail ("#2 Connection cannot be different");
- }catch (Exception e) {
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ transA = connA.BeginTransaction ();
+ transB = connB.BeginTransaction ();
+
+ SqlCommand cmdA = new SqlCommand ("select * from employee", connA, transA);
+
+ SqlCommand cmdB = new SqlCommand ("select * from employee", connA, transA);
+ using (SqlDataReader reader = cmdB.ExecuteReader ()) {
#if NET_2_0
- Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
- "#3 Incorrect Exception : " + e);
+ cmdA.Transaction = transA;
+ Assert.AreSame (transA, cmdA.Transaction, "#A1");
#else
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
- "#3 Incorrect Exception : " + e);
+ try {
+ cmdA.Transaction = transA;
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // The SqlCommand is currently busy
+ // Open, Fetching
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+
+ Assert.AreSame (transA, cmdA.Transaction, "#A5");
+ }
#endif
- }finally {
- conn1.Close ();
- conn.Close ();
- }
- }
- // Need to add more tests
- [Test]
#if NET_2_0
- [ExpectedException (typeof(ArgumentOutOfRangeException))]
+ cmdA.Transaction = transB;
+ Assert.AreSame (transB, cmdA.Transaction, "#B1");
#else
- [ExpectedException (typeof(ArgumentException))]
+ try {
+ cmdA.Transaction = transB;
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // The SqlCommand is currently busy
+ // Open, Fetching
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+
+ Assert.AreSame (transA, cmdA.Transaction, "#B5");
+ }
#endif
- public void UpdatedRowSourceTest ()
- {
- cmd = new SqlCommand ();
- Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource,
- "#1 Default value is both");
- cmd.UpdatedRowSource = UpdateRowSource.None;
- Assert.AreEqual (UpdateRowSource.None, cmd.UpdatedRowSource,
- "#2");
- cmd.UpdatedRowSource = (UpdateRowSource) (-1);
+#if NET_2_0
+ cmdA.Transaction = null;
+ Assert.IsNull (cmdA.Transaction, "#C1");
+#else
+ try {
+ cmdA.Transaction = null;
+ Assert.Fail ("#C1");
+ } catch (InvalidOperationException ex) {
+ // The SqlCommand is currently busy
+ // Open, Fetching
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
+ Assert.IsNull (ex.InnerException, "#C3");
+ Assert.IsNotNull (ex.Message, "#C4");
+
+ Assert.AreSame (transA, cmdA.Transaction, "#C5");
+ }
+#endif
+ }
+
+ cmdA.Transaction = transA;
+ Assert.AreSame (transA, cmdA.Transaction, "#D1");
+ cmdA.Transaction = transB;
+ Assert.AreSame (transB, cmdA.Transaction, "#D2");
+ } finally {
+ if (transA != null)
+ transA.Dispose ();
+ if (transB != null)
+ transA.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
+ }
}
[Test]
- public void ExecuteNonQueryTempProcedureTest () {
+ public void ExecuteNonQueryTempProcedureTest ()
+ {
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
try {
ConnectionManager.Singleton.OpenConnection ();
}
}
- // Test for bug #76778
- // Test for a case, when query size is greater than the block size
- [Test]
+ [Test] // bug #319598
public void LongQueryTest ()
{
SqlConnection conn = new SqlConnection (
conn.Open ();
SqlCommand cmd = conn.CreateCommand ();
String value = new String ('a', 10000);
- cmd.CommandText = String.Format ("Select '{0}'", value);
+ cmd.CommandText = String.Format ("Select '{0}'", value);
cmd.ExecuteNonQuery ();
}
}
- // Test for bug #76778
- // To make sure RPC (when implemented) works ok..
- [Test]
+ [Test] // bug #319598
public void LongStoredProcTest()
{
SqlConnection conn = new SqlConnection (
connectionString + ";Pooling=false");
using (conn) {
conn.Open ();
- /*int size = conn.PacketSize ; */
+ /*int size = conn.PacketSize;*/
SqlCommand cmd = conn.CreateCommand ();
- // create a temp stored proc ..
+ // create a temp stored proc
cmd.CommandText = "Create Procedure #sp_tmp_long_params ";
cmd.CommandText += "@p1 nvarchar (4000), ";
cmd.CommandText += "@p2 nvarchar (4000), ";
cmd.CommandText += "As ";
cmd.CommandText += "Begin ";
cmd.CommandText += "Set @p4 = N'Hello' ";
- cmd.CommandText += "Return 2 ";
- cmd.CommandText += "End";
+ cmd.CommandText += "Return 2 ";
+ cmd.CommandText += "End";
cmd.ExecuteNonQuery ();
//execute the proc
cmd.CommandType = CommandType.StoredProcedure;
- cmd.CommandText = "#sp_tmp_long_params";
+ cmd.CommandText = "#sp_tmp_long_params";
String value = new String ('a', 4000);
SqlParameter p1 = new SqlParameter ("@p1",
SqlParameter p4 = new SqlParameter ("@p4",
SqlDbType.NVarChar,4000);
- p4.Direction = ParameterDirection.Output;
+ p4.Direction = ParameterDirection.Output;
// for now, name shud be @RETURN_VALUE
// can be changed once RPC is implemented
SqlParameter p5 = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
- p5.Direction = ParameterDirection.ReturnValue ;
+ p5.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add (p1);
cmd.Parameters.Add (p2);
}
}
- // Test for bug #76880
- [Test]
+ [Test] // bug #319694
public void DateTimeParameterTest ()
{
- SqlConnection conn = new SqlConnection (connectionString);
+ SqlConnection conn = new SqlConnection (connectionString);
using (conn) {
conn.Open ();
SqlCommand cmd = conn.CreateCommand ();
* used as value for a numeric parameter (bug #66630)
*/
[Test]
- public void EnumParameterTest() {
+ public void EnumParameterTest ()
+ {
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
try {
ConnectionManager.Singleton.OpenConnection ();
}
}
- /**
- * The below test does not need a connection but since the setup opens
- * the connection i will need to close it
- */
[Test]
- public void CloneTest() {
- ConnectionManager.Singleton.OpenConnection ();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = null;
- cmd.CommandText = "sp_insert";
- cmd.CommandType = CommandType.StoredProcedure;
- Object TestPar = DBNull.Value;
- cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
- cmd.Parameters["@TestPar1"].Value = TestPar;
-#if NET_2_0
- cmd.Parameters.AddWithValue ("@BirthDate", DateTime.Now);
-#else
- cmd.Parameters.Add ("@BirthDate", DateTime.Now);
-#endif
- cmd.DesignTimeVisible = true;
- cmd.CommandTimeout = 100;
- Object clone1 = ((ICloneable)(cmd)).Clone();
- SqlCommand cmd1 = (SqlCommand) clone1;
- Assert.AreEqual(2, cmd1.Parameters.Count);
- Assert.AreEqual(100, cmd1.CommandTimeout);
-#if NET_2_0
- cmd1.Parameters.AddWithValue ("@test", DateTime.Now);
-#else
- cmd1.Parameters.Add ("@test", DateTime.Now);
-#endif
- // to check that it is deep copy and not a shallow copy of the
- // parameter collection
- Assert.AreEqual(3, cmd1.Parameters.Count);
- Assert.AreEqual(2, cmd.Parameters.Count);
+ public void CloneTest ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ SqlTransaction trans = conn.BeginTransaction ();
+
+ cmd = new SqlCommand ();
+ cmd.Connection = conn;
+ cmd.Transaction = trans;
+
+ SqlCommand clone = (((ICloneable) (cmd)).Clone ()) as SqlCommand;
+ Assert.AreSame (conn, clone.Connection);
+ Assert.AreSame (trans, clone.Transaction);
}
[Test]
query += " select 'data' end";
SqlConnection conn = new SqlConnection (connectionString);
SqlCommand cmd = conn.CreateCommand ();
- cmd.CommandText = query ;
+ cmd.CommandText = query;
conn.Open ();
cmd.ExecuteNonQuery ();
conn.Open ();
SqlCommand cmd = conn.CreateCommand ();
- int label = 0 ;
- string error = "";
+ int label = 0;
+ string error = string.Empty;
while (label != -1) {
try {
switch (label) {
case 0 :
- // Test BigInt Param
+ // Test BigInt Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query, "bigint"));
- rpc_helper_function (cmd, SqlDbType.BigInt, 0, Int64.MaxValue);
- rpc_helper_function (cmd, SqlDbType.BigInt, 0, Int64.MinValue);
+ String.Format (create_query, "bigint"));
+ rpc_helper_function (cmd, SqlDbType.BigInt, 0,
+ Int64.MaxValue, Int64.MaxValue,
+ Int64.MaxValue);
+ rpc_helper_function (cmd, SqlDbType.BigInt, 0,
+ Int64.MinValue, Int64.MinValue,
+ Int64.MinValue);
+ rpc_helper_function (cmd, SqlDbType.BigInt, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 1 :
- // Test Binary Param
+ // Test Binary Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query, "binary(5)"));
- //rpc_helper_function (cmd, SqlDbType.Binary, 0, new byte[] {});
- rpc_helper_function (cmd, SqlDbType.Binary, 5, new byte[] {1,2,3,4,5});
+ String.Format (create_query, "binary(5)"));
+ rpc_helper_function (cmd, SqlDbType.Binary, 5,
+ new byte [] { 1, 2, 3, 4, 5 },
+ new byte [] { 1, 2, 3, 4, 5 },
+ new byte [] { 1, 2, 3, 4, 5 });
+ /*
+ rpc_helper_function (cmd, SqlDbType.Binary, 5,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
+ */
+ rpc_helper_function (cmd, SqlDbType.Binary, 2,
+ new byte [0],
+ new byte [] { 0, 0, 0, 0, 0 },
+ new byte [] { 0, 0 });
break;
case 2 :
// Test Bit Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query, "bit"));
- rpc_helper_function (cmd, SqlDbType.Bit, 0, true);
- rpc_helper_function (cmd, SqlDbType.Bit, 0, false);
+ String.Format (create_query, "bit"));
+ rpc_helper_function (cmd, SqlDbType.Bit, 0,
+ true, true, true);
+ rpc_helper_function (cmd, SqlDbType.Bit, 0,
+ false, false, false);
+ rpc_helper_function (cmd, SqlDbType.Bit, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 3 :
- // Testing Char
+ // Testing Char
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query, "char(10)"));
- rpc_helper_function (cmd, SqlDbType.Char, 10, "characters");
+ String.Format (create_query, "char(10)"));
+ rpc_helper_function (cmd, SqlDbType.Char, 10,
+ "characters", "characters",
+ "characters");
/*
- rpc_helper_function (cmd, SqlDbType.Char, 10, "");
- rpc_helper_function (cmd, SqlDbType.Char, 10, null);
+ rpc_helper_function (cmd, SqlDbType.Char, 3,
+ "characters", "cha ",
+ "cha");
+ rpc_helper_function (cmd, SqlDbType.Char, 3,
+ string.Empty, " ",
+ " ");
*/
+ rpc_helper_function (cmd, SqlDbType.Char, 5,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 4 :
// Testing DateTime
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query, "datetime"));
- rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2079-06-06 23:59:00");
- /*
- rpc_helper_function (cmd, SqlDbType.DateTime, 10, "");
- rpc_helper_function (cmd, SqlDbType.DateTime, 10, null);
- */
+ String.Format (create_query, "datetime"));
+ rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2079-06-06 23:59:00",
+ new DateTime (2079, 6, 6, 23, 59, 0),
+ new DateTime (2079, 6, 6, 23, 59, 0));
+ rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2009-04-12 10:39:45",
+ new DateTime (2009, 4, 12, 10, 39, 45),
+ new DateTime (2009, 4, 12, 10, 39, 45));
+ rpc_helper_function (cmd, SqlDbType.DateTime, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 5 :
// Test Decimal Param
- DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"decimal(10,2)"));
+ DBHelper.ExecuteNonQuery (conn,
+ String.Format (create_query, "decimal(10,2)"));
+ rpc_helper_function (cmd, SqlDbType.Decimal, 0,
+ 10.665, 10.67, 11);
+ // FIXME: NUnit 2.2.0 bug
/*
- rpc_helper_function (cmd, SqlDbType.Decimal, 0, 10.01);
- rpc_helper_function (cmd, SqlDbType.Decimal, 0, -10.01);
+ rpc_helper_function (cmd, SqlDbType.Decimal, 0,
+ 0m, 0m, 0m);
*/
+ rpc_helper_function (cmd, SqlDbType.Decimal, 0,
+ -5.657, -5.66m, -6);
+ rpc_helper_function (cmd, SqlDbType.Decimal, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 6 :
// Test Float Param
- DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"float"));
- rpc_helper_function (cmd, SqlDbType.Float, 0, 10.0);
- rpc_helper_function (cmd, SqlDbType.Float, 0, 0);
- /*
- rpc_helper_function (cmd, SqlDbType.Float, 0, null);
- */
+ DBHelper.ExecuteNonQuery (conn,
+ String.Format (create_query, "float"));
+ rpc_helper_function (cmd, SqlDbType.Float, 0,
+ 10.0, 10.0, 10.0);
+ rpc_helper_function (cmd, SqlDbType.Float, 0,
+ 10.54, 10.54, 10.54);
+ rpc_helper_function (cmd, SqlDbType.Float, 0,
+ 0, 0, 0);
+ rpc_helper_function (cmd, SqlDbType.Float, 0,
+ -5.34, -5.34, -5.34);
+ rpc_helper_function (cmd, SqlDbType.Float, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 7 :
// Testing Image
/* NOT WORKING*/
break;
case 8 :
- // Test Integer Param
+ // Test Integer Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"int"));
- rpc_helper_function (cmd, SqlDbType.Int, 0, 10);
- /*
- rpc_helper_function (cmd, SqlDbType.Int, 0, null);
- */
+ String.Format (create_query, "int"));
+ rpc_helper_function (cmd, SqlDbType.Int, 0,
+ 10, 10, 10);
+ rpc_helper_function (cmd, SqlDbType.Int, 0,
+ 0, 0, 0);
+ rpc_helper_function (cmd, SqlDbType.Int, 0,
+ -5, -5, -5);
+ rpc_helper_function (cmd, SqlDbType.Int, 0,
+ int.MaxValue, int.MaxValue,
+ int.MaxValue);
+ rpc_helper_function (cmd, SqlDbType.Int, 0,
+ int.MinValue, int.MinValue,
+ int.MinValue);
+ rpc_helper_function (cmd, SqlDbType.Int, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 9 :
- // Test Money Param
+ // Test Money Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"money"));
+ String.Format (create_query, "money"));
+ // FIXME: NUnit 2.2.0 bug
/*
- rpc_helper_function (cmd, SqlDbType.Money, 0, 10.0);
- rpc_helper_function (cmd, SqlDbType.Money, 0, null);
+ rpc_helper_function (cmd, SqlDbType.Money, 0,
+ 10m, 10m, 10m);
+ rpc_helper_function (cmd, SqlDbType.Money, 0,
+ 10.54, 10.54m, 10.54m);
+ rpc_helper_function (cmd, SqlDbType.Money, 0,
+ 0, 0m, 0m);
*/
+ /*
+ rpc_helper_function (cmd, SqlDbType.Money, 0,
+ -5.34, -5.34m, -5.34m);
+ */
+ rpc_helper_function (cmd, SqlDbType.Money, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 23 :
- // Test NChar Param
+ // Test NChar Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"nchar(10)"));
+ String.Format (create_query, "nchar(10)"));
+ rpc_helper_function (cmd, SqlDbType.NChar, 10,
+ "characters", "characters",
+ "characters");
+ rpc_helper_function (cmd, SqlDbType.NChar, 3,
+ "characters", "cha ",
+ "cha");
+ rpc_helper_function (cmd, SqlDbType.NChar, 3,
+ string.Empty, " ",
+ " ");
/*
- rpc_helper_function (cmd, SqlDbType.NChar, 10, "nchar");
- rpc_helper_function (cmd, SqlDbType.NChar, 10, "");
- rpc_helper_function (cmd, SqlDbType.NChar, 10, null);
+ rpc_helper_function (cmd, SqlDbType.NChar, 5,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
*/
break;
case 10 :
- // Test NText Param
+ // Test NText Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"ntext"));
+ String.Format (create_query, "ntext"));
/*
rpc_helper_function (cmd, SqlDbType.NText, 0, "ntext");
rpc_helper_function (cmd, SqlDbType.NText, 0, "");
- rpc_helper_function (cmd, SqlDbType.NText, 0, null);
+ rpc_helper_function (cmd, SqlDbType.NText, 0, null);
*/
break;
case 11 :
- // Test NVarChar Param
+ // Test NVarChar Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"nvarchar(10)"));
- rpc_helper_function (cmd, SqlDbType.NVarChar, 10, "nvarchar");
- rpc_helper_function (cmd, SqlDbType.NVarChar, 10, "");
- //rpc_helper_function (cmd, SqlDbType.NVarChar, 10, null);
+ String.Format (create_query, "nvarchar(10)"));
+ rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
+ "nvarchar", "nvarchar", "nvarchar");
+ rpc_helper_function (cmd, SqlDbType.NVarChar, 3,
+ "nvarchar", "nva", "nva");
+ /*
+ rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
+ string.Empty, string.Empty, string.Empty);
+ rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
+ DBNull.Value, DBNull.Value, DBNull.Value);
+ */
break;
case 12 :
- // Test Real Param
+ // Test Real Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"real"));
- rpc_helper_function (cmd, SqlDbType.Real, 0, 10.0);
- //rpc_helper_function (cmd, SqlDbType.Real, 0, null);
+ String.Format (create_query, "real"));
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 10m, 10m, 10m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 10d, 10m, 10m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 0, 0m, 0m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 3.54d, 3.54m, 3.54m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 10, 10m, 10m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 10.5f, 10.5m, 10.5m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 3.5d, 3.5m, 3.5m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ 4.54m, 4.54m, 4.54m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ -4.54m, -4.54m, -4.54m);
+ rpc_helper_function (cmd, SqlDbType.Real, 0,
+ DBNull.Value, DBNull.Value, DBNull.Value);
break;
case 13 :
- // Test SmallDateTime Param
+ // Test SmallDateTime Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"smalldatetime"));
- rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0, "6/6/2079 11:59:00 PM");
- /*
- rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0, "");
- rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0, null);
- */
+ String.Format (create_query, "smalldatetime"));
+ rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0,
+ "6/6/2079 11:59:00 PM",
+ new DateTime (2079, 6, 6, 23, 59, 0),
+ new DateTime (2079, 6, 6, 23, 59, 0));
+ rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 14 :
- // Test SmallInt Param
+ // Test SmallInt Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"smallint"));
- rpc_helper_function (cmd, SqlDbType.SmallInt, 0, 10);
- rpc_helper_function (cmd, SqlDbType.SmallInt, 0, -10);
- //rpc_helper_function (cmd, SqlDbType.SmallInt, 0, null);
+ String.Format (create_query, "smallint"));
+ rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
+ 10, 10, 10);
+ rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
+ -10, -10, -10);
+ rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
+ short.MaxValue, short.MaxValue,
+ short.MaxValue);
+ rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
+ short.MinValue, short.MinValue,
+ short.MinValue);
+ rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
break;
case 15 :
- // Test SmallMoney Param
+ // Test SmallMoney Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"smallmoney"));
+ String.Format (create_query, "smallmoney"));
+ // FIXME: NUnit 2.2.0 bug
/*
- rpc_helper_function (cmd, SqlDbType.SmallMoney, 0, 10.0);
- rpc_helper_function (cmd, SqlDbType.SmallMoney, 0, -10.0);
- rpc_helper_function (cmd, SqlDbType.SmallMoney, 0, null);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ 10.0d, 10m, 10m);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ 0, 0m, 0m);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ 3.54d, 3.54m, 3.54m);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ 10, 10m, 10m);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ 10.5f, 10.5m, 10.5m);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ 3.5d, 3.5m, 3.5m);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ 4.54m, 4.54m, 4.54m);
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ -4.54m, -4.54m, -4.54m);
*/
+ rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
+ DBNull.Value, DBNull.Value, DBNull.Value);
break;
case 16 :
- // Test Text Param
+ // Test Text Param
DBHelper.ExecuteNonQuery (conn,
- String.Format(create_query,"text"));
+ String.Format (create_query, "text"));
/*
rpc_helper_function (cmd, SqlDbType.Text, 0, "text");
rpc_helper_function (cmd, SqlDbType.Text, 0, "");
*/
break;
case 17 :
- // Test TimeStamp Param
+ // Test TimeStamp Param
/* NOT WORKING
DBHelper.ExecuteNonQuery (conn,
String.Format(create_query,"timestamp"));
rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
- rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, null);
+ rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, null);
*/
break;
case 18 :
- // Test TinyInt Param
+ // Test TinyInt Param
DBHelper.ExecuteNonQuery (conn,
String.Format(create_query,"tinyint"));
- /*
- rpc_helper_function (cmd, SqlDbType.TinyInt, 0, 10);
- rpc_helper_function (cmd, SqlDbType.TinyInt, 0, -10);
- rpc_helper_function (cmd, SqlDbType.TinyInt, 0, null);
- */
+ rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
+ 10.0d, 10m, 10m);
+ rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
+ 0, 0, 0);
+ rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
+ byte.MaxValue, byte.MaxValue, byte.MaxValue);
+ rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
+ byte.MinValue, byte.MinValue, byte.MinValue);
break;
case 19 :
- // Test UniqueIdentifier Param
+ // Test UniqueIdentifier Param
/*
DBHelper.ExecuteNonQuery (conn,
String.Format(create_query,"uniqueidentifier"));
rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, "0f159bf395b1d04f8c2ef5c02c3add96");
- rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, null);
+ rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, null);
*/
break;
case 20 :
- // Test VarBinary Param
+ // Test VarBinary Param
/* NOT WORKING
DBHelper.ExecuteNonQuery (conn,
String.Format(create_query,"varbinary (10)"));
rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
- rpc_helper_function (cmd, SqlDbType.VarBinary, 0, null);
+ rpc_helper_function (cmd, SqlDbType.VarBinary, 0, null);
*/
break;
case 21 :
// Test Varchar Param
DBHelper.ExecuteNonQuery (conn,
String.Format(create_query,"varchar(10)"));
- rpc_helper_function (cmd, SqlDbType.VarChar, 10, "VarChar");
+ rpc_helper_function (cmd, SqlDbType.VarChar, 7,
+ "VarChar", "VarChar", "VarChar");
+ rpc_helper_function (cmd, SqlDbType.VarChar, 5,
+ "Var", "Var", "Var");
+ /*
+ rpc_helper_function (cmd, SqlDbType.VarChar, 3,
+ "Varchar", "Var", "Var");
+ rpc_helper_function (cmd, SqlDbType.VarChar, 10,
+ string.Empty, string.Empty, string.Empty);
+ rpc_helper_function (cmd, SqlDbType.VarChar, 10,
+ DBNull.Value, DBNull.Value,
+ DBNull.Value);
+ */
break;
case 22 :
- // Test Variant Param
+ // Test Variant Param
/* NOT WORKING
DBHelper.ExecuteNonQuery (conn,
String.Format(create_query,"variant"));
rpc_helper_function (cmd, SqlDbType.Variant, 0, );
rpc_helper_function (cmd, SqlDbType.Variant, 0, );
- rpc_helper_function (cmd, SqlDbType.Variant, 0, null);
+ rpc_helper_function (cmd, SqlDbType.Variant, 0, null);
*/
- break;
+ break;
default :
label = -2;
- break;
+ break;
}
- }catch (AssertionException e) {
- error += String.Format (" Case {0} INCORRECT VALUE : {1}\n",label, e.Message);
- }catch (Exception e) {
- error += String.Format (" Case {0} NOT WORKING : {1}\n",label, e.Message);
+ }catch (AssertionException ex) {
+ error += String.Format (" Case {0} INCORRECT VALUE : {1}\n", label, ex.ToString ());
+ }catch (Exception ex) {
+ error += String.Format (" Case {0} NOT WORKING : {1}\n", label, ex.ToString ());
}
label++;
if (label != -1)
DBHelper.ExecuteNonQuery (conn, drop_query);
}
- if (error != String.Empty)
+
+ if (error.Length != 0)
Assert.Fail (error);
}
- private void rpc_helper_function (SqlCommand cmd, SqlDbType type, int size, object val)
+ private void rpc_helper_function (SqlCommand cmd, SqlDbType type, int size, object input, object expectedRead, object expectedOut)
{
- cmd.Parameters.Clear ();
- SqlParameter param1 ;
- SqlParameter param2 ;
- if (size != 0) {
- param1 = new SqlParameter ("@param1", type, size);
- param2 = new SqlParameter ("@param2", type, size);
- }
- else {
- param1 = new SqlParameter ("@param1", type);
- param2 = new SqlParameter ("@param2", type);
- }
+ cmd.Parameters.Clear ();
+ SqlParameter param1;
+ SqlParameter param2;
+ if (size != 0) {
+ param1 = new SqlParameter ("@param1", type, size);
+ param2 = new SqlParameter ("@param2", type, size);
+ } else {
+ param1 = new SqlParameter ("@param1", type);
+ param2 = new SqlParameter ("@param2", type);
+ }
- SqlParameter retval = new SqlParameter ("retval", SqlDbType.Int);
- param1.Value = val;
- param1.Direction = ParameterDirection.Input;
- param2.Direction = ParameterDirection.Output;
- retval.Direction = ParameterDirection.ReturnValue;
- cmd.Parameters.Add (param1);
- cmd.Parameters.Add (param2);
- cmd.Parameters.Add (retval);
- cmd.CommandText = "#tmp_sp_param_test";
- cmd.CommandType = CommandType.StoredProcedure;
- using (SqlDataReader reader = cmd.ExecuteReader ()) {
- while (reader.Read ()) {
- if (param1.Value != null && param1.Value.GetType () == typeof (string))
- Assert.AreEqual (param1.Value,
- reader.GetValue(0).ToString (),"#1");
- else
- Assert.AreEqual (param1.Value,
- reader.GetValue(0),"#1");
- }
- }
- if (param1.Value != null && param1.Value.GetType () == typeof (string) && param2.Value != null)
- Assert.AreEqual (param1.Value.ToString (), param2.Value.ToString (), "#2");
- else
- Assert.AreEqual (param1.Value, param2.Value, "#2");
- Assert.AreEqual (5, retval.Value, "#3");
+ SqlParameter retval = new SqlParameter ("retval", SqlDbType.Int);
+ param1.Value = input;
+ param1.Direction = ParameterDirection.Input;
+ param2.Direction = ParameterDirection.Output;
+ retval.Direction = ParameterDirection.ReturnValue;
+ cmd.Parameters.Add (param1);
+ cmd.Parameters.Add (param2);
+ cmd.Parameters.Add (retval);
+ cmd.CommandText = "#tmp_sp_param_test";
+ cmd.CommandType = CommandType.StoredProcedure;
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#1");
+ Assert.AreEqual (expectedRead, reader.GetValue (0), "#2");
+ Assert.IsFalse (reader.Read (), "#3");
+ }
+ Assert.AreEqual (expectedOut, param2.Value, "#4");
+ Assert.AreEqual (5, retval.Value, "#5");
}
[Test]
- [ExpectedException (typeof (InvalidOperationException))]
public void OutputParamSizeTest1 ()
{
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
p1.DbType = DbType.String;
p1.IsNullable = false;
cmd.Parameters.Add (p1);
- cmd.ExecuteNonQuery ();
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // String[0]: the Size property has an invalid
+ // size of 0
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
}
[Test]
- [ExpectedException (typeof (InvalidOperationException))]
public void OutputParamSizeTest2 ()
{
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
p1.DbType = DbType.String;
p1.IsNullable = false;
cmd.Parameters.Add (p1);
- cmd.ExecuteNonQuery ();
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // String[0]: the Size property has an invalid
+ // size of 0
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
}
[Test]
- [ExpectedException (typeof (InvalidOperationException))]
public void OutputParamSizeTest3 ()
{
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
p1.DbType = DbType.String;
p1.IsNullable = true;
cmd.Parameters.Add (p1);
- cmd.ExecuteNonQuery ();
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // String[0]: the Size property has an invalid
+ // size of 0
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
}
[Test]
- [ExpectedException (typeof (InvalidOperationException))]
public void OutputParamSizeTest4 ()
{
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
p1.DbType = DbType.String;
p1.IsNullable = true;
cmd.Parameters.Add (p1);
- cmd.ExecuteNonQuery ();
+
+ try {
+ cmd.ExecuteNonQuery ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // String[0]: the Size property has an invalid
+ // size of 0
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
}
-#if NET_2_0
+#if NET_2_0
[Test]
- public void NotificationTest ()
+ public void NotificationTest ()
{
cmd = new SqlCommand ();
SqlNotificationRequest notification = new SqlNotificationRequest("MyNotification","MyService",15);
}
[Test]
- public void NotificationAutoEnlistTest ()
+ public void NotificationAutoEnlistTest ()
{
cmd = new SqlCommand ();
Assert.AreEqual (true, cmd.NotificationAutoEnlist, "#1 Default value of the property should be true");
Assert.AreEqual (false, cmd.NotificationAutoEnlist, "#2 The value of the property should be false after setting it to false");
}
- [Test]
+ [Test]
public void BeginExecuteXmlReaderTest ()
{
cmd = new SqlCommand ();
string connectionString1 = null;
connectionString1 = ConnectionManager.Singleton.ConnectionString + "Asynchronous Processing=true";
try {
- SqlConnection conn1 = new SqlConnection (connectionString1);
+ SqlConnection conn1 = new SqlConnection (connectionString1);
conn1.Open ();
- cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA" ;
+ cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA";
cmd.Connection = conn1;
IAsyncResult result = cmd.BeginExecuteXmlReader ();
XmlReader reader = cmd.EndExecuteXmlReader (result);
- while (reader.Read ())
- {
+ while (reader.Read ()) {
if (reader.LocalName.ToString () == "employee")
- {
- Assert.AreEqual ("kumar", reader["lname"], "#1 ");
- }
+ Assert.AreEqual ("kumar", reader["lname"], "#1 ");
}
} finally {
ConnectionManager.Singleton.CloseConnection ();
{
cmd = new SqlCommand ();
try {
- SqlConnection conn = new SqlConnection (connectionString);
+ SqlConnection conn = new SqlConnection (connectionString);
conn.Open ();
- cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA" ;
+ cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA";
cmd.Connection = conn;
try {
Assert.Fail ("Expected Exception InvalidOperationException not thrown");
} finally {
ConnectionManager.Singleton.CloseConnection ();
- }
+ }
}
-
+#endif
+
[Test]
public void SqlCommandDisposeTest ()
{
} finally {
reader.Dispose();
ConnectionManager.Singleton.CloseConnection ();
- }
- }
-
- [Test]
- public void CloneObjTest ()
- {
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = "sp_insert";
- cmd.CommandType = CommandType.StoredProcedure;
- Object TestPar = DBNull.Value;
- cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
- cmd.Parameters ["@TestPar1"].Value = TestPar;
-#if NET_2_0
- cmd.Parameters.AddWithValue ("@BirthDate", DateTime.Now);
-#else
- cmd.Parameters.Add ("@BirthDate", DateTime.Now);
-#endif
- cmd.DesignTimeVisible = true;
- cmd.CommandTimeout = 100;
- SqlCommand cmd1 = cmd.Clone ();
- Assert.AreEqual (2, cmd1.Parameters.Count);
- Assert.AreEqual (100, cmd1.CommandTimeout);
-#if NET_2_0
- cmd1.Parameters.AddWithValue ("@test", DateTime.Now);
-#else
- cmd1.Parameters.Add ("@test", DateTime.Now);
-#endif
- Assert.AreEqual (3, cmd1.Parameters.Count);
- Assert.AreEqual (2, cmd.Parameters.Count);
+ }
}
-#endif
+
private void bug326182_OutputParamMixupTestCommon (int paramOrder,
out int param0Val,
out int param1Val,
try {
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
ConnectionManager.Singleton.OpenConnection ();
- string create_proc = "CREATE procedure sp_326182 ( " + Environment.NewLine +
- "@param0 int out," + Environment.NewLine +
+ string create_proc = "CREATE procedure #sp_326182 ( " + Environment.NewLine +
+ "@param0 int out," + Environment.NewLine +
"@param1 int out," + Environment.NewLine +
"@param2 int out," + Environment.NewLine +
"@param3 int out" + Environment.NewLine +
"set @param2 = 102" + Environment.NewLine +
"set @param3 = 103" + Environment.NewLine +
"return 2";
- string drop_proc = "drop procedure sp_326182";
try {
SqlParameter param0 = new SqlParameter ("@param0", SqlDbType.Int);
cmd.CommandTimeout = 90;
cmd.ExecuteNonQuery ();
- cmd.CommandText = "dbo.[sp_326182]";
+ cmd.CommandText = "dbo.[#sp_326182]";
cmd.CommandType = CommandType.StoredProcedure;
param0.Direction = ParameterDirection.Output;
rvalVal = (int)cmd.Parameters["@RETURN_VALUE"].Value;
param2Val = (int)cmd.Parameters["@param2"].Value;
param0Val = (int)cmd.Parameters["@param0"].Value;
-
+ } finally {
/* Delete the created stored procedure */
- cmd.Parameters.Clear ();
- cmd.CommandText = drop_proc;
- cmd.CommandType = CommandType.Text;
+ cmd = conn.CreateCommand ();
+ cmd.CommandText = "drop procedure #sp_326182";
cmd.ExecuteNonQuery ();
- } finally {
+
cmd.Dispose();
cmd = null;
}
} finally {
ConnectionManager.Singleton.CloseConnection ();
conn = null;
- }
+ }
}
[Test]
Assert.AreEqual (100, param0Val);
}
- private enum Status {
+ private enum Status
+ {
OK = 0,
Error = 3
}
//
-// SqlDataAdapterTest.cs - NUnit Test Cases for testing the
-// SqlDataAdapter class
+// SqlConnectionTest.cs - NUnit Test Cases for testing the
+// SqlConnection class
// Author:
// Senganal T (tsenganal@novell.com)
//
[Category ("sqlserver")]
public class SqlConnectionTest
{
- SqlConnection conn = null ;
+ SqlConnection conn;
String connectionString = ConnectionManager.Singleton.ConnectionString;
-
- ArrayList invalidConnectionStrings = null;
- int stateChangeEventCount = 0;
- int disposedEventCount = 0;
- int infoMessageEventCount = 0;
-
- void populateTestData ()
- {
- invalidConnectionStrings = new ArrayList ();
- // shud be got from a config file ..
- //list of invalid and valid conn strings;
- invalidConnectionStrings.Add ("InvalidConnectionString");
- invalidConnectionStrings.Add ("invalidKeyword=10");
- invalidConnectionStrings.Add ("Packet Size=511");
- invalidConnectionStrings.Add ("Packet Size=32768");
- invalidConnectionStrings.Add ("Connect Timeout=-1");
- invalidConnectionStrings.Add ("Max Pool Size=-1");
- invalidConnectionStrings.Add ("Min Pool Size=-1");
- }
+ ArrayList events;
[SetUp]
public void SetUp ()
{
+ events = new ArrayList ();
}
[TearDown]
conn.Dispose ();
}
- [Test]
- public void DefaultConstructorTest ()
- {
- SqlConnection conn = new SqlConnection ();
- Assert.AreEqual ("", conn.ConnectionString,
- "#1 Default Connection String should be empty");
- Assert.AreEqual (15, conn.ConnectionTimeout,
- "#2 Default ConnectionTimeout should be 15" );
- Assert.AreEqual ("", conn.Database,
- "#3 Default Database should be empty");
- Assert.AreEqual ("", conn.DataSource,
- "#4 Default DataSource should be empty");
- Assert.AreEqual (8192, conn.PacketSize,"#5 Default Packet Size is 8192");
- Assert.AreEqual (Dns.GetHostName().ToUpper (), conn.WorkstationId.ToUpper (),
- "#6 Default Workstationid shud be hostname");
- Assert.AreEqual (ConnectionState.Closed, conn.State,
- "#7 Connection State shud be closed by default");
- }
-
[Test]
public void OverloadedConstructorTest ()
{
- // Test Exceptions are thrown for Invalid Connection Strings
- int count=0 ;
- populateTestData ();
- foreach (String invalidConnString in invalidConnectionStrings) {
- count++;
- try {
- conn = new SqlConnection ((string)invalidConnString);
- Assert.Fail ("#1 Exception must be thrown");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(ArgumentException), e.GetType(),
- "Incorrect Exception" + e.StackTrace);
- }
- }
-
- //check synonyms..
+ //check synonyms.
//do i need to check for all the synonyms..
- conn = new SqlConnection (
- "Timeout=10;Connect Timeout=20;Connection Timeout=30");
- Assert.AreEqual (30, conn.ConnectionTimeout,
- "## The last set value shud be taken");
- conn = new SqlConnection (
- "Connect Timeout=100;Connection Timeout=200;Timeout=300");
- Assert.AreEqual (300, conn.ConnectionTimeout,
- "## The last set value shud be taken");
- conn = new SqlConnection (
- "Connection Timeout=1000;Timeout=2000;Connect Timeout=3000");
- Assert.AreEqual (3000, conn.ConnectionTimeout,
- "## The last set value shud be taken");
+ conn = new SqlConnection ("Timeout=10;Connect Timeout=20;Connection Timeout=30");
+ Assert.AreEqual (30, conn.ConnectionTimeout, "#A1");
+ conn = new SqlConnection ("Connect Timeout=100;Connection Timeout=200;Timeout=300");
+ Assert.AreEqual (300, conn.ConnectionTimeout, "#A2");
+ conn = new SqlConnection ("Connection Timeout=1000;Timeout=2000;Connect Timeout=3000");
+ Assert.AreEqual (3000, conn.ConnectionTimeout, "#A3");
- // Test if properties are set correctly
-
//'==' doesent work correctly in both msdotnet and mono
/*
conn = new SqlConnection ("server=local==host;database=tmp;");
Assert.AreEqual ("local==host", conn.DataSource,
"# Datasource name is set incorrectly");
*/
- string connStr = "Server='loca\"lhost';Database='''Db'; packet Size=\"512\";";
- connStr += "connect Timeout=20;Workstation Id=\"'\"\"desktop\";";
+ string connStr = "Server='loca\"lhost';Database='''Db'; packet Size=\"512\";"
+ + "connect Timeout=20;Workstation Id=\"'\"\"desktop\";";
conn = new SqlConnection (connStr);
- Assert.AreEqual (connStr , conn.ConnectionString , "#1");
- Assert.AreEqual ("loca\"lhost" , conn.DataSource , "#2");
- Assert.AreEqual ("'Db" , conn.Database , "#3");
- Assert.AreEqual (512 , conn.PacketSize , "#4");
- Assert.AreEqual (20 , conn.ConnectionTimeout , "#5");
- Assert.AreEqual ("'\"desktop" , conn.WorkstationId , "#6");
- Assert.AreEqual (ConnectionState.Closed , conn.State , "#7");
+ Assert.AreEqual (connStr , conn.ConnectionString , "#B1");
+ Assert.AreEqual ("loca\"lhost" , conn.DataSource , "#B2");
+ Assert.AreEqual ("'Db" , conn.Database , "#B3");
+ Assert.AreEqual (512 , conn.PacketSize , "#B4");
+ Assert.AreEqual (20 , conn.ConnectionTimeout , "#B5");
+ Assert.AreEqual ("'\"desktop" , conn.WorkstationId , "#B6");
}
[Test]
- public void OpenTest ()
+ public void Open ()
{
conn = new SqlConnection (connectionString);
- ArrayList validIncorrectConnStrings = new ArrayList();
- string validConnString = connectionString;
-
- validIncorrectConnStrings.Add (
- validConnString+"user id=invalidLogin");
- validIncorrectConnStrings.Add (
- validConnString+"database=invalidDB");
- validIncorrectConnStrings.Add (
- validConnString+";password=invalidPassword");
- validIncorrectConnStrings.Add (
- validConnString+";server=invalidServerName");
-
- int count=0;
- foreach (string connString in validIncorrectConnStrings) {
- count++;
- try {
- conn.ConnectionString = connString;
- conn.Open();
- Assert.Fail (String.Format (
- "#1_{0} Incorrect Connection String",count));
-
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof (SqlException), e.GetType (),
- "#2 Incorrect Exception" + e.StackTrace);
- }
- }
-
- // Test connection is Opened for a valid Connection String
- conn.ConnectionString = connectionString;
+ conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
conn.Open ();
- Assert.AreEqual (ConnectionState.Open, conn.State,
- "#3 Connection State Should be OPEN");
- // Test Exception is thrown on opening an OPEN Connection
- try {
- conn.Open ();
- Assert.AreEqual (typeof (InvalidOperationException), null,
- "#1 Connection is Already Open");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
- "#2 Incorect Exception.");
- }
- conn.Close();
+ Assert.AreEqual (ConnectionState.Open, conn.State, "#1");
+ Assert.AreEqual (1, events.Count, "#2");
+ StateChangeEventArgs args = events [0] as StateChangeEventArgs;
+ Assert.IsNotNull (args, "#3");
+ Assert.AreEqual (ConnectionState.Closed, args.OriginalState, "#4");
+ Assert.AreEqual (ConnectionState.Open, args.CurrentState, "#5");
- /*
- // Test if localhost is assumed when servername is empty/missing
- // NOTE : msdotnet contradicts doc
+ conn.Close ();
+ }
+
+ [Test]
+ public void Open_Connection_Open ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
- Assumes the server is localhost .. need to test this with mono on windows
- conn.ConnectionString = connectionString + "server=;";
try {
conn.Open ();
- }catch (Exception e) {
- Assert.Fail ("## If server name is not given or empty ,localhost shud be tried");
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // The connection was not closed. The connection's
+ // current state is open
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ } finally {
+ conn.Close ();
}
- ex = null;
- conn.Close ();
- */
}
[Test]
- public void OpenTest_1 ()
+ public void Open_ConnectionString_Incorrect ()
{
- SqlConnection conn = new SqlConnection ();
+ Assert.Ignore ("NotWorking");
+
+ // login invalid
+ conn = new SqlConnection (connectionString + "user id=invalidLogin");
+ try {
+ conn.Open ();
+ Assert.Fail ("#A1");
+ } catch (SqlException ex) {
+ // Login failed for user 'invalidLogin'
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#A2");
+ Assert.AreEqual ((byte) 14, ex.Class, "#A3");
+ Assert.IsNull (ex.InnerException, "#A4");
+ Assert.IsNotNull (ex.Message, "#A5");
+ Assert.IsTrue (ex.Message.IndexOf ("'invalidLogin'") != -1, "#A6");
+ Assert.AreEqual (18456, ex.Number, "#A7");
+ Assert.AreEqual ((byte) 1, ex.State, "#A8");
+ } finally {
+ conn.Close ();
+ }
+
+ // database invalid
+ conn = new SqlConnection (connectionString + "database=invalidDB");
+ try {
+ conn.Open ();
+ Assert.Fail ("#B1");
+ } catch (SqlException ex) {
+ // Cannot open database "invalidDB" requested
+ // by the login. The login failed
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
+ Assert.AreEqual ((byte) 11, ex.Class, "#B3");
+ Assert.IsNull (ex.InnerException, "#B4");
+ Assert.IsNotNull (ex.Message, "#B5");
+ Assert.IsTrue (ex.Message.IndexOf ("\"invalidDB\"") != -1, "#B6");
+ Assert.AreEqual (4060, ex.Number, "#B7");
+ Assert.AreEqual ((byte) 1, ex.State, "#B8");
+ } finally {
+ conn.Close ();
+ }
- conn.ConnectionString = "";
+ // password invalid
+ conn = new SqlConnection (connectionString + ";password=invalidPassword");
try {
conn.Open ();
- Assert.Fail ("#1 Should throw ArgumentException and not SqlException");
- } catch (InvalidOperationException) {
+ Assert.Fail ("#C1");
+ } catch (SqlException ex) {
+ // Login failed for user '...'
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#C2");
+ Assert.AreEqual ((byte) 14, ex.Class, "#C3");
+ Assert.IsNull (ex.InnerException, "#C4");
+ Assert.IsNotNull (ex.Message, "#C5");
+ Assert.AreEqual (18456, ex.Number, "#C6");
+ Assert.AreEqual ((byte) 1, ex.State, "#C7");
+ } finally {
+ conn.Close ();
}
- conn.ConnectionString = " ";
+ // server invalid
+ conn = new SqlConnection (connectionString + ";server=invalidServerName");
try {
conn.Open ();
- Assert.Fail ("#2 Should throw ArgumentException and not SqlException");
- } catch (InvalidOperationException) {
+ Assert.Fail ("#D1");
+ } catch (SqlException ex) {
+ // An error has occurred while establishing a
+ // connection to the server...
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#D2");
+ Assert.AreEqual ((byte) 20, ex.Class, "#D3");
+ Assert.IsNull (ex.InnerException, "#D4");
+ Assert.IsNotNull (ex.Message, "#D5");
+#if NET_2_0
+ Assert.AreEqual (53, ex.Number, "#D6");
+#else
+ Assert.AreEqual (17, ex.Number, "#D6");
+#endif
+ Assert.AreEqual ((byte) 0, ex.State, "#D7");
+ } finally {
+ conn.Close ();
}
}
- [Test]
- public void CreateCommandTest ()
+ [Test] // bug #412574
+ public void Close ()
{
- conn = new SqlConnection (connectionString);
- IDbCommand cmd = conn.CreateCommand ();
- Assert.AreSame (conn, cmd.Connection,
- "#1 Connection instance should be the same");
- }
+ Assert.Ignore ("bug #412574");
- [Test]
- public void CloseTest ()
- {
conn = new SqlConnection (connectionString);
conn.Open ();
- Assert.AreEqual (ConnectionState.Open, conn.State,
- "#1 Connection State should be : Open");
+ conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
conn.Close ();
- Assert.AreEqual (ConnectionState.Closed, conn.State,
- "#1 Connection State Should : Closed");
- // Test Closing an already closed connection is Valid..
+
+ Assert.AreEqual (ConnectionState.Closed, conn.State, "#1");
+ Assert.AreEqual (1, events.Count, "#2");
+ StateChangeEventArgs args = events [0] as StateChangeEventArgs;
+ Assert.IsNotNull (args, "#3");
+ Assert.AreEqual (ConnectionState.Open, args.OriginalState, "#4");
+ Assert.AreEqual (ConnectionState.Closed, args.CurrentState, "5");
+
conn.Close ();
+
+ Assert.AreEqual (1, events.Count, "#6");
}
[Test]
- public void DisposeTest ()
+ public void ChangeDatabase ()
{
- SqlConnection conn = new SqlConnection (connectionString);
- conn.Dispose ();
- Assert.AreEqual ("", conn.ConnectionString,
- "#1 Dispose shud make the Connection String empty");
- Assert.AreEqual (15, conn.ConnectionTimeout,
- "#2 Default ConnectionTimeout : 15" );
- Assert.AreEqual ("", conn.Database,
- "#3 Default Database : empty");
- Assert.AreEqual ("", conn.DataSource,
- "#4 Default DataSource : empty");
- Assert.AreEqual (8192, conn.PacketSize,
- "#5 Default Packet Size : 8192");
- Assert.AreEqual (Dns.GetHostName().ToUpper (), conn.WorkstationId.ToUpper (),
- "#6 Default Workstationid : hostname");
- Assert.AreEqual (ConnectionState.Closed, conn.State,
- "#7 Default State : CLOSED ");
-
- conn = new SqlConnection ();
- //shud not throw exception
- conn.Dispose ();
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ conn.ChangeDatabase ("master");
+ Assert.AreEqual ("master", conn.Database);
}
[Test]
- public void ChangeDatabaseTest ()
+ public void ChangeDatabase_DatabaseName_DoesNotExist ()
{
conn = new SqlConnection (connectionString);
+ conn.Open ();
+
String database = conn.Database;
- //Test if exception is thrown if connection is closed
try {
- conn.ChangeDatabase ("database");
- Assert.AreEqual (typeof (InvalidOperationException), null,
- "#1 Connection is Closed");
- }catch (AssertionException e){
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof (InvalidOperationException), e.GetType(),
- "#2 Incorrect Exception : " + e.StackTrace);
+ conn.ChangeDatabase ("doesnotexist");
+ Assert.Fail ("#1");
+ } catch (SqlException ex) {
+ // Could not locate entry in sysdatabases for
+ // database 'doesnotexist'. No entry found with
+ // that name. Make sure that the name is entered
+ // correctly.
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
+ Assert.AreEqual ((byte) 16, ex.Class, "#3");
+ Assert.IsNull (ex.InnerException, "#4");
+ Assert.IsNotNull (ex.Message, "#5");
+ Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#6");
+ Assert.AreEqual (911, ex.Number, "#7");
+ Assert.AreEqual ((byte) 1, ex.State, "#8");
+
+ Assert.AreEqual (database, conn.Database, "#9");
+ } finally {
+ conn.Close ();
}
+ }
- //Test if exception is thrown for invalid Database Names
- //need to add more to the list
+ [Test]
+ public void ChangeDatabase_DatabaseName_Empty ()
+ {
+ conn = new SqlConnection (connectionString);
conn.Open ();
- String[] InvalidDatabaseNames = {"", null, " "};
- for (int i = 0; i < InvalidDatabaseNames.Length ; ++i) {
- try {
- conn.ChangeDatabase (InvalidDatabaseNames[i]);
- Assert.AreEqual (typeof (ArgumentException), null,
- string.Format ("#3_{0} Exception not thrown",i));
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(ArgumentException), e.GetType (),
- string.Format( "#4_{0} Incorrect Exception : {1}",
- i, e.StackTrace));
- }
- Assert.AreEqual (database, conn.Database,
- "#4 The Database shouldnt get changed if Operation Failed");
+ try {
+ conn.ChangeDatabase (string.Empty);
+ Assert.Fail ("#1");
+ } catch (ArgumentException ex) {
+ // Database cannot be null, the empty string,
+ // or string of only whitespace
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName);
}
-
- //Test if exception is thrown if database name is non-existent
+ }
+
+ [Test]
+ public void ChangeDatabase_DatabaseName_Null ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
try {
- conn.ChangeDatabase ("invalidDB");
- Assert.Fail ("#5 Exception must be thrown if database doesent exist");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(SqlException), e.GetType (),
- "#6 Incorrect Exception" + e.StackTrace);
+ conn.ChangeDatabase ((string) null);
+ Assert.Fail ("#1");
+ } catch (ArgumentException ex) {
+ // Database cannot be null, the empty string,
+ // or string of only whitespace
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName);
}
- conn.Close ();
+ }
- //Test if '-' is a valid character in a database name
- //TODO : Check for database names that have more special Characters..
- conn.ConnectionString = connectionString;
+ [Test] // bug #412581
+ public void ChangeDatabase_DatabaseName_Whitespace ()
+ {
+#if NET_2_0
+ Assert.Ignore ("bug #412581");
+#endif
+
+ conn = new SqlConnection (connectionString);
conn.Open ();
try {
- conn.ChangeDatabase ("mono-test");
- Assert.AreEqual ("mono-test", conn.Database,
- "#7 Database name should be mono-test");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e){
- Assert.Fail ("#8 Unexpected Exception : DB Name can have a '-' : "
- + e);
+ conn.ChangeDatabase (" ");
+ Assert.Fail ("#1");
+#if NET_2_0
+ } catch (SqlException ex) {
+ // Could not locate entry in sysdatabases for
+ // database ' '. No entry found with that name.
+ // Make sure that the name is entered correctly
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
+ Assert.AreEqual ((byte) 16, ex.Class, "#3");
+ Assert.IsNull (ex.InnerException, "#4");
+ Assert.IsNotNull (ex.Message, "#5");
+ Assert.IsTrue (ex.Message.IndexOf ("' '") != -1, "#6");
+ Assert.AreEqual (911, ex.Number, "#7");
+ Assert.AreEqual ((byte) 1, ex.State, "#8");
}
+#else
+ } catch (ArgumentException ex) {
+ // Database cannot be null, the empty string,
+ // or string of only whitespace
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName);
+ }
+#endif
}
[Test]
}
[Test]
- public void BeginTransactionTest()
+ public void BeginTransaction ()
{
conn = new SqlConnection (connectionString);
- SqlTransaction trans = null ;
+ conn.Open ();
- try {
- trans = conn.BeginTransaction ();
- Assert.Fail ("#1 Connection must be Open to Begin a Transaction");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof (InvalidOperationException), e.GetType(),
- "#2 Incorrect Exception" + e.StackTrace);
- }
+ SqlTransaction trans = conn.BeginTransaction ();
+ Assert.AreSame (conn, trans.Connection, "#A1");
+ Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel, "#A2");
+ trans.Rollback ();
- conn.Open ();
trans = conn.BeginTransaction ();
- Assert.AreSame (conn, trans.Connection,
- "#3 Transaction should reference the same connection");
- Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel,
- "#4 Isolation Level shud be ReadCommitted");
- trans.Rollback ();
try {
- trans = conn.BeginTransaction ();
- trans = conn.BeginTransaction ();
conn.BeginTransaction ();
- Assert.Fail ("#5 Parallel Transactions are not supported");
- }catch (AssertionException e) {
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
- "#6 Incorrect Exception" + e.StackTrace);
- }finally {
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // SqlConnection does not support parallel transactions
+ Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+ } finally {
trans.Rollback();
}
trans = conn.BeginTransaction ();
trans.Rollback ();
trans = conn.BeginTransaction ();
- trans.Commit();
+ trans.Commit ();
trans = conn.BeginTransaction ();
- }catch {
- Assert.Fail ("#7 Transaction can be opened after a rollback/commit");
- }finally {
+ } finally {
trans.Rollback ();
}
}
[Test]
- public void ConnectionStringPropertyTest ()
+ public void ConnectionString ()
{
- conn = new SqlConnection (connectionString) ;
- // Test Repeated Keyoword ..Should take the latest value
- conn.ConnectionString = conn.ConnectionString + ";server=RepeatedServer;" ;
- Assert.AreEqual ("RepeatedServer", ((SqlConnection)conn).DataSource,
- "#1 if keyword is repeated, the latest value should be taken");
+ conn = new SqlConnection (connectionString);
+ // Test Repeated Keyoword should take the latest value
+ conn.ConnectionString = conn.ConnectionString + ";server=RepeatedServer;";
+ Assert.AreEqual ("RepeatedServer", ((SqlConnection)conn).DataSource, "#A1");
conn.ConnectionString += ";database=gen;Initial Catalog=gen1";
- Assert.AreEqual ("gen1", conn.Database,
- "#2 database and initial catalog are synonyms .. ");
+ Assert.AreEqual ("gen1", conn.Database, "#A2");
- // Test if properties are set correctly
+ // Test if properties are set correctly
string str = "server=localhost1;database=db;user id=user;";
str += "password=pwd;Workstation ID=workstation;Packet Size=512;";
str += "Connect Timeout=10";
conn.ConnectionString = str;
- Assert.AreEqual ("localhost1", conn.DataSource,
- "#3 DataSource name should be same as passed");
- Assert.AreEqual ("db", conn.Database,
- "#4 Database name shud be same as passed");
- Assert.AreEqual (ConnectionState.Closed, conn.State,
- "#5 Connection shud be in closed state");
- Assert.AreEqual ("workstation", conn.WorkstationId,
- "#6 Workstation Id shud be same as passed");
- Assert.AreEqual (512, conn.PacketSize,
- "#7 Packetsize shud be same as passed");
- Assert.AreEqual (10, conn.ConnectionTimeout,
- "#8 ConnectionTimeout shud be same as passed");
+ Assert.AreEqual ("localhost1", conn.DataSource, "#B1");
+ Assert.AreEqual ("db", conn.Database, "#B2");
+ Assert.AreEqual (ConnectionState.Closed, conn.State, "#B3");
+ Assert.AreEqual ("workstation", conn.WorkstationId, "#B4");
+ Assert.AreEqual (512, conn.PacketSize, "#B5");
+ Assert.AreEqual (10, conn.ConnectionTimeout, "#B6");
- // Test if any leftover values exist from previous invocation.
+ // Test if any leftover values exist from previous invocation
conn.ConnectionString = connectionString;
- conn.ConnectionString = "";
- Assert.AreEqual ("", conn.DataSource,
- "#9 Datasource shud be reset to Default : Empty");
- Assert.AreEqual ("", conn.Database,
- "#10 Database shud reset to Default : Empty");
- Assert.AreEqual (8192, conn.PacketSize,
- "#11 Packetsize shud be reset to Default : 8192");
- Assert.AreEqual (15, conn.ConnectionTimeout,
- "#12 ConnectionTimeour shud be reset to Default : 15");
- Assert.AreEqual (Dns.GetHostName ().ToUpper (), conn.WorkstationId.ToUpper (),
- "#13 WorkstationId shud be reset to Default : Hostname");
-
- // Test Argument Exception is thrown for Invalid Connection Strings
- foreach (string connString in invalidConnectionStrings) {
- try {
- conn.ConnectionString = connString;
- Assert.Fail (
- "#14 Exception should be thrown");
- }catch (AssertionException e){
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof (ArgumentException), e.GetType(),
- "#15 Incorrect Exception" + e.StackTrace);
- }
- }
+ conn.ConnectionString = string.Empty;
+ Assert.AreEqual (string.Empty, conn.DataSource, "#C1");
+ Assert.AreEqual ("", conn.Database, "#C2");
+#if NET_2_0
+ Assert.AreEqual (8000, conn.PacketSize, "#C3");
+#else
+ Assert.AreEqual (8192, conn.PacketSize, "#C3");
+#endif
+ Assert.AreEqual (15, conn.ConnectionTimeout, "#C4");
+ Assert.IsTrue (string.Compare (conn.WorkstationId, Environment.MachineName, true) == 0, "#C5");
+ }
- // Test if ConnectionString is read-only when Connection is OPEN
+ [Test]
+ public void ConnectionString_Connection_Open ()
+ {
+ conn = new SqlConnection (connectionString);
conn.ConnectionString = connectionString;
- conn.Open() ;
+ conn.Open ();
try {
- Assert.AreEqual (conn.State, ConnectionState.Open,
- "#16 Connection shud be open");
- conn.ConnectionString = "server=localhost;database=tmp;" ;
- Assert.Fail (
- "#17 ConnectionString should Read-Only when Connection is Open");
- }catch (AssertionException e){
- throw e;
- }catch (Exception e) {
- Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
- "#18 Incorrect Exception" + e.StackTrace);
+ conn.ConnectionString = "server=localhost;database=tmp;";
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // Not allowed to change the 'ConnectionString'
+ // property. The connection's current state is open
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ } finally {
+ conn.Close ();
}
- conn.Close ();
}
[Test]
public void ServerVersionTest ()
- {
+ {
conn = new SqlConnection (connectionString);
// Test InvalidOperation Exception is thrown if Connection is CLOSED
try {
string s = conn.ServerVersion;
- Assert.Fail ("#1 InvalidOperation Exception Must be thrown if conn is closed");
- Assert.AreEqual ("", s, "#1a Should be an empty string");
- } catch (AssertionException e){
- throw e;
- } catch (Exception e){
- Assert.AreEqual(typeof (InvalidOperationException), e.GetType (),
- "#2 Incorrect Exception" + e.StackTrace);
+ Assert.Fail ("#A1:" + s);
+ } catch (InvalidOperationException ex) {
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
}
// Test if Release Version is as per specification.
conn.Open ();
- String[] version = conn.ServerVersion.Split ('.') ;
+ String [] version = conn.ServerVersion.Split ('.');
Assert.AreEqual (2, version[0].Length,
- "#2 The Major release shud be exactly 2 characters");
+ "#B1 The Major release shud be exactly 2 characters");
Assert.AreEqual (2, version[1].Length,
- "#3 The Minor release shud be exactly 2 characters");
+ "#B2 The Minor release shud be exactly 2 characters");
Assert.AreEqual (4, version[2].Length,
- "#4 The Release version should be exactly 4 digits");
+ "#B3 The Release version should be exactly 4 digits");
}
[Test]
- public void DatabasePropertyTest ()
+ public void Database ()
{
conn = new SqlConnection (connectionString);
- string database = conn.Database ;
+ string database = conn.Database;
// Test if database property is updated when a query changes database
conn.Open ();
- SqlCommand cmd = new SqlCommand ("use [mono-test]" , conn);
+ SqlCommand cmd = new SqlCommand ("use [master]" , conn);
cmd.ExecuteNonQuery ();
- Assert.AreEqual ("mono-test", conn.Database,
- "#1 DATABASE name shud change if query changes the db");
+ Assert.AreEqual ("master", conn.Database, "#1");
conn.Close ();
- Assert.AreEqual (database, conn.Database,
- "#2 Shud be back to default value");
+ Assert.AreEqual (database, conn.Database, "#2");
// Test if the database property is reset on re-opening the connection
conn.ConnectionString = connectionString;
- conn.Open ();
- Assert.AreEqual (database, conn.Database,
- "#3 Shud be back to original value");
+ conn.Open ();
+ Assert.AreEqual (database, conn.Database, "#3");
conn.Close ();
}
- [Test]
- public void StateChangeEventTest ()
+ [Test] // bug #412571
+ public void Dispose ()
{
- conn = new SqlConnection (connectionString);
- conn.StateChange += new StateChangeEventHandler (
- StateChangeHandlerTest1);
- using (conn) {
- conn.Open ();
- }
- Assert.AreEqual (2, stateChangeEventCount,
- "#1 The handler shud be called twice");
- stateChangeEventCount =0 ;
- conn.StateChange -= new StateChangeEventHandler (
- StateChangeHandlerTest1);
- // NOTE : Need to check the behavior if an exception is raised
- // in a handler
- }
+ Assert.Ignore ("bug #412571");
+
+ StateChangeEventArgs stateChangeArgs;
+ EventArgs disposedArgs;
- [Test]
- public void DisposedEventTest ()
- {
conn = new SqlConnection (connectionString);
- conn.Disposed += new EventHandler (DisposedEventHandlerTest1);
+ conn.Disposed += new EventHandler (Connection_Disposed);
+ conn.Open ();
+ conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
+ Assert.AreEqual (0, events.Count, "#A1");
+ conn.Dispose ();
+ Assert.AreEqual (ConnectionState.Closed, conn.State, "#A2");
+ Assert.AreEqual (2, events.Count, "#A3");
+
+ stateChangeArgs = events [0] as StateChangeEventArgs;
+ Assert.IsNotNull (stateChangeArgs, "#B1");
+ Assert.AreEqual (typeof (StateChangeEventArgs), stateChangeArgs.GetType (), "#B2");
+ Assert.AreEqual (ConnectionState.Open, stateChangeArgs.OriginalState, "#B3");
+ Assert.AreEqual (ConnectionState.Closed, stateChangeArgs.CurrentState, "B4");
+
+ disposedArgs = events [1] as EventArgs;
+ Assert.IsNotNull (disposedArgs, "#C1");
+ Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#C2");
+
conn.Dispose ();
- Assert.AreEqual (1, disposedEventCount,
- "#1 Disposed eventhandler shud be called");
+
+ Assert.AreEqual (ConnectionState.Closed, conn.State, "#D1");
+ Assert.AreEqual (3, events.Count, "#D2");
+
+ disposedArgs = events [2] as EventArgs;
+ Assert.IsNotNull (disposedArgs, "#E1");
+ Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#E2");
}
- void StateChangeHandlerTest1 (object sender , StateChangeEventArgs e)
+ void Connection_StateChange (object sender , StateChangeEventArgs e)
{
- Assert.IsTrue ((e.CurrentState != e.OriginalState),
- "#1 Current and Original state shud be different");
- Assert.AreEqual (e.CurrentState, conn.State,
- "The conn state and the arg received in event shud be same");
- stateChangeEventCount++ ;
+ events.Add (e);
}
- void DisposedEventHandlerTest1 (object sender , EventArgs e)
+ void Connection_Disposed (object sender , EventArgs e)
{
- disposedEventCount++;
+ events.Add (e);
}
-
+
#if NET_2_0
[Test]
public void FireInfoMessageEventOnUserErrorsTest ()
{
- conn = new SqlConnection ();
+ conn = new SqlConnection ();
Assert.AreEqual(false, conn.FireInfoMessageEventOnUserErrors, "#1 The default value should be false");
conn.FireInfoMessageEventOnUserErrors = true;
Assert.AreEqual(true, conn.FireInfoMessageEventOnUserErrors, "#1 The value should be true after setting the property to true");
connBuilder.Password = tmpPassword;
SqlConnection.ChangePassword (connBuilder.ConnectionString, oldPassword); // Modify to the original password
}
-
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ChangePasswordNullConnStringTest ()
- {
- conn = new SqlConnection (connectionString);
- SqlConnection.ChangePassword (null, "mono");
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ChangePasswordNullPasswordTest ()
- {
- conn = new SqlConnection (connectionString);
- SqlConnection.ChangePassword (connectionString, null);
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ChangePasswordEmptyPasswordTest ()
- {
- conn = new SqlConnection (connectionString);
- SqlConnection.ChangePassword (connectionString, "");
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ChangePasswordExceedPasswordTest ()
- {
- conn = new SqlConnection (connectionString);
- SqlConnection.ChangePassword (connectionString,"ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
- }
-#endif
+#endif
}
+
#if NET_2_0
[TestFixture]
[Category ("sqlserver")]
conn = new SqlConnection(connectionString);
conn.Open();
}
+
[TearDown]
public void TearDown()
{
conn.Close();
}
+
[Test]
public void GetSchemaTest1()
{
}
Assert.AreEqual(true, flag, "#GS1 failed");
}
+
[Test]
[ExpectedException(typeof(ArgumentException))]
public void GetSchemaTest2()
{
conn.GetSchema(null);
}
+
[Test]
- public void GetSchemaTest3()
+ public void GetSchemaTest3 ()
{
+ Assert.Ignore ("We currently have no foreign keys defined in the test database");
+
bool flag = false;
DataTable tab1 = conn.GetSchema("ForeignKeys");
foreach (DataRow row in tab1.Rows)
}
Assert.AreEqual(true, flag, "#GS3 failed");
}
+
[Test]
public void GetSchemaTest4()
{
}
Assert.AreEqual(true, flag, "#GS4 failed");
}
+
[Test]
public void GetSchemaTest5()
{
}
Assert.AreEqual(true, flag, "#GS5 failed");
}
+
[Test]
public void GetSchemaTest6()
{
}
Assert.AreEqual(true, flag, "#GS6 failed");
}
+
[Test]
public void GetSchemaTest7()
{
}
Assert.AreEqual(true, flag, "#GS7 failed");
}
+
[Test]
public void GetSchemaTest8()
{
}
Assert.AreEqual(true, flag, "#GS8 failed");
}
+
[Test]
public void GetSchemaTest9()
{
}
Assert.AreEqual(true, flag, "#GS9 failed");
}
+
+ [Test]
public void GetSchemaTest10()
{
bool flag = false;
}
Assert.AreEqual(true, flag, "#GS10 failed");
}
- public void GetSchemaTest11()
+
+ [Test]
+ public void GetSchemaTest11 ()
{
+ Assert.Ignore ("Incorrect syntax near 'TABLE_SCHEMA'");
+
bool flag = false;
DataTable tab1 = conn.GetSchema("Views");
flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
}
Assert.AreEqual(true, flag, "#GS11 failed");
}
- public void GetSchemaTest12()
+
+ [Test]
+ public void GetSchemaTest12 ()
{
+ Assert.Ignore ("Incorrect syntax near '('");
+
bool flag = false;
DataTable tab1 = conn.GetSchema("ViewColumns");
flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
}
Assert.AreEqual(true, flag, "#GS12 failed");
}
- public void GetSchemaTest13()
+
+ [Test]
+ public void GetSchemaTest13 ()
{
+ Assert.Ignore ("The multi-part identifier \"assportemblies.name\" could not be bound");
+
bool flag = false;
- DataTable tab1 = conn.GetSchema("UserDefineTypes");
+ DataTable tab1 = conn.GetSchema("UserDefinedTypes");
flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
foreach (DataRow row in tab1.Rows)
{
}
Assert.AreEqual(true, flag, "#GS13 failed");
}
+
[Test]
public void GetSchemaTest14()
{
}
Assert.AreEqual(true, flag, "#GS14 failed");
}
+
[Test]
public void GetSchemaTest15()
{
}
Assert.AreEqual(true, flag, "#GS15 failed");
}
+
[Test]
public void GetSchemaTest16()
{
}
Assert.AreEqual(true, flag, "#GS16 failed");
}
+
[Test]
public void GetSchemaTest17()
{
}
Assert.AreEqual(true, flag, "#GS17 failed");
}
+
[Test]
public void GetSchemaTest18()
{
}
Assert.AreEqual(true, flag, "#GS18 failed");
}
+
[Test]
[ExpectedException (typeof (ArgumentException))]
public void GetSchemaTest19 ()
String [] restrictions = new String[1];
conn.GetSchema("RESTRICTIONS", restrictions);
}
+
[Test]
public void GetSchemaTest20 ()
{
}
Assert.AreEqual(true, flag, "#GS20 failed");
}
+
[Test]
public void GetSchemaTest21()
{
}
Assert.AreEqual(true, flag, "#GS22 failed");
}
+
[Test]
public void GetSchemaTest23()
{
}
Assert.AreEqual(false, flag, "#GS23 failed");
}
+
[Test]
public void GetSchemaTest24()
{
}
Assert.AreEqual(false, flag, "#GS24 failed");
}
+
[Test]
[ExpectedException (typeof (ArgumentException))]
public void GetSchemaTest25 ()
namespace MonoTests.System.Data.SqlClient
{
-
[TestFixture]
[Category ("sqlserver")]
public class SqlTransactionTest
{
+ SqlConnection conn;
+ SqlTransaction trans;
+ String connectionString;
+
+ [SetUp]
+ public void SetUp ()
+ {
+ connectionString = ConnectionManager.Singleton.ConnectionString;
+ }
+
+ [TearDown]
+ public void TearDown ()
+ {
+ if (conn != null)
+ conn.Dispose ();
+ if (trans != null)
+ trans.Dispose ();
+ }
+
+ [Test]
+ public void Commit ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+
+ try {
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ using (trans = connA.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, connA, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#A1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#A2");
+ Assert.IsFalse (reader.Read (), "#A3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connB);
+ cmd.CommandTimeout = 2;
+ try {
+ cmd.ExecuteReader ();
+ Assert.Fail ("#B1");
+ } catch (SqlException ex) {
+ // Timeout expired. The timeout period
+ // elapsed prior to completion of the
+ // operation or the server is not responding
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
+#if NET_2_0
+ Assert.AreEqual ((byte) 11, ex.Class, "#B3");
+#else
+ Assert.AreEqual ((byte) 10, ex.Class, "#B3");
+#endif
+ Assert.IsNull (ex.InnerException, "#B4");
+ Assert.IsNotNull (ex.Message, "#B5");
+ Assert.AreEqual (-2, ex.Number, "#B6");
+ Assert.AreEqual ((byte) 0, ex.State, "#B7");
+ }
+
+ trans.Commit ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connB);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#C1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
+ Assert.IsFalse (reader.Read (), "#C3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#D1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
+ Assert.IsFalse (reader.Read (), "#D3");
+ }
+ }
+ } finally {
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Commit_Connection_Closed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ conn.Close ();
+
+ try {
+ trans.Commit ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B");
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Commit_Reader_Open ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlCommand cmd;
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ try {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ cmd = new SqlCommand ("select @@version", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ try {
+ trans.Commit ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // There is already an open DataReader
+ // associated with this Command which
+ // must be closed first
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#B1");
+ }
+
+ trans.Dispose ();
+ conn.Close ();
+
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#C1");
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Commit_Transaction_Committed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Commit ();
+
+ try {
+ trans.Commit ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#B1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
+ Assert.IsFalse (reader.Read (), "#B3");
+ }
+
+ conn.Close ();
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#C1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
+ Assert.IsFalse (reader.Read (), "#C3");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Commit_Transaction_Disposed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Dispose ();
+
+ try {
+ trans.Commit ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Commit_Transaction_Rolledback ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Rollback ();
+
+ try {
+ trans.Commit ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Rollback ("SAVE1");
+ trans.Commit ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#D1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
+ Assert.IsFalse (reader.Read (), "#D3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#E1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Connection_Transaction_Committed ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Commit ();
+
+ Assert.IsNull (trans.Connection);
+ }
+
+ [Test]
+ public void Connection_Transaction_Disposed ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Dispose ();
+
+ Assert.IsNull (trans.Connection);
+ }
+
+ [Test]
+ public void Connection_Transaction_Open ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ Assert.AreSame (conn, trans.Connection);
+ }
+
+ [Test]
+ public void Connection_Transaction_Rolledback ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ try {
+ trans.Rollback ();
+ Assert.IsNull (trans.Connection);
+ } finally {
+ trans.Dispose ();
+ }
+
+ trans = conn.BeginTransaction ();
+ trans.Save ("SAVE1");
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.AreSame (conn, trans.Connection);
+ } finally {
+ trans.Dispose ();
+ }
+ }
+
+ [Test]
+ public void Connection_Transaction_Saved ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Save ("SAVE1");
+
+ Assert.AreSame (conn, trans.Connection);
+ }
+
+ [Test]
+ public void Dispose ()
+ {
+ string sql;
+ SqlCommand cmd = null;
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Dispose ();
+ trans.Dispose ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#1");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#2");
+ }
+ } finally {
+ if (cmd != null)
+ cmd.Dispose ();
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Dispose_Connection_Closed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ conn.Close ();
+
+ trans.Dispose ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read ());
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
[Test]
- public void TransactionCommitTest ()
+ public void Dispose_Reader_Open ()
{
- SqlConnection conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
+ Assert.Ignore ("NotWorking");
+
try {
+ conn = new SqlConnection (connectionString);
conn.Open ();
- using (SqlTransaction transaction = conn.BeginTransaction())
- {
- string sSql = "Insert into employee (id, fname, dob, doj) Values (6005, 'NovellBangalore', '1989-02-11', '2005-07-22')";
- SqlCommand command = new SqlCommand();
- command.Connection = conn;
- command.CommandText = sSql;
- command.Transaction = transaction;
- command.CommandType = CommandType.Text;
- command.ExecuteNonQuery();
- transaction.Commit();
+
+ trans = conn.BeginTransaction ();
+
+ SqlCommand cmd = new SqlCommand ("select * from employee", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ try {
+ trans.Dispose ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // There is already an open DataReader
+ // associated with this Connection
+ // which must be closed first
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ try {
+ trans.Dispose ();
+ Assert.Fail ("#B1");
+ } catch (InvalidOperationException ex) {
+ // There is already an open DataReader
+ // associated with this Connection
+ // which must be closed first
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+ }
}
- } catch (SqlException ex) {
- // SqlException is thrown when running test-case for 1.0 and 2.0 profile
- // Violation of PRIMARY KEY constraint 'PK__employee__5DF5D7ED'.
- // Cannot insert duplicate key in object 'dbo.employee'.
+
+ trans.Dispose ();
} finally {
- DBHelper.ExecuteSimpleSP (conn, "sp_clean_person_table");
- if (conn != null && conn.State != ConnectionState.Closed)
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
conn.Close ();
}
+ }
+
+ [Test]
+ public void IsolationLevel_Reader_Open ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ try {
+ SqlCommand cmd = new SqlCommand ("select @@version", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel);
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ }
+ }
+
+ [Test]
+ public void IsolationLevel_Transaction_Committed ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Commit ();
+
+ try {
+ IsolationLevel iso = trans.IsolationLevel;
+ Assert.Fail ("#1:" + iso);
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
+ }
+
+ [Test]
+ public void IsolationLevel_Transaction_Disposed ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Dispose ();
+
+ try {
+ IsolationLevel iso = trans.IsolationLevel;
+ Assert.Fail ("#1:" + iso);
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
+ }
+
+ [Test]
+ public void IsolationLevel_Transaction_Open ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel);
+ }
+
+ [Test]
+ public void IsolationLevel_Transaction_Rolledback ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Rollback ();
+
+ try {
+ IsolationLevel iso = trans.IsolationLevel;
+ Assert.Fail ("#A1:" + iso);
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ } finally {
+ trans.Dispose ();
+ }
+ trans = conn.BeginTransaction ();
+ trans.Save ("SAVE1");
+ trans.Rollback ("SAVE1");
+
+ Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel, "#B1");
}
+ [Test]
+ public void IsolationLevel_Transaction_Saved ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Save ("SAVE1");
+ Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel);
+ }
+
+ [Test] // Rollback ()
+ public void Rollback1 ()
+ {
+ string sql;
+ SqlCommand cmd = null;
+ SqlConnection connA = null;
+ SqlConnection connB = null;
+
+ try {
+ connA = new SqlConnection (connectionString);
+ connA.Open ();
+
+ connB = new SqlConnection (connectionString);
+ connB.Open ();
+
+ using (trans = connA.BeginTransaction ()) {
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, connA, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#A1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#A2");
+ Assert.IsFalse (reader.Read (), "#A3");
+ }
+
+ trans.Rollback ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connB);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#C1");
+ }
+ }
+ } finally {
+ if (cmd != null)
+ cmd.Dispose ();
+ if (connA != null)
+ connA.Close ();
+ if (connB != null)
+ connB.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE2");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Rollback ();
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#D1");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#E1");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#F1");
+ }
+ } finally {
+ if (cmd != null)
+ cmd.Dispose ();
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+ trans.Rollback ();
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ }
+ }
+
+ [Test] // Rollback ()
+ public void Rollback1_Connection_Closed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ conn.Close ();
+
+ try {
+ trans.Rollback ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback ()
+ public void Rollback1_Reader_Open ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlCommand cmd;
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ try {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ cmd = new SqlCommand ("select @@version", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ try {
+ trans.Rollback ();
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // There is already an open DataReader
+ // associated with this Command which
+ // must be closed first
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ }
+ }
+
+ [Test] // Rollback ()
+ public void Rollback1_Transaction_Committed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Commit ();
+
+ try {
+ trans.Rollback ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#B1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
+ Assert.IsFalse (reader.Read (), "#B3");
+ }
+
+ conn.Close ();
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#C1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
+ Assert.IsFalse (reader.Read (), "#C3");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback ()
+ public void Rollback1_Transaction_Disposed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Dispose ();
+#if NET_2_0
+ trans.Rollback ();
+#else
+ try {
+ trans.Rollback ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+#endif
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback ()
+ public void Rollback1_Transaction_Rolledback ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Rollback ();
+
+ try {
+ trans.Rollback ();
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Rollback ("SAVE1");
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#C1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
+ Assert.IsFalse (reader.Read (), "#C3");
+ }
+
+ trans.Rollback ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#D1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2 ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ string sql;
+ SqlCommand cmd = null;
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE2");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE3");
+
+ trans.Rollback ("SAVE1");
+ trans.Commit ();
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#A1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#A2");
+ Assert.IsFalse (reader.Read (), "#A3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#C1");
+ }
+ } finally {
+ if (cmd != null)
+ cmd.Dispose ();
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE2");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ trans.Rollback ("SAVE1");
+ trans.Commit ();
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#D1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
+ Assert.IsFalse (reader.Read (), "#D3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#E1");
+ Assert.AreEqual ("BangaloreNovell", reader.GetString (0), "#E2");
+ Assert.IsFalse (reader.Read (), "#E3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#F1");
+ Assert.AreEqual ("Novell", reader.GetString (0), "#F2");
+ Assert.IsFalse (reader.Read (), "#F3");
+ }
+ } finally {
+ if (cmd != null)
+ cmd.Dispose ();
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_Connection_Closed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ conn.Close ();
+
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B");
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_Reader_Open ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlCommand cmd;
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ try {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ cmd = new SqlCommand ("select @@version", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // There is already an open DataReader
+ // associated with this Command which
+ // must be closed first
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_Transaction_Committed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Commit ();
+
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#B1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
+ Assert.IsFalse (reader.Read (), "#B3");
+ }
+
+ conn.Close ();
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#C1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
+ Assert.IsFalse (reader.Read (), "#C3");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_Transaction_Disposed ()
+ {
+#if NET_2_0
+ Assert.Ignore ("NotWorking");
+#endif
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Dispose ();
+
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_Transaction_Rolledback ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Rollback ();
+
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+ trans.Rollback ("SAVE1");
+
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.Fail ("#C1");
+ } catch (SqlException ex) {
+ // Cannot roll back SAVE1. No transaction
+ // or savepoint of that name was found
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#C2");
+ Assert.AreEqual ((byte) 16, ex.Class, "#C3");
+ Assert.IsNull (ex.InnerException, "#C4");
+ Assert.IsNotNull (ex.Message, "#C5");
+ Assert.IsTrue (ex.Message.IndexOf ("SAVE1") != -1, "#C6");
+ Assert.AreEqual (6401, ex.Number, "#C7");
+ Assert.AreEqual ((byte) 1, ex.State, "#C8");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#D1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_TransactionName_DoesNotExist ()
+ {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ try {
+ trans.Rollback ("SAVE1");
+ Assert.Fail ("#1");
+ } catch (SqlException ex) {
+ // Cannot roll back SAVE1. No transaction
+ // or savepoint of that name was found
+ Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
+ Assert.AreEqual ((byte) 16, ex.Class, "#3");
+ Assert.IsNull (ex.InnerException, "#4");
+ Assert.IsNotNull (ex.Message, "#5");
+ Assert.IsTrue (ex.Message.IndexOf ("SAVE1") != -1, "#6");
+ Assert.AreEqual (6401, ex.Number, "#7");
+ Assert.AreEqual ((byte) 1, ex.State, "#8");
+ }
+
+ trans.Commit ();
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_TransactionName_Empty ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ try {
+ trans.Rollback (string.Empty);
+ Assert.Fail ("#1");
+ } catch (ArgumentException ex) {
+ // Invalid transaction or invalid name
+ // for a point at which to save within
+ // the transaction
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName, "#5");
+ }
+
+ trans.Commit ();
+ }
+ }
+
+ [Test] // Rollback (String)
+ public void Rollback2_TransactionName_Null ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ try {
+ trans.Rollback ((string) null);
+ Assert.Fail ("#1");
+ } catch (ArgumentException ex) {
+ // Invalid transaction or invalid name
+ // for a point at which to save within
+ // the transaction
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName, "#5");
+ }
+
+ trans.Commit ();
+ }
+ }
+
+ [Test]
+ public void Save_Connection_Closed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ conn.Close ();
+
+ try {
+ trans.Save ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Save ()
+ {
+ string sql;
+ SqlCommand cmd = null;
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE2");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#A1");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#C1");
+ }
+ } finally {
+ if (cmd != null)
+ cmd.Dispose ();
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE2");
+
+ sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Commit ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#D1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
+ Assert.IsFalse (reader.Read (), "#D3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#E1");
+ Assert.AreEqual ("BangaloreNovell", reader.GetString (0), "#E2");
+ Assert.IsFalse (reader.Read (), "#E3");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#F1");
+ Assert.AreEqual ("Novell", reader.GetString (0), "#F2");
+ Assert.IsFalse (reader.Read (), "#F3");
+ }
+ } finally {
+ if (cmd != null)
+ cmd.Dispose ();
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Save_Reader_Open ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ SqlCommand cmd;
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ try {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ cmd = new SqlCommand ("select @@version", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ try {
+ trans.Save ("SAVE1");
+ Assert.Fail ("#1");
+ } catch (InvalidOperationException ex) {
+ // There is already an open DataReader
+ // associated with this Command which
+ // must be closed first
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ }
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ }
+ }
+
+ [Test]
+ public void Save_Transaction_Committed ()
+ {
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Commit ();
+
+ try {
+ trans.Save ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#B1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
+ Assert.IsFalse (reader.Read (), "#B3");
+ }
+
+ conn.Close ();
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#C1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
+ Assert.IsFalse (reader.Read (), "#C3");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Save_Transaction_Disposed ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Dispose ();
+
+ try {
+ trans.Save ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Save_Transaction_Rolledback ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Rollback ();
+
+ try {
+ trans.Save ("SAVE1");
+ Assert.Fail ("#A1");
+ } catch (InvalidOperationException ex) {
+ // This SqlTransaction has completed; it is no
+ // longer usable
+ Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ }
+
+ using (trans = conn.BeginTransaction ()) {
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ trans.Save ("SAVE1");
+ trans.Rollback ("SAVE1");
+ trans.Save ("SAVE1");
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#D1");
+ }
+ }
+ } finally {
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Save_TransactionName_Empty ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ try {
+ trans.Save (string.Empty);
+ Assert.Fail ("#A1");
+ } catch (ArgumentException ex) {
+ // Invalid transaction or invalid name
+ // for a point at which to save within
+ // the transaction
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ Assert.IsNull (ex.ParamName, "#A5");
+ }
+
+ trans.Rollback ();
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsFalse (reader.Read (), "#B1");
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
+
+ [Test]
+ public void Save_TransactionName_Null ()
+ {
+ Assert.Ignore ("NotWorking");
+
+ try {
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ trans = conn.BeginTransaction ();
+
+ string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
+ SqlCommand cmd = new SqlCommand (sql, conn, trans);
+ cmd.ExecuteNonQuery ();
+ cmd.Dispose ();
+
+ try {
+ trans.Save ((string) null);
+ Assert.Fail ("#A1");
+ } catch (ArgumentException ex) {
+ // Invalid transaction or invalid name
+ // for a point at which to save within
+ // the transaction
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ Assert.IsNull (ex.ParamName, "#A5");
+ }
+
+ trans.Commit ();
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+
+ cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
+ using (SqlDataReader reader = cmd.ExecuteReader ()) {
+ Assert.IsTrue (reader.Read (), "#B1");
+ Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
+ Assert.IsFalse (reader.Read (), "#B3");
+ }
+ } finally {
+ if (trans != null)
+ trans.Dispose ();
+ if (conn != null)
+ conn.Close ();
+
+ conn = new SqlConnection (connectionString);
+ conn.Open ();
+ DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
+ }
+ }
}
}