OleDbConnection_ConnectionString.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Copyright (c) 2006 Mainsoft Co.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Text;
  25. using System.Data;
  26. using System.Data.OleDb ;
  27. using System.Collections;
  28. using MonoTests.System.Data.Utils;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Data.OleDb
  31. {
  32. [TestFixture]
  33. public class OleDbConnection_ConnectionString : GHTBase
  34. {
  35. private Exception exp = null;
  36. private OleDbConnection con = new OleDbConnection();
  37. public static void Main()
  38. {
  39. OleDbConnection_ConnectionString tc = new OleDbConnection_ConnectionString();
  40. Exception exp = null;
  41. try
  42. {
  43. tc.BeginTest("OleDbConnection_ConnectionString");
  44. tc.run();
  45. }
  46. catch(Exception ex){exp = ex;}
  47. finally {tc.EndTest(exp);}
  48. }
  49. public void run()
  50. {
  51. SetConnectionString();
  52. ConstractorWithConnectionString();
  53. DB2MissingProperties();
  54. TestCaseForBug3925();
  55. }
  56. #region Tests
  57. [Test]
  58. public void DB2MissingProperties()
  59. {
  60. if (ConnectedDataProvider.GetDbType() != DataBaseServer.DB2)
  61. {
  62. Log("The 'DB2MissingProperties' subtest did not ran because it is relevant only for runs on DB2." );
  63. return;
  64. }
  65. string[] propsToUse;
  66. string description;
  67. //The complete list of properties is:
  68. //Provider, Password, User ID, Data Source, HostName, Port, Location, retrieveMessagesFromServerOnGetMessage
  69. description = "Test connection string without 'retrieveMessagesFromServerOnGetMessage'";
  70. propsToUse = new string[] {"Provider", "Password", "User ID", "Data Source", "HostName", "Port", "Location"};
  71. DoTestWithSpecificProperties(propsToUse, description);
  72. description = "Test connection string without 'HostName'";
  73. propsToUse = new string[] {"Provider", "Password", "User ID", "Data Source", "Port", "Location", "retrieveMessagesFromServerOnGetMessage"};
  74. DoTestWithSpecificProperties(propsToUse, description);
  75. description = "Test connection string without 'Port'";
  76. propsToUse = new string[] {"Provider", "Password", "User ID", "Data Source", "HostName", "Location", "retrieveMessagesFromServerOnGetMessage"};
  77. DoTestWithSpecificProperties(propsToUse, description);
  78. description = "Test connection string without 'Location'";
  79. propsToUse = new string[] {"Provider", "Password", "User ID", "Data Source", "HostName", "Port", "retrieveMessagesFromServerOnGetMessage"};
  80. DoTestWithSpecificProperties(propsToUse, description);
  81. description = "Test connection string without 'HostName' & 'Port'";
  82. propsToUse = new string[] {"Provider", "Password", "User ID", "Data Source", "Location", "retrieveMessagesFromServerOnGetMessage"};
  83. DoTestWithSpecificProperties(propsToUse, description);
  84. }
  85. void DoTestWithSpecificProperties(string[] propsToUse, string description)
  86. {
  87. try
  88. {
  89. BeginCase(description);
  90. exp = null;
  91. Hashtable connectionProps = GetConnectionStringProps(ConnectedDataProvider.ConnectionString);
  92. string actualConString = CreateConStringWithProps(propsToUse, connectionProps);
  93. con = new OleDbConnection(actualConString);
  94. con.Open();
  95. Compare(con.State, ConnectionState.Open);
  96. }
  97. catch(Exception ex)
  98. {
  99. exp = ex;
  100. }
  101. finally
  102. {
  103. EndCase(exp);
  104. exp=null;
  105. if (con != null && con.State == ConnectionState.Open)
  106. {
  107. con.Close();
  108. }
  109. }
  110. }
  111. [Test]
  112. public void ConstractorWithConnectionString()
  113. {
  114. con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  115. try
  116. {
  117. BeginCase("Constructor with ConnectionString");
  118. Compare(con.ConnectionString, MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  119. }
  120. catch(Exception ex)
  121. {
  122. exp = ex;
  123. }
  124. finally
  125. {
  126. EndCase(exp);
  127. exp = null;
  128. }
  129. }
  130. [Test]
  131. public void SetConnectionString()
  132. {
  133. con.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  134. try
  135. {
  136. BeginCase("Set ConnectionString");
  137. Compare(con.ConnectionString, MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  138. }
  139. catch(Exception ex)
  140. {
  141. exp = ex;
  142. }
  143. finally
  144. {
  145. EndCase(exp);
  146. exp = null;
  147. }
  148. }
  149. [Test]
  150. #if !JAVA
  151. [Category ("NotWorking")]
  152. #endif
  153. public void TestCaseForBug3925()
  154. {
  155. exp=null;
  156. try
  157. {
  158. BeginCase("Test Case for bug #3925");
  159. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer)
  160. {
  161. Skip("This test currently runs only on SQLServer in java.");
  162. return;
  163. }
  164. Hashtable conProps = GetConnectionStringProps(ConnectedDataProvider.ConnectionString);
  165. string server = (string)conProps["Data Source"];
  166. string user = (string)conProps["User Id"];
  167. string password = (string)conProps["Password"];
  168. string database = (string)conProps["Initial Catalog"];
  169. string jdbcUrlTemplate = "JdbcDriverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver;JdbcURL=\"jdbc:microsoft:sqlserver://{0};User={1};Password={2};DatabaseName={3}\"";
  170. string conStr = string.Format(jdbcUrlTemplate, server, user, password, database);
  171. con = new OleDbConnection(conStr);
  172. con.Open();
  173. Compare(ConnectionState.Open, con.State);
  174. }
  175. catch (Exception ex)
  176. {
  177. exp = ex;
  178. }
  179. finally
  180. {
  181. EndCase(exp);
  182. exp = null;
  183. if (con != null && con.State != ConnectionState.Closed)
  184. {
  185. con.Close();
  186. }
  187. }
  188. }
  189. #endregion
  190. #region Utilities
  191. private string CreateConStringWithProps(string[] propsToUse, Hashtable connectionProps)
  192. {
  193. StringBuilder actualConStringBuilder = new StringBuilder();
  194. foreach(string prop in propsToUse)
  195. {
  196. string propString = string.Format("{0}={1}", prop, connectionProps[prop]);
  197. actualConStringBuilder.Append(propString);
  198. actualConStringBuilder.Append(";");
  199. }
  200. return actualConStringBuilder.ToString();
  201. }
  202. private Hashtable GetConnectionStringProps(string connectionString)
  203. {
  204. string[] connectionStringProps = connectionString.Split(';');
  205. Hashtable connectionProps = new Hashtable();
  206. foreach(string prop in connectionStringProps)
  207. {
  208. string[] keyValue = prop.Split('=');
  209. if (keyValue.Length == 2)
  210. {
  211. connectionProps.Add(keyValue[0], keyValue[1]);
  212. }
  213. else
  214. {
  215. connectionProps.Add(prop, prop);
  216. }
  217. }
  218. return connectionProps;
  219. }
  220. #endregion
  221. }
  222. }