2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Reflection;
31 using System.Collections;
34 //this class is used at the WebServices Test Harness
40 private const int ARRAY_SIZE = 7;
42 public static object GHTTypeGenerator( Type t )
45 if ( t.Name == "XmlNode" || t.Name == "XmlElement" || t.Name == "DataSet" || t.Name == "DataTable" )
47 result = GetRandomValue( t );
50 //====================================================================================
52 //====================================================================================
53 else if ( isPrimitive( t ) )
55 result = GetRandomValue( t );
58 //====================================================================================
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).
65 result = GenerateArray(t);
68 //====================================================================================
70 //====================================================================================
71 else if ( isCollection( t ) )
73 result = GenerateCollection(t);
76 //====================================================================================
78 //====================================================================================
81 result = Activator_CreateInstance( t );
82 result = Generate ( result );
87 public static object GHTObjModifier( object obj )
89 if ( isPrimitive( obj.GetType() ) )
91 return GetModifiedValue( obj );
93 else if ( obj.GetType().IsArray )
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();
102 ElementType = obj.GetType().GetElementType();
104 if ( isPrimitive(ElementType) )
106 Array arr = (Array)obj;
107 for (int i=0; i < arr.Length; i++)
109 arr.SetValue(GetModifiedValue( arr.GetValue( i ) ), i );
115 Array arr = (Array)obj;
116 for ( int i=0; i < arr.Length; i++ )
118 object new_obj = arr.GetValue( i );
119 new_obj = GHTObjModifier( new_obj );
120 arr.SetValue( new_obj, i );
125 else if ( obj.GetType().IsEnum )
127 Array a = Enum.GetValues(obj.GetType());
129 return a.GetValue(a.Length-2);
131 return a.GetValue(a.Length-1); //leave the same value
133 else if (obj.GetType().Name == "DataTable")
135 ModifyDataTable((System.Data.DataTable)obj);
138 else if (obj.GetType().Name == "DataSet")
140 ModifyDataSet((System.Data.DataSet)obj);
143 else if (obj.GetType().Name == "XmlNode" || obj.GetType().Name == "XmlElement")
145 ModifyXmlElement((System.Xml.XmlElement)obj);
148 else if (isCollection(obj.GetType()))
150 ModifyCollection(obj);
162 static object GetRandomValue(Type t)
164 object objOut = null;
166 System.Threading.Thread.Sleep(10);
167 System.Random rnd = new System.Random(unchecked((int)DateTime.Now.Ticks));
169 if (t.FullName == "System.Boolean")
171 objOut = System.Convert.ToBoolean(rnd.Next(0, 1));
174 else if (t.FullName == "System.Byte")
176 objOut = System.Convert.ToByte(rnd.Next(System.Byte.MinValue+1, System.Byte.MaxValue-128));
179 else if (t.FullName == "System.Char")
181 objOut = System.Convert.ToChar(rnd.Next(System.Char.MinValue+65, System.Char.MaxValue-128));
184 else if (t.FullName == "System.DateTime")
186 //GH precision is only milliseconds
187 objOut = System.Convert.ToDateTime(new System.DateTime(632083133257660000));
190 else if (t.FullName == "System.Decimal")
192 objOut = System.Convert.ToDecimal(rnd.Next(System.Int16.MinValue+1, System.Int16.MaxValue-128));
195 else if (t.FullName == "System.Double")
197 //give max length of "MaxLength" digits
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);
204 else if (t.FullName == "System.Int16")
206 objOut = System.Convert.ToInt16(rnd.Next(System.Int16.MinValue+1,System.Int16.MaxValue-128));
209 else if (t.FullName == "System.Int32")
211 objOut = System.Convert.ToInt32(rnd.Next(System.Int16.MinValue+1,System.Int16.MaxValue-128));
214 else if (t.FullName == "System.Int64")
216 objOut = System.Convert.ToInt64(rnd.Next(System.Int16.MinValue+1,System.Int16.MaxValue-128));
219 else if (t.FullName == "System.SByte")
221 objOut = System.Convert.ToSByte(rnd.Next(System.SByte.MinValue+1,System.SByte.MaxValue-128));
224 else if (t.FullName == "System.Single")
226 objOut = System.Convert.ToSingle(rnd.Next(System.Int16.MinValue+1, System.Int16.MaxValue-128));
229 else if (t.FullName == "System.String")
231 long size = DateTime.Now.Ticks;
233 if (size==0) size = 16;
234 for (int i=0; i<size ;i++)
236 str += System.Convert.ToChar(rnd.Next(System.Byte.MinValue+65, System.Byte.MaxValue-128));
241 else if (t.FullName == "System.UInt16")
243 objOut = System.Convert.ToUInt16(rnd.Next(System.UInt16.MinValue+1,System.UInt16.MaxValue-128));
246 else if (t.FullName == "System.UInt32")
248 objOut = System.Convert.ToUInt32(rnd.Next((int)System.UInt32.MinValue+1,System.Int32.MaxValue-128));
251 else if (t.FullName == "System.UInt64")
253 objOut = System.Convert.ToUInt64(rnd.Next((int)System.UInt64.MinValue+1,System.Int32.MaxValue-128));
256 else if (t.FullName == "System.Data.DataTable")
258 objOut = GenerateDataTable();
261 else if (t.FullName == "System.Data.DataSet")
263 objOut = GenerateDataSet();
266 else if (t.FullName == "System.Xml.XmlNode" || t.FullName == "System.Xml.XmlElement")
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>" +
273 // "<author>Carson</author>" +
274 // "<price format=\"dollar\">31.95</price>" +
275 // "<pubdate>05/01/2001</pubdate>" +
278 // "<publisher>MSPress</publisher>" +
279 // "<state>WA</state>" +
286 throw new System.NotImplementedException("GetRandomValue error: Type " + t.Name + " not implemented.");
290 static object GetModifiedValue(object objIn)
292 object objOut = null;
294 if (objIn.GetType().FullName == "System.Boolean")
296 bool BoolVar =!(bool)objIn;
299 else if (objIn.GetType().FullName == "System.Byte")
301 if ((byte)objIn == byte.MaxValue)
304 return System.Convert.ToByte((byte)objIn + (byte)1);
306 else if (objIn.GetType().FullName == "System.Char")
308 if ((char)objIn == char.MaxValue)
311 return System.Convert.ToChar((char)objIn + (char)1);
313 else if (objIn.GetType().FullName == "System.DateTime")
315 objOut = System.Convert.ToDateTime(objIn);
316 objOut = ((DateTime)objOut).AddHours(1);
319 else if (objIn.GetType().FullName == "System.Decimal")
321 if ((decimal)objIn == decimal.MaxValue)
324 objOut = System.Convert.ToDecimal(System.Convert.ToDecimal(objIn) + (decimal)1);
327 else if (objIn.GetType().FullName == "System.Double")
329 if ((double)objIn == double.MaxValue)
332 objOut = System.Convert.ToDouble(System.Convert.ToDouble(objIn) + (double)1);
335 else if (objIn.GetType().FullName == "System.Int16")
337 if ((Int16)objIn == Int16.MaxValue)
340 objOut = System.Convert.ToInt16(System.Convert.ToInt16(objIn) + (Int16)1);
343 else if (objIn.GetType().FullName == "System.Int32")
345 if ((Int32)objIn == Int32.MaxValue)
348 objOut = System.Convert.ToInt32(System.Convert.ToInt32(objIn) + (Int32)1);
351 else if (objIn.GetType().FullName == "System.Int64")
353 if ((Int64)objIn == Int64.MaxValue)
356 objOut = System.Convert.ToInt64(System.Convert.ToInt64(objIn) + (Int64)1);
359 else if (objIn.GetType().FullName == "System.SByte")
361 if ((SByte)objIn == SByte.MaxValue)
364 objOut = System.Convert.ToSByte(System.Convert.ToSByte(objIn) + (SByte)1);
367 else if (objIn.GetType().FullName == "System.Single")
369 if ((Single)objIn == Single.MaxValue)
372 objOut = System.Convert.ToSingle(System.Convert.ToSingle(objIn) + (Single)1);
375 else if (objIn.GetType().FullName == "System.String")
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();
384 objOut += strin[ii].ToString().ToLower();
387 else if (objIn.GetType().FullName == "System.UInt16")
389 if ((UInt16)objIn == UInt16.MaxValue)
392 objOut = System.Convert.ToUInt16(System.Convert.ToUInt16(objIn) + (UInt16)1);
395 else if (objIn.GetType().FullName == "System.UInt32")
397 if ((UInt32)objIn == UInt32.MaxValue)
400 objOut = System.Convert.ToUInt32(System.Convert.ToUInt32(objIn) + (UInt32)1);
403 else if (objIn.GetType().FullName == "System.UInt64")
405 if ((UInt64)objIn == UInt64.MaxValue)
408 objOut = System.Convert.ToUInt64(System.Convert.ToUInt64(objIn) + (UInt64)1);
413 throw new System.NotImplementedException("GetModifiedValue error: Type " + objIn.GetType().FullName + " not implemented.");
419 static void Modify( object obj )
421 if ( obj.GetType().IsEnum )
423 Array a = Enum.GetValues(obj.GetType());
425 obj = a.GetValue(a.Length-2);
427 obj = a.GetValue(a.Length-1); //leave the same value
433 mic = obj.GetType().GetMembers( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic );
434 foreach ( MemberInfo mi in mic )
436 // ---------- FieldInfo ----------
437 if ( mi is FieldInfo )
439 FieldInfo field = (FieldInfo)mi;
440 Type fieldType = field.FieldType;
443 if ( fieldType.IsArray )
445 // is array of primitive
446 if ( isPrimitive( fieldType.GetElementType() ) )
448 Array arr = (Array)field.GetValue( obj );
449 for (int i=0; i < arr.Length; i++)
451 arr.SetValue(GetModifiedValue( arr.GetValue( i ) ), i );
453 field.SetValue( obj, arr );
456 // is array of user defined type
457 if ( !isSystem( fieldType ) && !isCollection( fieldType ) )
459 Array arr = (Array)field.GetValue( obj );
460 for ( int i=0; i < arr.Length; i++ )
462 object new_obj = arr.GetValue( i );
464 arr.SetValue( new_obj, i );
466 field.SetValue( obj, arr );
472 if ( isPrimitive( fieldType ) )
474 field.SetValue( obj, GetModifiedValue( field.GetValue( obj ) ) );
476 if ( !isSystem( fieldType ) && !isCollection( fieldType ) )
478 object new_obj = field.GetValue( obj );
480 field.SetValue( obj, new_obj );
483 if ( isObject( fieldType ) )
485 object new_obj = field.GetValue( obj );
487 field.SetValue( obj, new_obj );
493 // ---------- PropertyInfo ----------
495 if ( mi is PropertyInfo )
497 PropertyInfo prop = (PropertyInfo)mi;
499 if ( prop.PropertyType.IsArray )
501 // is array of primitive type member
502 if ( isPrimitive( prop.PropertyType.GetElementType() ) )
504 Array arr = (Array)prop.GetValue( obj, null );
505 for (int i=0; i < arr.Length; i++)
507 arr.SetValue(GetModifiedValue( arr.GetValue( i ) ), i );
509 prop.SetValue( obj, arr, null );
512 //is array user defined type
513 if ( !isSystem( prop.PropertyType ) && !isCollection( prop.PropertyType ) )
515 Array arr = (Array)prop.GetValue( obj, null );
516 for ( int i=0; i < arr.Length; i++ )
518 object new_obj = arr.GetValue( i );
520 arr.SetValue( new_obj, i );
522 prop.SetValue( obj, arr, null );
528 if ( isPrimitive( prop.PropertyType ) )
530 prop.SetValue( obj, GetModifiedValue( prop.GetValue( obj, null ) ), null );
534 if ( !isSystem( prop.PropertyType ) && !isCollection( prop.PropertyType ) )
536 object new_obj = prop.GetValue( obj, null );
538 prop.SetValue( obj, new_obj, null );
542 if ( isObject( prop.PropertyType ) )
544 object new_obj = prop.GetValue( obj, null );
546 prop.SetValue( obj, new_obj, null );
554 static void ModifyDataSet(DataSet ds)
556 foreach (DataTable dt in ds.Tables)
561 static void ModifyDataTable(DataTable dt)
563 foreach(DataRow dr in dt.Rows)
565 foreach (DataColumn dc in dt.Columns)
567 switch (dc.DataType.Name)
570 dr[dc] = dr[dc].ToString() + "mod";
573 dr[dc] = Convert.ToInt32( dr[dc] ) * 100;
580 static void ModifyXmlElement(System.Xml.XmlElement xmlElem)
582 xmlElem.InnerText = "54321";
583 // xmlElem.InnerXml = "<books>" +
585 // "<author>Carson</author>" +
586 // "<price format=\"dollar\">33.99</price>" +
587 // "<pubdate>01/01/2003</pubdate>" +
590 // "<publisher>MisPress</publisher>" +
591 // "<state>CA</state>" +
596 static void ModifyCollection(object co)
598 for (int i=0; i < ((IList)co).Count; i++)
600 object o = ((IList)co)[i];
601 o = GHTObjModifier(o);
606 static object Generate( object obj )
610 if ( obj == null ) return null;
612 if (obj.GetType().IsEnum)
614 Array a = Enum.GetValues(obj.GetType());
615 return a.GetValue(a.Length-1);
618 if ( isObject( obj.GetType() ))
620 //obj = GetRandomValue( typeof( System.String ) );
625 mic = obj.GetType().GetMembers( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic );
626 foreach ( MemberInfo mi in mic )
630 if ( mi is FieldInfo )
632 FieldInfo field = (FieldInfo)mi;
634 // is array of primitive
636 if ( field.FieldType.IsArray )
638 if ( isPrimitive( field.FieldType.GetElementType() ) )
640 Array arr = Array.CreateInstance( field.FieldType.GetElementType(), ARRAY_SIZE);
641 for (int i=0; i < arr.Length; i++)
643 arr.SetValue(GetRandomValue( field.FieldType.GetElementType() ), i );
645 field.SetValue( obj, arr );
650 if ( isPrimitive( field.FieldType ) )
652 field.SetValue( obj, GetRandomValue( field.FieldType ) );
656 // Collection type member
658 if ( isCollection( field.FieldType ) )
660 object new_obj = Activator_CreateInstance(field.FieldType);
662 MethodInfo mm = null;
663 MethodInfo [] mmi = field.FieldType.GetMethods(BindingFlags.DeclaredOnly |
664 BindingFlags.Instance |
665 BindingFlags.Public);
668 foreach ( MethodInfo m in mmi )
670 if ( m.Name == "Add" )
679 ParameterInfo [] pi = mm.GetParameters();
680 Type prmType1 = pi[0].ParameterType;
681 Type prmType2 = null;
682 if (pi.Length > 1) prmType2 = pi[1].ParameterType;
684 if ( field.FieldType.GetInterface("IList") != null )
686 for ( int i=0; i < ARRAY_SIZE; i++ )
688 if ( isPrimitive( prmType1 ) )
690 ((IList)new_obj).Add( GetRandomValue( prmType1 ) );
694 object prm_obj = Activator_CreateInstance( prmType1 );
695 ((IList)new_obj).Add( Generate( prm_obj ) );
698 field.SetValue( obj, new_obj );
701 if ( prmType2 != null)
703 if ( field.FieldType.GetInterface("IDictionary") != null)
705 for ( int i=0; i < ARRAY_SIZE; i++ )
707 if ( isPrimitive( prmType1 ) && isPrimitive( prmType2 ) )
709 ((IDictionary)new_obj).Add( GetRandomValue( prmType1 ), GetRandomValue( prmType2 ) );
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 ) );
719 field.SetValue( obj, new_obj );
724 // is array of user defined type
726 if ( field.FieldType.IsArray )
728 if ( !isCollection( field.FieldType ) )
730 Array arr = Array.CreateInstance( field.FieldType.GetElementType(), ARRAY_SIZE );
731 for ( int i=0; i < arr.Length; i++ )
733 object new_obj = GHTTypeGenerator( field.FieldType.GetElementType() );
734 arr.SetValue( new_obj, i );
736 field.SetValue( obj, arr );
741 if ( !isCollection( field.FieldType ) )
743 object new_obj = GHTTypeGenerator( field.FieldType );
744 field.SetValue( obj, new_obj );
747 } // user defined type
751 if ( isObject( field.FieldType ) )
753 object new_obj = Activator_CreateInstance(field.FieldType);
755 field.SetValue( obj, new_obj );
761 if ( mi is PropertyInfo )
763 PropertyInfo prop = (PropertyInfo)mi;
765 // is array of primitive type member
767 if ( prop.PropertyType.IsArray )
769 if ( isPrimitive( prop.PropertyType.GetElementType() ) )
771 Array arr = Array.CreateInstance( prop.PropertyType.GetElementType(), ARRAY_SIZE);
772 for (int i=0; i < arr.Length; i++)
774 arr.SetValue(GetRandomValue( prop.PropertyType.GetElementType() ), i );
776 prop.SetValue( obj, arr, null );
781 if ( isPrimitive( prop.PropertyType ) )
783 prop.SetValue( obj, GetRandomValue( prop.PropertyType ), null );
787 // Colletion type member
789 if ( isCollection( prop.PropertyType ) )
791 object new_obj = Activator_CreateInstance( prop.PropertyType );
793 MethodInfo mm = null;
794 MethodInfo [] mmi = prop.PropertyType.GetMethods( BindingFlags.DeclaredOnly |
795 BindingFlags.Instance |
796 BindingFlags.Public);
799 foreach ( MethodInfo m in mmi )
801 if ( m.Name == "Add" )
810 ParameterInfo [] pi = mm.GetParameters();
811 Type prmType1 = pi[0].ParameterType;
812 Type prmType2 = null;
813 if (pi.Length > 1) prmType2 = pi[1].ParameterType;
815 if ( prop.PropertyType.GetInterface("IList") != null )
817 for ( int i=0; i < ARRAY_SIZE; i++ )
819 if ( isPrimitive( prmType1 ) )
821 ((IList)new_obj).Add( GetRandomValue( prmType1 ) );
825 object prm_obj = Activator_CreateInstance( prmType1 );
826 ((IList)new_obj).Add( Generate( prm_obj ) );
829 prop.SetValue( obj, new_obj, null );
832 if ( prmType2 != null)
834 if ( prop.PropertyType.GetInterface("IDictionary") != null)
836 for ( int i=0; i < ARRAY_SIZE; i++ )
838 if ( isPrimitive( prmType1 ) && isPrimitive( prmType2 ) )
840 ((IDictionary)new_obj).Add( GetRandomValue( prmType1 ), GetRandomValue( prmType2 ) );
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 ) );
849 prop.SetValue( obj, new_obj, null );
855 // is array user defined type
857 if ( prop.PropertyType.IsArray )
859 if ( !isSystem( prop.PropertyType ) && !isCollection( prop.PropertyType ) )
861 Array arr = Array.CreateInstance( prop.PropertyType.GetElementType(), ARRAY_SIZE );
862 for ( int i=0; i < arr.Length; i++ )
864 object new_obj = Activator_CreateInstance( prop.PropertyType.GetElementType() );
866 arr.SetValue( new_obj, i );
868 prop.SetValue( obj, arr, null );
873 if ( !isSystem( prop.PropertyType ) && !isCollection( prop.PropertyType ) )
875 object new_obj = Activator_CreateInstance( prop.PropertyType );
877 prop.SetValue( obj, new_obj, null );
879 } // user defined type
883 if ( isObject( prop.PropertyType ) )
885 object new_obj = Activator_CreateInstance( prop.PropertyType );
887 prop.SetValue( obj, new_obj, null );
895 static DataSet GenerateDataSet()
897 string strTemp = string.Empty;
898 DataSet ds = new DataSet("CustOrdersDS");
899 DataTable dtCusts = new DataTable("Customers");
901 ds.Tables.Add(dtCusts);
903 DataTable dtOrders = new DataTable("Orders");
904 ds.Tables.Add(dtOrders);
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;
915 // make the ID column part of the PrimaryKey
917 dtCusts.PrimaryKey = new DataColumn[] {dc};
919 // add name and company columns with length restrictions
920 // and default values
921 dc = dtCusts.Columns.Add("Name", typeof(String));
923 dc.DefaultValue = "nobody";
924 dc = dtCusts.Columns.Add("Company", typeof(String));
926 dc.DefaultValue = "nonexistent";
929 for (int i=0; i < 10; i++)
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);
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;
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));
957 for (int i=0; i < 10; i++)
960 DataRow dr = dtOrders.NewRow();
962 dr["Date"] = GetRandomValue(typeof(DateTime));
964 dtOrders.Rows.Add(dr);
967 // make the ID column part of the PrimaryKey
969 dtOrders.PrimaryKey = new DataColumn[] {dc};
973 static DataTable GenerateDataTable()
975 DataTable dt = new DataTable("Customers");
976 string strTemp = string.Empty;
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;
987 // make the ID column part of the PrimaryKey
989 dt.PrimaryKey = new DataColumn[] {dc};
991 // add name and company columns with length restrictions
992 //' and default values
993 dc = dt.Columns.Add("Name", typeof(String));
995 dc.DefaultValue = "nobody";
996 dc = dt.Columns.Add("Company", typeof(String));
998 dc.DefaultValue = "nonexistent";
1001 for (int i=0; i < 10; i++)
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;
1015 static object GenerateCollection(Type t)
1017 object new_obj = Activator_CreateInstance( t );
1019 MethodInfo MI = null;
1020 MethodInfo [] arrMI = t.GetMethods(BindingFlags.DeclaredOnly |
1021 BindingFlags.Instance |
1022 BindingFlags.Public);
1024 foreach ( MethodInfo m in arrMI )
1026 if ( m.Name == "Add" )
1035 ParameterInfo [] pi = MI.GetParameters();
1036 Type prmType1 = pi[0].ParameterType;
1037 Type prmType2 = null;
1038 if (pi.Length > 1) prmType2 = pi[1].ParameterType;
1040 if ( t.GetInterface("IList") != null )
1042 for ( int i=0; i < ARRAY_SIZE; i++ )
1044 if ( isPrimitive( prmType1 ) )
1046 ((IList)new_obj).Add( GetRandomValue( prmType1 ) );
1050 //object prm_obj = Activator_CreateInstance( prmType1 );
1051 //((IList)new_obj).Add( Generate( prm_obj ) );
1052 ((IList)new_obj).Add(GHTTypeGenerator(prmType1));
1058 if ( prmType2 != null)
1060 if ( t.GetInterface("IDictionary") != null)
1062 for ( int i=0; i < ARRAY_SIZE; i++ )
1064 if ( isPrimitive( prmType1 ) && isPrimitive( prmType2 ) )
1066 ((IDictionary)new_obj).Add( GetRandomValue( prmType1 ), GetRandomValue( prmType2 ) );
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 ) );
1078 }// if ( MI != null )
1082 static Array GenerateArray(Type t)
1084 if ( isPrimitive( t.GetElementType() ) )
1086 Array arr = Array.CreateInstance( t.GetElementType(), ARRAY_SIZE);
1087 for (int i=0; i < arr.Length; i++)
1089 arr.SetValue(GetRandomValue( t.GetElementType() ), i );
1095 Array arr = Array.CreateInstance( t.GetElementType(), ARRAY_SIZE );
1096 for ( int i=0; i < arr.Length; i++ )
1098 //object new_obj = Activator_CreateInstance( t.GetElementType() );
1099 //Generate( new_obj );
1101 object new_obj = GHTTypeGenerator(t.GetElementType());
1102 arr.SetValue( new_obj, i );
1109 static bool isPrimitive(Type t)
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;
1118 static bool isSystem(Type t)
1120 if ( t.FullName == "System.Collections" ) return false;
1121 if ( t.FullName.StartsWith("System.") ) return true;
1125 static bool isObject(Type t)
1127 if ( t.FullName == "System.Object" ) return true;
1131 static bool isCollection(Type t)
1133 if ( t.GetInterface("IList") != null) return true;
1134 if ( t.GetInterface("IDictionary") != null) return true;
1135 if ( t.GetInterface("ICollection") != null) return true;
1141 static object Activator_CreateInstance(Type t)
1147 Array a = Enum.GetNames(t);
1148 return Enum.Parse(t,a.GetValue(0).ToString());
1151 return t.GetConstructor(new Type[]{}).Invoke(new object[]{});
1153 catch( Exception ex )
1155 throw new Exception("Activator - Could not create type " + t.Name + " - " + ex.Message);