DataProvider.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.Data;
  30. using System.Data.OleDb ;
  31. using System.IO;
  32. using GHTUtils.Data;
  33. using System.Collections;
  34. // Provide All Data required by the diffderent tests e.g.DataTable, DataRow ...
  35. namespace GHTUtils
  36. {
  37. public class DataProvider
  38. {
  39. #region Constatntas
  40. #region Private
  41. //A string containing all printable charachters.
  42. private const string SAMPLE_STRING = "abcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*()_+-=[]\\|;:,./<>? ";
  43. #endregion
  44. #endregion
  45. public static System.Data.DataTable CreateChildDataTable()
  46. {
  47. System.Data.DataTable dtChild = new System.Data.DataTable("Child");
  48. dtChild.Columns.Add("ParentId",typeof(int));
  49. dtChild.Columns.Add("ChildId",typeof(int));
  50. dtChild.Columns.Add("String1",typeof(string));
  51. dtChild.Columns.Add("String2",typeof(string));
  52. dtChild.Columns.Add("ChildDateTime",typeof(DateTime));
  53. dtChild.Columns.Add("ChildDouble",typeof(double));
  54. dtChild.Rows.Add(new object[] {1,1,"1-String1","1-String2",new DateTime(2000,1,1,0,0,0,0),1.534});
  55. dtChild.Rows.Add(new object[] {1,2,"2-String1","2-String2",DateTime.MaxValue ,-1.534});
  56. dtChild.Rows.Add(new object[] {1,3,"3-String1","3-String2",DateTime.MinValue,double.MaxValue/10000});
  57. dtChild.Rows.Add(new object[] {2,1,"1-String1","1-String2",new DateTime(1973,6,20,0,0,0,0),double.MinValue*10000});
  58. dtChild.Rows.Add(new object[] {2,2,"2-String1","2-String2",new DateTime(2008,12,1,13,59,59,59),0.45});
  59. dtChild.Rows.Add(new object[] {2,3,"3-String1","3-String2",new DateTime(2003,1,1,1,1,1,1),0.55});
  60. dtChild.Rows.Add(new object[] {5,1,"1-String1","1-String2",new DateTime(2002,1,1,1,1,1,1),0});
  61. dtChild.Rows.Add(new object[] {5,2,"2-String1","2-String2",new DateTime(2001,1,1,1,1,1,1),10});
  62. dtChild.Rows.Add(new object[] {5,3,"3-String1","3-String2",new DateTime(2000,1,1,1,1,1,1),20});
  63. dtChild.Rows.Add(new object[] {6,1,"1-String1","1-String2",new DateTime(2000,1,1,1,1,1,0),25});
  64. dtChild.Rows.Add(new object[] {6,2,"2-String1","2-String2",new DateTime(2000,1,1,1,1,0,0),30});
  65. dtChild.Rows.Add(new object[] {6,3,"3-String1","3-String2",new DateTime(2000,1,1,0,0,0,0),35});
  66. dtChild.AcceptChanges();
  67. return dtChild;
  68. }
  69. public static System.Data.DataTable CreateParentDataTable()
  70. {
  71. System.Data.DataTable dtParent = new System.Data.DataTable("Parent");
  72. dtParent.Columns.Add("ParentId",typeof(int));
  73. dtParent.Columns.Add("String1",typeof(string));
  74. dtParent.Columns.Add("String2",typeof(string));
  75. dtParent.Columns.Add("ParentDateTime",typeof(DateTime));
  76. dtParent.Columns.Add("ParentDouble",typeof(double));
  77. dtParent.Columns.Add("ParentBool",typeof(bool));
  78. dtParent.Rows.Add(new object[] {1,"1-String1","1-String2",new DateTime(2005,1,1,0,0,0,0),1.534,true});
  79. dtParent.Rows.Add(new object[] {2,"2-String1","2-String2",new DateTime(2004,1,1,0,0,0,1),-1.534,true});
  80. dtParent.Rows.Add(new object[] {3,"3-String1","3-String2",new DateTime(2003,1,1,0,0,1,0),double.MinValue*10000,false});
  81. dtParent.Rows.Add(new object[] {4,"4-String1","4-String2",new DateTime(2002,1,1,0,1,0,0),double.MaxValue/10000,true});
  82. dtParent.Rows.Add(new object[] {5,"5-String1","5-String2",new DateTime(2001,1,1,1,0,0,0),0.755,true});
  83. dtParent.Rows.Add(new object[] {6,"6-String1","6-String2",new DateTime(2000,1,1,0,0,0,0),0.001,false});
  84. dtParent.AcceptChanges();
  85. return dtParent;
  86. }
  87. //This method replace the DataSet GetXmlSchema method
  88. //used to compare DataSets
  89. //Created by Ofer (13-Nov-03) becuase DataSet GetXmlSchema method is not yet implemented in java
  90. public static string GetDSSchema(DataSet ds)
  91. {
  92. string strSchema = "DataSet Name=" + ds.DataSetName + "\n";
  93. //Get relations
  94. foreach (DataRelation dl in ds.Relations)
  95. {
  96. strSchema += "\t" + "DataRelation Name=" + dl.RelationName ;
  97. foreach (DataColumn dc in dl.ParentColumns)
  98. strSchema += "\t" + "ParentColummn=" + dc.ColumnName ;
  99. foreach (DataColumn dc in dl.ChildColumns )
  100. strSchema += "\t" + "ChildColumn=" + dc.ColumnName ;
  101. strSchema += "\n";
  102. }
  103. //Get teables
  104. foreach (DataTable dt in ds.Tables)
  105. {
  106. strSchema += "Table=" + dt.TableName + "\t";
  107. //Get Constraints
  108. strSchema += "Constraints =";
  109. foreach (Constraint cs in dt.Constraints )
  110. strSchema += cs.GetType().Name + ", ";
  111. strSchema += "\n";
  112. //Get PrimaryKey Columns
  113. strSchema += "PrimaryKey Columns index:=";
  114. foreach (DataColumn dc in dt.PrimaryKey)
  115. strSchema += dc.Ordinal + ", ";
  116. strSchema += "\n";
  117. //Get Columns
  118. foreach (DataColumn dc in dt.Columns)
  119. {
  120. strSchema += "ColumnName=" + dc.ColumnName + "\t" +
  121. "ColumnType=" + dc.DataType.Name + "\t" +
  122. "AllowDBNull=" + dc.AllowDBNull.ToString() + "\t" +
  123. "DefaultValue=" + dc.DefaultValue.ToString() + "\t" +
  124. "Unique=" + dc.Unique.ToString() + "\t" +
  125. "ReadOnly=" + dc.ReadOnly.ToString() + "\n" ;
  126. }
  127. strSchema += "\n";
  128. }
  129. return strSchema;
  130. }
  131. public static DataTable CreateUniqueConstraint()
  132. {
  133. DataTable dt = GHTUtils.DataProvider.CreateParentDataTable();
  134. return CreateUniqueConstraint(dt);
  135. }
  136. public static DataTable CreateUniqueConstraint(DataTable dt)
  137. {
  138. Constraint con = new UniqueConstraint(dt.Columns["ParentId"]);
  139. dt.Constraints.Add(con);
  140. return dt;
  141. }
  142. public static void TryToBreakUniqueConstraint()
  143. {
  144. //Create the constraint
  145. DataTable dt = CreateUniqueConstraint();
  146. //Try to violate the constraint
  147. DataRow dr1 = dt.NewRow();
  148. dr1[0] = 1;
  149. dt.Rows.Add(dr1);
  150. }
  151. public static DataSet CreateForigenConstraint()
  152. {
  153. DataTable parent = GHTUtils.DataProvider.CreateParentDataTable();
  154. DataTable child = GHTUtils.DataProvider.CreateChildDataTable();
  155. DataSet ds = new DataSet();
  156. ds.Tables.Add(parent);
  157. ds.Tables.Add(child);
  158. Constraint con1 = new ForeignKeyConstraint(parent.Columns[0],child.Columns[0]);
  159. child.Constraints.Add(con1);
  160. return ds;
  161. }
  162. public static void TryToBreakForigenConstraint()
  163. {
  164. DataSet ds = CreateForigenConstraint();
  165. //Code to break:
  166. DataRow dr = ds.Tables[1].NewRow();
  167. dr[0]=7;
  168. ds.Tables[1].Rows.Add(dr);
  169. ds.AcceptChanges();
  170. ds.EnforceConstraints=true;
  171. }
  172. }
  173. }