Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DbDataReaderTest.cs
1 // DbDataReaderTest.cs - NUnit Test Cases for DbDataReader class
2 //
3 // Author:
4 //      Mika Aalto (mika@aalto.pro)
5 //
6 // Copyright (C) 2014 Mika Aalto
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27
28
29 using NUnit.Framework;
30 using System;
31 using System.Data;
32 using System.Data.Common;
33 using System.IO;
34
35 namespace MonoTests.System.Data.Common
36 {
37         [TestFixture]
38         public class DbDataReaderTest
39         {
40                 DbDataReaderMock dataReader;
41
42                 [SetUp]
43                 public void SetUp ()
44                 {
45                         //Setup test data table
46                         DataTable testData = new DataTable ();
47                         testData.Columns.Add ("text_col", typeof(string));
48                         testData.Columns.Add ("binary_col", typeof(byte[]));
49
50                         testData.Rows.Add ("row_1", new byte[] { 0xde, 0xad, 0xbe, 0xef });
51                         testData.Rows.Add ("row_2", DBNull.Value);
52                         testData.Rows.Add ("row_3", new byte[] { 0x00 });
53
54                         dataReader = new DbDataReaderMock (testData);
55
56                         Assert.AreEqual (3, testData.Rows.Count);
57                 }
58
59                 [TearDown]
60                 public void TearDown ()
61                 {
62                 }
63
64                 [Test]
65                 public void GetFieldValueTest ()
66                 {
67                         //First row
68                         dataReader.Read ();
69                         Assert.AreEqual ("row_1", dataReader.GetFieldValue<string> (0), "#1");
70                         byte[] expected_data = new byte[] { 0xde, 0xad, 0xbe, 0xef };
71                         byte[] actual_data = dataReader.GetFieldValue<byte[]> (1);
72                         Assert.AreEqual (expected_data.Length, actual_data.Length, "#2");
73                         for (int i = 0; i < expected_data.Length; i++) {
74                                 Assert.AreEqual (expected_data [i], actual_data [i], "#3 at index " + i);
75                         }
76
77                         //Second row where data row column value is DBNull
78                         dataReader.Read ();
79                         Assert.AreEqual ("row_2", dataReader.GetFieldValue<string> (0), "#4");
80                         try {
81                                 actual_data = dataReader.GetFieldValue<byte[]> (1);
82                                 Assert.Fail ("GetFieldValue method should throw InvalidCastException for DBNull values #5");
83                         } catch (InvalidCastException) {
84                                 //This is expected
85                         }
86
87                         //Third row
88                         dataReader.Read ();
89                         Assert.AreEqual ("row_3", dataReader.GetFieldValue<string> (0), "#6");
90                         expected_data = new byte[] { 0x00 };
91                         actual_data = dataReader.GetFieldValue<byte[]> (1);
92                         Assert.AreEqual (expected_data.Length, actual_data.Length, "#7");
93                         Assert.AreEqual (expected_data [0], actual_data [0], "#8");
94                 }
95
96                 [Test]
97                 public void GetStreamTest ()
98                 {
99                         int testColOrdinal = 1;
100                         byte[] buffer = new byte[1024];
101
102                         dataReader.Read ();
103                         Stream stream = dataReader.GetStream (testColOrdinal);
104                         Assert.IsNotNull (stream, "Stream from datareader is null #1");
105
106                         //Read stream content to byte buffer
107                         int data_length = stream.Read (buffer, 0, buffer.Length);
108
109                         //Verify that content is expected
110                         byte[] expected = new byte[] { 0xde, 0xad, 0xbe, 0xef };
111                         Assert.AreEqual (expected.Length, data_length, "#2");
112                         for (int i = 0; i < expected.Length; i++) {
113                                 Assert.AreEqual (expected [i], buffer [i], "#3 at index " + i);
114                         }
115
116                         //Get DBNull value stream
117                         Assert.IsTrue (dataReader.Read ());
118                         stream = dataReader.GetStream (testColOrdinal);
119                         Assert.AreEqual (0, stream.Length, "#4");
120
121                         //Get single byte value stream
122                         Assert.IsTrue (dataReader.Read ());
123                         stream = dataReader.GetStream (testColOrdinal);
124                         expected = new byte[] { 0x00 };
125                         Assert.AreEqual (expected.Length, stream.Length, "#5");
126                         Assert.AreEqual (expected [0], stream.ReadByte (), "#6");
127                 }
128
129                 [Test]
130                 public void GetTextReader ()
131                 {
132                         int testColOrdinal = 0;
133
134                         //Read first row
135                         dataReader.Read ();
136                         TextReader textReader = dataReader.GetTextReader (testColOrdinal);
137                         Assert.IsNotNull (textReader, "return value from datareader GetTextReader method is null #1");
138
139                         string txt = textReader.ReadToEnd ();
140                         Assert.AreEqual ("row_1", txt, "#2");
141
142                         //Move to second row
143                         Assert.IsTrue (dataReader.Read ());
144                         textReader = dataReader.GetTextReader (testColOrdinal);
145                         txt = textReader.ReadToEnd ();
146                         Assert.AreEqual ("row_2", txt, "#3");
147
148                         //Move to third row
149                         Assert.IsTrue (dataReader.Read ());
150                         textReader = dataReader.GetTextReader (testColOrdinal);
151                         txt = textReader.ReadToEnd ();
152                         Assert.AreEqual ("row_3", txt, "#4");
153
154                         Assert.IsFalse (dataReader.Read (), "#5");
155                 }
156
157         }
158 }
159