63246a0b138d39181c4268c63d070757d84d2fec
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Transactions;
6 using Xunit;
7
8 namespace System.Data.SqlClient.ManualTesting.Tests
9 {
10     public class TransactionEnlistmentTest
11     {
12         [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
13         public static void TestAutoEnlistment_TxScopeComplete()
14         {
15             RunTestSet(TestCase_AutoEnlistment_TxScopeComplete);
16         }
17
18         [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
19         public static void TestAutoEnlistment_TxScopeNonComplete()
20         {
21             RunTestSet(TestCase_AutoEnlistment_TxScopeNonComplete);
22         }
23
24         [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
25         public static void TestManualEnlistment_Enlist()
26         {
27             RunTestSet(TestCase_ManualEnlistment_Enlist);
28         }
29
30         [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
31         public static void TestManualEnlistment_NonEnlist()
32         {
33             RunTestSet(TestCase_ManualEnlistment_NonEnlist);
34         }
35
36         [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
37         public static void TestManualEnlistment_Enlist_TxScopeComplete()
38         {
39             RunTestSet(TestCase_ManualEnlistment_Enlist_TxScopeComplete);
40         }
41
42
43
44
45         private static void TestCase_AutoEnlistment_TxScopeComplete()
46         {
47             SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
48             builder.Enlist = true;
49             ConnectionString = builder.ConnectionString;
50
51             using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
52             {
53                 using (SqlConnection connection = new SqlConnection(ConnectionString))
54                 {
55                     connection.Open();
56                     using (SqlCommand command = connection.CreateCommand())
57                     {
58                         command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
59                         command.ExecuteNonQuery();
60                     }
61                 }
62                 txScope.Complete();
63             }
64
65             DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
66             Assert.True(result.Rows.Count == 1);
67             Assert.True(string.Equals(result.Rows[0][0], InputCol2));
68         }
69
70         private static void TestCase_AutoEnlistment_TxScopeNonComplete()
71         {
72             SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
73             builder.Enlist = true;
74             ConnectionString = builder.ConnectionString;
75
76             using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
77             {
78                 using (SqlConnection connection = new SqlConnection(ConnectionString))
79                 {
80                     connection.Open();
81                     using (SqlCommand command = connection.CreateCommand())
82                     {
83                         command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
84                         command.ExecuteNonQuery();
85                     }
86                 }
87             }
88
89             DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
90             Assert.True(result.Rows.Count == 0);
91         }
92
93         private static void TestCase_ManualEnlistment_Enlist()
94         {
95             SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
96             builder.Enlist = false;
97             ConnectionString = builder.ConnectionString;
98
99             using (SqlConnection connection = new SqlConnection(ConnectionString))
100             {
101                 connection.Open();
102                 using (TransactionScope txScope = new TransactionScope())
103                 {
104                     connection.EnlistTransaction(Transactions.Transaction.Current);
105                     using (SqlCommand command = connection.CreateCommand())
106                     {
107                         command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
108                         command.ExecuteNonQuery();
109                     }
110                 }
111             }
112
113             DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
114             Assert.True(result.Rows.Count == 0);
115         }
116
117         private static void TestCase_ManualEnlistment_NonEnlist()
118         {
119             SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
120             builder.Enlist = false;
121             ConnectionString = builder.ConnectionString;
122
123             using (SqlConnection connection = new SqlConnection(ConnectionString))
124             {
125                 connection.Open();
126                 using (TransactionScope txScope = new TransactionScope())
127                 {
128                     using (SqlCommand command = connection.CreateCommand())
129                     {
130                         command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
131                         command.ExecuteNonQuery();
132                     }
133                 }
134             }
135
136             DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
137             Assert.True(result.Rows.Count == 1);
138             Assert.True(string.Equals(result.Rows[0][0], InputCol2));
139         }
140
141         private static void TestCase_ManualEnlistment_Enlist_TxScopeComplete()
142         {
143             SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
144             builder.Enlist = false;
145             ConnectionString = builder.ConnectionString;
146
147             using (SqlConnection connection = new SqlConnection(ConnectionString))
148             {
149                 connection.Open();
150                 using (TransactionScope txScope = new TransactionScope())
151                 {
152                     connection.EnlistTransaction(Transactions.Transaction.Current);
153                     using (SqlCommand command = connection.CreateCommand())
154                     {
155                         command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
156                         command.ExecuteNonQuery();
157                     }
158                     txScope.Complete();
159                 }
160             }
161
162             DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
163             Assert.True(result.Rows.Count == 1);
164             Assert.True(string.Equals(result.Rows[0][0], InputCol2));
165         }
166
167
168
169
170         private static string TestTableName;
171         private static string ConnectionString;
172         private const int InputCol1 = 1;
173         private const string InputCol2 = "One";
174
175         private static void RunTestSet(Action TestCase)
176         {
177             SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr);
178             builder.ConnectTimeout = 5;
179
180             builder.Pooling = true;
181             ConnectionString = builder.ConnectionString;
182
183             RunTestFormat(TestCase);
184
185             builder.Pooling = false;
186             ConnectionString = builder.ConnectionString;
187
188             RunTestFormat(TestCase);
189         }
190
191         private static void RunTestFormat(Action testCase)
192         {
193             TestTableName = DataTestUtility.GenerateTableName();
194             DataTestUtility.RunNonQuery(ConnectionString, $"create table {TestTableName} (col1 int, col2 text)");
195             try
196             {
197                 testCase();
198             }
199             finally
200             {
201                 DataTestUtility.RunNonQuery(ConnectionString, $"drop table {TestTableName}");
202             }
203         }
204     }
205 }