2 // SqlBytesTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlBytes
5 // Nagappan A (anagappan@novell.com)
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
35 using System.Data.SqlTypes;
36 using System.Threading;
37 using System.Globalization;
39 namespace MonoTests.System.Data.SqlTypes
42 public class SqlBytesTest
47 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
52 public void SqlBytesItem ()
54 SqlBytes bytes = new SqlBytes ();
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");
62 bytes = new SqlBytes (b);
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");
70 bytes = new SqlBytes (b);
71 Assert.AreEqual (bytes [0], 0, "");
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");
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");
86 public void SqlBytesLength ()
89 SqlBytes bytes = new SqlBytes ();
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");
96 bytes = new SqlBytes (b);
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");
104 bytes = new SqlBytes (b);
105 Assert.AreEqual (bytes.Length, 10, "#3 Should be 10");
108 public void SqlBytesMaxLength ()
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");
116 bytes = new SqlBytes (b);
117 Assert.AreEqual (bytes.MaxLength, 10, "#3 Should return 10");
120 public void SqlBytesNull ()
123 SqlBytes bytes = SqlBytes.Null;
124 Assert.AreEqual (bytes.IsNull, true, "#1 Should return true");
127 public void SqlBytesStorage ()
130 SqlBytes bytes = new SqlBytes ();
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");
137 bytes = new SqlBytes (b);
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");
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);
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");
157 public void SqlBytesValue ()
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");
164 Assert.AreEqual (b1 [0], 0, "#2 Should be same");
165 Assert.AreEqual (b2 [0], 10, "#3 Should be same");
168 public void SqlBytesSetLength ()
170 byte [] b1 = new byte [10];
171 SqlBytes bytes = new SqlBytes ();
173 bytes.SetLength (20);
174 Assert.Fail ("Should throw SqlTypeException");
175 } catch (Exception ex) {
176 Assert.AreEqual (typeof (SqlTypeException), ex.GetType (), "Should throw SqlTypeException");
178 bytes = new SqlBytes (b1);
179 Assert.AreEqual (bytes.Length, 10, "#1 Should be same");
181 bytes.SetLength (-1);
182 Assert.Fail ("Should throw ArgumentOutOfRangeException");
183 } catch (Exception ex) {
184 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
187 bytes.SetLength (11);
188 Assert.Fail ("Should throw ArgumentOutOfRangeException");
189 } catch (Exception ex) {
190 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
193 Assert.AreEqual (bytes.Length, 2, "#2 Should be same");
196 public void SqlBytesSetNull ()
198 byte [] b1 = new byte [10];
199 SqlBytes bytes = new SqlBytes (b1);
200 Assert.AreEqual (bytes.Length, 10, "#1 Should be same");
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");
208 Assert.AreEqual (true, bytes.IsNull, "#2 Should be same");
211 public void GetXsdTypeTest ()
213 XmlQualifiedName qualifiedName = SqlBytes.GetXsdType (null);
214 NUnit.Framework.Assert.AreEqual ("base64Binary", qualifiedName.Name, "#A01");
219 public void Read_SuccessTest1 ()
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];
225 bytes.Read (0, b2, 0, (int) bytes.Length);
226 Assert.AreEqual (bytes.Value [5], b2 [5], "#1 Should be equal");
230 [ExpectedException (typeof (ArgumentNullException))]
231 public void Read_NullBufferTest ()
233 byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
234 SqlBytes bytes = new SqlBytes (b1);
237 bytes.Read (0, b2, 0, 10);
238 Assert.Fail ("#2 Should throw ArgumentNullException");
242 [ExpectedException (typeof (ArgumentOutOfRangeException))]
243 public void Read_InvalidCountTest1 ()
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];
249 bytes.Read (0, b2, 0, 10);
250 Assert.Fail ("#3 Should throw ArgumentOutOfRangeException");
254 [ExpectedException (typeof (ArgumentOutOfRangeException))]
255 public void Read_NegativeOffsetTest ()
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];
261 bytes.Read (-1, b2, 0, 4);
262 Assert.Fail ("#4 Should throw ArgumentOutOfRangeException");
266 [ExpectedException (typeof (ArgumentOutOfRangeException))]
267 public void Read_NegativeOffsetInBufferTest ()
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];
273 bytes.Read (0, b2, -1, 4);
274 Assert.Fail ("#5 Should throw ArgumentOutOfRangeException");
278 [ExpectedException (typeof (ArgumentOutOfRangeException))]
279 public void Read_InvalidOffsetInBufferTest ()
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];
285 bytes.Read (0, b2, 8, 4);
286 Assert.Fail ("#6 Should throw ArgumentOutOfRangeException");
290 [ExpectedException (typeof (SqlNullValueException))]
291 public void Read_NullInstanceValueTest ()
293 byte [] b2 = new byte [5];
294 SqlBytes bytes = new SqlBytes ();
296 bytes.Read (0, b2, 8, 4);
297 Assert.Fail ("#7 Should throw SqlNullValueException");
301 public void Read_SuccessTest2 ()
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];
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");
313 [ExpectedException (typeof (SqlNullValueException))]
314 public void Read_NullBufferAndInstanceValueTest ()
317 SqlBytes bytes = new SqlBytes ();
319 bytes.Read (0, b2, 8, 4);
323 [ExpectedException (typeof (ArgumentOutOfRangeException))]
324 public void Read_NegativeCountTest ()
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];
330 bytes.Read (0, b2, 0, -1);
331 Assert.Fail ("#11 Should throw ArgumentOutOfRangeException");
335 [ExpectedException (typeof (ArgumentOutOfRangeException))]
336 public void Read_InvalidCountTest2 ()
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];
342 bytes.Read (0, b2, 3, 4);
343 Assert.Fail ("#12 Should throw ArgumentOutOfRangeException");
348 public void Write_SuccessTest1 ()
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);
354 bytes.Write (0, b1, 0, (int) b1.Length);
355 Assert.AreEqual (bytes.Value [0], b1 [0], "#1 Should be same");
359 [ExpectedException (typeof (ArgumentOutOfRangeException))]
360 public void Write_NegativeOffsetTest ()
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);
366 bytes.Write (-1, b1, 0, (int) b1.Length);
367 Assert.Fail ("#2 Should throw ArgumentOutOfRangeException");
371 [ExpectedException (typeof (SqlTypeException))]
372 public void Write_InvalidOffsetTest ()
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);
378 bytes.Write (bytes.Length+5, b1, 0, (int) b1.Length);
379 Assert.Fail ("#3 Should throw SqlTypeException");
383 [ExpectedException (typeof (ArgumentOutOfRangeException))]
384 public void Write_NegativeOffsetInBufferTest ()
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);
390 bytes.Write (0, b1, -1, (int) b1.Length);
391 Assert.Fail ("#4 Should throw ArgumentOutOfRangeException");
395 [ExpectedException (typeof (ArgumentOutOfRangeException))]
396 public void Write_InvalidOffsetInBufferTest ()
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);
402 bytes.Write (0, b1, b1.Length+5, (int) b1.Length);
403 Assert.Fail ("#5 Should throw ArgumentOutOfRangeException");
407 [ExpectedException (typeof (ArgumentOutOfRangeException))]
408 public void Write_InvalidCountTest1 ()
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);
414 bytes.Write (0, b1, 0, (int) b1.Length+5);
415 Assert.Fail ("#6 Should throw ArgumentOutOfRangeException");
419 [ExpectedException (typeof (SqlTypeException))]
420 public void Write_InvalidCountTest2 ()
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);
426 bytes.Write (8, b1, 0, (int) b1.Length);
427 Assert.Fail ("#7 Should throw SqlTypeException");
431 [ExpectedException (typeof (ArgumentNullException))]
432 public void Write_NullBufferTest ()
434 byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
436 SqlBytes bytes = new SqlBytes (b1);
438 bytes.Write (0, b2, 0, 10);
439 Assert.Fail ("#8 Should throw ArgumentNullException");
443 [ExpectedException (typeof (SqlTypeException))]
444 public void Write_NullInstanceValueTest ()
446 byte [] b1 = { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42 };
447 SqlBytes bytes = new SqlBytes();
449 bytes.Write (0, b1, 0, 10);
450 Assert.Fail ("#9 Should throw SqlTypeException");
454 [ExpectedException (typeof (ArgumentNullException))]
455 public void Write_NullBufferAndInstanceValueTest ()
458 SqlBytes bytes = new SqlBytes();
460 bytes.Write (0, b1, 0, 10);
461 Assert.Fail ("#9 Should throw ArgumentNullException");
465 public void Write_SuccessTest2 ()
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);
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");
477 [ExpectedException (typeof (ArgumentOutOfRangeException))]
478 public void Write_NegativeCountTest ()
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);
484 bytes.Write (0, b1, 0, -1);
485 Assert.Fail ("#11 Should throw ArgumentOutOfRangeException");