2005-05-04 Sureshkumar T <tsureshkumar@novell.com>
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableTest.cs
index 81314b105e0e54c39197ae2f508155a2dd7de470..34f6eeb1190bc7b6d16eecee834d4d9e779952f6 100644 (file)
@@ -8,10 +8,36 @@
 // (C) 2003 Martin Willemoes Hansen
 // 
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using NUnit.Framework;
 using System;
 using System.Data;
 using System.Globalization;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Xml;
 
 namespace MonoTests.System.Data
 {
@@ -758,7 +784,7 @@ namespace MonoTests.System.Data
 
                        DataColumn col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table.Columns.Add (col);
                        UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
                        table.Constraints.Add (uc);
@@ -766,16 +792,16 @@ namespace MonoTests.System.Data
                                                                                                                            
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table.Columns.Add (col);
                        
                        col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table1.Columns.Add (col);
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table1.Columns.Add (col);
 
                        DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
@@ -824,12 +850,12 @@ namespace MonoTests.System.Data
 
                        DataColumn col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table.Columns.Add (col);
                                                                                                                              
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table.Columns.Add (col);
                        
                        DataRow row = table.NewRow ();
@@ -858,24 +884,24 @@ namespace MonoTests.System.Data
 
                        DataColumn col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table.Columns.Add (col);
                        UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
                        table.Constraints.Add (uc);
                 
                        col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table1.Columns.Add (col);
                                                                                                                              
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table.Columns.Add (col);
                        
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table1.Columns.Add (col);
                        DataRow row = table.NewRow ();
                        row ["Id"] = 147;
@@ -898,8 +924,11 @@ namespace MonoTests.System.Data
                        DataColumn[] colArray = {table.Columns[0]};
                        table.PrimaryKey = colArray;
                        table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
+#if NET_1_1 // This prevents further tests after .NET 1.1.
+#else
                        CultureInfo cultureInfo = new CultureInfo ("en-gb");
                        table.Locale = cultureInfo;
+#endif
 
                        row = table1.NewRow ();
                        row ["Name"] = "Abc";
@@ -924,7 +953,10 @@ namespace MonoTests.System.Data
                        AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
                        AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
                        AssertEquals ("#A08", false ,cloneTable.HasErrors);
+#if NET_1_1
+#else
                        AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
+#endif
                        AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
                        AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
                        AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
@@ -942,7 +974,10 @@ namespace MonoTests.System.Data
                        AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
                        AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
                        AssertEquals ("#A23", true ,copyTable.HasErrors);
+#if NET_1_1
+#else
                        AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
+#endif
                        AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
                        AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
                        AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
@@ -957,7 +992,7 @@ namespace MonoTests.System.Data
                        DataTable table = new DataTable ();
                        DataColumn col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        col.DefaultValue = 47;
                        table.Columns.Add (col);
                        UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
@@ -965,7 +1000,7 @@ namespace MonoTests.System.Data
                 
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        col.DefaultValue = "Hello";
                        table.Columns.Add (col);
                 
@@ -983,12 +1018,7 @@ namespace MonoTests.System.Data
                                table.EndLoadData ();
                                Fail ("#A01");
                        }
-                       catch (Exception e) {
-                               if (e.GetType () != typeof (AssertionException))
-                                       AssertEquals ("#A02", "Column 'Id' contains non-unique values",e.Message);
-                                else
-                                        Console.WriteLine (e);
-
+                       catch (ConstraintException) {
                        }
                }
                [Test]
@@ -998,14 +1028,14 @@ namespace MonoTests.System.Data
 
                        DataColumn col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table.Columns.Add (col);
                        UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
                        table.Constraints.Add (uc);
                                                                                                                              
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table.Columns.Add (col);                        
 
                        DataRow row = table.NewRow ();
@@ -1052,12 +1082,12 @@ namespace MonoTests.System.Data
                        DataTable table = new DataTable ();
                        DataColumn col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table.Columns.Add (col);
-                        
+
                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table.Columns.Add (col);
                         
                        DataRow row = table.NewRow ();
@@ -1081,40 +1111,50 @@ namespace MonoTests.System.Data
                        AssertEquals ("#A03", DataRowState.Modified, table.Rows [2].RowState);
                        AssertEquals ("#A04", DataRowState.Added, table.Rows [3].RowState);
                }
-               [Test]
-               public void ClearReset () //To test Clear and Reset methods
+
+                [Test]
+               public void ImportRowDetachedTest ()
                {
                        DataTable table = new DataTable ();
-                       DataTable table1 = new DataTable ();
-                
-                       DataSet set = new DataSet ();
-                       set.Tables.Add (table);
-                       set.Tables.Add (table1);
-                
                        DataColumn col = new DataColumn ();
                        col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
+                       col.DataType = Type.GetType ("System.Int32");
                        table.Columns.Add (col);
-                       UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
-                       table.Constraints.Add (uc);
-                       table.CaseSensitive = false;
-                
-                       col = new DataColumn ();
+
+                        table.PrimaryKey = new DataColumn [] {col};
+
+                        col = new DataColumn ();
                        col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
+                       col.DataType = Type.GetType ("System.String");
                        table.Columns.Add (col);
+                        
+                       DataRow row = table.NewRow ();
+                       row ["Id"] = 147;
+                       row ["name"] = "Abc";
+
+                        // keep silent as ms.net ;-), though this is not useful.
+                        table.ImportRow (row); 
+               }
+
+               [Test]
+               public void ClearReset () //To test Clear and Reset methods
+               {
+                       DataTable table = new DataTable ("table");
+                       DataTable table1 = new DataTable ("table1");
                 
-                       col = new DataColumn ();
-                       col.ColumnName = "Id";
-                       col.DataType = System.Type.GetType ("System.Int32");
-                       table1.Columns.Add (col);
-                
-                       col = new DataColumn ();
-                       col.ColumnName = "Name";
-                       col.DataType = System.Type.GetType ("System.String");
-                       table1.Columns.Add (col);
+                       DataSet set = new DataSet ();
+                       set.Tables.Add (table);
+                       set.Tables.Add (table1);
 
-                       DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
+                        table.Columns.Add ("Id", typeof (int));
+                        table.Columns.Add ("Name", typeof (string));
+                        table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
+                        table.CaseSensitive = false;
+                        
+                        table1.Columns.Add ("Id", typeof (int));
+                        table1.Columns.Add ("Name", typeof (string));
+
+                        DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
                        set.Relations.Add (dr);
                 
                        DataRow row = table.NewRow ();
@@ -1127,33 +1167,249 @@ namespace MonoTests.System.Data
                        row ["Name"] = "roopa";
                        table.Rows.Add (row);
                 
+                       AssertEquals (2, table.Rows.Count);
+                       AssertEquals (1, table.ChildRelations.Count);
                        try {
                                table.Reset ();
-                               Fail ("#A01");
-                       }
-                        catch (Exception e) {
-                               if (e.GetType () != typeof (AssertionException)) {
-                                       AssertEquals ("#A02", "Cannot clear table Parent because ForeignKeyConstraint DR enforces Child.", e.Message);
-                               }
+                               Fail ("#A01, should have thrown ArgumentException");
                        }
-                       try {
-                               table.Clear ();
-                               Fail ("#A03");
-                       }
-                       catch (Exception e) {
-                               if (e.GetType () != typeof (AssertionException)) {
-                                       AssertEquals ("#A04", "Cannot clear table Parent because ForeignKeyConstraint DR enforces Child.", e.Message);
-                               }
+                        catch (ArgumentException) {
                        }
+                       AssertEquals ("#CT01", 0, table.Rows.Count);
+                       AssertEquals ("#CT02", 0, table.ChildRelations.Count);
+                       AssertEquals ("#CT03", 0, table.ParentRelations.Count);
+                       AssertEquals ("#CT04", 0, table.Constraints.Count);
+
                        table1.Reset ();
                        AssertEquals ("#A05", 0, table1.Rows.Count);
                        AssertEquals ("#A06", 0, table1.Constraints.Count);
                        AssertEquals ("#A07", 0, table1.ParentRelations.Count);
                
+                        // clear test
                        table.Clear ();
                        AssertEquals ("#A08", 0, table.Rows.Count);
+#if NET_1_1
+                       AssertEquals ("#A09", 0, table.Constraints.Count);
+#else
                        AssertEquals ("#A09", 1, table.Constraints.Count);
-                       AssertEquals ("#A05", 0, table.ChildRelations.Count);
+#endif
+                       AssertEquals ("#A10", 0, table.ChildRelations.Count);
+
+               }
+
+                [Test]
+                public void ClearTest ()
+                {
+                        DataTable table = new DataTable ("test");
+                        table.Columns.Add ("id", typeof (int));
+                        table.Columns.Add ("name", typeof (string));
+                        
+                        table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
+                        
+                        table.Rows.Add (new object [] { 1, "mono 1" });
+                        table.Rows.Add (new object [] { 2, "mono 2" });
+                        table.Rows.Add (new object [] { 3, "mono 3" });
+                        table.Rows.Add (new object [] { 4, "mono 4" });
+
+                        table.AcceptChanges ();
+#if NET_2_0
+                        _tableClearedEventFired = false;
+                        table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
+#endif // NET_2_0
+                        
+                        table.Clear ();
+#if NET_2_0
+                        AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
+#endif // NET_2_0
+                        
+                        DataRow r = table.Rows.Find (1);
+                        AssertEquals ("#1 should have cleared", true, r == null);
+
+                        // try adding new row. indexes should have cleared
+                        table.Rows.Add (new object [] { 2, "mono 2" });
+                        AssertEquals ("#2 should add row", 1, table.Rows.Count);
+                }
+#if NET_2_0
+                private bool _tableClearedEventFired = false;
+                private void OnTableCleared (object src, DataTableClearEventArgs args)
+                {
+                        _tableClearedEventFired = true;
+                }
+#endif // NET_2_0
+                
+
+               [Test]
+               public void Serialize ()
+               {
+                       MemoryStream fs = new MemoryStream ();
+                       
+                       // Construct a BinaryFormatter and use it 
+                       // to serialize the data to the stream.
+                       BinaryFormatter formatter = new BinaryFormatter();
+               
+                       // Create an array with multiple elements refering to 
+                       // the one Singleton object.
+                       DataTable dt = new DataTable();
+               
+               
+                       dt.Columns.Add(new DataColumn("Id", typeof(string)));
+                       dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
+                       dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
+                       dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
+                       dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
+               
+                       DataRow loRowToAdd;
+                       loRowToAdd = dt.NewRow();
+                       loRowToAdd[0] = "a";
+                       loRowToAdd[1] = "b";
+                       loRowToAdd[2] = "c";
+                       loRowToAdd[3] = "d";
+                       loRowToAdd[4] = "e";
+                                               
+                       dt.Rows.Add(loRowToAdd);
+               
+                       DataTable[] dtarr = new DataTable[] {dt}; 
+               
+                       // Serialize the array elements.
+                       formatter.Serialize(fs, dtarr);
+               
+                       // Deserialize the array elements.
+                       fs.Position = 0;
+                       DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
+               
+                       DataSet ds = new DataSet();
+                       ds.Tables.Add(a2[0]);
+               
+                       StringWriter sw = new StringWriter ();
+                       ds.WriteXml(sw);
+                       XmlDocument doc = new XmlDocument ();
+                       doc.LoadXml (sw.ToString ());
+                       AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
+               }
+
+               [Test]
+               [ExpectedException (typeof (DataException))]
+               [NUnit.Framework.Category ("NotWorking")]
+               public void SetPrimaryKeyAssertsNonNull ()
+               {
+                       DataTable dt = new DataTable ("table");
+                       dt.Columns.Add ("col1");
+                       dt.Columns.Add ("col2");
+                       dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
+                       dt.Rows.Add (new object [] {1, 3});
+                       dt.Rows.Add (new object [] {DBNull.Value, 3});
+
+                       dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
+               }
+
+               [Test]
+               [ExpectedException (typeof (NoNullAllowedException))]
+               public void PrimaryKeyColumnChecksNonNull ()
+               {
+                       DataTable dt = new DataTable ("table");
+                       dt.Columns.Add ("col1");
+                       dt.Columns.Add ("col2");
+                       dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
+                       dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
+                       dt.Rows.Add (new object [] {1, 3});
+                       dt.Rows.Add (new object [] {DBNull.Value, 3});
+               }
+
+               void RowChanging (object o, DataRowChangeEventArgs e)
+               {
+                       AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
+                       rowChangingRowChanging = true;
+               }
+
+               void RowChanged (object o, DataRowChangeEventArgs e)
+               {
+                       AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
+                       rowChangingRowChanged = true;
+               }
+
+               bool rowChangingRowChanging, rowChangingRowChanged;
+               DataRowAction rowChangingExpectedAction;
+
+               [Test]
+               public void RowChanging ()
+               {
+                       DataTable dt = new DataTable ("table");
+                       dt.Columns.Add ("col1");
+                       dt.Columns.Add ("col2");
+                       dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
+                       dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
+                       rowChangingExpectedAction = DataRowAction.Add;
+                       dt.Rows.Add (new object [] {1, 2});
+                       Assert ("changing,Added", rowChangingRowChanging);
+                       Assert ("changed,Added", rowChangingRowChanged);
+                       rowChangingExpectedAction = DataRowAction.Change;
+                       dt.Rows [0] [0] = 2;
+                       Assert ("changing,Changed", rowChangingRowChanging);
+                       Assert ("changed,Changed", rowChangingRowChanged);
                }
-       }
+
+                [Test]
+                public void CloneSubClassTest()
+                {
+                        MyDataTable dt1 = new MyDataTable();
+                        MyDataTable dt = (MyDataTable)(dt1.Clone());
+                        AssertEquals("A#01",2,MyDataTable.count);
+                }
+
+                DataRowAction rowActionChanging = DataRowAction.Nothing;
+                DataRowAction rowActionChanged  = DataRowAction.Nothing;
+                [Test]
+                public void AcceptChangesTest ()
+                {
+                        DataTable dt = new DataTable ("test");
+                        dt.Columns.Add ("id", typeof (int));
+                        dt.Columns.Add ("name", typeof (string));
+                        
+                        dt.Rows.Add (new object [] { 1, "mono 1" });
+
+                        dt.RowChanged  += new DataRowChangeEventHandler (OnRowChanged);
+                        dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
+
+                        try {
+                                rowActionChanged = rowActionChanging = DataRowAction.Nothing;
+                                dt.AcceptChanges ();
+
+                                AssertEquals ("#1 should have fired event and set action to commit",
+                                              DataRowAction.Commit, rowActionChanging);
+                                AssertEquals ("#2 should have fired event and set action to commit",
+                                              DataRowAction.Commit, rowActionChanged);
+
+                        } finally {
+                                dt.RowChanged  -= new DataRowChangeEventHandler (OnRowChanged);
+                                dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
+
+                        }
+                }
+
+                public void OnRowChanging (object src, DataRowChangeEventArgs args)
+                {
+                        rowActionChanging = args.Action;
+                }
+                
+                public void OnRowChanged (object src, DataRowChangeEventArgs args)
+                {
+                        rowActionChanged = args.Action;
+                }
+                
+                                                                                                    
+        }
+                                                                                                    
+                                                                                                    
+         public  class MyDataTable:DataTable {
+                                                                                                    
+             public static int count = 0;
+                                                                                                    
+             public MyDataTable() {
+                                                                                                    
+                    count++;
+             }
+                                                                                                    
+         }
+
+       
 }