4
4
5
5
using System ;
6
6
using System . Data ;
7
- using System . Reflection ;
8
7
using Xunit ;
9
8
10
9
namespace Microsoft . Data . SqlClient . UnitTests ;
@@ -17,18 +16,16 @@ public class SqlCommandSetTest
17
16
/// <summary>
18
17
/// Verifies that key properties throw an ObjectDisposedException after the SqlCommandSet has been disposed.
19
18
/// </summary>
20
- /// <remarks>
21
- /// These properties are private, requiring reflection to access.
22
- /// </remarks>
23
- [ Theory ]
24
- [ InlineData ( "BatchCommand" ) ]
25
- [ InlineData ( "CommandList" ) ]
26
- public void GetDisposedProperty_Throws ( string propertyName )
19
+ [ Fact ]
20
+ public void GetDisposedProperty_Throws ( )
27
21
{
28
22
SqlCommandSet cmdSet = new ( ) ;
29
23
cmdSet . Dispose ( ) ;
30
24
31
- ObjectDisposedException ex = GetProperty_Throws < ObjectDisposedException > ( cmdSet , propertyName ) ;
25
+ ObjectDisposedException ex = Assert . Throws < ObjectDisposedException > ( ( ) => _ = cmdSet . BatchCommand ) ;
26
+ Assert . Contains ( "disposed" , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
27
+
28
+ ex = Assert . Throws < ObjectDisposedException > ( ( ) => _ = cmdSet . CommandList ) ;
32
29
Assert . Contains ( "disposed" , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
33
30
}
34
31
@@ -152,7 +149,7 @@ public void InvalidCommandBehaviorValidateCommandBehavior_Throws()
152
149
{
153
150
SqlCommandSet cmdSet = new ( ) ;
154
151
155
- ArgumentOutOfRangeException ex = InvokeMethod_Throws < ArgumentOutOfRangeException > ( cmdSet , " ValidateCommandBehavior" , " ExecuteNonQuery", ( CommandBehavior ) 64 ) ;
152
+ ArgumentOutOfRangeException ex = Assert . Throws < ArgumentOutOfRangeException > ( ( ) => cmdSet . ValidateCommandBehavior ( " ExecuteNonQuery", ( CommandBehavior ) 64 ) ) ;
156
153
Assert . Contains ( "CommandBehavior" , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
157
154
}
158
155
@@ -164,50 +161,16 @@ public void NotSupportedCommandBehaviorValidateCommandBehavior_Throws()
164
161
{
165
162
SqlCommandSet cmdSet = new ( ) ;
166
163
167
- ArgumentOutOfRangeException ex = InvokeMethod_Throws < ArgumentOutOfRangeException > ( cmdSet , " ValidateCommandBehavior" , " ExecuteNonQuery", CommandBehavior . KeyInfo ) ;
164
+ ArgumentOutOfRangeException ex = Assert . Throws < ArgumentOutOfRangeException > ( ( ) => cmdSet . ValidateCommandBehavior ( " ExecuteNonQuery", CommandBehavior . KeyInfo ) ) ;
168
165
Assert . Contains ( "not supported" , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
169
166
}
170
167
171
- #region private methods
172
-
173
- private static T GetProperty_Throws < T > ( SqlCommandSet instance , string propertyName )
174
- where T : Exception
175
- => InvokeMethod_Throws < T > ( instance ,
176
- typeof ( SqlCommandSet )
177
- . GetProperty ( propertyName , BindingFlags . NonPublic | BindingFlags . Instance )
178
- . GetGetMethod ( true ) ,
179
- [ ] ) ;
180
-
181
- private static T InvokeMethod_Throws < T > ( SqlCommandSet instance , string methodName , params object [ ] values )
182
- where T : Exception
183
- => InvokeMethod_Throws < T > ( instance ,
184
- typeof ( SqlCommandSet )
185
- . GetMethod ( methodName , BindingFlags . NonPublic | BindingFlags . Instance ) ,
186
- values ) ;
187
-
188
- private static T InvokeMethod_Throws < T > ( SqlCommandSet instance , MethodInfo methodInfo , params object [ ] values )
189
- where T : Exception
190
- {
191
- return Assert . Throws < T > ( ( ) =>
192
- {
193
- try
194
- {
195
- methodInfo . Invoke ( instance , values ) ;
196
- }
197
- catch ( TargetInvocationException e )
198
- {
199
- throw e . InnerException ;
200
- }
201
- } ) ;
202
- }
203
-
204
168
private static SqlCommand GenerateBadCommand ( CommandType cType )
205
169
{
206
170
SqlCommand cmd = new ( "Test" ) ;
207
171
// There's validation done on the CommandType property, but we need to create one that avoids the check for the test case.
208
- typeof ( SqlCommand ) . GetField ( " _commandType" , BindingFlags . NonPublic | BindingFlags . Instance ) . SetValue ( cmd , cType ) ;
172
+ cmd . _commandType = cType ;
209
173
210
174
return cmd ;
211
175
}
212
- #endregion
213
176
}
0 commit comments