2 // SqlCharsTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlChars
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.
32 using NUnit.Framework;
36 using System.Data.SqlTypes;
37 using System.Threading;
38 using System.Globalization;
41 using System.Xml.Serialization;
45 namespace MonoTests.System.Data.SqlTypes
48 public class SqlCharsTest
53 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
58 public void SqlCharsItem ()
60 SqlChars chars = new SqlChars ();
62 Assert.AreEqual (chars [0], 0, "#1 Should throw SqlNullValueException");
63 Assert.Fail ("Should throw SqlNullValueException");
64 } catch (Exception ex) {
65 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
68 chars = new SqlChars (b);
70 Assert.AreEqual (chars [0], 0, "#2 Should throw SqlNullValueException");
71 Assert.Fail ("Should throw SqlNullValueException");
72 } catch (Exception ex) {
73 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
76 chars = new SqlChars (b);
77 Assert.AreEqual (chars [0], 0, "");
79 Assert.AreEqual (chars [-1], 0, "");
80 Assert.Fail ("Should throw ArgumentOutOfRangeException");
81 } catch (Exception ex) {
82 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
85 Assert.AreEqual (chars [10], 0, "");
86 Assert.Fail ("Should throw ArgumentOutOfRangeException");
87 } catch (Exception ex) {
88 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
92 public void SqlCharsLength ()
95 SqlChars chars = new SqlChars ();
97 Assert.AreEqual (chars.Length, 0, "#1 Should throw SqlNullValueException");
98 Assert.Fail ("Should throw SqlNullValueException");
99 } catch (Exception ex) {
100 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
102 chars = new SqlChars (b);
104 Assert.AreEqual (chars.Length, 0, "#2 Should throw SqlNullValueException");
105 Assert.Fail ("Should throw SqlNullValueException");
106 } catch (Exception ex) {
107 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
110 chars = new SqlChars (b);
111 Assert.AreEqual (chars.Length, 10, "#3 Should be 10");
114 public void SqlCharsMaxLength ()
117 SqlChars chars = new SqlChars ();
118 Assert.AreEqual (chars.MaxLength, -1, "#1 Should return -1");
119 chars = new SqlChars (b);
120 Assert.AreEqual (chars.MaxLength, -1, "#2 Should return -1");
122 chars = new SqlChars (b);
123 Assert.AreEqual (chars.MaxLength, 10, "#3 Should return 10");
126 public void SqlCharsNull ()
129 SqlChars chars = SqlChars.Null;
130 Assert.AreEqual (chars.IsNull, true, "#1 Should return true");
133 public void SqlCharsStorage ()
136 SqlChars chars = new SqlChars ();
138 Assert.AreEqual (chars.Storage, StorageState.Buffer, "#1 Should throw SqlNullValueException");
139 Assert.Fail ("Should throw SqlNullValueException");
140 } catch (Exception ex) {
141 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
143 chars = new SqlChars (b);
145 Assert.AreEqual (chars.Storage, StorageState.Buffer, "#2 Should throw SqlNullValueException");
146 Assert.Fail ("Should throw SqlNullValueException");
147 } catch (Exception ex) {
148 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
151 chars = new SqlChars (b);
152 Assert.AreEqual (chars.Storage, StorageState.Buffer, "#3 Should be StorageState.Buffer");
155 public void SqlCharsValue ()
157 char [] b1 = new char [10];
158 SqlChars chars = new SqlChars (b1);
159 char [] b2 = chars.Value;
160 Assert.AreEqual (b1 [0], b2 [0], "#1 Should be same");
162 Assert.AreEqual (b1 [0], 0, "#2 Should be same");
163 Assert.AreEqual (b2 [0], '1', "#3 Should be same");
166 public void SqlCharsSetLength ()
168 char [] b1 = new char [10];
169 SqlChars chars = new SqlChars ();
171 chars.SetLength (20);
172 Assert.Fail ("Should throw SqlTypeException");
173 } catch (Exception ex) {
174 Assert.AreEqual (typeof (SqlTypeException), ex.GetType (), "Should throw SqlTypeException");
176 chars = new SqlChars (b1);
177 Assert.AreEqual (chars.Length, 10, "#1 Should be same");
179 chars.SetLength (-1);
180 Assert.Fail ("Should throw ArgumentOutOfRangeException");
181 } catch (Exception ex) {
182 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
185 chars.SetLength (11);
186 Assert.Fail ("Should throw ArgumentOutOfRangeException");
187 } catch (Exception ex) {
188 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
191 Assert.AreEqual (chars.Length, 2, "#2 Should be same");
194 public void SqlCharsSetNull ()
196 char [] b1 = new char [10];
197 SqlChars chars = new SqlChars (b1);
198 Assert.AreEqual (chars.Length, 10, "#1 Should be same");
201 Assert.AreEqual (chars.Length, 10, "#1 Should not be same");
202 Assert.Fail ("Should throw SqlNullValueException");
203 } catch (Exception ex) {
204 Assert.AreEqual (typeof (SqlNullValueException), ex.GetType (), "Should throw SqlNullValueException");
206 Assert.AreEqual (true, chars.IsNull, "#2 Should be same");
210 public void GetXsdTypeTest ()
212 XmlQualifiedName qualifiedName = SqlChars.GetXsdType (null);
213 NUnit.Framework.Assert.AreEqual ("string", qualifiedName.Name, "#A01");
216 internal void ReadWriteXmlTestInternal (string xml,
228 test = new SqlString (testval);
229 ser = new XmlSerializer(typeof(SqlString));
230 sw = new StringWriter ();
231 xw = new XmlTextWriter (sw);
233 ser.Serialize (xw, test);
235 Assert.AreEqual (xml, sw.ToString (), unit_test_id);
236 Console.WriteLine ("{0} - Got: {1}", unit_test_id, sw.ToString ());
238 sr = new StringReader (xml);
239 xr = new XmlTextReader (sr);
240 test1 = (SqlString)ser.Deserialize (xr);
242 Assert.AreEqual (testval, test1.Value, unit_test_id);
243 Console.WriteLine ("{0} - Got: {1}", unit_test_id, test1.Value);
247 public void ReadWriteXmlTest ()
249 string xml1 = "<?xml version=\"1.0\" encoding=\"utf-16\"?><SqlString>This is a test string</SqlString>";
250 string xml2 = "<?xml version=\"1.0\" encoding=\"utf-16\"?><SqlString>a</SqlString>";
251 string strtest1 = "This is a test string";
254 ReadWriteXmlTestInternal (xml1, strtest1, "BA01");
255 ReadWriteXmlTestInternal (xml2, strtest2.ToString (), "BA02");