GHTValueGen.cs 31 KB

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