New test.
[mono.git] / mcs / class / System.Data / Test / ProviderTests / System.Data.SqlClient / SqlConnectionStringBuilderTest.cs
1 // SqlConnectionStringBuilderTest.cs - NUnit Test Cases for Testing the 
2 // SqlConnectionStringBuilder class
3 //
4 // Author: 
5 //      Sureshkumar T (tsureshkumar@novell.com)
6 //
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 #region Using directives
33
34 using System;
35 using System.Text;
36 using System.Collections;
37
38 using System.Data;
39 using System.Data.SqlClient;
40
41 using NUnit.Framework;
42
43 #endregion
44
45 namespace MonoTests.System.Data.Common
46 {
47
48         [TestFixture]
49         [Category ("sqlserver")]
50         public class SqlConnectionStringBuilderTest
51         {
52                 private SqlConnectionStringBuilder builder = null;
53                 
54                 [Test]
55                 public void DefaultValuestTest ()
56                 {
57                         builder = new SqlConnectionStringBuilder ();
58                         Assert.AreEqual ("", builder.ConnectionString, "#DV1 default values is wrong");
59                 }
60
61                 [Test]
62                 public void DefaultValuestTest2 ()
63                 {
64                         builder = new SqlConnectionStringBuilder ("SERVER=localhost;");
65                         Assert.AreEqual ("Data Source=localhost", builder.ConnectionString, "#DVT1 default values is wrong");
66                 }
67
68                 [Test]
69                 public void PropertiesTest ()
70                 {
71                         builder = new SqlConnectionStringBuilder ("SERVER=localhost;");
72                         builder.AsynchronousProcessing = true;
73                         builder.ApplicationName = "mono test";
74                         Assert.AreEqual (true, 
75                                          builder.ConnectionString.Contains ("Asynchronous Processing=True"),
76                                          "#PT1 boolean value must be true");
77                 }
78                 
79                 [Test]
80                 public void ItemTest ()
81                 {
82                         builder = new SqlConnectionStringBuilder ("SERVER=localhost;");
83                         builder ["Network Library"] = "DBMSSOCN";
84                         Assert.AreEqual (true, 
85                                          builder.ConnectionString.Contains ("Network Library=dbmssocn"),
86                                          "#PT1 network library should exist");
87                 }
88
89                 public void NullTest ()
90                 {
91                         builder = new SqlConnectionStringBuilder ("SERVER=localhost;Network=DBMSSOCN");
92                         builder ["Network Library"] = null;
93                         Assert.AreEqual ("Data Source=localhost", builder.ConnectionString,
94                                          "#NT1 should remove the key if set with null");
95                 }
96
97                 public void ContainsKeyTest ()
98                 {
99                         builder = new SqlConnectionStringBuilder ("SERVER=localhost;Network=DBMSSOCN");
100                         Assert.AreEqual (true, builder.ContainsKey ("NETWORK"),
101                                          "#CKT1 should say true");
102                         Assert.AreEqual (false, builder.ContainsKey ("ABCD"),
103                                          "#CKT2 should say false");
104                 }
105                 
106                 [Test, ExpectedException (typeof (ArgumentException))]
107                 public void InvalidKeyTest ()
108                 {
109                         builder = new SqlConnectionStringBuilder ("SERVER=localhost;Network=DBMSSOCN");
110                         int value = (int) builder ["ABCD"];
111                         value++; // to avoid warning
112                 }
113
114                 [Test]
115                 public void RemoveTest ()
116                 {
117                         builder = new SqlConnectionStringBuilder ("SERVER = localhost ;Network=DBMSSOCN");
118                         // non existing key
119                         Assert.AreEqual (false, builder.Remove ("ABCD"),
120                                          "#RT1 cannot remove non existant key");
121                         Assert.AreEqual (true, builder.Remove ("NETWORK library"),
122                                          "#RT2 should remove the key");
123                         Assert.AreEqual ("Data Source=localhost", builder.ConnectionString,
124                                          "#RT3 should have removed the key");
125                 }
126
127                 [Test]
128                 public void TrustServerCertificateTest ()
129                 {
130                         builder = new SqlConnectionStringBuilder ();
131                         Assert.AreEqual (false, builder.TrustServerCertificate, "#1 The default value should be false");
132                         builder.TrustServerCertificate = true;
133                         Assert.AreEqual (true, builder.TrustServerCertificate, "#2 The value returned should be true after setting the value of TrustServerCertificate to true");
134                         Assert.AreEqual ("Trust Server Certificate=True", builder.ConnectionString, "#3 The value of the key TrustServerCertificate should be added to the connection string");
135                 }
136                 
137                 [Test]
138                 public void TypeSystemVersionTest ()
139                 {
140                         builder = new SqlConnectionStringBuilder ();
141                         Assert.AreEqual ("Latest", builder.TypeSystemVersion, "#1 The default value for the property should be Latest");
142                         builder.TypeSystemVersion = "SQL Server 2005";
143                         Assert.AreEqual ("SQL Server 2005", builder.TypeSystemVersion, "#2 The value for the property should be SQL Server 2005 after setting this value");
144                         Assert.AreEqual ("Type System Version=SQL Server 2005", builder.ConnectionString, "#3 The value of the key Type System Version should be added to the connection string");
145                 }
146
147                 [Test]
148                 public void UserInstanceTest ()
149                 {
150                         builder = new SqlConnectionStringBuilder ();
151                         Assert.AreEqual (false, builder.UserInstance, "#1 The default value for the property should be false");
152                         builder.UserInstance = true;
153                         Assert.AreEqual (true, builder.UserInstance, "#2 The value for the property should be true after setting its value to true");
154                         Assert.AreEqual ("User Instance=True", builder.ConnectionString, "#3 The value of the key User Instance should be added to the connection string");
155                 }
156
157                 [Test]
158                 public void SettingUserInstanceTest ()
159                 {
160                         builder = new SqlConnectionStringBuilder ();
161                         builder["User Instance"] = true;
162                         Assert.AreEqual ("User Instance=True", builder.ConnectionString, "#1 The value of the key User Instance should be added to the connection string");                     
163                 }
164
165                 [Test]
166                 public void ContextConnectionTest ()
167                 {
168                         builder = new SqlConnectionStringBuilder ();
169                         Assert.AreEqual (false, builder.ContextConnection, "#1 The default value for the property should be false");
170                         builder.ContextConnection = true;
171                         Assert.AreEqual (true, builder.ContextConnection, "#2 The value for the property should be true after setting its value to true");                      
172                         Assert.AreEqual ("Context Connection=True", builder.ConnectionString, "#3 The value of the key Context Connection should be added to the connection string");
173                 }
174
175                 [Test]
176                 public void SettingContextConnectionTest ()
177                 {
178                         builder = new SqlConnectionStringBuilder ();
179                         builder["Context Connection"] = true;
180                         Assert.AreEqual ("Context Connection=True", builder.ConnectionString, "#1 The value of the key Context Connection should be added to the connection string");                   
181                 }
182         }
183 }
184
185 #endif // NET_2_0