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