[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Data / Test / System.Data.SqlTypes / SqlBytesTest.cs
1 //
2 // SqlBytesTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlBytes
3 //
4 // Authors:
5 //   Nagappan A (anagappan@novell.com)
6 //
7 //
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Xml;
35 using System.Data.SqlTypes;
36 using System.Threading;
37 using System.Globalization;
38
39 namespace MonoTests.System.Data.SqlTypes
40 {
41         [TestFixture]
42         public class SqlBytesTest
43         {
44                 [SetUp]
45                 public void SetUp ()
46                 {
47                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
48                 }
49
50                 // Test constructor
51                 [Test]
52                 public void SqlBytesItem ()
53                 {
54                         SqlBytes bytes = new SqlBytes ();
55                         try {
56                                 Assert.AreEqual (bytes [0], 0, "#1 Should throw SqlNullValueException");
57                                 Assert.Fail ("Should throw SqlNullValueException");
58                         } catch (Exception ex) {
59                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
60                         }
61                         byte [] b = null;
62                         bytes = new SqlBytes (b);
63                         try {
64                                 Assert.AreEqual (bytes [0], 0, "#2 Should throw SqlNullValueException");
65                                 Assert.Fail ("Should throw SqlNullValueException");
66                         } catch (Exception ex) {
67                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
68                         }
69                         b = new byte [10];
70                         bytes = new SqlBytes (b);
71                         Assert.AreEqual (bytes [0], 0, "");
72                         try {
73                                 Assert.AreEqual (bytes [-1], 0, "");
74                                 Assert.Fail ("Should throw ArgumentOutOfRangeException");
75                         } catch (Exception ex) {
76                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
77                         }
78                         try {
79                                 Assert.AreEqual (bytes [10], 0, "");
80                                 Assert.Fail ("Should throw ArgumentOutOfRangeException");
81                         } catch (Exception ex) {
82                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
83                         }
84                 }
85                 [Test]
86                 public void SqlBytesLength ()
87                 {
88                         byte [] b = null;
89                         SqlBytes bytes = new SqlBytes ();
90                         try {
91                                 Assert.AreEqual (bytes.Length, 0, "#1 Should throw SqlNullValueException");
92                                 Assert.Fail ("Should throw SqlNullValueException");
93                         } catch (Exception ex) {
94                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
95                         }
96                         bytes = new SqlBytes (b);
97                         try {
98                                 Assert.AreEqual (bytes.Length, 0, "#2 Should throw SqlNullValueException");
99                                 Assert.Fail ("Should throw SqlNullValueException");
100                         } catch (Exception ex) {
101                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
102                         }
103                         b = new byte [10];
104                         bytes = new SqlBytes (b);
105                         Assert.AreEqual (bytes.Length, 10, "#3 Should be 10");
106                 }
107                 [Test]
108                 public void SqlBytesMaxLength ()
109                 {
110                         byte [] b = null;
111                         SqlBytes bytes = new SqlBytes ();
112                         Assert.AreEqual (bytes.MaxLength, -1, "#1 Should return -1");
113                         bytes = new SqlBytes (b);
114                         Assert.AreEqual (bytes.MaxLength, -1, "#2 Should return -1");
115                         b = new byte [10];
116                         bytes = new SqlBytes (b);
117                         Assert.AreEqual (bytes.MaxLength, 10, "#3 Should return 10");
118                 }
119                 [Test]
120                 public void SqlBytesNull ()
121                 {
122                         byte [] b = null;
123                         SqlBytes bytes = SqlBytes.Null;
124                         Assert.AreEqual (bytes.IsNull, true, "#1 Should return true");
125                 }
126                 [Test]
127                 public void SqlBytesStorage ()
128                 {
129                         byte [] b = null;
130                         SqlBytes bytes = new SqlBytes ();
131                         try {
132                                 Assert.AreEqual (bytes.Storage, StorageState.Buffer, "#1 Should throw SqlNullValueException");
133                                 Assert.Fail ("Should throw SqlNullValueException");
134                         } catch (Exception ex) {
135                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
136                         }
137                         bytes = new SqlBytes (b);
138                         try {
139                                 Assert.AreEqual (bytes.Storage, StorageState.Buffer, "#2 Should throw SqlNullValueException");
140                                 Assert.Fail ("Should throw SqlNullValueException");
141                         } catch (Exception ex) {
142                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
143                         }
144                         b = new byte [10];
145                         bytes = new SqlBytes (b);
146                         Assert.AreEqual (bytes.Storage, StorageState.Buffer, "#3 Should be StorageState.Buffer");
147                         FileStream fs = null;
148                         bytes = new SqlBytes (fs);
149                         try {
150                                 Assert.AreEqual (bytes.Storage, StorageState.Buffer, "#4 Should throw SqlNullValueException");
151                                 Assert.Fail ("Should throw SqlNullValueException");
152                         } catch (Exception ex) {
153                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
154                         }
155                 }
156                 [Test]
157                 public void SqlBytesValue ()
158                 {
159                         byte [] b1 = new byte [10];
160                         SqlBytes bytes = new SqlBytes (b1);
161                         byte [] b2 = bytes.Value;
162                         Assert.AreEqual (b1 [0], b2 [0], "#1 Should be same");
163                         b2 [0] = 10;
164                         Assert.AreEqual (b1 [0], 0, "#2 Should be same");
165                         Assert.AreEqual (b2 [0], 10, "#3 Should be same");
166                 }
167                 [Test]
168                 public void SqlBytesSetLength ()
169                 {
170                         byte [] b1 = new byte [10];
171                         SqlBytes bytes = new SqlBytes ();
172                         try {
173                                 bytes.SetLength (20);
174                                 Assert.Fail ("Should throw SqlTypeException");
175                         } catch (Exception ex) {
176                                 Assert.AreEqual (typeof (SqlTypeException), ex.GetType (), "Should throw SqlTypeException");
177                         }
178                         bytes = new SqlBytes (b1);
179                         Assert.AreEqual (bytes.Length, 10, "#1 Should be same");
180                         try {
181                                 bytes.SetLength (-1);
182                                 Assert.Fail ("Should throw ArgumentOutOfRangeException");
183                         } catch (Exception ex) {
184                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
185                         }
186                         try {
187                                 bytes.SetLength (11);
188                                 Assert.Fail ("Should throw ArgumentOutOfRangeException");
189                         } catch (Exception ex) {
190                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
191                         }
192                         bytes.SetLength (2);
193                         Assert.AreEqual (bytes.Length, 2, "#2 Should be same");
194                 }
195                 [Test]
196                 public void SqlBytesSetNull ()
197                 {
198                         byte [] b1 = new byte [10];
199                         SqlBytes bytes = new SqlBytes (b1);
200                         Assert.AreEqual (bytes.Length, 10, "#1 Should be same");
201                         bytes.SetNull ();
202                         try {
203                                 Assert.AreEqual (bytes.Length, 10, "#1 Should not be same");
204                                 Assert.Fail ("Should throw SqlNullValueException");
205                         } catch (Exception ex) {
206                                 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
207                         }
208                         Assert.AreEqual (true, bytes.IsNull, "#2 Should be same");
209                 }
210                 [Test]
211                 public void GetXsdTypeTest ()
212                 {
213                         XmlQualifiedName qualifiedName = SqlBytes.GetXsdType (null);
214                         NUnit.Framework.Assert.AreEqual ("base64Binary", qualifiedName.Name, "#A01");
215                 }
216
217                 /* Read tests */
218                 [Test]
219                 public void Read_SuccessTest1 ()
220                 {
221                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
222                         SqlBytes bytes = new SqlBytes (b1);
223                         byte [] b2 = new byte [10];
224
225                         bytes.Read (0, b2, 0, (int) bytes.Length);
226                         Assert.AreEqual (bytes.Value [5], b2 [5], "#1 Should be equal");
227                 }
228                 
229                 [Test]
230                 [ExpectedException (typeof (ArgumentNullException))]
231                 public void Read_NullBufferTest ()
232                 {                       
233                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
234                         SqlBytes bytes = new SqlBytes (b1);                     
235                         byte [] b2 = null;
236                         
237                         bytes.Read (0, b2, 0, 10);
238                         Assert.Fail ("#2 Should throw ArgumentNullException");
239                 }
240                 
241                 [Test]
242                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
243                 public void Read_InvalidCountTest1 ()
244                 {                       
245                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
246                         SqlBytes bytes = new SqlBytes (b1);                     
247                         byte [] b2 = new byte [5]; 
248                         
249                         bytes.Read (0, b2, 0, 10);
250                         Assert.Fail ("#3 Should throw ArgumentOutOfRangeException");
251                 }
252                 
253                 [Test]
254                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
255                 public void Read_NegativeOffsetTest ()
256                 {                       
257                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
258                         SqlBytes bytes = new SqlBytes (b1);                     
259                         byte [] b2 = new byte [5];
260                         
261                         bytes.Read (-1, b2, 0, 4);
262                         Assert.Fail ("#4 Should throw ArgumentOutOfRangeException");
263                 }
264                 
265                 [Test]
266                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
267                 public void Read_NegativeOffsetInBufferTest ()
268                 {                       
269                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
270                         SqlBytes bytes = new SqlBytes (b1);                     
271                         byte [] b2 = new byte [5];
272                         
273                         bytes.Read (0, b2, -1, 4);
274                         Assert.Fail ("#5 Should throw ArgumentOutOfRangeException");
275                 }
276
277                 [Test]
278                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
279                 public void Read_InvalidOffsetInBufferTest ()
280                 {                       
281                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
282                         SqlBytes bytes = new SqlBytes (b1);                     
283                         byte [] b2 = new byte [5];
284                         
285                         bytes.Read (0, b2, 8, 4);
286                         Assert.Fail ("#6 Should throw ArgumentOutOfRangeException");
287                 }
288                 
289                 [Test]
290                 [ExpectedException (typeof (SqlNullValueException))]
291                 public void Read_NullInstanceValueTest ()
292                 {                       
293                         byte [] b2 = new byte [5];
294                         SqlBytes bytes = new SqlBytes ();
295                         
296                         bytes.Read (0, b2, 8, 4);
297                         Assert.Fail ("#7 Should throw SqlNullValueException");
298                 }
299                 
300                 [Test]
301                 public void Read_SuccessTest2 ()
302                 {
303                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };                        
304                         SqlBytes bytes = new SqlBytes (b1);
305                         byte [] b2 = new byte [10];
306                         
307                         bytes.Read (5, b2, 0, 10);
308                         Assert.AreEqual (bytes.Value [5], b2 [0], "#8 Should be same");
309                         Assert.AreEqual (bytes.Value [9], b2 [4], "#9 Should be same");
310                 }
311                 
312                 [Test]
313                 [ExpectedException (typeof (SqlNullValueException))]
314                 public void Read_NullBufferAndInstanceValueTest ()
315                 {                       
316                         byte [] b2 = null;
317                         SqlBytes bytes = new SqlBytes ();
318                         
319                         bytes.Read (0, b2, 8, 4);
320                 }
321                 
322                 [Test]
323                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
324                 public void Read_NegativeCountTest ()
325                 {                       
326                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
327                         SqlBytes bytes = new SqlBytes (b1);                     
328                         byte [] b2 = new byte [5];
329                         
330                         bytes.Read (0, b2, 0, -1);
331                         Assert.Fail ("#11 Should throw ArgumentOutOfRangeException");
332                 }
333                 
334                 [Test]
335                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
336                 public void Read_InvalidCountTest2 ()
337                 {                       
338                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
339                         SqlBytes bytes = new SqlBytes (b1);                     
340                         byte [] b2 = new byte [5]; 
341                         
342                         bytes.Read (0, b2, 3, 4);
343                         Assert.Fail ("#12 Should throw ArgumentOutOfRangeException");
344                 }
345                 
346                 /* Write Tests */
347                 [Test]
348                 public void Write_SuccessTest1 ()
349                 {
350                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
351                         byte [] b2 = new byte[10];
352                         SqlBytes bytes = new SqlBytes (b2);
353                         
354                         bytes.Write (0, b1, 0, (int) b1.Length);
355                         Assert.AreEqual (bytes.Value [0], b1 [0], "#1 Should be same");
356                 }
357                 
358                 [Test]
359                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
360                 public void Write_NegativeOffsetTest ()
361                 {
362                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
363                         byte [] b2 = new byte [10];
364                         SqlBytes bytes = new SqlBytes (b2);
365                         
366                         bytes.Write (-1, b1, 0, (int) b1.Length);
367                         Assert.Fail ("#2 Should throw ArgumentOutOfRangeException");
368                 }
369                 
370                 [Test]
371                 [ExpectedException (typeof (SqlTypeException))]
372                 public void Write_InvalidOffsetTest ()
373                 {
374                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
375                         byte [] b2 = new byte [10];
376                         SqlBytes bytes = new SqlBytes (b2);
377                         
378                         bytes.Write (bytes.Length+5, b1, 0, (int) b1.Length);
379                         Assert.Fail ("#3 Should throw SqlTypeException");
380                 }
381                 
382                 [Test]
383                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
384                 public void Write_NegativeOffsetInBufferTest ()
385                 {
386                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
387                         byte [] b2 = new byte [10];
388                         SqlBytes bytes = new SqlBytes (b2);
389                         
390                         bytes.Write (0, b1, -1, (int) b1.Length);
391                         Assert.Fail ("#4 Should throw ArgumentOutOfRangeException");
392                 }
393                 
394                 [Test]
395                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
396                 public void Write_InvalidOffsetInBufferTest ()
397                 {
398                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
399                         byte [] b2 = new byte [10];
400                         SqlBytes bytes = new SqlBytes (b2);
401                         
402                         bytes.Write (0, b1, b1.Length+5, (int) b1.Length);
403                         Assert.Fail ("#5 Should throw ArgumentOutOfRangeException");
404                 }
405                 
406                 [Test]
407                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
408                 public void Write_InvalidCountTest1 ()
409                 {
410                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
411                         byte [] b2 = new byte [10];
412                         SqlBytes bytes = new SqlBytes (b2);
413                         
414                         bytes.Write (0, b1, 0, (int) b1.Length+5);
415                         Assert.Fail ("#6 Should throw ArgumentOutOfRangeException");
416                 }
417                 
418                 [Test]
419                 [ExpectedException (typeof (SqlTypeException))]
420                 public void Write_InvalidCountTest2 ()
421                 {
422                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
423                         byte [] b2 = new byte [10];
424                         SqlBytes bytes = new SqlBytes (b2);
425                         
426                         bytes.Write (8, b1, 0, (int) b1.Length);
427                         Assert.Fail ("#7 Should throw SqlTypeException");
428                 }
429                 
430                 [Test]
431                 [ExpectedException (typeof (ArgumentNullException))]
432                 public void Write_NullBufferTest ()
433                 {
434                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
435                         byte [] b2 = null;
436                         SqlBytes bytes = new SqlBytes (b1);
437                         
438                         bytes.Write (0, b2, 0, 10);
439                         Assert.Fail ("#8 Should throw ArgumentNullException");
440                 }
441                 
442                 [Test]
443                 [ExpectedException (typeof (SqlTypeException))]
444                 public void Write_NullInstanceValueTest ()
445                 {
446                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
447                         SqlBytes bytes = new SqlBytes();
448                         
449                         bytes.Write (0, b1, 0, 10);
450                         Assert.Fail ("#9 Should throw SqlTypeException");
451                 }
452                 
453                 [Test]
454                 [ExpectedException (typeof (ArgumentNullException))]
455                 public void Write_NullBufferAndInstanceValueTest ()
456                 {
457                         byte [] b1 = null;
458                         SqlBytes bytes = new SqlBytes();
459                         
460                         bytes.Write (0, b1, 0, 10);
461                         Assert.Fail ("#9 Should throw ArgumentNullException");
462                 }
463                 
464                 [Test]
465                 public void Write_SuccessTest2 ()
466                 {
467                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
468                         byte [] b2 = new byte [20];
469                         SqlBytes bytes = new SqlBytes (b2);
470                         
471                         bytes.Write (8, b1, 0, 10);
472                         Assert.AreEqual (bytes.Value [8], b1 [0], "#10 Should be same");
473                         Assert.AreEqual (bytes.Value [17], b1 [9], "#10 Should be same");
474                 }
475                 
476                 [Test]
477                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
478                 public void Write_NegativeCountTest ()
479                 {
480                         byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
481                         byte [] b2 = new byte [10];
482                         SqlBytes bytes = new SqlBytes (b2);
483                         
484                         bytes.Write (0, b1, 0, -1);
485                         Assert.Fail ("#11 Should throw ArgumentOutOfRangeException");
486                 }
487         }
488 }