Browse Source

In System.Data.Common:
2004-11-22 Sureshkumar T <[email protected]>

* DbConnectionStringBuilder.cs: Class for helping creation of db
connection strings added.

In Test/System.Data.Common:
2004-11-22 Sureshkumar T <[email protected]>

* DbConnectionStringBuilderTest.cs: Test cases for
DbConnectionStringBuilder class. These are independant tests.


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

Sureshkumar T 21 years ago
parent
commit
8e13652448

+ 5 - 0
mcs/class/System.Data/System.Data.Common/ChangeLog

@@ -1,3 +1,8 @@
+2004-11-22  Sureshkumar T  <[email protected]>
+
+	* DbConnectionStringBuilder.cs: Class for helping creation of db
+	connection strings added.
+
 2004-10-01  Sureshkumar T  <[email protected]>
 
 	* DbProviderFactories.cs: Implemented all the stubs. Added functionality for

+ 388 - 0
mcs/class/System.Data/System.Data.Common/DbConnectionStringBuilder.cs

@@ -0,0 +1,388 @@
+//
+// System.Data.Common.DbConnectionStringBuilder.cs
+//
+// Author:
+//   Sureshkumar T ([email protected])
+//
+// 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.
+//
+
+#if NET_2_0
+
+namespace System.Data.Common
+{
+
+        using System;
+        using System.Text;
+        using System.Reflection;
+        using System.Collections;
+        using System.ComponentModel;
+        using System.Collections.Generic;
+
+        using System.Data;
+        using System.Data.Common;
+
+
+        [CLSCompliant (true)]
+        public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable,
+                IDictionary<string, object>,
+                ICollection<KeyValuePair<string, object>>,
+                IEnumerable<KeyValuePair<string, object>>,
+                ICustomTypeDescriptor
+        {
+                #region Fields
+                Dictionary<string, object> _dictionary = null;
+                #endregion Fields
+
+                #region Constructors
+                public DbConnectionStringBuilder ()
+                {
+                        Init ();
+                }
+
+                public DbConnectionStringBuilder (bool useFirstKeyValue)
+                {
+                        throw new NotImplementedException ();
+                }
+
+                private void Init ()
+                {
+                        _dictionary = new Dictionary<string, object> ();
+                }
+
+                #endregion // Constructors
+
+                #region Properties
+                public bool BrowsableConnectionString
+                {
+                        get { throw new NotImplementedException (); }
+                        set { throw new NotImplementedException (); }
+                }
+                public string ConnectionString
+                {
+                        get
+                        {
+                                IDictionary<string, object> dictionary = (IDictionary <string, object>) _dictionary;
+                                string conn = "";
+                                foreach (string key in dictionary.Keys) {
+                                        conn += key + "=" + dictionary [key].ToString () + ";";
+                                }
+                                conn = conn.TrimEnd (';');
+                                return conn;
+                        }
+                        set { throw new NotImplementedException (); }
+                }
+                public virtual int Count
+                {
+                        get { return _dictionary.Count; }
+                }
+
+                public virtual bool IsFixedSize
+                {
+                        get { return false; }
+                }
+
+                public bool IsReadOnly
+                {
+                        get { throw new NotImplementedException (); }
+                }
+
+                public virtual object this [string keyword]
+                {
+                        get
+                        {
+                                if (ContainsKey (keyword))
+                                        return _dictionary [keyword];
+                                else
+                                        throw new ArgumentException ();
+                        }
+                        set { Add (keyword, value); }
+                }
+                public virtual ICollection Keys
+                {
+                        get { return (ICollection) ( (IDictionary <string, object>)_dictionary).Keys; }
+                }
+
+                ICollection<string> IDictionary<string, object>.Keys
+                {
+                        get { return (ICollection<string>) ( (IDictionary<string, object>) _dictionary).Keys; }
+                }
+
+                ICollection<object> IDictionary<string, object>.Values
+                {
+                        get { return (ICollection<object>) ( (IDictionary<string, object>)_dictionary).Values; }
+                }
+
+                bool ICollection.IsSynchronized
+                {
+                        get { throw new NotImplementedException (); }
+                }
+
+                object ICollection.SyncRoot
+                {
+                        get { throw new NotImplementedException (); }
+                }
+
+                object IDictionary.this [object keyword]
+                {
+                        get { return this [(string) keyword]; }
+                        set { this [(string) keyword] = value; }
+                }
+
+                public virtual ICollection Values
+                {
+                        get { return (ICollection) ( (IDictionary<string, object>)_dictionary).Values; }
+                }
+
+                #endregion // Properties
+
+
+                #region Methods
+
+
+                public void Add (string keyword, object value)
+                {
+                        if (ContainsKey (keyword)) {
+                                _dictionary [keyword] = value;
+                        } else {
+                                _dictionary.Add (keyword, value);
+                        }
+
+                }
+
+                public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
+                {
+                        if (builder.Length > 0) {
+                                char lastChar = builder [builder.Length];
+                                if (lastChar != ';' && lastChar != ' ')
+                                        builder.Append (';');
+                                else if (lastChar == ' ' && !builder.ToString ().Trim ().EndsWith (";"))
+                                        builder.Append (';');
+                        }
+                        builder.AppendFormat ("{0}={1}", keyword, value);
+                }
+
+                public virtual void Clear ()
+                {
+                        _dictionary.Clear ();
+                }
+
+                public virtual bool ContainsKey (string keyword)
+                {
+                        return _dictionary.ContainsKey (keyword);
+                }
+
+                public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
+                {
+                        bool ret = true;
+                        try {
+                                if (Count != connectionStringBuilder.Count)
+                                        ret = false;
+                                else {
+                                        foreach (string key in Keys) {
+                                                if (!this [key].Equals (connectionStringBuilder [key])) {
+                                                        ret = false;
+                                                        break;
+                                                }
+                                        }
+                                }
+                        } catch (ArgumentException e) {
+                                ret = false;
+                        }
+                        return ret;
+                }
+
+                public virtual bool Remove (string keyword)
+                {
+                        return _dictionary.Remove (keyword);
+                }
+
+                [Obsolete ("Do not use. Please use the Remove method.")]
+                public virtual void Reset (string keyword)
+                {
+                        throw new NotImplementedException ();
+                }
+
+                public virtual bool ShouldSerialize (string keyword)
+                {
+                        throw new NotImplementedException ();
+                }
+
+                void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> keyValuePair)
+                {
+                        Add (keyValuePair.Key, keyValuePair.Value);
+                }
+
+                bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> keyValuePair)
+                {
+                        return ContainsKey (keyValuePair.Key);
+                }
+
+                void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int index)
+                {
+                        if (index + Count > array.Length)
+                                throw new ArgumentException ("The destination does not have enough length!");
+                        foreach (KeyValuePair<string, object> keyValue in this) {
+                                array [index++] = keyValue;
+                        }
+                }
+
+                bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> keyValuePair)
+                {
+                        return Remove (keyValuePair.Key);
+                }
+
+                IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator ()
+                {
+                        return _dictionary.GetEnumerator ();
+                }
+
+                void ICollection.CopyTo (Array array, int index)
+                {
+
+                        KeyValuePair <string, object> [] arr = null;
+                        try {
+                                arr = (KeyValuePair<string, object> []) array;
+                        } catch (InvalidCastException e) {
+                                throw new ArgumentException (
+                                                             "Target array type is not compatible with the type of items in the collection."
+                                                             );
+                        }
+                        ICollection<KeyValuePair<string, object>> ptr = (ICollection<KeyValuePair<string, object>>) this;
+                        ptr.CopyTo (arr, index);
+
+                }
+
+                void IDictionary.Add (object keyword, object value)
+                {
+                        this.Add ((string) keyword, value);
+                }
+
+                bool IDictionary.Contains (object keyword)
+                {
+                        return ContainsKey ((string) keyword);
+                }
+
+                IDictionaryEnumerator IDictionary.GetEnumerator ()
+                {
+                        return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
+                }
+
+                void IDictionary.Remove (object keyword)
+                {
+                        Remove ((string) keyword);
+                }
+
+                IEnumerator IEnumerable.GetEnumerator ()
+                {
+                        return (IEnumerator) _dictionary.GetEnumerator ();
+                }
+
+                private static object _staticAttributeCollection = null;
+                AttributeCollection ICustomTypeDescriptor.GetAttributes ()
+                {
+                        object value = _staticAttributeCollection;
+                        if (value == null) {
+                                CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
+                                DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
+                                Attribute [] attrs = {clsAttr, defMemAttr};
+                                value = new AttributeCollection (attrs);
+                        }
+                        System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
+                        return _staticAttributeCollection as AttributeCollection;
+                }
+
+                string ICustomTypeDescriptor.GetClassName ()
+                {
+                        return this.GetType ().ToString ();
+                }
+
+                string ICustomTypeDescriptor.GetComponentName ()
+                {
+                        return null;
+                }
+
+                TypeConverter ICustomTypeDescriptor.GetConverter ()
+                {
+                        return new CollectionConverter ();
+                }
+
+                EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
+                {
+                        return null;
+                }
+
+                PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
+                {
+                        return null;
+                }
+
+                object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
+                {
+                        return null;
+                }
+
+                EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
+                {
+                        return EventDescriptorCollection.Empty;
+                }
+
+                EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
+                {
+                        return EventDescriptorCollection.Empty;
+                }
+
+                PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
+                {
+                        return PropertyDescriptorCollection.Empty;
+                }
+
+                PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
+                {
+                        return PropertyDescriptorCollection.Empty;
+                }
+
+                object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
+                {
+                        throw new NotImplementedException ();
+                }
+
+                public override string ToString ()
+                {
+                        return ConnectionString;
+                }
+
+                public virtual bool TryGetValue (string keyword, out object value)
+                {
+                        // FIXME : not sure, difference between this [keyword] and this method
+                        bool found = ContainsKey (keyword);
+                        if (found)
+                                value = this [keyword];
+                        else
+                                value = null;
+                        return found;
+                }
+
+                #endregion // Public Methods
+        }
+}
+#endif // NET_2_0 using

+ 5 - 0
mcs/class/System.Data/Test/System.Data.Common/ChangeLog

@@ -1,3 +1,8 @@
+2004-11-22  Sureshkumar T  <[email protected]>
+
+	* DbConnectionStringBuilderTest.cs: Test cases for
+	DbConnectionStringBuilder class. These are independant tests.
+
 2004-09-14  Sebastien Pouliot  <[email protected]>
 
 	* DBDataPermissionTest.cs: New. Unit tests for DBDataPermission.

+ 261 - 0
mcs/class/System.Data/Test/System.Data.Common/DbConnectionStringBuilderTest.cs

@@ -0,0 +1,261 @@
+// DbConnectionStringBuilderTest.cs - NUnit Test Cases for Testing the 
+// DbConnectionStringBuilder class
+//
+// Author: 
+//      Sureshkumar T ([email protected])
+//
+//
+// 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.
+//
+
+#if NET_2_0
+
+#region Using directives
+
+using System;
+using System.Text;
+
+using System.Data;
+using System.Reflection;
+using System.Data.Common;
+using System.ComponentModel;
+using System.Data.SqlClient;
+using System.Collections.Specialized;
+using System.Collections.Generic;
+
+using NUnit.Framework;
+
+#endregion
+
+namespace MonoTests.System.Data.Common
+{
+
+        [TestFixture]
+        public class DbConnectionStringBuilderTest
+        {
+                private DbConnectionStringBuilder builder = null;
+                private const string SERVER = "SERVER";
+                private const string SERVER_VALUE = "localhost";
+
+                [SetUp]
+                public void SetUp ()
+                {
+                        builder = new DbConnectionStringBuilder ();
+                }
+
+                [Test]
+                public void AddTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
+                                         "Adding to connection String failed!");
+                }
+
+                [Test]
+                public void ClearTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        builder.Clear ();
+                        Assert.AreEqual ("", builder.ConnectionString,
+                                         "Clearing connection String failed!");
+                }
+
+                [Test]
+                public void AddDuplicateTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        builder.Add (SERVER, SERVER_VALUE);
+                        // should allow duplicate addition. rather, it should re-assign
+                        Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
+                                         "Duplicates addition does not change the value!");
+                }
+
+                [Test]
+                [ExpectedException (typeof (ArgumentException))]
+                public void InvalidKeyTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        string value = builder ["###"].ToString (); // some invalid key values
+                        Assert.Fail ("Should have thrown exception!");
+                }
+
+                [Test]
+                public void RemoveTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        builder.Remove (SERVER);
+                        Assert.AreEqual ("", builder.ConnectionString, "Remove does not work!");
+                }
+
+                [Test]
+                public void ContainsKeyTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        bool value = builder.ContainsKey (SERVER);
+                        Assert.IsTrue (value, "Contains does not work!");
+                }
+
+                [Test]
+                public void EquivalentToTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        DbConnectionStringBuilder sb2 = new DbConnectionStringBuilder ();
+                        sb2.Add (SERVER, SERVER_VALUE);
+                        bool value = builder.EquivalentTo (sb2);
+                        Assert.IsTrue (value, "builder comparision does not work!");
+
+                        // negative tests
+                        sb2.Add (SERVER + "1", SERVER_VALUE);
+                        value = builder.EquivalentTo (sb2);
+                        Assert.IsFalse (value, "builder comparision does not work for not equivalent strings!");
+                }
+
+                [Test]
+                public void AppendKeyValuePairTest ()
+                {
+                        StringBuilder sb = new StringBuilder ();
+                        DbConnectionStringBuilder.AppendKeyValuePair (sb, SERVER, SERVER_VALUE);
+                        Assert.AreEqual (SERVER + "=" + SERVER_VALUE, sb.ToString (),
+                                         "adding key value pair to existing string builder fails!");
+                }
+
+                [Test]
+                public void ToStringTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        string str = builder.ToString ();
+                        string value = builder.ConnectionString;
+                        Assert.AreEqual (value, str,
+                                         "ToString shoud return ConnectionString!");
+                }
+
+                [Test]
+                public void ItemTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        string value = (string) builder [SERVER];
+                        Assert.AreEqual (SERVER_VALUE, value,
+                                         "Item indexor does not retrun correct value!");
+                }
+
+                [Test, Ignore ("FIXME : commented for a missing feature in gmcs, (CopyTo)")]
+                public void IDictionaryCopyToTest ()
+                {
+                        KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [2];
+                        builder.Add (SERVER, SERVER_VALUE);
+                        builder.Add (SERVER + "1", SERVER_VALUE + "1");
+                        IDictionary<string, object> s = builder;
+                        //FIXME : s.CopyTo (dict, 0);
+                        Assert.AreEqual (SERVER, dict [0].Key, "not equal");
+                        Assert.AreEqual (SERVER_VALUE, dict [0].Value, "not equal");
+                        Assert.AreEqual (SERVER + "1", dict [1].Key, "not equal");
+                        Assert.AreEqual (SERVER_VALUE + "1", dict [1].Value, "not equal");
+                }
+
+                [Test, Ignore ("FIXME: commented for a missing feature in gmcs (CopyTo)")]
+                [ExpectedException (typeof (ArgumentException))]
+                public void NegIDictionaryCopyToTest ()
+                {
+                        KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [1];
+                        builder.Add (SERVER, SERVER_VALUE);
+                        builder.Add (SERVER + "1", SERVER_VALUE + "1");
+                        IDictionary<string, object> s = builder;
+                        //FIXME : s.CopyTo (dict, 0);
+                        Assert.Fail ("Exception Destination Array not enough is not thrown!");
+                }
+
+                [Test, Ignore ("FIXME : currently mono is not supporting casting from generic type to"+
+                 " non generic type")]
+                public void ICollectionCopyToTest ()
+                {
+
+                        KeyValuePair <string, object> [] arr = new KeyValuePair <string, object> [2];
+                        builder.Add (SERVER, SERVER_VALUE);
+                        builder.Add (SERVER + "1", SERVER_VALUE + "1");
+                        System.Collections.ICollection s = builder;
+                        s.CopyTo ((Array) arr, 0);
+                        Assert.AreEqual (SERVER, arr [0].Key, "not equal");
+                        Assert.AreEqual (SERVER_VALUE, arr [0].Value, "not equal");
+                        Assert.AreEqual (SERVER + "1", arr [1].Key, "not equal");
+                        Assert.AreEqual (SERVER_VALUE + "1", arr [1].Value, "not equal");
+                }
+
+                [Test]
+                [ExpectedException (typeof (ArgumentException))]
+                public void NegICollectionCopyToTest ()
+                {
+                        string [] arr = new string [2];
+                        builder.Add (SERVER, SERVER_VALUE);
+                        builder.Add (SERVER + "1", SERVER_VALUE + "1");
+                        System.Collections.ICollection s = builder;
+                        s.CopyTo ((Array) arr, 0);
+                }
+
+                [Test]
+                public void TryGetValueTest ()
+                {
+                        builder.Add (SERVER, SERVER_VALUE);
+                        object value = "";
+                        bool result = builder.TryGetValue (SERVER, out value);
+                        Assert.AreEqual (SERVER_VALUE, (string) value,
+                                         "TryGetValue does not return correct value in out parameter!");
+                        Assert.IsTrue (result, "TryGetValue does not return true for existant key!");
+
+                        result = builder.TryGetValue ("@@@@", out value);
+                        Assert.IsFalse (result, "TryGetValue does not return false for non-existant key!");
+                        Assert.IsNull ((string) value,
+                                       "TryGetValue does not return correct value in out parameter for non existant key!");
+                }
+
+                [Test]
+                public void ICTD_GetClassNameTest ()
+                {
+                        ICustomTypeDescriptor ictd = (ICustomTypeDescriptor) builder;
+                        string className = ictd.GetClassName ();
+                        Assert.AreEqual (builder.GetType ().ToString (), className, "Should return class name!");
+
+                        AttributeCollection collection = ictd.GetAttributes ();
+                        Assert.AreEqual (2, collection.Count);
+                        object [] attr = builder.GetType ().GetCustomAttributes (typeof (DefaultMemberAttribute), false);
+                        if (attr.Length > 0) {
+                                DefaultMemberAttribute defAtt = (DefaultMemberAttribute) attr [0];
+                                Assert.AreEqual ("Item", defAtt.MemberName, "default memeber attribute is not set!");
+                        } else
+                                Assert.Fail ("DbConnectionStringBuilder class does not implement DefaultMember attribute");
+
+                        string compName = ictd.GetComponentName ();
+                        Assert.IsNull (compName, "");
+
+                        TypeConverter converter = ictd.GetConverter ();
+                        Assert.AreEqual (typeof (CollectionConverter), converter.GetType (), "");
+
+                        EventDescriptor evtDesc = ictd.GetDefaultEvent ();
+                        Assert.IsNull (evtDesc, "");
+
+                        PropertyDescriptor property = ictd.GetDefaultProperty ();
+                        Assert.IsNull (property, "");
+
+                }
+        }
+}
+
+#endif // NET_2_0