* DbCommandBuilder.cs: When CatalogSeparator or SchemaSeparator are
authorGert Driesen <drieseng@users.sourceforge.net>
Tue, 30 Dec 2008 10:21:41 +0000 (10:21 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Tue, 30 Dec 2008 10:21:41 +0000 (10:21 -0000)
not set (or explicitly set to null or empty string), then return
default separator character. Added value check for CatalogLocation and
ConflictOption. Removed regions.
* DbCommandBuilderTest.cs: Added tests for properties.
* OdbcCommandBuilderTest.cs: Added tests for properties.

svn path=/trunk/mcs/; revision=122230

mcs/class/System.Data/System.Data.Common/ChangeLog
mcs/class/System.Data/System.Data.Common/DbCommandBuilder.cs
mcs/class/System.Data/Test/System.Data.Common/ChangeLog
mcs/class/System.Data/Test/System.Data.Common/DbCommandBuilderTest.cs
mcs/class/System.Data/Test/System.Data.Odbc/ChangeLog
mcs/class/System.Data/Test/System.Data.Odbc/OdbcCommandBuilderTest.cs

index 9e58037b729d910c14dbb3ed56f1a40a445a02c1..d0a548655d2f6b6e10abd519ae855cded37193d7 100644 (file)
@@ -1,3 +1,10 @@
+2008-12-30  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * DbCommandBuilder.cs: When CatalogSeparator or SchemaSeparator are
+       not set (or explicitly set to null or empty string), then return
+       default separator character. Added value check for CatalogLocation and
+       ConflictOption. Removed regions.
+
 2008-12-30  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * DbCommandBuilder.cs: Fixed default value for ConflicOption.
index 450503169e6d1f21ea0559093a060cf74338c173..f11c507e8d74ada22427a6e4f5711dcc623388ed 100644 (file)
 
 using System.ComponentModel;
 using System.Data;
+using System.Globalization;
 using System.Text;
 
 namespace System.Data.Common {
        public abstract class DbCommandBuilder : Component
        {
-               bool _setAllValues = false;
-               bool _disposed = false;
+               bool _setAllValues;
+               bool _disposed;
 
                DataTable _dbSchemaTable;
-               DbDataAdapter _dbDataAdapter = null;
+               DbDataAdapter _dbDataAdapter;
                private CatalogLocation _catalogLocation = CatalogLocation.Start;
                private ConflictOption _conflictOption = ConflictOption.CompareAllSearchableValues;
 
                private string _tableName;
-               private string _catalogSeperator = ".";
+               private string _catalogSeparator;
                private string _quotePrefix;
                private string _quoteSuffix;
-               private string _schemaSeperator = ".";
-               private DbCommand _dbCommand = null;
-
-               // Used to construct WHERE clauses
-               static readonly string clause1 = "({0} = 1 AND {1} IS NULL)";
-               static readonly string clause2 = "({0} = {1})";
+               private string _schemaSeparator;
+               private DbCommand _dbCommand;
 
                DbCommand _deleteCommand;
                DbCommand _insertCommand;
                DbCommand _updateCommand;
 
-               #region Constructors
+               static readonly string SEPARATOR_DEFAULT = ".";
+               // Used to construct WHERE clauses
+               static readonly string clause1 = "({0} = 1 AND {1} IS NULL)";
+               static readonly string clause2 = "({0} = {1})";
 
                protected DbCommandBuilder ()
                {
                }
 
-               #endregion // Constructors
-
-               #region Properties
-
                private void BuildCache (bool closeConnection)
                {
                        DbCommand sourceCommand = SourceCommand;
@@ -393,19 +389,31 @@ namespace System.Data.Common {
                [DefaultValue (CatalogLocation.Start)]
                public virtual CatalogLocation CatalogLocation {
                        get { return _catalogLocation; }
-                       set { _catalogLocation = value; }
+                       set {
+                               CheckEnumValue (typeof (CatalogLocation),
+                                       (int) value);
+                               _catalogLocation = value;
+                       }
                }
 
                [DefaultValue (".")]
                public virtual string CatalogSeparator {
-                       get { return _catalogSeperator; }
-                       set { if (value != null) _catalogSeperator = value; }
+                       get {
+                               if (_catalogSeparator == null || _catalogSeparator.Length == 0)
+                                       return SEPARATOR_DEFAULT;
+                               return _catalogSeparator;
+                       }
+                       set { _catalogSeparator = value; }
                }
 
                [DefaultValue (ConflictOption.CompareAllSearchableValues)]
                public virtual ConflictOption ConflictOption {
                        get { return _conflictOption; }
-                       set { _conflictOption = value; }
+                       set {
+                               CheckEnumValue (typeof (ConflictOption),
+                                       (int) value);
+                               _conflictOption = value;
+                       }
                }
 
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
@@ -451,15 +459,19 @@ namespace System.Data.Common {
 
                [DefaultValue (".")]
                public virtual string SchemaSeparator {
-                       get { return _schemaSeperator; }
-                       set {  if (value != null) _schemaSeperator = value; }
+                       get {
+                               if (_schemaSeparator == null || _schemaSeparator.Length == 0)
+                                       return SEPARATOR_DEFAULT;
+                               return _schemaSeparator;
+                       }
+                       set { _schemaSeparator = value; }
                }
 
                [DefaultValue (false)]
                public bool SetAllValues {
                        get { return _setAllValues; }
                        set { _setAllValues = value; }
-               }               
+               }
 
                private DbCommand SourceCommand {
                        get {
@@ -468,9 +480,6 @@ namespace System.Data.Common {
                                return null;
                        }
                }
-               #endregion // Properties
-
-               #region Methods
 
                protected abstract void ApplyParameterInfo (DbParameter parameter, 
                                                            DataRow row, 
@@ -622,7 +631,17 @@ namespace System.Data.Common {
                                return rdr.GetSchemaTable ();
                }
 
-               #endregion // Methods
+               static void CheckEnumValue (Type type, int value)
+               {
+                       if (Enum.IsDefined (type, value))
+                               return;
+
+                       string typename = type.Name;
+                       string msg = string.Format (CultureInfo.CurrentCulture,
+                               "Value {0} is not valid for {1}.", value,
+                               typename);
+                       throw new ArgumentOutOfRangeException (typename, msg);
+               }
        }
 }
 
index d621a3fe13de760e8addc6e38d7b4a2327e74a5d..bdd0ab2178c336a684c8b123af6137a55feff285 100644 (file)
@@ -1,3 +1,7 @@
+2008-12-30  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * DbCommandBuilderTest.cs: Added tests for properties.
+
 2008-12-30  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * DbCommandBuilderTest.cs: Added tests for QuotePrefix and
index 5e2182042cab239988dc98b11f138ca12f733846..b74eb6b29cc8df15442556f8d59b782607f58582 100644 (file)
@@ -38,6 +38,81 @@ namespace MonoTests.System.Data.Common
        [TestFixture]
        public class DbCommandBuilderTest
        {
+               [Test]
+               public void CatalogLocationTest ()
+               {
+                       MyCommandBuilder cb = new MyCommandBuilder ();
+                       Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
+                       cb.CatalogLocation = CatalogLocation.End;
+                       Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#2");
+               }
+
+               [Test]
+               public void CatalogLocation_Value_Invalid ()
+               {
+                       MyCommandBuilder cb = new MyCommandBuilder ();
+                       cb.CatalogLocation = CatalogLocation.End;
+                       try {
+                               cb.CatalogLocation = (CatalogLocation) 666;
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // The CatalogLocation enumeration value, 666, is invalid
+                               Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsTrue (ex.Message.IndexOf ("CatalogLocation") != -1, "#5:" + ex.Message);
+                               Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
+                               Assert.AreEqual ("CatalogLocation", ex.ParamName, "#7");
+                       }
+                       Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#8");
+               }
+
+               [Test]
+               public void CatalogSeparator ()
+               {
+                       MyCommandBuilder cb = new MyCommandBuilder ();
+                       Assert.AreEqual (".", cb.CatalogSeparator, "#1");
+                       cb.CatalogSeparator = "a";
+                       Assert.AreEqual ("a", cb.CatalogSeparator, "#2");
+                       cb.CatalogSeparator = null;
+                       Assert.AreEqual (".", cb.CatalogSeparator, "#3");
+                       cb.CatalogSeparator = "b";
+                       Assert.AreEqual ("b", cb.CatalogSeparator, "#4");
+                       cb.CatalogSeparator = string.Empty;
+                       Assert.AreEqual (".", cb.CatalogSeparator, "#5");
+                       cb.CatalogSeparator = " ";
+                       Assert.AreEqual (" ", cb.CatalogSeparator, "#6");
+               }
+
+               [Test]
+               public void ConflictOptionTest ()
+               {
+                       MyCommandBuilder cb = new MyCommandBuilder ();
+                       Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
+                       cb.ConflictOption = ConflictOption.CompareRowVersion;
+                       Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
+               }
+
+               [Test]
+               public void ConflictOption_Value_Invalid ()
+               {
+                       MyCommandBuilder cb = new MyCommandBuilder ();
+                       cb.ConflictOption = ConflictOption.CompareRowVersion;
+                       try {
+                               cb.ConflictOption = (ConflictOption) 666;
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // The ConflictOption enumeration value, 666, is invalid
+                               Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
+                               Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
+                               Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
+                       }
+                       Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
+               }
+
                [Test]
                public void QuotePrefix ()
                {
@@ -68,6 +143,25 @@ namespace MonoTests.System.Data.Common
                        Assert.AreEqual ("'\"", cb.QuoteSuffix, "#4");
                        cb.QuoteSuffix = string.Empty;
                        Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#5");
+                       cb.QuoteSuffix = " ";
+                       Assert.AreEqual (" ", cb.QuoteSuffix, "#6");
+               }
+
+               [Test]
+               public void SchemaSeparator ()
+               {
+                       MyCommandBuilder cb = new MyCommandBuilder ();
+                       Assert.AreEqual (".", cb.SchemaSeparator, "#1");
+                       cb.SchemaSeparator = "a";
+                       Assert.AreEqual ("a", cb.SchemaSeparator, "#2");
+                       cb.SchemaSeparator = null;
+                       Assert.AreEqual (".", cb.SchemaSeparator, "#3");
+                       cb.SchemaSeparator = "b";
+                       Assert.AreEqual ("b", cb.SchemaSeparator, "#4");
+                       cb.SchemaSeparator = string.Empty;
+                       Assert.AreEqual (".", cb.SchemaSeparator, "#5");
+                       cb.SchemaSeparator = " ";
+                       Assert.AreEqual (" ", cb.SchemaSeparator, "#6");
                }
 
                private class MyCommandBuilder : DbCommandBuilder
index 3213cad6fda5041cff8914f5c534ac4c3075f65e..30888fe2cc55095df18f6748f636c389e6730790 100644 (file)
@@ -1,3 +1,7 @@
+2008-12-30  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * OdbcCommandBuilderTest.cs: Added tests for properties.
+
 2008-12-30  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * OdbcCommandBuilder.cs: Added test for ConflictOption default value.
index dbfc2af5ca3eba410da85abab32c82d551c76cfc..48ce9319d389b3593259c37d1c2b34b3b51c8ea7 100644 (file)
@@ -38,6 +38,82 @@ namespace MonoTests.System.Data.Odbc
        [TestFixture]
        public class OdbcCommandBuilderTest
        {
+#if NET_2_0
+               [Test]
+               public void CatalogLocationTest ()
+               {
+                       OdbcCommandBuilder cb = new OdbcCommandBuilder ();
+                       Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
+                       cb.CatalogLocation = CatalogLocation.End;
+                       Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#2");
+               }
+
+               [Test]
+               public void CatalogLocation_Value_Invalid ()
+               {
+                       OdbcCommandBuilder cb = new OdbcCommandBuilder ();
+                       cb.CatalogLocation = CatalogLocation.End;
+                       try {
+                               cb.CatalogLocation = (CatalogLocation) 666;
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // The CatalogLocation enumeration value, 666, is invalid
+                               Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsTrue (ex.Message.IndexOf ("CatalogLocation") != -1, "#5:" + ex.Message);
+                               Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
+                       }
+                       Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#6");
+               }
+
+               [Test]
+               public void CatalogSeparator ()
+               {
+                       OdbcCommandBuilder cb = new OdbcCommandBuilder ();
+                       Assert.AreEqual (".", cb.CatalogSeparator, "#1");
+                       cb.CatalogSeparator = "a";
+                       Assert.AreEqual ("a", cb.CatalogSeparator, "#2");
+                       cb.CatalogSeparator = null;
+                       Assert.AreEqual (".", cb.CatalogSeparator, "#3");
+                       cb.CatalogSeparator = "b";
+                       Assert.AreEqual ("b", cb.CatalogSeparator, "#4");
+                       cb.CatalogSeparator = string.Empty;
+                       Assert.AreEqual (".", cb.CatalogSeparator, "#5");
+                       cb.CatalogSeparator = " ";
+                       Assert.AreEqual (" ", cb.CatalogSeparator, "#6");
+               }
+
+               [Test]
+               public void ConflictOptionTest ()
+               {
+                       OdbcCommandBuilder cb = new OdbcCommandBuilder ();
+                       Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
+                       cb.ConflictOption = ConflictOption.CompareRowVersion;
+                       Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
+               }
+
+               [Test]
+               public void ConflictOption_Value_Invalid ()
+               {
+                       OdbcCommandBuilder cb = new OdbcCommandBuilder ();
+                       cb.ConflictOption = ConflictOption.CompareRowVersion;
+                       try {
+                               cb.ConflictOption = (ConflictOption) 666;
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // The ConflictOption enumeration value, 666, is invalid
+                               Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
+                               Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
+                               Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
+                       }
+                       Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
+               }
+#endif
+
                [Test]
                public void QuotePrefix ()
                {
@@ -68,6 +144,8 @@ namespace MonoTests.System.Data.Odbc
                        Assert.AreEqual ("'\"", cb.QuoteSuffix, "#4");
                        cb.QuoteSuffix = string.Empty;
                        Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#5");
+                       cb.QuoteSuffix = " ";
+                       Assert.AreEqual (" ", cb.QuoteSuffix, "#6");
                }
 
 #if NET_2_0
@@ -306,13 +384,20 @@ namespace MonoTests.System.Data.Odbc
                }
 
                [Test]
-               public void DefaultPropertiesTest ()
+               public void SchemaSeparator ()
                {
                        OdbcCommandBuilder cb = new OdbcCommandBuilder ();
-                       Assert.AreEqual (".", cb.CatalogSeparator, "#2");
-                       Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#3");
-                       Assert.AreEqual (".", cb.SchemaSeparator, "#4");
-                       Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
+                       Assert.AreEqual (".", cb.SchemaSeparator, "#1");
+                       cb.SchemaSeparator = "a";
+                       Assert.AreEqual ("a", cb.SchemaSeparator, "#2");
+                       cb.SchemaSeparator = null;
+                       Assert.AreEqual (".", cb.SchemaSeparator, "#3");
+                       cb.SchemaSeparator = "b";
+                       Assert.AreEqual ("b", cb.SchemaSeparator, "#4");
+                       cb.SchemaSeparator = string.Empty;
+                       Assert.AreEqual (".", cb.SchemaSeparator, "#5");
+                       cb.SchemaSeparator = " ";
+                       Assert.AreEqual (" ", cb.SchemaSeparator, "#6");
                }
 #endif
        }