Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mcs / class / System.Data / Test / ProviderTests / ProviderIndependant / IDbCommandTest.cs
1 // IDbCommandTest.cs - NUnit Test Cases for testing the
2 // IDbCommand implemented classes.
3 //
4 // Authors:
5 //      Sureshkumar T (tsureshkumar@novell.com)
6 // 
7 // Copyright (c) 2004 Novell Inc., and the individuals listed on the
8 // ChangeLog entries.
9 //
10 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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;
30
31 using System;
32 using System.Data;
33 using System.Data.Common;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Data.Connected
37 {
38         [TestFixture]
39         [Category ("odbc"), Category ("sqlserver")]
40         public class CommandTest
41         {
42                 IDbConnection conn;
43                 IDbCommand cmd;
44
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         conn = ConnectionManager.Instance.Sql.Connection;
49                         cmd = conn.CreateCommand ();
50                 }
51
52                 [TearDown]
53                 public void TearDown ()
54                 {
55                         if (cmd != null)
56                                 cmd.Dispose ();
57                         ConnectionManager.Instance.Close ();
58                 }
59
60                 [Test]
61                 public void ExecuteNonQuery_CommandText_Empty ()
62                 {
63                         try {
64                                 cmd.ExecuteNonQuery ();
65                                 Assert.Fail ("#A1");
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);
73                         }
74
75                         cmd.CommandText = string.Empty;
76
77                         try {
78                                 cmd.ExecuteNonQuery ();
79                                 Assert.Fail ("#B1");
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);
87                         }
88
89                         cmd.CommandText = null;
90
91                         try {
92                                 cmd.ExecuteNonQuery ();
93                                 Assert.Fail ("#C1");
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);
101                         }
102                 }
103
104                 [Test]
105                 public void ExecuteReader_CommandText_Empty ()
106                 {
107                         try {
108                                 cmd.ExecuteReader ();
109                                 Assert.Fail ("#A1");
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);
117                         }
118
119                         cmd.CommandText = string.Empty;
120
121                         try {
122                                 cmd.ExecuteReader ();
123                                 Assert.Fail ("#B1");
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);
131                         }
132
133                         cmd.CommandText = null;
134
135                         try {
136                                 cmd.ExecuteReader ();
137                                 Assert.Fail ("#C1");
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);
145                         }
146                 }
147
148                 [Test] // bug #462947
149                 public void ExecuteReader_Connection_Reuse ()
150                 {
151                         cmd.CommandText = "SELECT type_blob FROM binary_family where id = 1";
152
153                         CommandBehavior behavior = CommandBehavior.SequentialAccess |
154                                 CommandBehavior.SingleResult;
155
156                         using (IDataReader reader = cmd.ExecuteReader (behavior)) {
157                                 Assert.IsTrue (reader.Read (), "#A1");
158
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");
164                         }
165
166                         ConnectionManager.Instance.Sql.CloseConnection ();
167                         conn = ConnectionManager.Instance.Sql.Connection;
168
169                         using (IDataReader reader = cmd.ExecuteReader (behavior)) {
170                                 Assert.IsTrue (reader.Read (), "#B1");
171
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");
177                         }
178
179                         using (IDataReader reader = cmd.ExecuteReader (behavior)) {
180                                 Assert.IsTrue (reader.Read (), "#C");
181                         }
182
183                         ConnectionManager.Instance.Sql.CloseConnection ();
184                         conn = ConnectionManager.Instance.Sql.Connection;
185
186
187                         using (IDataReader reader = cmd.ExecuteReader (behavior)) {
188                                 Assert.IsTrue (reader.Read (), "#D");
189                         }
190
191                         using (IDataReader reader = cmd.ExecuteReader (behavior)) {
192                                 Assert.IsTrue (reader.Read (), "#E");
193                         }
194                 }
195
196                 [Test]
197                 public void ExecuteScalar ()
198                 {
199                         cmd.CommandText = "select count(*) from employee where id < 3";
200                         Assert.AreEqual (2, (int) Convert.ChangeType (cmd.ExecuteScalar (),
201                                                                       typeof (int)),
202                                          "#1");
203                         cmd.Dispose ();
204
205                         cmd = conn.CreateCommand ();
206                         cmd.CommandText = "select id from employee where id = 666";
207                         Assert.IsNull (cmd.ExecuteScalar (), "#2");
208                 }
209         }
210 }