63dee0c8b61050665e8798b105fb1c9c7d35fb13
[mono.git] / mcs / class / System.Data / Test / ProviderTests / System.Data.OleDb.jvm / OleDbDataReader / OleDbDataReader_GetValue.cs
1 // 
2 // Copyright (c) 2006 Mainsoft Co.
3 // 
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23
24 using System;
25 using System.Data;
26 using System.Data.OleDb;
27
28 using MonoTests.System.Data.Utils;
29
30 using MonoTests.System.Data.Utils.Data;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Data.OleDb
35 {
36         [TestFixture]
37         public class OleDbDataReader_GetValue : ADONetTesterClass
38         {
39                 Exception exp;
40                 OleDbConnection con;
41                 char [] Result;
42
43                 [SetUp]
44                 public void SetUp()
45                 {
46                                 base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
47
48                                 con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
49                                 Result = new char[100];
50                                 con.Open();
51                 }
52
53                 [TearDown]
54                 public void TearDown()
55                 {
56                         if (con.State == ConnectionState.Open) con.Close();
57                 }
58
59                 public static void Main()
60                 {
61                         OleDbDataReader_GetValue tc = new OleDbDataReader_GetValue();
62                         tc.exp = null;
63                         try
64                         {
65                                 tc.BeginTest("OleDbDataReader_GetValue");
66                                 tc.SetUp();
67                                 tc.run();
68                         }
69                         catch (Exception ex)
70                         {
71                                 tc.exp = ex;
72                         }
73                         finally 
74                         {
75                                 tc.EndTest(tc.exp);
76                                 tc.TearDown();
77                         }
78                 }
79
80                 [Test]
81                 public void run()
82                 {
83                         TypesTests(ConnectedDataProvider.GetSimpleDbTypesParameters());
84                         TypesTests(ConnectedDataProvider.GetExtendedDbTypesParameters());
85                 }
86
87                 [Test]
88                 public void SimpleTest()
89                 {
90                         OleDbDataReader rdr = null;
91                         exp = null;
92                         try
93                         {
94                                 BeginCase("check value");
95                                 
96                                 OleDbCommand cmd = new OleDbCommand("Select LastName From Employees Where EmployeeID = 100", con);
97                                 rdr = cmd.ExecuteReader();
98                                 rdr.Read();
99
100                                 Object obj = rdr.GetValue(0); 
101                                 Compare(obj.ToString(), "Last100");
102                         } 
103                         catch (Exception ex)
104                         {
105                                 exp = ex;
106                         }
107                         finally
108                         {
109                                 if (rdr != null)
110                                 {
111                                         rdr.Close();
112                                 }
113                                 EndCase(exp);
114                                 exp = null;
115                         }
116                 }
117                 private void TypesTests(DbTypeParametersCollection typesToTest)
118                 {
119                         exp = null;
120
121                         DbTypeParametersCollection currentlyTested = new DbTypeParametersCollection(typesToTest.TableName);
122                         string rowId = "13289";
123                         object dbValue;
124                         OleDbDataReader rdr = null;
125                         OleDbConnection selectCon = null;
126                         DataBaseServer testedDbServer = ConnectedDataProvider.GetDbType();
127
128                         foreach (DbTypeParameter currentParamType in typesToTest)
129                         {
130                                 BeginCase("Test value of db type: " + currentParamType.DbTypeName);
131                                 //Prepare data:
132                                 rowId = string.Format("13289_{0}", this.TestCaseNumber);
133                                 currentlyTested.Clear();
134                                 currentlyTested.Add(currentParamType);
135                                 currentlyTested.ExecuteInsert(rowId);
136
137                                 try
138                                 {
139                                         currentlyTested.ExecuteSelectReader(rowId, out rdr, out selectCon);
140                                         rdr.Read();
141                                         dbValue = WorkaroundOracleCharsPaddingLimitation(testedDbServer, currentParamType, rdr.GetValue(0));
142                                         if (currentParamType.Value.GetType().IsArray)
143                                         {
144                                                 Compare(dbValue as Array, currentParamType.Value as Array);
145                                         }
146                                         else
147                                         {
148                                                 Compare(dbValue, currentParamType.Value);
149                                         }
150                                 } 
151                                 catch(Exception ex)
152                                 {
153                                         exp = ex;
154                                 }
155                                 finally
156                                 {
157                                         EndCase(exp);
158                                         exp = null;
159                                         if (rdr != null && !rdr.IsClosed)
160                                         {
161                                                 rdr.Close();
162                                         }
163                                         if (selectCon != null && selectCon.State != ConnectionState.Closed)
164                                         {
165                                                 selectCon.Close();
166                                         }
167                                         currentlyTested.ExecuteDelete(rowId);
168                                 }
169                         }
170                 }
171                 /// <summary>
172                 /// This is a workaround for the extra white spaces added in oracle to NCHAR & NVARCHAR values.
173                 /// The problem is a documented GH limitation, see bug #3417.
174                 /// The workaround is to trim the lemgth of the returned string to the specified length of the parameter/column.
175                 /// </summary>
176                 /// <param name="testedServer">The database server we are currently running on.</param>
177                 /// <param name="val">The value returned from the database.</param>
178                 /// <returns>The normalized value..</returns>
179                 private object WorkaroundOracleCharsPaddingLimitation(DataBaseServer testedServer, DbTypeParameter currentParam, object val)
180                 {
181                         object origVal = val;
182                         string dbTypeName = currentParam.DbTypeName.ToUpper();
183                         if ( (testedServer == DataBaseServer.Oracle) && (dbTypeName == "CHAR" || dbTypeName == "NCHAR") )
184                         {
185                                 val = ((string)val).Substring(0, currentParam.Size);
186                                 Log(string.Format("Worked around oracle chars padding limitation by triming '{0}' to '{1}'", origVal, val));
187                         }
188                         return val;
189                 }
190
191
192         }
193 }