Merge pull request #1513 from akoeplinger/remove-net20-ifdefs
[mono.git] / mcs / class / System.Data / Test / System.Data.OleDb / OleDbConnectionTest.cs
1 //
2 // OleDbConnectionTest.cs - NUnit Test Cases for testing the
3 //                          OleDbConnectionTest class
4 // Author:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (c) 2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Data;
31 using System.Data.OleDb;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Data.OleDb
36 {
37         [TestFixture]
38         public class OleDbConnectionTest
39         {
40                 const string CONNECTION_STRING = "Provider=sqloledb;Data Source=SQLSRV;Initial Catalog=Mono;";
41
42                 [Test] // OleDbConnection ()
43                 public void Constructor1 ()
44                 {
45                         OleDbConnection cn = new OleDbConnection ();
46
47                         Assert.AreEqual (string.Empty, cn.ConnectionString, "#1");
48                         Assert.AreEqual (15, cn.ConnectionTimeout, "#2");
49                         Assert.IsNull (cn.Container, "#3");
50                         Assert.AreEqual (string.Empty, cn.Database, "#4");
51                         Assert.AreEqual (string.Empty, cn.DataSource, "#5");
52                         Assert.AreEqual (string.Empty, cn.Provider, "#6");
53                         Assert.IsNull (cn.Site, "#7");
54                         Assert.AreEqual (ConnectionState.Closed, cn.State, "#8");
55                 }
56
57                 [Test] // OleDbConnection (string)
58                 public void Constructor2 ()
59                 {
60                         OleDbConnection cn = new OleDbConnection (CONNECTION_STRING);
61                         Assert.AreEqual (CONNECTION_STRING, cn.ConnectionString, "#A1");
62                         Assert.AreEqual (15, cn.ConnectionTimeout, "#A2");
63                         Assert.IsNull (cn.Container, "#A3");
64                         //Assert.AreEqual ("Mono", cn.Database, "#A4");
65                         //Assert.AreEqual ("SQLSRV", cn.DataSource, "#A5");
66                         //Assert.AreEqual ("sqloledb", cn.Provider, "#A6");
67                         Assert.IsNull (cn.Site, "#A7");
68                         Assert.AreEqual (ConnectionState.Closed, cn.State, "#A8");
69
70                         cn = new OleDbConnection ((string) null);
71                         Assert.AreEqual (string.Empty, cn.ConnectionString, "#B1");
72                         Assert.AreEqual (15, cn.ConnectionTimeout, "#B2");
73                         Assert.IsNull (cn.Container, "#B3");
74                         Assert.AreEqual (string.Empty, cn.Database, "#B4");
75                         Assert.AreEqual (string.Empty, cn.DataSource, "#B5");
76                         Assert.AreEqual (string.Empty, cn.Provider, "#B6");
77                         Assert.IsNull (cn.Site, "#B7");
78                         Assert.AreEqual (ConnectionState.Closed, cn.State, "#B8");
79                 }
80
81                 [Test]
82                 public void BeginTransaction_Connection_Closed ()
83                 {
84                         OleDbConnection cn = new OleDbConnection ();
85
86                         try {
87                                 cn.BeginTransaction ();
88                                 Assert.Fail ("#A1");
89                         } catch (InvalidOperationException ex) {
90                                 // Invalid operation. The connection is closed
91                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
92                                 Assert.IsNull (ex.InnerException, "#A3");
93                                 Assert.IsNotNull (ex.Message, "#A4");
94                         }
95
96                         try {
97                                 cn.BeginTransaction ((IsolationLevel) 666);
98                                 Assert.Fail ("#B1");
99                         } catch (InvalidOperationException ex) {
100                                 // Invalid operation. The connection is closed
101                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
102                                 Assert.IsNull (ex.InnerException, "#B3");
103                                 Assert.IsNotNull (ex.Message, "#B4");
104                         }
105
106                         try {
107                                 cn.BeginTransaction (IsolationLevel.Serializable);
108                                 Assert.Fail ("#C1");
109                         } catch (InvalidOperationException ex) {
110                                 // Invalid operation. The connection is closed
111                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
112                                 Assert.IsNull (ex.InnerException, "#C3");
113                                 Assert.IsNotNull (ex.Message, "#C4");
114                         }
115                 }
116
117                 [Test]
118                 public void ConnectionString ()
119                 {
120                         OleDbConnection cn = new OleDbConnection ();
121                         cn.ConnectionString = CONNECTION_STRING;
122                         Assert.AreEqual (CONNECTION_STRING, cn.ConnectionString, "#1");
123                         cn.ConnectionString = null;
124                         Assert.AreEqual (string.Empty, cn.ConnectionString, "#2");
125                         cn.ConnectionString = CONNECTION_STRING;
126                         Assert.AreEqual (CONNECTION_STRING, cn.ConnectionString, "#3");
127                         cn.ConnectionString = string.Empty;
128                         Assert.AreEqual (string.Empty, cn.ConnectionString, "#4");
129                 }
130
131                 [Test]
132                 public void GetSchema_Connection_Closed ()
133                 {
134                         OleDbConnection cn = new OleDbConnection ();
135
136                         try {
137                                 cn.GetSchema ();
138                                 Assert.Fail ("#A1");
139                         } catch (InvalidOperationException ex) {
140                                 // Invalid operation. The connection is closed
141                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
142                                 Assert.IsNull (ex.InnerException, "#B3");
143                                 Assert.IsNotNull (ex.Message, "#B4");
144                         }
145
146                         try {
147                                 cn.GetSchema ("Tables");
148                                 Assert.Fail ("#B1");
149                         } catch (InvalidOperationException ex) {
150                                 // Invalid operation. The connection is closed
151                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
152                                 Assert.IsNull (ex.InnerException, "#B3");
153                                 Assert.IsNotNull (ex.Message, "#B4");
154                         }
155
156                         try {
157                                 cn.GetSchema ((string) null);
158                                 Assert.Fail ("#C1");
159                         } catch (InvalidOperationException ex) {
160                                 // Invalid operation. The connection is closed
161                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
162                                 Assert.IsNull (ex.InnerException, "#C3");
163                                 Assert.IsNotNull (ex.Message, "#C4");
164                         }
165
166                         try {
167                                 cn.GetSchema ("Tables", new string [] { "master" });
168                                 Assert.Fail ("#D1");
169                         } catch (InvalidOperationException ex) {
170                                 // Invalid operation. The connection is closed
171                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
172                                 Assert.IsNull (ex.InnerException, "#D3");
173                                 Assert.IsNotNull (ex.Message, "#D4");
174                         }
175
176                         try {
177                                 cn.GetSchema ((string) null, new string [] { "master" });
178                                 Assert.Fail ("#E1");
179                         } catch (InvalidOperationException ex) {
180                                 // Invalid operation. The connection is closed
181                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
182                                 Assert.IsNull (ex.InnerException, "#E3");
183                                 Assert.IsNotNull (ex.Message, "#E4");
184                         }
185
186                         try {
187                                 cn.GetSchema ("Tables", (string []) null);
188                                 Assert.Fail ("#F1");
189                         } catch (InvalidOperationException ex) {
190                                 // Invalid operation. The connection is closed
191                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
192                                 Assert.IsNull (ex.InnerException, "#F3");
193                                 Assert.IsNotNull (ex.Message, "#F4");
194                         }
195
196                         try {
197                                 cn.GetSchema ((string) null, (string []) null);
198                                 Assert.Fail ("#G1");
199                         } catch (InvalidOperationException ex) {
200                                 // Invalid operation. The connection is closed
201                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#G2");
202                                 Assert.IsNull (ex.InnerException, "#G3");
203                                 Assert.IsNotNull (ex.Message, "#G4");
204                         }
205                 }
206
207                 [Test]
208                 public void ServerVersion_Connection_Closed ()
209                 {
210                         OleDbConnection cn = new OleDbConnection ();
211                         try {
212                                 Assert.Fail ("#A1:" + cn.ServerVersion);
213                         } catch (InvalidOperationException ex) {
214                                 // Invalid operation. The connection is closed
215                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
216                                 Assert.IsNull (ex.InnerException, "#A3");
217                                 Assert.IsNotNull (ex.Message, "#A4");
218                         }
219
220                         cn = new OleDbConnection (CONNECTION_STRING);
221                         try {
222                                 Assert.Fail ("#B1:" + cn.ServerVersion);
223                         } catch (InvalidOperationException ex) {
224                                 // Invalid operation. The connection is closed
225                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
226                                 Assert.IsNull (ex.InnerException, "#B3");
227                                 Assert.IsNotNull (ex.Message, "#B4");
228                         }
229                 }
230         }
231 }