2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Data / Test / System.Data.SqlTypes / SqlBinaryTest.cs
1 //
2 // SqlBinaryTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlBinary
3 //
4 // Authors:
5 //   Ville Palo (vi64pa@koti.soon.fi)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) 2002 Ville Palo
9 // (C) 2003 Martin Willemoes Hansen
10 // 
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using NUnit.Framework;
36 using System;
37 using System.Xml;
38 using System.Data.SqlTypes;
39
40 namespace MonoTests.System.Data.SqlTypes
41 {
42         [TestFixture]
43         public class SqlBinaryTest {
44         
45                 SqlBinary Test1;
46                 SqlBinary Test2;
47                 SqlBinary Test3;
48
49                 [SetUp]
50                 public void GetReady() 
51                 {
52                         byte [] b1 = new Byte [2];
53                         byte [] b2 = new Byte [3];
54                         byte [] b3 = new Byte [2];
55
56                         b1 [0] = 240;
57                         b1 [1] = 15;
58                         b2 [0] = 10;
59                         b2 [1] = 10;
60                         b2 [2] = 10;
61                         b3 [0] = 240;
62                         b3 [1] = 15;
63
64                         Test1 = new SqlBinary(b1);
65                         Test2 = new SqlBinary(b2);
66                         Test3 = new SqlBinary(b3);
67                 }
68
69                 // Test constructor
70                 [Test]
71                 public void Create()
72                 {
73                         byte [] b = new byte [3];                        
74                         SqlBinary Test = new SqlBinary (b);
75                         Assert.IsTrue (!(Test.IsNull) , "#A01");
76                 }
77
78                 // Test public fields
79                 [Test]
80                 public void PublicFields()
81                 {
82                         Assert.IsTrue (SqlBinary.Null.IsNull, "#B01");
83                 }
84
85                 // Test properties
86                 [Test]
87                 public void Properties()
88                 {
89                         byte [] b = new byte [2];
90                         b [0] = 64;
91                         b [1] = 128;
92
93                         SqlBinary TestBinary = new SqlBinary (b);
94
95                         // IsNull
96                         Assert.IsTrue (SqlBinary.Null.IsNull, "#C01");
97
98                         // Item
99                         Assert.AreEqual ((byte)128, TestBinary [1], "#C02");
100                         Assert.AreEqual ((byte)64, TestBinary [0], "#C03");
101
102                         // FIXME: MSDN says that should throw SqlNullValueException
103                         // but throws IndexOutOfRangeException
104                         try {
105                                 byte test = TestBinary [TestBinary.Length];
106                                 Assert.Fail ("#C04");
107                         } catch (Exception e) {
108                                 Assert.AreEqual (typeof (IndexOutOfRangeException), e.GetType (), "#C05");
109                         }
110                  
111                         try {
112                                 byte test = SqlBinary.Null [2];
113                                 Assert.Fail ("#C06");
114                         } catch (Exception e) {
115                                 Assert.AreEqual (typeof (SqlNullValueException), e.GetType (), "#C07");
116                         }
117
118                         // Length
119                         Assert.AreEqual (2, TestBinary.Length, "#C08");
120
121                         try {
122                                 int test = SqlBinary.Null.Length;
123                                 Assert.Fail ("#C09");
124                         } catch (Exception e) {
125                                 Assert.AreEqual (typeof (SqlNullValueException), e.GetType (), "#C10");
126                         }
127
128                         // Value
129                         Assert.AreEqual ((byte)128, TestBinary [1], "#C11");
130                         Assert.AreEqual ((byte)64, TestBinary [0], "#C12");
131
132                         try {
133                                 Byte [] test = SqlBinary.Null.Value;
134                                 Assert.Fail ("#C13");
135                         } catch (Exception e) {
136                                 Assert.AreEqual (typeof (SqlNullValueException), e.GetType (), "#C14");
137                         }
138                 }
139
140                 // Methods 
141                 [Test]
142                 public void ComparisonMethods()
143                 {
144                         // GreaterThan
145                         Assert.IsTrue (SqlBinary.GreaterThan (Test1, Test2).Value, "#D01");
146                         Assert.IsTrue (SqlBinary.GreaterThan (Test3, Test2).Value, "#D02");
147                         Assert.IsTrue (!SqlBinary.GreaterThan (Test2, Test1).Value, "#D03");
148                         
149                         // GreaterThanOrEqual
150                         Assert.IsTrue (SqlBinary.GreaterThanOrEqual (Test1, Test2).Value, "#D04");
151                         Assert.IsTrue (SqlBinary.GreaterThanOrEqual (Test1, Test2).Value, "#D05");
152                         Assert.IsTrue (!SqlBinary.GreaterThanOrEqual (Test2, Test1).Value, "#D06");
153
154                         // LessThan
155                         Assert.IsTrue (!SqlBinary.LessThan (Test1, Test2).Value, "#D07");
156                         Assert.IsTrue (!SqlBinary.LessThan (Test3, Test2).Value, "#D08");
157                         Assert.IsTrue (SqlBinary.LessThan (Test2, Test1).Value, "#D09");
158
159                         // LessThanOrEqual
160                         Assert.IsTrue (!SqlBinary.LessThanOrEqual (Test1, Test2).Value, "#D10");
161                         Assert.IsTrue (SqlBinary.LessThanOrEqual (Test3, Test1).Value, "#D11");
162                         Assert.IsTrue (SqlBinary.LessThanOrEqual (Test2, Test1).Value, "#D12");
163
164                         // Equals
165                         Assert.IsTrue (!Test1.Equals (Test2), "#D13");
166                         Assert.IsTrue (!Test3.Equals (Test2), "#D14");
167                         Assert.IsTrue (Test3.Equals (Test1), "#D15");
168
169                         // NotEquals
170                         Assert.IsTrue (SqlBinary.NotEquals (Test1, Test2).Value, "#D16");
171                         Assert.IsTrue (!SqlBinary.NotEquals (Test3, Test1).Value, "#D17");
172                         Assert.IsTrue (SqlBinary.NotEquals (Test2, Test1).Value, "#D18");
173                 }
174
175                 [Test]
176                 public void CompareTo()
177                 {
178                         SqlString TestString = new SqlString ("This is a test");
179                         
180                         Assert.IsTrue (Test1.CompareTo(Test2) > 0, "#E01");
181                         Assert.IsTrue (Test2.CompareTo(Test1) < 0, "#E02");
182                         Assert.IsTrue (Test1.CompareTo(Test3) == 0, "#E03");
183                         
184                         try {
185                                 Test1.CompareTo (TestString);
186                                 Assert.Fail ("#E04");
187                         } catch(Exception e) {
188                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "#E05");
189                         }
190                 }
191
192                 [Test]
193                 public void GetHashCodeTest()
194                 {
195                         Assert.AreEqual (Test1.GetHashCode (), Test1.GetHashCode (), "#F01");
196                         Assert.IsTrue (Test2.GetHashCode () !=  Test1.GetHashCode (), "#F02");
197                 }
198
199                 [Test]
200                 public void GetTypeTest()
201                 {
202                         Assert.AreEqual ("System.Data.SqlTypes.SqlBinary", Test1.GetType().ToString(), "#G01");
203                 }
204
205                 [Test]
206                 public void Concat()
207                 {                       
208                         SqlBinary TestBinary;
209
210                         TestBinary = SqlBinary.Concat (Test2, Test3);
211                         Assert.AreEqual ((byte)15, TestBinary [4], "H01");
212
213                         TestBinary = SqlBinary.Concat (Test1, Test2);
214                         Assert.AreEqual ((byte)240, TestBinary [0], "#H02");
215                         Assert.AreEqual ((byte)15, TestBinary [1], "#H03");
216                 }
217
218                 [Test]
219                 public void ToSqlGuid()
220                 {
221                         SqlBinary TestBinary = new SqlBinary (new byte [16]);
222                         SqlGuid TestGuid = TestBinary.ToSqlGuid ();
223                         Assert.IsTrue (!TestGuid.IsNull, "#I01");
224                 }
225
226                 [Test]
227                 public void ToStringTest()
228                 {
229                         Assert.AreEqual ("SqlBinary(3)", Test2.ToString (), "#J01");
230                         Assert.AreEqual ("SqlBinary(2)", Test1.ToString (), "#J02");
231                 }
232
233                 // OPERATORS
234                 [Test]
235                 public void AdditionOperator()
236                 {
237                         SqlBinary TestBinary = Test1 + Test2;
238                         Assert.AreEqual ((byte)240, TestBinary [0], "#K01");
239                         Assert.AreEqual ((byte)15, TestBinary [1], "#K02");
240                 }
241
242                 [Test]
243                 public void ComparisonOperators()
244                 {
245                         // Equality
246                         Assert.IsTrue (!(Test1 == Test2).Value, "#L01");
247                         Assert.IsTrue ((Test3 == Test1).Value, "#L02");
248
249                         // Greater than
250                         Assert.IsTrue ((Test1 > Test2).Value, "#L03");
251                         Assert.IsTrue (!(Test3 > Test1).Value, "#L04");
252
253                         // Greater than or equal
254                         Assert.IsTrue ((Test1 >= Test2).Value, "#L05");
255                         Assert.IsTrue ((Test3 >= Test2).Value, "#L06");
256
257                         // Inequality
258                         Assert.IsTrue ((Test1 != Test2).Value, "#L07");
259                         Assert.IsTrue (!(Test3 != Test1).Value, "#L08");
260
261                         // Less than
262                         Assert.IsTrue (!(Test1 < Test2).Value, "#L09");
263                         Assert.IsTrue (!(Test3 < Test2).Value, "#L10");
264
265                         // Less than or equal
266                         Assert.IsTrue (!(Test1 <= Test2).Value, "#L11");
267                         Assert.IsTrue ((Test3 <= Test1).Value, "#L12");
268                 }
269
270                 [Test]
271                 public void SqlBinaryToByteArray() 
272                 {
273                         byte [] TestByteArray = (Byte[])Test1;
274                         Assert.AreEqual ((byte)240, TestByteArray[0], "#M01");
275                 }
276
277                 [Test]
278                 public void SqlGuidToSqlBinary()
279                 {
280                         byte [] TestByteArray = new Byte [16];
281                         TestByteArray [0] = 15;
282                         TestByteArray [1] = 200;
283                         SqlGuid TestGuid = new SqlGuid (TestByteArray);
284                         
285                         SqlBinary TestBinary = (SqlBinary)TestGuid;
286                         Assert.AreEqual ((byte)15, TestBinary [0], "#N01");
287                 }
288
289                 [Test]
290                 public void ByteArrayToSqlBinary()
291                 {
292                         byte [] TestByteArray = new Byte [2];
293                         TestByteArray [0] = 15;
294                         TestByteArray [1] = 200;
295                         SqlBinary TestBinary = (SqlBinary)TestByteArray;
296                         Assert.AreEqual ((byte)15, TestBinary [0], "#O1");
297                 }
298 #if NET_2_0
299                 [Test]
300                 public void GetXsdTypeTest ()
301                 {
302                         XmlQualifiedName qualifiedName = SqlBinary.GetXsdType (null);
303                         Assert.AreEqual ("base64Binary", qualifiedName.Name, "#A01");
304                 }
305 #endif
306         }
307 }
308