1fc17625a7567e201ef2e8607e9ffe139a2e3a4f
[mono.git] / mcs / class / System.Data / Test / System.Data.SqlTypes / SqlCharsTest.cs
1 //
2 // SqlCharsTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlChars
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 #if NET_2_0
31
32 using NUnit.Framework;
33 using System;
34 using System.IO;
35 using System.Xml;
36 using System.Data.SqlTypes;
37 using System.Threading;
38 using System.Globalization;
39
40 #if NET_2_0
41 using System.Xml.Serialization;
42 using System.IO;
43 #endif 
44
45 namespace MonoTests.System.Data.SqlTypes
46 {
47         [TestFixture]
48         public class SqlCharsTest
49         {
50                 [SetUp]
51                 public void SetUp ()
52                 {
53                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
54                 }
55
56                 // Test constructor
57                 [Test]
58                 public void SqlCharsItem ()
59                 {
60                         SqlChars chars = new SqlChars ();
61                         try {
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");
66                         }
67                         char [] b = null;
68                         chars = new SqlChars (b);
69                         try {
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");
74                         }
75                         b = new char [10];
76                         chars = new SqlChars (b);
77                         Assert.AreEqual (chars [0], 0, "");
78                         try {
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");
83                         }
84                         try {
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");
89                         }
90                 }
91                 [Test]
92                 public void SqlCharsLength ()
93                 {
94                         char [] b = null;
95                         SqlChars chars = new SqlChars ();
96                         try {
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");
101                         }
102                         chars = new SqlChars (b);
103                         try {
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");
108                         }
109                         b = new char [10];
110                         chars = new SqlChars (b);
111                         Assert.AreEqual (chars.Length, 10, "#3 Should be 10");
112                 }
113                 [Test]
114                 public void SqlCharsMaxLength ()
115                 {
116                         char [] b = null;
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");
121                         b = new char [10];
122                         chars = new SqlChars (b);
123                         Assert.AreEqual (chars.MaxLength, 10, "#3 Should return 10");
124                 }
125                 [Test]
126                 public void SqlCharsNull ()
127                 {
128                         char [] b = null;
129                         SqlChars chars = SqlChars.Null;
130                         Assert.AreEqual (chars.IsNull, true, "#1 Should return true");
131                 }
132                 [Test]
133                 public void SqlCharsStorage ()
134                 {
135                         char [] b = null;
136                         SqlChars chars = new SqlChars ();
137                         try {
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");
142                         }
143                         chars = new SqlChars (b);
144                         try {
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");
149                         }
150                         b = new char [10];
151                         chars = new SqlChars (b);
152                         Assert.AreEqual (chars.Storage, StorageState.Buffer, "#3 Should be StorageState.Buffer");
153                 }
154                 [Test]
155                 public void SqlCharsValue ()
156                 {
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");
161                         b2 [0] = '1';
162                         Assert.AreEqual (b1 [0], 0, "#2 Should be same");
163                         Assert.AreEqual (b2 [0], '1', "#3 Should be same");
164                 }
165                 [Test]
166                 public void SqlCharsSetLength ()
167                 {
168                         char [] b1 = new char [10];
169                         SqlChars chars = new SqlChars ();
170                         try {
171                                 chars.SetLength (20);
172                                 Assert.Fail ("Should throw SqlTypeException");
173                         } catch (Exception ex) {
174                                 Assert.AreEqual (typeof (SqlTypeException), ex.GetType (), "Should throw SqlTypeException");
175                         }
176                         chars = new SqlChars (b1);
177                         Assert.AreEqual (chars.Length, 10, "#1 Should be same");
178                         try {
179                                 chars.SetLength (-1);
180                                 Assert.Fail ("Should throw ArgumentOutOfRangeException");
181                         } catch (Exception ex) {
182                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
183                         }
184                         try {
185                                 chars.SetLength (11);
186                                 Assert.Fail ("Should throw ArgumentOutOfRangeException");
187                         } catch (Exception ex) {
188                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "Should throw ArgumentOutOfRangeException");
189                         }
190                         chars.SetLength (2);
191                         Assert.AreEqual (chars.Length, 2, "#2 Should be same");
192                 }
193                 [Test]
194                 public void SqlCharsSetNull ()
195                 {
196                         char [] b1 = new char [10];
197                         SqlChars chars = new SqlChars (b1);
198                         Assert.AreEqual (chars.Length, 10, "#1 Should be same");
199                         chars.SetNull ();
200                         try {
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");
205                         }
206                         Assert.AreEqual (true, chars.IsNull, "#2 Should be same");
207                 }
208
209                 [Test]
210                 public void GetXsdTypeTest ()
211                 {
212                         XmlQualifiedName qualifiedName = SqlChars.GetXsdType (null);
213                         NUnit.Framework.Assert.AreEqual ("string", qualifiedName.Name, "#A01");
214                 }
215
216                 internal void ReadWriteXmlTestInternal (string xml, 
217                                                         string testval, 
218                                                         string unit_test_id)
219                 {
220                         SqlString test;
221                         SqlString test1;
222                         XmlSerializer ser;
223                         StringWriter sw;
224                         XmlTextWriter xw;
225                         StringReader sr;
226                         XmlTextReader xr;
227
228                         test = new SqlString (testval);
229                         ser = new XmlSerializer(typeof(SqlString));
230                         sw = new StringWriter ();
231                         xw = new XmlTextWriter (sw);
232                         
233                         ser.Serialize (xw, test);
234
235                         Assert.AreEqual (xml, sw.ToString (), unit_test_id);
236                         Console.WriteLine ("{0} - Got: {1}", unit_test_id, sw.ToString ());
237
238                         sr = new StringReader (xml);
239                         xr = new XmlTextReader (sr);
240                         test1 = (SqlString)ser.Deserialize (xr);
241
242                         Assert.AreEqual (testval, test1.Value, unit_test_id);
243                         Console.WriteLine ("{0} - Got: {1}", unit_test_id, test1.Value);
244                 }
245
246                 [Test]
247                 public void ReadWriteXmlTest ()
248                 {
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";
252                         char strtest2 = 'a';
253
254                         ReadWriteXmlTestInternal (xml1, strtest1, "BA01");
255                         ReadWriteXmlTestInternal (xml2, strtest2.ToString (), "BA02");
256                 }
257         }
258 }
259
260 #endif