1 // IDbCommandTest.cs - NUnit Test Cases for testing the
2 // IDbCommand implemented classes.
5 // Sureshkumar T (tsureshkumar@novell.com)
7 // Copyright (c) 2004 Novell Inc., and the individuals listed on the
11 // Permission is hereby granted, free of charge, to any person
12 // obtaining a copy of this software and associated documentation
13 // files (the "Software"), to deal in the Software without
14 // restriction, including without limitation the rights to use, copy,
15 // modify, merge, publish, distribute, sublicense, and/or sell copies
16 // of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 // SOFTWARE.using System;
33 using System.Data.Common;
34 using NUnit.Framework;
36 namespace MonoTests.System.Data.Connected
39 [Category ("odbc"), Category ("sqlserver")]
40 public class CommandTest
48 conn = ConnectionManager.Instance.Sql.Connection;
49 cmd = conn.CreateCommand ();
53 public void TearDown ()
57 ConnectionManager.Instance.Close ();
61 public void ExecuteNonQuery_CommandText_Empty ()
64 cmd.ExecuteNonQuery ();
66 } catch (InvalidOperationException ex) {
67 // ExecuteNonQuery: CommandText property
68 // has not been initialized
69 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
70 Assert.IsNull (ex.InnerException, "#A3");
71 Assert.IsNotNull (ex.Message, "#A4");
72 Assert.IsTrue (ex.Message.StartsWith ("ExecuteNonQuery"), "#A5:" + ex.Message);
75 cmd.CommandText = string.Empty;
78 cmd.ExecuteNonQuery ();
80 } catch (InvalidOperationException ex) {
81 // ExecuteNonQuery: CommandText property
82 // has not been initialized
83 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
84 Assert.IsNull (ex.InnerException, "#B3");
85 Assert.IsNotNull (ex.Message, "#B4");
86 Assert.IsTrue (ex.Message.StartsWith ("ExecuteNonQuery"), "#B5:" + ex.Message);
89 cmd.CommandText = null;
92 cmd.ExecuteNonQuery ();
94 } catch (InvalidOperationException ex) {
95 // ExecuteNonQuery: CommandText property
96 // has not been initialized
97 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
98 Assert.IsNull (ex.InnerException, "#C3");
99 Assert.IsNotNull (ex.Message, "#C4");
100 Assert.IsTrue (ex.Message.StartsWith ("ExecuteNonQuery"), "#C5:" + ex.Message);
105 public void ExecuteReader_CommandText_Empty ()
108 cmd.ExecuteReader ();
110 } catch (InvalidOperationException ex) {
111 // ExecuteReader: CommandText property
112 // has not been initialized
113 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
114 Assert.IsNull (ex.InnerException, "#A3");
115 Assert.IsNotNull (ex.Message, "#A4");
116 Assert.IsTrue (ex.Message.StartsWith ("ExecuteReader"), "#A5:" + ex.Message);
119 cmd.CommandText = string.Empty;
122 cmd.ExecuteReader ();
124 } catch (InvalidOperationException ex) {
125 // ExecuteReader: CommandText property
126 // has not been initialized
127 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
128 Assert.IsNull (ex.InnerException, "#B3");
129 Assert.IsNotNull (ex.Message, "#B4");
130 Assert.IsTrue (ex.Message.StartsWith ("ExecuteReader"), "#B5:" + ex.Message);
133 cmd.CommandText = null;
136 cmd.ExecuteReader ();
138 } catch (InvalidOperationException ex) {
139 // ExecuteReader: CommandText property
140 // has not been initialized
141 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
142 Assert.IsNull (ex.InnerException, "#C3");
143 Assert.IsNotNull (ex.Message, "#C4");
144 Assert.IsTrue (ex.Message.StartsWith ("ExecuteReader"), "#C5:" + ex.Message);
148 [Test] // bug #462947
149 public void ExecuteReader_Connection_Reuse ()
151 cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
153 CommandBehavior behavior = CommandBehavior.SequentialAccess |
154 CommandBehavior.SingleResult;
156 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
157 Assert.IsTrue (reader.Read (), "#A1");
159 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
160 byte [] val = new byte [totalsize];
161 long ret = reader.GetBytes (0, 0, val, 0, val.Length);
162 Assert.AreEqual (5, ret, "#A2");
163 Assert.AreEqual (new byte [] { 0x32, 0x56, 0x00, 0x44, 0x22 }, val, "#A3");
166 ConnectionManager.Instance.Sql.CloseConnection ();
167 conn = ConnectionManager.Instance.Sql.Connection;
169 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
170 Assert.IsTrue (reader.Read (), "#B1");
172 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
173 byte [] val = new byte [totalsize];
174 long ret = reader.GetBytes (0, 0, val, 0, val.Length);
175 Assert.AreEqual (5, ret, "#B2");
176 Assert.AreEqual (new byte [] { 0x32, 0x56, 0x00, 0x44, 0x22 }, val, "#B3");
179 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
180 Assert.IsTrue (reader.Read (), "#C");
183 ConnectionManager.Instance.Sql.CloseConnection ();
184 conn = ConnectionManager.Instance.Sql.Connection;
187 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
188 Assert.IsTrue (reader.Read (), "#D");
191 using (IDataReader reader = cmd.ExecuteReader (behavior)) {
192 Assert.IsTrue (reader.Read (), "#E");
197 public void ExecuteScalar ()
199 cmd.CommandText = "select count(*) from employee where id < 3";
200 Assert.AreEqual (2, (int) Convert.ChangeType (cmd.ExecuteScalar (),
205 cmd = conn.CreateCommand ();
206 cmd.CommandText = "select id from employee where id = 666";
207 Assert.IsNull (cmd.ExecuteScalar (), "#2");