* DataReaderTest.cs: Added tests for GetChars, GetOrdinal,
[mono.git] / mcs / class / System.Data / Test / ProviderTests / ProviderIndependant / DbDataReaderTest.cs
1 // DbDataReaderTest.cs - NUnit Test Cases for testing the
2 // DbDataReader family of classes
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (c) 2008 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person
10 // obtaining a copy of this software and associated documentation
11 // files (the "Software"), to deal in the Software without
12 // restriction, including without limitation the rights to use, copy,
13 // modify, merge, publish, distribute, sublicense, and/or sell copies
14 // of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to 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
24 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 // SOFTWARE.
28
29 #if NET_2_0
30
31 using System;
32 using System.Data;
33 using System.Data.Common;
34 using System.Data.Odbc;
35 using System.Data.SqlClient;
36 using System.Data.SqlTypes;
37 using System.Globalization;
38
39 using Mono.Data;
40
41 using NUnit.Framework;
42
43 namespace MonoTests.System.Data
44 {
45         [TestFixture]
46         [Category ("odbc")]
47         [Category ("sqlserver")]
48         public class DbDataReaderTest
49         {
50                 DbConnection conn;
51                 DbCommand cmd;
52                 DbDataReader rdr;
53
54                 [SetUp]
55                 public void SetUp ()
56                 {
57                         conn = ConnectionManager.Singleton.Connection;
58                         ConnectionManager.Singleton.OpenConnection ();
59                         cmd = conn.CreateCommand ();
60                 }
61
62                 [TearDown]
63                 public void TearDown ()
64                 {
65                         if (cmd != null)
66                                 cmd.Dispose ();
67                         if (rdr != null)
68                                 rdr.Dispose ();
69                         ConnectionManager.Singleton.CloseConnection ();
70                 }
71
72                 [Test]
73                 public void GetProviderSpecificValues_Reader_Closed ()
74                 {
75                         cmd.CommandText = "SELECT * FROM employee";
76                         rdr = cmd.ExecuteReader ();
77                         rdr.Close ();
78
79                         try {
80                                 rdr.GetProviderSpecificValues (null);
81                                 Assert.Fail ("#1");
82                         } catch (InvalidOperationException ex) {
83                                 // Invalid attempt to call MetaData
84                                 // when reader is closed
85                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
86                                 Assert.IsNull (ex.InnerException, "#3");
87                                 Assert.IsNotNull (ex.Message, "#4");
88                         }
89                 }
90
91                 [Test]
92                 public void GetProviderSpecificValues_Reader_NoData ()
93                 {
94                         cmd.CommandText = "SELECT * FROM employee where id = 6666";
95                         rdr = cmd.ExecuteReader ();
96
97                         try {
98                                 rdr.GetProviderSpecificValues (null);
99                                 Assert.Fail ("#A1");
100                         } catch (InvalidOperationException ex) {
101                                 // Invalid attempt to read when no data
102                                 // is present
103                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
104                                 Assert.IsNull (ex.InnerException, "#A3");
105                                 Assert.IsNotNull (ex.Message, "#A4");
106                         }
107
108                         Assert.IsFalse (rdr.Read (), "B");
109
110                         try {
111                                 rdr.GetProviderSpecificValues (null);
112                                 Assert.Fail ("#C1");
113                         } catch (InvalidOperationException ex) {
114                                 // Invalid attempt to read when no data
115                                 // is present
116                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
117                                 Assert.IsNull (ex.InnerException, "#C3");
118                                 Assert.IsNotNull (ex.Message, "#C4");
119                         }
120                 }
121         }
122 }
123
124 #endif // NET_2_0