Merge pull request #1345 from mattleibow/websocket-continuation-frame-fix
[mono.git] / mcs / class / System.Data / Test / System.Data.Tests.Mainsoft / GHTUtils / GHTValueGen.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Reflection;
31 using System.Collections;
32 using System.Data;
33
34 //this class is used at the WebServices Test Harness
35 namespace GHTUtils
36 {
37
38         public class ValueGen
39         {
40                 private const int ARRAY_SIZE = 7;
41
42                 public static object GHTTypeGenerator( Type t )
43                 {
44                         object result = null;
45                         if ( t.Name == "XmlNode" || t.Name == "XmlElement" || t.Name == "DataSet" || t.Name == "DataTable" )
46                         {
47                                 result = GetRandomValue( t );
48                                 return result;
49                         }
50                         //====================================================================================
51                         // Primitive
52                         //====================================================================================
53                         else if ( isPrimitive( t ) )
54                         {
55                                 result = GetRandomValue( t );
56                                 return result;
57                         }
58                         //====================================================================================
59                         // Array
60                         //====================================================================================
61                         else if (typeof(Array).IsAssignableFrom(t) ) 
62                                 //The Array class returns false because it is not an array.
63                                 //To check for an array, use code such as typeof(Array).IsAssignableFrom(type).
64                         {
65                                 result = GenerateArray(t);
66                                 return result;
67                         }
68                         //====================================================================================
69                         // Collection
70                         //====================================================================================
71                         else if ( isCollection( t ) ) 
72                         {
73                                 result = GenerateCollection(t);
74                                 return result;
75                         }
76                         //====================================================================================
77                         // User Defined Type
78                         //====================================================================================
79                         else
80                         {
81                                         result = Activator_CreateInstance( t );
82                                         result = Generate ( result );
83                         }
84                         return result;
85                 }
86
87                 public static object GHTObjModifier( object obj )
88                                 {
89                         if ( isPrimitive( obj.GetType() ) )
90                         {
91                                 return GetModifiedValue( obj );
92                         }
93                         else if ( obj.GetType().IsArray ) 
94                         {
95                                 Type ElementType;
96                                 //get the type of the elements in the array.
97                                 //work around of GH behavior for array of enums : will give type enum    (Array)obj).GetValue(0).GetType()
98                                 //                                              : will give type Int32   obj.GetType().GetElementType()
99                                 if ( ((Array)obj).Length > 0)
100                                         ElementType = ((Array)obj).GetValue(0).GetType();
101                                 else
102                     ElementType = obj.GetType().GetElementType();
103
104                                 if ( isPrimitive(ElementType) )
105                                 {
106                                         Array arr = (Array)obj;
107                                         for (int i=0; i < arr.Length; i++)
108                                         {
109                                                 arr.SetValue(GetModifiedValue( arr.GetValue( i ) ), i );
110                                         }
111                                         return arr;
112                                 }
113                                 else
114                                 {
115                                         Array arr = (Array)obj;
116                                         for ( int i=0; i < arr.Length; i++ )
117                                         {
118                                                 object new_obj  = arr.GetValue( i );
119                                                 new_obj = GHTObjModifier( new_obj );
120                                                 arr.SetValue( new_obj, i );
121                                         }
122                                         return arr;
123                                 }
124                         }
125                         else if ( obj.GetType().IsEnum ) 
126                         {
127                                 Array a = Enum.GetValues(obj.GetType());
128                                 if (a.Length >= 2)
129                                         return a.GetValue(a.Length-2);
130                                 else
131                                         return a.GetValue(a.Length-1); //leave the same value
132                         }
133                         else if (obj.GetType().Name == "DataTable")
134                         {
135                                 ModifyDataTable((System.Data.DataTable)obj);
136                                 return obj;
137                         }
138                         else if (obj.GetType().Name == "DataSet")
139                         {
140                                 ModifyDataSet((System.Data.DataSet)obj);
141                                 return obj;
142                         }
143                         else if (obj.GetType().Name == "XmlNode" || obj.GetType().Name == "XmlElement")
144                         {
145                                 ModifyXmlElement((System.Xml.XmlElement)obj);
146                                 return obj;
147                         }
148                         else if (isCollection(obj.GetType()))
149                         {
150                                 ModifyCollection(obj);
151                                 return obj;
152                         }
153                         else
154                         {
155                                 object result = obj;
156                                 Modify( result );
157                                 return result;
158                         }
159                                 }
160         
161
162                 static object GetRandomValue(Type t)
163                 {
164                         object objOut = null;
165                         string str = null;
166                         System.Threading.Thread.Sleep(10);
167                         System.Random rnd =     new     System.Random(unchecked((int)DateTime.Now.Ticks));
168
169                         if (t.FullName ==       "System.Boolean")
170                         {
171                                 objOut = System.Convert.ToBoolean(rnd.Next(0, 1));
172                                 return objOut;
173                         }
174                         else if (t.FullName == "System.Byte")
175                         {
176                                 objOut = System.Convert.ToByte(rnd.Next(System.Byte.MinValue+1, System.Byte.MaxValue-128));
177                                 return objOut;
178                         }
179                         else if (t.FullName == "System.Char")
180                         {
181                                 objOut = System.Convert.ToChar(rnd.Next(System.Char.MinValue+65, System.Char.MaxValue-128));
182                                 return objOut;
183                         }
184                         else if (t.FullName == "System.DateTime")
185                         {
186                                 //GH precision is only milliseconds
187                                 objOut = System.Convert.ToDateTime(new System.DateTime(632083133257660000));
188                                 return objOut;
189                         }
190                         else if (t.FullName == "System.Decimal")
191                         {
192                                 objOut = System.Convert.ToDecimal(rnd.Next(System.Int16.MinValue+1, System.Int16.MaxValue-128));
193                                 return objOut;
194                         }
195                         else if (t.FullName == "System.Double")
196                         {
197                                 //give max length of "MaxLength" digits
198                                 int MaxLength = 2;
199                                 str = rnd.NextDouble().ToString();
200                                 if (str.Length > MaxLength) str = str.Remove(MaxLength+1,str.Length-(MaxLength+1));
201                                 objOut = System.Convert.ToDouble(str);
202                                 return objOut;
203                         }
204                         else if (t.FullName == "System.Int16")
205                         {
206                                 objOut = System.Convert.ToInt16(rnd.Next(System.Int16.MinValue+1,System.Int16.MaxValue-128));
207                                 return objOut;
208                         }
209                         else if (t.FullName == "System.Int32")
210                         {
211                                 objOut = System.Convert.ToInt32(rnd.Next(System.Int16.MinValue+1,System.Int16.MaxValue-128));
212                                 return objOut;
213                         }
214                         else if (t.FullName == "System.Int64")
215                         {
216                                 objOut = System.Convert.ToInt64(rnd.Next(System.Int16.MinValue+1,System.Int16.MaxValue-128));
217                                 return objOut;
218                         }
219                         else if (t.FullName == "System.SByte")
220                         {
221                                 objOut = System.Convert.ToSByte(rnd.Next(System.SByte.MinValue+1,System.SByte.MaxValue-128));
222                                 return objOut;
223                         }
224                         else if (t.FullName == "System.Single")
225                         {
226                                 objOut = System.Convert.ToSingle(rnd.Next(System.Int16.MinValue+1, System.Int16.MaxValue-128));
227                                 return objOut;
228                         }
229                         else if (t.FullName == "System.String")
230                         {
231                                 long size = DateTime.Now.Ticks;
232                                 size = size % 99;
233                                 if (size==0) size = 16;
234                                 for     (int i=0; i<size ;i++)
235                                 {
236                                         str     += System.Convert.ToChar(rnd.Next(System.Byte.MinValue+65, System.Byte.MaxValue-128));
237                                 }
238                                 objOut = str;
239                                 return objOut;
240                         }
241                         else if (t.FullName == "System.UInt16")
242                         {
243                                 objOut = System.Convert.ToUInt16(rnd.Next(System.UInt16.MinValue+1,System.UInt16.MaxValue-128));
244                                 return objOut;
245                         }
246                         else if (t.FullName == "System.UInt32")
247                         {
248                                 objOut = System.Convert.ToUInt32(rnd.Next((int)System.UInt32.MinValue+1,System.Int32.MaxValue-128));
249                                 return objOut;
250                         }
251                         else if (t.FullName == "System.UInt64")
252                         {
253                                 objOut = System.Convert.ToUInt64(rnd.Next((int)System.UInt64.MinValue+1,System.Int32.MaxValue-128));
254                                 return objOut;
255                         }                                
256                         else if (t.FullName == "System.Data.DataTable")
257                         {
258                                 objOut = GenerateDataTable();
259                                 return objOut;
260                         }                                
261                         else if (t.FullName == "System.Data.DataSet")
262                         {
263                                 objOut = GenerateDataSet();
264                                 return objOut;
265                         }                                  
266                         else if (t.FullName == "System.Xml.XmlNode" || t.FullName == "System.Xml.XmlElement")
267                         {
268                                 System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
269                                 objOut = xmlDoc.CreateElement("myElement");
270                                 ((System.Xml.XmlElement)objOut).InnerText = "1234";    
271 //                              ((System.Xml.XmlElement)objOut).InnerXml = "<books>" +   
272 //                                      "<book>" + 
273 //                                      "<author>Carson</author>" + 
274 //                                      "<price format=\"dollar\">31.95</price>" + 
275 //                                      "<pubdate>05/01/2001</pubdate>" + 
276 //                                      "</book>" + 
277 //                                      "<pubinfo>" + 
278 //                                      "<publisher>MSPress</publisher>" + 
279 //                                      "<state>WA</state>" + 
280 //                                      "</pubinfo>" + 
281 //                                      "</books>";
282                                 return objOut;
283                         }
284                         else
285                         {
286                                 throw new System.NotImplementedException("GetRandomValue error: Type " + t.Name + "     not     implemented.");
287                         }
288                 }
289
290                 static object GetModifiedValue(object objIn)
291                 {
292                         object objOut = null;
293
294                         if (objIn.GetType().FullName == "System.Boolean")
295                         {
296                                 bool BoolVar =!(bool)objIn;
297                                 return BoolVar  ;
298                         }
299                         else if (objIn.GetType().FullName == "System.Byte")
300                         {
301                                 if ((byte)objIn == byte.MaxValue) 
302                                         return (byte)1;                         
303                                 else
304                                         return System.Convert.ToByte((byte)objIn + (byte)1);
305                         }
306                         else if (objIn.GetType().FullName == "System.Char")
307                         {
308                                 if ((char)objIn == char.MaxValue)
309                                         return (char)1;
310                                 else
311                                         return System.Convert.ToChar((char)objIn + (char)1);
312                         }
313                         else if (objIn.GetType().FullName == "System.DateTime")
314                         {
315                                 objOut = System.Convert.ToDateTime(objIn);
316                                 objOut = ((DateTime)objOut).AddHours(1);
317                                 return objOut;
318                         }
319                         else if (objIn.GetType().FullName == "System.Decimal")
320                         {
321                                 if ((decimal)objIn == decimal.MaxValue)
322                                         objOut = (decimal)1;
323                                 else 
324                                         objOut = System.Convert.ToDecimal(System.Convert.ToDecimal(objIn) + (decimal)1);
325                                 return objOut;
326                         }
327                         else if (objIn.GetType().FullName == "System.Double")
328                         {
329                                 if ((double)objIn == double.MaxValue)
330                                         objOut = (double)1;
331                                 else 
332                                         objOut = System.Convert.ToDouble(System.Convert.ToDouble(objIn) + (double)1);
333                                 return objOut;
334                         }
335                         else if (objIn.GetType().FullName == "System.Int16")
336                         {
337                                 if ((Int16)objIn == Int16.MaxValue)
338                                         objOut = (Int16)1;
339                                 else 
340                                         objOut = System.Convert.ToInt16(System.Convert.ToInt16(objIn) + (Int16)1);
341                                 return objOut;
342                         }
343                         else if (objIn.GetType().FullName == "System.Int32")
344                         {
345                                 if ((Int32)objIn == Int32.MaxValue)
346                                         objOut = (Int32)1;
347                                 else 
348                                         objOut = System.Convert.ToInt32(System.Convert.ToInt32(objIn) + (Int32)1);
349                                 return objOut;
350                         }
351                         else if (objIn.GetType().FullName == "System.Int64")
352                         {
353                                 if ((Int64)objIn == Int64.MaxValue)
354                                         objOut = (Int64)1;
355                                 else 
356                                         objOut = System.Convert.ToInt64(System.Convert.ToInt64(objIn) + (Int64)1);
357                                 return objOut;
358                         }
359                         else if (objIn.GetType().FullName == "System.SByte")
360                         {
361                                 if ((SByte)objIn == SByte.MaxValue)
362                                         objOut = (SByte)1;
363                                 else 
364                                         objOut = System.Convert.ToSByte(System.Convert.ToSByte(objIn) + (SByte)1);
365                                 return objOut;
366                         }
367                         else if (objIn.GetType().FullName == "System.Single")
368                         {
369                                 if ((Single)objIn == Single.MaxValue)
370                                         objOut = (Single)1;
371                                 else 
372                                         objOut = System.Convert.ToSingle(System.Convert.ToSingle(objIn) + (Single)1);
373                                 return objOut;
374                         }
375                         else if (objIn.GetType().FullName == "System.String")
376                         {
377                                 string strin;
378                                 strin = System.Convert.ToString(System.Convert.ToString(objIn));
379                                 objOut = System.Convert.ToString("");
380                                 for (int ii=0; ii < strin.Length; ii++)
381                                         if ( strin[ii] > 'Z' )
382                                                 objOut += strin[ii].ToString().ToUpper();
383                                         else    
384                                                 objOut += strin[ii].ToString().ToLower();
385                                 return objOut;
386                         }
387                         else if (objIn.GetType().FullName == "System.UInt16")
388                         {
389                                 if ((UInt16)objIn == UInt16.MaxValue)
390                                         objOut = (UInt16)1;
391                                 else 
392                                         objOut = System.Convert.ToUInt16(System.Convert.ToUInt16(objIn) + (UInt16)1);
393                                 return objOut;
394                         }
395                         else if (objIn.GetType().FullName == "System.UInt32")
396                         {
397                                 if ((UInt32)objIn == UInt32.MaxValue)
398                                         objOut = (UInt32)1;
399                                 else
400                                         objOut = System.Convert.ToUInt32(System.Convert.ToUInt32(objIn) + (UInt32)1);
401                                 return objOut;
402                         }
403                         else if (objIn.GetType().FullName == "System.UInt64")
404                         {
405                                 if ((UInt64)objIn == UInt64.MaxValue)
406                                         objOut = (UInt64)1;
407                                 else
408                                         objOut = System.Convert.ToUInt64(System.Convert.ToUInt64(objIn) + (UInt64)1);
409                                 return objOut;
410                         }                                
411                         else
412                         {
413                                 throw new System.NotImplementedException("GetModifiedValue error: Type " + objIn.GetType().FullName + " not     implemented.");
414                         }
415                 }
416                 
417
418
419                 static void Modify( object obj )
420                 {
421                         if ( obj.GetType().IsEnum ) 
422                         {
423                                 Array a = Enum.GetValues(obj.GetType());
424                                 if (a.Length >= 2)
425                                         obj = a.GetValue(a.Length-2);
426                                 else
427                                         obj = a.GetValue(a.Length-1); //leave the same value
428                                 return;
429                         }
430
431                         MemberInfo [] mic;
432
433                         mic = obj.GetType().GetMembers( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic );
434                         foreach ( MemberInfo mi in mic )
435                         {
436                                 // ---------- FieldInfo ---------- 
437                                 if ( mi is FieldInfo )
438                                 {
439                                         FieldInfo field = (FieldInfo)mi;
440                                         Type fieldType = field.FieldType;
441                                                         
442
443                                         if ( fieldType.IsArray )
444                                         {
445                                                 // is array of primitive
446                                                 if ( isPrimitive( fieldType.GetElementType() ) )
447                                                 {
448                                                         Array arr = (Array)field.GetValue( obj );
449                                                         for (int i=0; i < arr.Length; i++)
450                                                         {
451                                                                 arr.SetValue(GetModifiedValue( arr.GetValue( i ) ), i );
452                                                         }
453                                                         field.SetValue( obj, arr );
454                                                 }
455                                                 
456                                                 // is array of user defined type
457                                                 if ( !isSystem( fieldType ) &&  !isCollection( fieldType ) )
458                                                 {
459                                                         Array arr = (Array)field.GetValue( obj );
460                                                         for ( int i=0; i < arr.Length; i++ )
461                                                         {
462                                                                 object new_obj  = arr.GetValue( i );
463                                                                 Modify( new_obj );
464                                                                 arr.SetValue( new_obj, i );
465                                                         }
466                                                         field.SetValue( obj, arr );
467                                                 }
468
469                                         }
470                                         else
471                                         {
472                                                 if ( isPrimitive( fieldType ) )
473                                                 {
474                                                         field.SetValue( obj, GetModifiedValue( field.GetValue( obj ) ) );
475                                                 }
476                                                 if ( !isSystem( fieldType ) &&  !isCollection( fieldType ) )
477                                                 {
478                                                         object new_obj = field.GetValue( obj );
479                                                         Modify(new_obj);
480                                                         field.SetValue( obj, new_obj );
481                                                 }
482                                                 // object
483                                                 if ( isObject( fieldType ) )
484                                                 {
485                                                         object new_obj = field.GetValue( obj );
486                                                         Modify(new_obj);
487                                                         field.SetValue( obj, new_obj );
488                                                 } 
489                                         }
490                                 } // field info
491
492
493                                 // ---------- PropertyInfo ---------- 
494                                 //
495                                 if ( mi is PropertyInfo )
496                                 {
497                                         PropertyInfo prop = (PropertyInfo)mi;
498
499                                         if ( prop.PropertyType.IsArray )
500                                         {
501                                                 // is array of primitive type member
502                                                 if ( isPrimitive( prop.PropertyType.GetElementType() ) )
503                                                 {
504                                                         Array arr = (Array)prop.GetValue( obj, null );
505                                                         for (int i=0; i < arr.Length; i++)
506                                                         {
507                                                                 arr.SetValue(GetModifiedValue( arr.GetValue( i ) ), i );
508                                                         }
509                                                         prop.SetValue( obj, arr, null );
510                                                 }
511
512                                                 //is array user defined type
513                                                 if ( !isSystem( prop.PropertyType ) &&  !isCollection( prop.PropertyType ) )
514                                                 {
515                                                         Array arr = (Array)prop.GetValue( obj, null );
516                                                         for ( int i=0; i < arr.Length; i++ )
517                                                         {
518                                                                 object new_obj  = arr.GetValue( i );
519                                                                 Modify( new_obj );
520                                                                 arr.SetValue( new_obj, i );
521                                                         }
522                                                         prop.SetValue( obj, arr, null );
523                                                 }
524                                         }
525                                         else
526                                         {
527                                                 //primitive type
528                                                 if ( isPrimitive( prop.PropertyType ) )
529                                                 {
530                                                         prop.SetValue( obj, GetModifiedValue( prop.GetValue( obj, null ) ), null );
531                                                 }
532                                                 
533                                                 //user defined type
534                                                 if ( !isSystem( prop.PropertyType ) &&  !isCollection( prop.PropertyType ) )
535                                                 {
536                                                         object new_obj = prop.GetValue( obj, null );
537                                                         Modify(new_obj);
538                                                         prop.SetValue( obj, new_obj, null );
539                                                 }
540
541                                                 // object
542                                                 if ( isObject( prop.PropertyType ) )
543                                                 {
544                                                         object new_obj = prop.GetValue( obj, null );
545                                                         Modify(new_obj);
546                                                         prop.SetValue( obj, new_obj, null );
547                                                 } 
548                                         } 
549                                 } // field info
550                         } // for each
551                         //return obj;
552                 }
553
554                 static void ModifyDataSet(DataSet ds)
555                 {
556                         foreach (DataTable dt in ds.Tables)
557                         {
558                                 ModifyDataTable(dt);
559                         }
560                 }
561                 static void ModifyDataTable(DataTable dt)
562                 {
563                         foreach(DataRow dr in dt.Rows)
564                         {
565                                 foreach (DataColumn dc in dt.Columns)
566                                 {
567                                         switch (dc.DataType.Name)
568                                         {
569                                                 case "String":
570                                                         dr[dc] = dr[dc].ToString() + "mod"; 
571                                                         break;
572                                                 case "Int32":
573                                                         dr[dc] = Convert.ToInt32( dr[dc] ) * 100;
574                                                         break;
575                                         }
576                                 }
577                         }
578                 }
579
580                 static void ModifyXmlElement(System.Xml.XmlElement xmlElem)
581                 {
582                         xmlElem.InnerText = "54321";
583 //                      xmlElem.InnerXml = "<books>" +   
584 //                              "<book>" + 
585 //                              "<author>Carson</author>" + 
586 //                              "<price format=\"dollar\">33.99</price>" + 
587 //                              "<pubdate>01/01/2003</pubdate>" + 
588 //                              "</book>" + 
589 //                              "<pubinfo>" + 
590 //                              "<publisher>MisPress</publisher>" + 
591 //                              "<state>CA</state>" + 
592 //                              "</pubinfo>" + 
593 //                              "</books>";
594                 }
595
596                 static void ModifyCollection(object co)
597                 {
598                         for (int i=0; i < ((IList)co).Count; i++)
599                         {
600                                 object o = ((IList)co)[i];
601                                 o = GHTObjModifier(o);
602                                 ((IList)co)[i] = o;
603                         }
604                 }
605
606                 static object Generate( object obj )
607                 {
608                         MemberInfo [] mic;
609
610                         if ( obj == null ) return null;
611
612                         if (obj.GetType().IsEnum)
613                         {
614                                 Array a = Enum.GetValues(obj.GetType());
615                                 return a.GetValue(a.Length-1);
616                         }
617
618                         if ( isObject( obj.GetType() ))
619                         {
620                                 //obj = GetRandomValue( typeof( System.String ) );
621                                 obj = new object();
622                                 return obj;
623                         }
624
625                         mic = obj.GetType().GetMembers( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic );
626                         foreach ( MemberInfo mi in mic )
627                         {
628                                 // FieldInfo
629                                 //
630                                 if ( mi is FieldInfo )
631                                 {
632                                         FieldInfo field = (FieldInfo)mi;
633                                 
634                                         // is array of primitive
635                                         //
636                                         if ( field.FieldType.IsArray )
637                                         {
638                                                 if ( isPrimitive( field.FieldType.GetElementType() ) )
639                                                 {
640                                                         Array arr = Array.CreateInstance( field.FieldType.GetElementType(), ARRAY_SIZE);
641                                                         for (int i=0; i < arr.Length; i++)
642                                                         {
643                                                                 arr.SetValue(GetRandomValue( field.FieldType.GetElementType() ), i );
644                                                         }
645                                                         field.SetValue( obj, arr );
646                                                 }
647                                         }
648                                         else
649                                         {
650                                                 if ( isPrimitive( field.FieldType ) )
651                                                 {
652                                                         field.SetValue( obj, GetRandomValue( field.FieldType ) );
653                                                 }
654                                         }
655
656                                         // Collection type member
657                                         //
658                                         if ( isCollection( field.FieldType ) )
659                                         {
660                                                 object new_obj = Activator_CreateInstance(field.FieldType);
661
662                                                 MethodInfo mm = null;
663                                                 MethodInfo [] mmi = field.FieldType.GetMethods(BindingFlags.DeclaredOnly | 
664                                                         BindingFlags.Instance |
665                                                         BindingFlags.Public);
666
667
668                                                 foreach ( MethodInfo m in mmi ) 
669                                                 {
670                                                         if ( m.Name == "Add" ) 
671                                                         {
672                                                                 mm = m;
673                                                                 break;
674                                                         }
675                                                 }
676
677                                                 if ( mm != null ) 
678                                                 {
679                                                         ParameterInfo [] pi = mm.GetParameters();
680                                                         Type prmType1 = pi[0].ParameterType;
681                                                         Type prmType2 = null;
682                                                         if (pi.Length > 1) prmType2 = pi[1].ParameterType;
683
684                                                         if ( field.FieldType.GetInterface("IList") != null )
685                                                         {
686                                                                 for ( int i=0; i < ARRAY_SIZE; i++ )
687                                                                 {
688                                                                         if ( isPrimitive( prmType1 ) )
689                                                                         {
690                                                                                 ((IList)new_obj).Add( GetRandomValue( prmType1 ) );
691                                                                         }
692                                                                         else
693                                                                         {
694                                                                                 object prm_obj = Activator_CreateInstance( prmType1 );
695                                                                                 ((IList)new_obj).Add( Generate( prm_obj ) );
696                                                                         }
697                                                                 }
698                                                                 field.SetValue( obj, new_obj );
699                                                         }
700
701                                                         if ( prmType2 != null)
702                                                         {
703                                                                 if ( field.FieldType.GetInterface("IDictionary") != null)
704                                                                 {
705                                                                         for ( int i=0; i < ARRAY_SIZE; i++ )
706                                                                         {
707                                                                                 if ( isPrimitive( prmType1 ) && isPrimitive( prmType2 ) )
708                                                                                 {
709                                                                                         ((IDictionary)new_obj).Add( GetRandomValue( prmType1 ), GetRandomValue( prmType2 ) );
710                                                                                 }
711                                                                                 else
712                                                                                 {
713                                                                                         object prm_obj1 = Activator_CreateInstance( prmType1 );
714                                                                                         object prm_obj2 = Activator_CreateInstance( prmType2 );
715                                                                                         ((IDictionary)new_obj).Add( Generate( prm_obj1 ), Generate( prm_obj2 ) );
716                                                                                 }
717                                                                         }
718                                                                 }
719                                                                 field.SetValue( obj, new_obj );
720                                                         }
721                                                 }
722                                         } // collection
723
724                                         // is array of user defined type
725                                         //
726                                         if ( field.FieldType.IsArray )
727                                         {
728                                                 if ( !isCollection( field.FieldType ) )
729                                                 {
730                                                         Array arr  = Array.CreateInstance( field.FieldType.GetElementType(), ARRAY_SIZE );
731                                                         for ( int i=0; i < arr.Length; i++ )
732                                                         {
733                                                                 object new_obj = GHTTypeGenerator( field.FieldType.GetElementType() );
734                                                                 arr.SetValue( new_obj, i );
735                                                         }
736                                                         field.SetValue( obj, arr );
737                                                 }
738                                         }
739                                         else
740                                         {
741                                                 if ( !isCollection( field.FieldType ) )
742                                                 {
743                                                         object new_obj = GHTTypeGenerator( field.FieldType );
744                                                         field.SetValue( obj, new_obj );
745                                                 }
746
747                                         } // user defined type
748
749                                         // object
750                                         //
751                                         if ( isObject( field.FieldType ) )
752                                         {
753                                                 object new_obj = Activator_CreateInstance(field.FieldType);
754                                                 new_obj = "GH test";
755                                                 field.SetValue( obj, new_obj );
756                                         } // object
757                                 } // field info
758                         
759                                 // PropertyInfo
760                                 //
761                                 if ( mi is PropertyInfo )
762                                 {
763                                         PropertyInfo prop = (PropertyInfo)mi;
764
765                                         // is array of primitive type member
766                                         //
767                                         if ( prop.PropertyType.IsArray )
768                                         {
769                                                 if ( isPrimitive( prop.PropertyType.GetElementType() ) )
770                                                 {
771                                                         Array arr = Array.CreateInstance( prop.PropertyType.GetElementType(), ARRAY_SIZE);
772                                                         for (int i=0; i < arr.Length; i++)
773                                                         {
774                                                                 arr.SetValue(GetRandomValue( prop.PropertyType.GetElementType() ), i );
775                                                         }
776                                                         prop.SetValue( obj, arr, null );
777                                                 }
778                                         }
779                                         else
780                                         {
781                                                 if ( isPrimitive( prop.PropertyType ) )
782                                                 {
783                                                         prop.SetValue( obj, GetRandomValue( prop.PropertyType ), null );
784                                                 }
785                                         } // primitive
786
787                                         // Colletion type member
788                                         //
789                                         if ( isCollection( prop.PropertyType ) )
790                                         {
791                                                 object new_obj = Activator_CreateInstance( prop.PropertyType );
792
793                                                 MethodInfo mm = null;
794                                                 MethodInfo [] mmi = prop.PropertyType.GetMethods( BindingFlags.DeclaredOnly | 
795                                                         BindingFlags.Instance |
796                                                         BindingFlags.Public);
797
798
799                                                 foreach ( MethodInfo m in mmi ) 
800                                                 {
801                                                         if ( m.Name == "Add" ) 
802                                                         {
803                                                                 mm = m;
804                                                                 break;
805                                                         }
806                                                 }
807
808                                                 if ( mm != null ) 
809                                                 {
810                                                         ParameterInfo [] pi = mm.GetParameters();
811                                                         Type prmType1 = pi[0].ParameterType;
812                                                         Type prmType2 = null;
813                                                         if (pi.Length > 1) prmType2 = pi[1].ParameterType;
814
815                                                         if ( prop.PropertyType.GetInterface("IList") != null )
816                                                         {
817                                                                 for ( int i=0; i < ARRAY_SIZE; i++ )
818                                                                 {
819                                                                         if ( isPrimitive( prmType1 ) )
820                                                                         {
821                                                                                 ((IList)new_obj).Add( GetRandomValue( prmType1 ) );
822                                                                         }
823                                                                         else
824                                                                         {
825                                                                                 object prm_obj = Activator_CreateInstance( prmType1 );
826                                                                                 ((IList)new_obj).Add( Generate( prm_obj ) );
827                                                                         }
828                                                                 }
829                                                                 prop.SetValue( obj, new_obj, null );
830                                                         }
831
832                                                         if ( prmType2 != null)
833                                                         {
834                                                                 if ( prop.PropertyType.GetInterface("IDictionary") != null)
835                                                                 {
836                                                                         for ( int i=0; i < ARRAY_SIZE; i++ )
837                                                                         {
838                                                                                 if ( isPrimitive( prmType1 ) && isPrimitive( prmType2 ) )
839                                                                                 {
840                                                                                         ((IDictionary)new_obj).Add( GetRandomValue( prmType1 ), GetRandomValue( prmType2 ) );
841                                                                                 }
842                                                                                 else
843                                                                                 {
844                                                                                         object prm_obj1 = Activator_CreateInstance( prmType1 );
845                                                                                         object prm_obj2 = Activator_CreateInstance( prmType2 );
846                                                                                         ((IDictionary)new_obj).Add( Generate( prm_obj1 ), Generate( prm_obj2 ) );
847                                                                                 }
848                                                                         }
849                                                                         prop.SetValue( obj, new_obj, null );
850                                                                 }
851                                                         }
852                                                 }
853                                         } // collection
854
855                                         // is array user defined type
856                                         //
857                                         if ( prop.PropertyType.IsArray )
858                                         {
859                                                 if ( !isSystem( prop.PropertyType ) &&  !isCollection( prop.PropertyType ) )
860                                                 {
861                                                         Array arr  = Array.CreateInstance( prop.PropertyType.GetElementType(), ARRAY_SIZE );
862                                                         for ( int i=0; i < arr.Length; i++ )
863                                                         {
864                                                                 object new_obj  = Activator_CreateInstance( prop.PropertyType.GetElementType() );
865                                                                 Generate( new_obj );
866                                                                 arr.SetValue( new_obj, i );
867                                                         }
868                                                         prop.SetValue( obj, arr, null );
869                                                 }
870                                         }
871                                         else
872                                         {
873                                                 if ( !isSystem( prop.PropertyType ) &&  !isCollection( prop.PropertyType ) )
874                                                 {
875                                                         object new_obj = Activator_CreateInstance( prop.PropertyType );
876                                                         Generate(new_obj);
877                                                         prop.SetValue( obj, new_obj, null );
878                                                 }
879                                         } // user defined type
880
881                                         // object
882                                         //
883                                         if ( isObject( prop.PropertyType ) )
884                                         {
885                                                 object new_obj = Activator_CreateInstance( prop.PropertyType );
886                                                 new_obj = "GH test";
887                                                 prop.SetValue( obj, new_obj, null );
888                                         } // object
889                                 } // field info
890
891                         } // for each
892                         return obj;
893                 }
894
895                 static DataSet GenerateDataSet()
896                 {
897                         string strTemp = string.Empty;
898                         DataSet ds = new DataSet("CustOrdersDS");
899                         DataTable dtCusts = new DataTable("Customers");
900
901                         ds.Tables.Add(dtCusts);
902
903                         DataTable dtOrders = new DataTable("Orders");
904                         ds.Tables.Add(dtOrders);
905
906                         // add ID column with autoincrement numbering
907                         // and Unique constraint
908                         DataColumn dc = dtCusts.Columns.Add("ID", typeof(Int32));
909                         dc.AllowDBNull = false;
910                         dc.AutoIncrement = true;
911                         dc.AutoIncrementSeed = 1;
912                         dc.AutoIncrementStep = 1;
913                         dc.Unique = true;
914
915                         // make the ID column part of the PrimaryKey
916                         // for the table
917                         dtCusts.PrimaryKey = new DataColumn[] {dc};
918
919                         // add name and company columns with length restrictions
920                         // and default values
921                         dc = dtCusts.Columns.Add("Name", typeof(String));
922                         dc.MaxLength = 255;
923                         dc.DefaultValue = "nobody";
924                         dc = dtCusts.Columns.Add("Company", typeof(String));
925                         dc.MaxLength = 255;
926                         dc.DefaultValue = "nonexistent";
927
928                         // fill the table
929                         for (int i=0; i < 10; i++)
930                         {
931                                 DataRow dr = dtCusts.NewRow();
932                                 strTemp = (string)GetRandomValue(typeof(String));
933                                 if (strTemp.Length > 255) strTemp = strTemp.Remove(0,254);
934                                 dr["Name"] = strTemp;
935                                 strTemp = (string)GetRandomValue(typeof(String));
936                                 if (strTemp.Length > 255) strTemp = strTemp.Remove(0,254);
937                                 dr["Company"] = strTemp; 
938                                 dtCusts.Rows.Add(dr);
939                         }
940
941
942                         // add ID columns with autoincrement numbering
943                         // and Unique constraint
944                         dc = dtOrders.Columns.Add("ID", typeof(Int32));
945                         dc.AllowDBNull = false;
946                         dc.AutoIncrement = true;
947                         dc.AutoIncrementSeed = 1;
948                         dc.AutoIncrementStep = 1;
949                         dc.Unique = true;
950
951                         // add custid, date and total columns
952                         dtOrders.Columns.Add("CustID", typeof(Int32));
953                         dtOrders.Columns.Add("Date", typeof(DateTime));
954                         dtOrders.Columns.Add("Total", typeof(Decimal));
955
956                 
957                         for (int i=0; i < 10; i++)
958                         {
959                         
960                                 DataRow dr = dtOrders.NewRow();
961                                 dr["CustID"] = i;
962                                 dr["Date"] = GetRandomValue(typeof(DateTime));
963                                 dr["Total"] = i * i;
964                                 dtOrders.Rows.Add(dr);
965                         }
966
967                         // make the ID column part of the PrimaryKey
968                         // for the table
969                         dtOrders.PrimaryKey = new DataColumn[] {dc};
970
971                         return ds;
972                 }
973                 static DataTable GenerateDataTable()
974                 {
975                         DataTable dt = new DataTable("Customers");
976                         string strTemp = string.Empty;
977
978                         // add ID column with autoincrement numbering
979                         // and Unique constraint
980                         DataColumn dc = dt.Columns.Add("ID", typeof(Int32));
981                         dc.AllowDBNull = false;
982                         dc.AutoIncrement = true;
983                         dc.AutoIncrementSeed = 1;
984                         dc.AutoIncrementStep = 1;
985                         dc.Unique = true;
986
987                         // make the ID column part of the PrimaryKey
988                         // for the table
989                         dt.PrimaryKey = new DataColumn[] {dc};
990
991                         // add name and company columns with length restrictions
992                         //' and default values
993                         dc = dt.Columns.Add("Name", typeof(String));
994                         dc.MaxLength = 255;
995                         dc.DefaultValue = "nobody";
996                         dc = dt.Columns.Add("Company", typeof(String));
997                         dc.MaxLength = 255;
998                         dc.DefaultValue = "nonexistent";
999
1000                         // fill the table
1001                         for (int i=0; i < 10; i++)
1002                         {
1003                                 DataRow dr = dt.NewRow();
1004                                 strTemp = (string)GetRandomValue(typeof(String));
1005                                 if (strTemp.Length > 255) strTemp = strTemp.Remove(0,254);
1006                                 dr["Name"] = strTemp;
1007                                 strTemp = (string)GetRandomValue(typeof(String));
1008                                 if (strTemp.Length > 255) strTemp = strTemp.Remove(0,254);
1009                                 dr["Company"] = strTemp; 
1010                                 dt.Rows.Add(dr);
1011                         }
1012                         return dt;
1013                 }
1014
1015                 static object GenerateCollection(Type t)
1016                 {
1017                         object new_obj = Activator_CreateInstance( t );
1018
1019                         MethodInfo MI = null;
1020                         MethodInfo [] arrMI = t.GetMethods(BindingFlags.DeclaredOnly | 
1021                                 BindingFlags.Instance |
1022                                 BindingFlags.Public);
1023
1024                         foreach ( MethodInfo m in arrMI ) 
1025                         {
1026                                 if ( m.Name == "Add" ) 
1027                                 {
1028                                         MI = m;
1029                                         break;
1030                                 }
1031                         }
1032
1033                         if ( MI != null ) 
1034                         {
1035                                 ParameterInfo [] pi = MI.GetParameters();
1036                                 Type prmType1 = pi[0].ParameterType;
1037                                 Type prmType2 = null;
1038                                 if (pi.Length > 1) prmType2 = pi[1].ParameterType;
1039
1040                                 if ( t.GetInterface("IList") != null )
1041                                 {
1042                                         for ( int i=0; i < ARRAY_SIZE; i++ )
1043                                         {
1044                                                 if ( isPrimitive( prmType1 ) )
1045                                                 {
1046                                                         ((IList)new_obj).Add( GetRandomValue( prmType1 ) );
1047                                                 }
1048                                                 else
1049                                                 {
1050                                                         //object prm_obj = Activator_CreateInstance( prmType1 );
1051                                                         //((IList)new_obj).Add( Generate( prm_obj ) );
1052                                                         ((IList)new_obj).Add(GHTTypeGenerator(prmType1));
1053                                                 }
1054                                         }
1055                                         return new_obj;
1056                                 }
1057
1058                                 if ( prmType2 != null)
1059                                 {
1060                                         if ( t.GetInterface("IDictionary") != null)
1061                                         {
1062                                                 for ( int i=0; i < ARRAY_SIZE; i++ )
1063                                                 {
1064                                                         if ( isPrimitive( prmType1 ) && isPrimitive( prmType2 ) )
1065                                                         {
1066                                                                 ((IDictionary)new_obj).Add( GetRandomValue( prmType1 ), GetRandomValue( prmType2 ) );
1067                                                         }
1068                                                         else
1069                                                         {
1070                                                                 object prm_obj1 = Activator_CreateInstance( prmType1 );
1071                                                                 object prm_obj2 = Activator_CreateInstance( prmType2 );
1072                                                                 ((IDictionary)new_obj).Add( Generate( prm_obj1 ), Generate( prm_obj2 ) );
1073                                                         }
1074                                                 }
1075                                         }
1076                                         return new_obj;
1077                                 }
1078                         }// if ( MI != null ) 
1079                         return new_obj;
1080                 }
1081
1082                 static Array GenerateArray(Type t)
1083                 {
1084                         if ( isPrimitive( t.GetElementType() ) )
1085                         {
1086                                 Array arr = Array.CreateInstance( t.GetElementType(), ARRAY_SIZE);
1087                                 for (int i=0; i < arr.Length; i++)
1088                                 {
1089                                         arr.SetValue(GetRandomValue( t.GetElementType() ), i );
1090                                 }
1091                                 return arr;
1092                         }
1093                         else
1094                         {
1095                                 Array arr  = Array.CreateInstance( t.GetElementType(), ARRAY_SIZE );
1096                                 for ( int i=0; i < arr.Length; i++ )
1097                                 {
1098                                         //object new_obj  = Activator_CreateInstance( t.GetElementType() );
1099                                         //Generate( new_obj );
1100
1101                                         object new_obj  = GHTTypeGenerator(t.GetElementType());
1102                                         arr.SetValue( new_obj, i );
1103                                 }
1104                                 return arr;
1105                         }
1106                 }
1107
1108
1109                 static bool isPrimitive(Type t)
1110                 {
1111                         if ( t.IsPrimitive ) return true;
1112                         if ( t.Name == "String" ) return true;
1113                         if ( t.Name == "DateTime" ) return true;
1114                         if ( t.Name == "Decimal" ) return true;
1115                         return false;
1116                 }
1117
1118                 static bool isSystem(Type t)
1119                 {
1120                         if ( t.FullName == "System.Collections" ) return false;
1121                         if ( t.FullName.StartsWith("System.") ) return true;
1122                         return false;
1123                 }
1124
1125                 static bool isObject(Type t)
1126                 {
1127                         if ( t.FullName == "System.Object" ) return true;
1128                         return false;
1129                 }
1130
1131                 static bool isCollection(Type t)
1132                 {
1133                         if ( t.GetInterface("IList") != null) return true;
1134                         if ( t.GetInterface("IDictionary") != null) return true;
1135                         if ( t.GetInterface("ICollection") != null) return true;
1136                         return false;
1137                 }
1138
1139
1140
1141                 static object Activator_CreateInstance(Type t)
1142                 {
1143                         try
1144                         {
1145                                 if (t.IsEnum)
1146                                 {
1147                                         Array a = Enum.GetNames(t);
1148                                         return Enum.Parse(t,a.GetValue(0).ToString());
1149                                 }
1150                                 else
1151                     return t.GetConstructor(new Type[]{}).Invoke(new object[]{});
1152                         }
1153                         catch( Exception ex )
1154                         {
1155                                 throw new Exception("Activator - Could not create type " + t.Name + " - " + ex.Message);
1156                         }
1157                 }
1158
1159         }       
1160
1161 }