public void Add ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			builder.Add ("driverid", "420");
			builder.Add ("driverid", "560");
			builder.Add ("DriverID", "840");
			Assert.AreEqual ("840", builder ["driverId"], "#A1");
			Assert.IsTrue (builder.ContainsKey ("driverId"), "#A2");
			builder.Add ("Driver", "OdbcDriver");
			Assert.AreEqual ("OdbcDriver", builder.Driver, "#B1");
			Assert.AreEqual ("OdbcDriver", builder ["Driver"], "#B2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#B3");
			builder.Add ("Driver", "{OdbcDriver");
			Assert.AreEqual ("{OdbcDriver", builder.Driver, "#C1");
			Assert.AreEqual ("{OdbcDriver", builder ["Driver"], "#C2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#C3");
			builder.Add ("Dsn", "MyDsn");
			Assert.AreEqual ("MyDsn", builder.Dsn, "#D1");
			Assert.AreEqual ("MyDsn", builder ["Dsn"], "#D2");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#D3");
			builder.Add ("dsN", "MyDsn2");
			Assert.AreEqual ("MyDsn2", builder.Dsn, "#E1");
			Assert.AreEqual ("MyDsn2", builder ["Dsn"], "#E2");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#E3");
		}
Example #2
0
File: test.cs Project: mono/gert
	static string CreateOdbcConnectionString ()
	{
#if NET_2_0
		OdbcConnectionStringBuilder csb = new OdbcConnectionStringBuilder ();
		csb.Driver = "SQL Server";
#else
		StringBuilder sb = new StringBuilder ();
		sb.Append ("Driver={SQL Server};");
#endif

		string serverName = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_HOST");
		if (serverName == null)
			throw CreateEnvironmentVariableNotSetException ("MONO_TESTS_SQL_HOST");
#if NET_2_0
		csb.Add ("Server", serverName);
#else
		sb.AppendFormat ("Server={0};", serverName);
#endif

		string dbName = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_DB");
		if (dbName == null)
			throw CreateEnvironmentVariableNotSetException ("MONO_TESTS_SQL_DB");
#if NET_2_0
		csb.Add ("Database", dbName);
#else
		sb.AppendFormat ("Database={0};", dbName);
#endif

		string userName = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_USER");
		if (userName != null)
#if NET_2_0
			csb.Add ("Uid", userName);
#else
			sb.AppendFormat ("Uid={0};", userName);
#endif

		string pwd = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_PWD");
		if (pwd != null)
#if NET_2_0
			csb.Add ("Pwd", pwd);
#else
			sb.AppendFormat ("Pwd={0};", pwd);
#endif

#if NET_2_0
		return csb.ToString ();
#else
		return sb.ToString ();
#endif
	}
       /// <summary>
       /// Create a new drive.  Create a connection to the database file and set
       /// the Connection property in the PSDriveInfo.
       /// </summary>
       /// <param name="drive">
       /// Information describing the drive to add.
       /// </param>
       /// <returns>The added drive.</returns>
       protected override PSDriveInfo NewDrive(PSDriveInfo drive)
       {
           // check if drive object is null
           if (drive == null)
           {
               WriteError(new ErrorRecord(
                   new ArgumentNullException("drive"),
                   "NullDrive",
                   ErrorCategory.InvalidArgument,
                   null)
               );

               return null;
           }

           // check if drive root is not null or empty
           // and if its an existing file
           if (String.IsNullOrEmpty(drive.Root) || (File.Exists(drive.Root) == false))
           {
               WriteError(new ErrorRecord(
                   new ArgumentException("drive.Root"),
                   "NoRoot",
                   ErrorCategory.InvalidArgument,
                   drive)
               );

               return null;
           }


           // create a new drive and create an ODBC connection to the new drive
           AccessDBPSDriveInfo accessDBPSDriveInfo = new AccessDBPSDriveInfo(drive);

           OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();

           builder.Driver = "Microsoft Access Driver (*.mdb)";
           builder.Add("DBQ", drive.Root);

           OdbcConnection conn = new OdbcConnection(builder.ConnectionString);
           conn.Open();
           accessDBPSDriveInfo.Connection = conn;

           return accessDBPSDriveInfo;
       } // NewDrive
		public void TryGetValue ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			object value = null;

			builder ["DriverID"] = "790";
			builder.Add ("Server", "C:\\");
			Assert.IsTrue (builder.TryGetValue ("DriverID", out value), "#A1");
			Assert.AreEqual ("790", value, "#A2");
			Assert.IsTrue (builder.TryGetValue ("SERVER", out value), "#B1");
			Assert.AreEqual ("C:\\", value, "#B2");
			Assert.IsFalse (builder.TryGetValue (string.Empty, out value), "#C1");
			Assert.IsNull (value, "#C2");
			Assert.IsFalse (builder.TryGetValue ("a;", out value), "#D1");
			Assert.IsNull (value, "#D2");
			Assert.IsFalse (builder.TryGetValue ("\r", out value), "#E1");
			Assert.IsNull (value, "#E2");
			Assert.IsFalse (builder.TryGetValue (" ", out value), "#F1");
			Assert.IsNull (value, "#F2");
			Assert.IsFalse (builder.TryGetValue ("doesnotexist", out value), "#G1");
			Assert.IsNull (value, "#G2");
			Assert.IsTrue (builder.TryGetValue ("Driver", out value), "#H1");
			Assert.AreEqual (string.Empty, value, "#H2");
			Assert.IsTrue (builder.TryGetValue ("Dsn", out value), "#I1");
			Assert.AreEqual (string.Empty, value, "#I2");
			builder ["Driver"] = "SQL Server";
			Assert.IsTrue (builder.TryGetValue ("Driver", out value), "#J1");
			Assert.AreEqual ("SQL Server", value, "#J2");
			builder.Dsn = "AdventureWorks";
			Assert.IsTrue (builder.TryGetValue ("Dsn", out value), "#K1");
			Assert.AreEqual ("AdventureWorks", value, "#K2");
		}
		public void Remove ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			Assert.IsFalse (builder.Remove ("Dsn"), "#A1");
			Assert.IsFalse (builder.Remove ("Driver"), "#A2");
			builder.Add ("DriverID", "790");
			builder ["DefaultDir"] = "C:\\";
			Assert.IsTrue (builder.Remove ("DriverID"), "#B1");
			Assert.IsFalse (builder.ContainsKey ("DriverID"), "#B2");
			Assert.IsFalse (builder.Remove ("DriverID"), "#B3");
			Assert.IsFalse (builder.ContainsKey ("DriverID"), "#B4");
			Assert.IsTrue (builder.Remove ("defaulTdIr"), "#B5");
			Assert.IsFalse (builder.ContainsKey ("DefaultDir"), "#B6");
			Assert.IsFalse (builder.Remove ("defaulTdIr"), "#B7");
			Assert.IsFalse (builder.Remove ("userid"), "#B8");
			Assert.IsFalse (builder.Remove (string.Empty), "#B9");
			Assert.IsFalse (builder.Remove ("\r"), "#B10");
			Assert.IsFalse (builder.Remove ("a;"), "#B11");
			builder.Dsn = "myDsn";
			Assert.IsTrue (builder.Remove ("dSn"), "#C1");
			Assert.IsTrue (builder.ContainsKey ("dSn"), "#C2");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#C3");
			Assert.AreEqual (string.Empty, builder.Dsn, "#C4");
			Assert.IsFalse (builder.Remove ("Dsn"), "#C5");
			builder.Driver = "SQL Server";
			Assert.IsTrue (builder.Remove ("driVer"), "#D1");
			Assert.IsTrue (builder.ContainsKey ("driVer"), "#D2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#D3");
			Assert.AreEqual (string.Empty, builder.Driver, "#D4");
			Assert.IsFalse (builder.Remove ("Driver"), "#D5");
			builder ["Dsn"] = "myDsn";
			Assert.IsTrue (builder.Remove ("Dsn"), "#E1");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#E2");
			Assert.AreEqual (string.Empty, builder.Dsn, "#E3");
			Assert.IsFalse (builder.Remove ("Dsn"), "#E4");
			builder ["Driver"] = "SQL Server";
			Assert.IsTrue (builder.Remove ("Driver"), "#F1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#F2");
			Assert.AreEqual (string.Empty, builder.Driver, "#F3");
			Assert.IsFalse (builder.Remove ("Driver"), "#F4");
		}
		public void ContainsKey ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			builder ["SourceType"] = "DBC";
			builder.Add ("Port", "56");
			Assert.IsTrue (builder.ContainsKey ("SourceType"), "#1");
			Assert.IsTrue (builder.ContainsKey ("Port"), "#2");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#3");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#4");
			Assert.IsFalse (builder.ContainsKey ("xyz"), "#5");
			builder.Dsn = "myDsn";
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#6");
			builder.Driver = "SQL Server";
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#7");
			builder ["Dsn"] = "myDsn";
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#8");
			builder ["Driver"] = "SQL Server";
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#9");
			builder ["abc"] = "pqr";
			Assert.IsTrue (builder.ContainsKey ("ABC"), "#10");
			Assert.IsFalse (builder.ContainsKey (string.Empty), "#11");
		}
		public void Clear ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			builder ["Dbq"] = "C:\\Data.xls";
			builder.Driver = "SQL Server";
			builder.Dsn = "AdventureWorks";
			builder.Add ("Port", "56");
			builder.Clear ();
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#1");
			Assert.IsFalse (builder.ContainsKey ("Dbq"), "#2");
			Assert.AreEqual (string.Empty, builder.Driver, "#3");
			Assert.AreEqual (string.Empty, builder.Dsn, "#4");
			Assert.IsFalse (builder.ContainsKey ("Port"), "#5");
		}
		public void Add_Keyword_Null ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			try {
				builder.Add (null, "abc");
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("keyword", ex.ParamName, "#5");
			}
		}
		public void Add_Keyword_Invalid ()
		{
			string [] invalid_keywords = new string [] {
				string.Empty,
				" ",
				" abc",
				"abc ",
				"\r",
				"ab\rc",
				";abc",
				"a\0b"
				};

			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			for (int i = 0; i < invalid_keywords.Length; i++) {
				string keyword = invalid_keywords [i];
				try {
					builder.Add (keyword, "abc");
					Assert.Fail ("#1:" + i);
				} catch (ArgumentException ex) {
					// Invalid keyword, contain one or more of 'no characters',
					// 'control characters', 'leading or trailing whitespace'
					// or 'leading semicolons'
					Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:"+ i);
					Assert.IsNull (ex.InnerException, "#3:" + i);
					Assert.IsNotNull (ex.Message, "#4:" + i);
					Assert.IsTrue (ex.Message.IndexOf ("'" + keyword + "'") == -1, "#5:" + i);
					Assert.AreEqual (keyword, ex.ParamName, "#6:" + i);
				}
			}
		}