1 //
2 // SqlGuidTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlGuid
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 SqlGuidTest {
44 
45 		// 00000a01-0000-0000-0000-000000000000
46 		private SqlGuid Test1;
47 
48         	// 00000f64-0000-0000-0000-000000000000
49         	private SqlGuid Test2;
50         	private SqlGuid Test3;
51 
52 		// 0000fafa-0000-0000-0000-000000000000
53 		private SqlGuid Test4;
54 
55 		[SetUp]
GetReady()56                 public void GetReady()
57                 {
58                 	byte [] b1 = new byte [16];
59                 	byte [] b2 = new byte [16];
60                 	byte [] b3 = new byte [16];
61                 	byte [] b4 = new byte [16];
62 
63                 	b1 [0] = 1;
64                 	b1 [1] = 10;
65                 	b2 [0] = 100;
66                 	b2 [1] = 15;
67                 	b3 [0] = 100;
68                 	b3 [1] = 15;
69                 	b4 [0] = 250;
70                 	b4 [1] = 250;
71 
72                    	Test1 = new SqlGuid (b1);
73 			Test2 = new SqlGuid (b2);
74                 	Test3 = new SqlGuid (b3);
75                 	Test4 = new SqlGuid (b4);
76                 }
77 
78                 // Test constructor
79 		[Test]
Create()80                 public void Create()
81                 {
82 			// SqlGuid (Byte[])
83 			byte [] b = new byte [16];
84                 	b [0] = 100;
85                 	b [1] = 200;
86 
87                 	try {
88                 		SqlGuid Test = new SqlGuid (b);
89 
90 				// SqlGuid (Guid)
91 				Guid TestGuid = new Guid (b);
92                 		Test = new SqlGuid (TestGuid);
93 
94 				// SqlGuid (string)
95 				Test = new SqlGuid ("12345678-1234-1234-1234-123456789012");
96 
97 				// SqlGuid (int, short, short, byte, byte, byte, byte, byte, byte, byte, byte)
98                 		Test = new SqlGuid (10, 1, 2, 13, 14, 15, 16, 17, 19, 20 ,21);
99 
100                 	} catch (Exception e) {
101                 		Assert.Fail ("#A01 " + e);
102                 	}
103                 }
104 
105                 // Test public fields
106 		[Test]
PublicFields()107                 public void PublicFields()
108                 {
109                         Assert.IsTrue (SqlGuid.Null.IsNull, "#B01");
110                 }
111 
112                 // Test properties
113 		[Test]
Properties()114                 public void Properties()
115                 {
116                 	Guid ResultGuid = new Guid ("00000f64-0000-0000-0000-000000000000");
117 			Assert.IsTrue (!Test1.IsNull, "#C01");
118                 	Assert.IsTrue (SqlGuid.Null.IsNull, "#C02");
119                 	Assert.AreEqual (ResultGuid, Test2.Value, "#C03");
120                 }
121 
122                 // PUBLIC METHODS
123 		[Test]
CompareTo()124                 public void CompareTo()
125                 {
126 			String TestString = "This is a test string";
127 			SqlGuid test1 = new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE");
128 			SqlGuid test2 = new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE");
129 			SqlGuid test3 = new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE");
130                         Assert.IsTrue (Test1.CompareTo (Test3) <  0, "#D01");
131                         Assert.IsTrue (Test4.CompareTo (Test1) > 0, "#D02");
132                         Assert.IsTrue (Test3.CompareTo (Test2) == 0, "#D03");
133                         Assert.IsTrue (Test4.CompareTo (SqlGuid.Null) > 0, "#D04");
134 			Assert.IsTrue (test1.CompareTo (test2) >  0, "#D05");
135 			Assert.IsTrue (test3.CompareTo (test2) <  0, "#D06");
136 
137                         try {
138                                 Test1.CompareTo (TestString);
139                                 Assert.Fail("#D05");
140                         } catch(Exception e) {
141                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "#D06");
142                         }
143                 }
144 
145 		[Test]
EqualsMethods()146                 public void EqualsMethods()
147                 {
148                         Assert.IsTrue (!Test1.Equals (Test2), "#E01");
149                         Assert.IsTrue (!Test2.Equals (Test4), "#E02");
150                         Assert.IsTrue (!Test2.Equals (new SqlString ("TEST")), "#E03");
151                         Assert.IsTrue (Test2.Equals (Test3), "#E04");
152 
153                         // Static Equals()-method
154                         Assert.IsTrue (SqlGuid.Equals (Test2, Test3).Value, "#E05");
155                         Assert.IsTrue (!SqlGuid.Equals (Test1, Test2).Value, "#E06");
156                 }
157 
158 		[Test]
GetHashCodeTest()159                 public void GetHashCodeTest()
160                 {
161                         Assert.AreEqual (Test1.GetHashCode (), Test1.GetHashCode (), "#F01");
162                 	Assert.IsTrue (Test1.GetHashCode () != Test2.GetHashCode (), "#F02");
163                         Assert.AreEqual (Test3.GetHashCode (), Test2.GetHashCode (), "#F02");
164                 }
165 
166 		[Test]
GetTypeTest()167                 public void GetTypeTest()
168                 {
169                         Assert.AreEqual ("System.Data.SqlTypes.SqlGuid", Test1.GetType ().ToString (), "#G01");
170                         Assert.AreEqual ("System.Guid", Test3.Value.GetType ().ToString (), "#G02");
171                 }
172 
173 		[Test]
Greaters()174                 public void Greaters()
175                 {
176                         // GreateThan ()
177                         Assert.IsTrue (!SqlGuid.GreaterThan (Test1, Test2).Value, "#H01");
178                         Assert.IsTrue (SqlGuid.GreaterThan (Test2, Test1).Value, "#H02");
179                         Assert.IsTrue (!SqlGuid.GreaterThan (Test2, Test3).Value, "#H03");
180                         // GreaterTharOrEqual ()
181                         Assert.IsTrue (!SqlGuid.GreaterThanOrEqual (Test1, Test2).Value, "#H04");
182                         Assert.IsTrue (SqlGuid.GreaterThanOrEqual (Test2, Test1).Value, "#H05");
183                         Assert.IsTrue (SqlGuid.GreaterThanOrEqual (Test2, Test3).Value, "#H06");
184                 }
185 
186 		[Test]
Lessers()187                 public void Lessers()
188                 {
189                         // LessThan()
190                         Assert.IsTrue (!SqlGuid.LessThan (Test2, Test3).Value, "#I01");
191                         Assert.IsTrue (!SqlGuid.LessThan (Test2, Test1).Value, "#I02");
192                         Assert.IsTrue (SqlGuid.LessThan (Test1, Test2).Value, "#I03");
193 
194                         // LessThanOrEqual ()
195                         Assert.IsTrue (SqlGuid.LessThanOrEqual (Test1, Test2).Value, "#I04");
196                         Assert.IsTrue (!SqlGuid.LessThanOrEqual (Test2, Test1).Value, "#I05");
197                         Assert.IsTrue (SqlGuid.LessThanOrEqual (Test2, Test3).Value, "#I06");
198                         Assert.IsTrue (SqlGuid.LessThanOrEqual (Test4, SqlGuid.Null).IsNull, "#I07");
199                 }
200 
201 		[Test]
NotEquals()202                 public void NotEquals()
203                 {
204                         Assert.IsTrue (SqlGuid.NotEquals (Test1, Test2).Value, "#J01");
205                         Assert.IsTrue (SqlGuid.NotEquals (Test2, Test1).Value, "#J02");
206                         Assert.IsTrue (SqlGuid.NotEquals (Test3, Test1).Value, "#J03");
207                         Assert.IsTrue (!SqlGuid.NotEquals (Test3, Test2).Value, "#J04");
208                         Assert.IsTrue (SqlGuid.NotEquals (SqlGuid.Null, Test2).IsNull, "#J05");
209                 }
210 
211 		[Test]
Parse()212                 public void Parse()
213                 {
214                         try {
215                                 SqlGuid.Parse (null);
216                                 Assert.Fail ("#K01");
217                         } catch (Exception e) {
218                                 Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "#K02");
219                         }
220 
221                         try {
222                                 SqlGuid.Parse ("not-a-number");
223                                 Assert.Fail ("#K03");
224                         } catch (Exception e) {
225                                 Assert.AreEqual (typeof (FormatException), e.GetType (), "#K04");
226                         }
227 
228                          try {
229                                 SqlGuid.Parse ("9e400");
230                                 Assert.Fail ("#K05");
231                         } catch (Exception e) {
232                                 Assert.AreEqual (typeof (FormatException), e.GetType (), "#K06");
233                         }
234 
235                         Assert.AreEqual(new Guid("87654321-0000-0000-0000-000000000000"), SqlGuid.Parse ("87654321-0000-0000-0000-000000000000").Value, "#K07");
236                 }
237 
238 		[Test]
Conversions()239                 public void Conversions()
240                 {
241 			// ToByteArray ()
242 			Assert.AreEqual ((byte)1, Test1.ToByteArray () [0], "#L01");
243 			Assert.AreEqual ((byte)15, Test2.ToByteArray () [1], "#L02");
244 
245 			// ToSqlBinary ()
246 			byte [] b = new byte [2];
247                 	b [0] = 100;
248                 	b [1] = 15;
249 
250                         Assert.AreEqual (new SqlBinary (b), Test3.ToSqlBinary (), "#L03");
251 
252                         // ToSqlString ()
253                         Assert.AreEqual ("00000a01-0000-0000-0000-000000000000",  Test1.ToSqlString ().Value, "#L04");
254                         Assert.AreEqual ("0000fafa-0000-0000-0000-000000000000", Test4.ToSqlString ().Value, "#L05");
255 
256                         // ToString ()
257                         Assert.AreEqual ("00000a01-0000-0000-0000-000000000000", Test1.ToString (), "#L06");
258                         Assert.AreEqual ("0000fafa-0000-0000-0000-000000000000", Test4.ToString (), "#L07");
259                 }
260 
261                 // OPERATORS
262 
263 		[Test]
ThanOrEqualOperators()264                 public void ThanOrEqualOperators()
265                 {
266                         // == -operator
267                         Assert.IsTrue ((Test3 == Test2).Value, "#M01");
268                         Assert.IsTrue (!(Test1 == Test2).Value, "#M02");
269                         Assert.IsTrue ((Test1 == SqlGuid.Null).IsNull, "#M03");
270 
271                         // != -operator
272                         Assert.IsTrue (!(Test2 != Test3).Value, "#M04");
273                         Assert.IsTrue ((Test1 != Test3).Value, "#M05");
274                         Assert.IsTrue ((Test1 != SqlGuid.Null).IsNull, "#M06");
275 
276                         // > -operator
277                         Assert.IsTrue ((Test2 > Test1).Value, "#M07");
278                         Assert.IsTrue (!(Test1 > Test3).Value, "#M08");
279                         Assert.IsTrue (!(Test3 > Test2).Value, "#M09");
280                         Assert.IsTrue ((Test1 > SqlGuid.Null).IsNull, "#M10");
281 
282                         // >=  -operator
283                         Assert.IsTrue (!(Test1 >= Test3).Value, "#M12");
284                         Assert.IsTrue ((Test3 >= Test1).Value, "#M13");
285                         Assert.IsTrue ((Test3 >= Test2).Value, "#M14");
286                         Assert.IsTrue ((Test1 >= SqlGuid.Null).IsNull, "#M15");
287 
288                         // < -operator
289                         Assert.IsTrue (!(Test2 < Test1).Value, "#M16");
290                         Assert.IsTrue ((Test1 < Test3).Value, "#M17");
291                         Assert.IsTrue (!(Test2 < Test3).Value, "#M18");
292                         Assert.IsTrue ((Test1 < SqlGuid.Null).IsNull, "#M19");
293 
294                         // <= -operator
295                         Assert.IsTrue ((Test1 <= Test3).Value, "#M20");
296                         Assert.IsTrue (!(Test3 <= Test1).Value, "#M21");
297                         Assert.IsTrue ((Test2 <= Test3).Value, "#M22");
298                         Assert.IsTrue ((Test1 <= SqlGuid.Null).IsNull, "#M23");
299                 }
300 
301 		[Test]
SqlBinaryToSqlGuid()302 		public void SqlBinaryToSqlGuid()
303 		{
304 			byte [] b = new byte [16];
305 			b [0] = 100;
306 			b [1] = 200;
307 			SqlBinary TestBinary = new SqlBinary (b);
308 
309 			Assert.AreEqual (new Guid("0000c864-0000-0000-0000-000000000000"), ((SqlGuid)TestBinary).Value, "#N01");
310 		}
311 
312 		[Test]
SqlGuidToGuid()313 		public void SqlGuidToGuid()
314 		{
315 			Assert.AreEqual (new Guid("00000a01-0000-0000-0000-000000000000"), (Guid)Test1, "#O01");
316 			Assert.AreEqual (new Guid("00000f64-0000-0000-0000-000000000000"), (Guid)Test2, "#O02");
317 		}
318 
319 		[Test]
SqlStringToSqlGuid()320                 public void SqlStringToSqlGuid()
321                 {
322                         SqlString TestString = new SqlString ("Test string");
323                         SqlString TestString100 = new SqlString ("0000c864-0000-0000-0000-000000000000");
324 
325                         Assert.AreEqual (new Guid("0000c864-0000-0000-0000-000000000000"), ((SqlGuid)TestString100).Value, "#P01");
326 
327                         try {
328                                 SqlGuid test = (SqlGuid)TestString;
329                                 Assert.Fail ("#P02");
330                         } catch(Exception e) {
331                                 Assert.AreEqual (typeof (FormatException), e.GetType (), "#P03");
332                         }
333                 }
334 
335 		[Test]
GuidToSqlGuid()336 		public void GuidToSqlGuid()
337 		{
338 			Guid TestGuid = new Guid("0000c864-0000-0000-0000-000007650000");
339 			Assert.AreEqual (new SqlGuid("0000c864-0000-0000-0000-000007650000"), (SqlGuid)TestGuid, "#Q01");
340 		}
341 		[Test]
GetXsdTypeTest()342 		public void GetXsdTypeTest ()
343 		{
344 			XmlQualifiedName qualifiedName = SqlGuid.GetXsdType (null);
345 			Assert.AreEqual ("string", qualifiedName.Name, "#A01");
346 		}
347         }
348 }
349