Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / System.Data.OracleClient / Test / System.Data.OracleClient / OracleLobTest.cs
1 //
2 // OracleParameterTest.cs -
3 //      NUnit Test Cases for OracleLob
4 //
5 // Author:
6 //      Leszek Ciesielski  <skolima@gmail.com>
7 //
8 // Copyright (C) 2006 Forcom (http://www.forcom.com.pl/)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System.Configuration;
32 using System.Globalization;
33 using System.Threading;
34 using System.Data.OracleClient;
35 using System.Data;
36 using System.IO;
37 using System;
38
39 namespace MonoTests.System.Data.OracleClient {
40
41         [TestFixture]
42         public class OracleLobTest {
43
44                 String connection_string;
45                 OracleConnection connection;
46                 OracleCommand command;
47
48                 [TestFixtureSetUp]
49                 public void FixtureSetUp ()
50                 {
51                         connection_string = Environment.GetEnvironmentVariable ("MONO_TESTS_ORACLE_CONNECTION_STRING");
52                         if(connection_string == null)
53                                 Assert.Ignore ("Please consult README.tests.");
54                 }
55
56                 [SetUp]
57                 public void SetUp ()
58                 {
59                         connection = new OracleConnection (connection_string);
60                         connection.Open ();
61
62                         using (command = connection.CreateCommand ()) {
63                                 // create the tables
64                                 command.CommandText =
65                                                 "create table lob_test (id number(10), lobo blob)";
66                                 command.ExecuteNonQuery ();
67                         }
68                 }
69
70                 [TearDown]
71                 public void TearDown ()
72                 {
73                         using (command = connection.CreateCommand ()) {
74                                 command.CommandText = "drop table lob_test";
75                                 command.ExecuteNonQuery ();
76                         }
77
78                         connection.Close ();
79                         connection.Dispose ();
80                 }
81
82                 [Test] // regression for bug #78898
83                 public void PositionIs0BasedTest ()
84                 {
85                         using (command = connection.CreateCommand ()) { // reusing command from SetUp causes parameter names mismatch
86                                 // insert test values
87                                 command.CommandText =
88                                                 "insert into lob_test (id, lobo) values (11, '00000000000000000000000000000')";
89                                 command.ExecuteNonQuery ();
90
91                                 // select for writing test values
92                                 command.CommandText =
93                                                 "select lobo from lob_test where id = 11 for update";
94
95                                 using (OracleDataReader reader = command.ExecuteReader ()) {
96                                         if (reader.Read ()) {
97                                                 OracleLob lob = reader.GetOracleLob (0);
98                                                 Assert.AreEqual (0, lob.Position, "Lob index is not 0 - based.");
99                                                 lob.Seek (1, SeekOrigin.Current);
100                                                 Assert.AreEqual (1, lob.Position, "Lob seek placed position wrongly.");
101                                                 lob.Seek (0, SeekOrigin.End);
102                                                 Assert.AreEqual (lob.Length , lob.Position, "Lob end is too far away.");
103                                                 TrySeek (lob, -lob.Length, SeekOrigin.End, 0, 1);
104                                                 TrySeek (lob, -lob.Length + 5, SeekOrigin.End, 5, 2);
105                                                 try {
106                                                         lob.Seek (5, SeekOrigin.End);
107                                                         Assert.Fail ("Illegal seek succeeded.");
108                                                 }
109                                                 catch (ArgumentOutOfRangeException) { // exception is required
110                                                 }
111                                                 lob.Seek (lob.Length - 5, SeekOrigin.Begin);
112                                                 Assert.AreEqual (lob.Length - 5 , lob.Position, "Lob position has unexpected value.");
113                                                 lob.Seek (0, SeekOrigin.Begin);
114                                                 TryRead (lob, 10, 1);
115                                                 lob.Seek (5, SeekOrigin.Begin);
116                                                 lob.Position = 0;
117                                                 TryRead (lob, 10, 2);
118                                                 lob.Position = lob.Length;
119                                                 TryRead (lob, 0, 3);
120                                                 lob.Seek (-1, SeekOrigin.Current);
121                                                 TryRead (lob, 1, 4);
122                                         }
123                                         else {
124                                                 Assert.Fail ("Expected records not found.");
125                                         }
126                                 }
127                         }
128                 }
129
130                 void TrySeek(OracleLob lob, long offset, SeekOrigin start, long expected, int id)
131                 {
132                         try {
133                                 lob.Seek (offset, start);
134                                 Assert.AreEqual (expected, lob.Position, "Lob position was unexpected [" + id + ']');
135                         }
136                         catch (ArgumentOutOfRangeException) {
137                                 Assert.Fail ("Unable to perform a legal seek [" + id + ']');
138                         }
139                 }
140
141                 void TryRead(OracleLob lob, long expectedCount, int id)
142                 {
143                         try {
144                                 long numberRead = lob.Read (new byte [10], 0, 10);
145                                 Assert.AreEqual (expectedCount, numberRead, "Wrong number of bytes read [" + id + ']');
146                         }
147                         catch (OracleException e) {
148                                 if (e.Code == 24801)
149                                         Assert.Fail ("Unable to perform a legal read [" + id + ']');
150                                 else throw;
151                         }
152                 }
153         }
154 }