@@ -8,26 +8,26 @@ namespace Microsoft.Data.SqlClient.UnitTests
8
8
{
9
9
public class SqlCommandSetTest
10
10
{
11
- private static Assembly mds = Assembly . GetAssembly ( typeof ( SqlConnection ) ) ;
12
-
13
11
[ Theory ]
14
12
[ InlineData ( "BatchCommand" ) ]
15
13
[ InlineData ( "CommandList" ) ]
16
14
public void GetDisposedProperty_Throws ( string propertyName )
17
15
{
18
- var cmdSet = CreateInstance ( ) ;
19
- CallMethod ( cmdSet , "Dispose" ) ;
20
- Exception ex = GetProperty_Throws ( cmdSet , propertyName ) ;
21
- VerifyException < ObjectDisposedException > ( ex , "disposed" ) ;
16
+ SqlCommandSet cmdSet = new ( ) ;
17
+ cmdSet . Dispose ( ) ;
18
+
19
+ ObjectDisposedException ode = GetProperty_Throws < ObjectDisposedException > ( cmdSet , propertyName ) ;
20
+ Assert . Contains ( "disposed" , ode . Message , StringComparison . OrdinalIgnoreCase ) ;
22
21
}
23
22
24
23
[ Fact ]
25
24
public void AppendCommandWithEmptyString_Throws ( )
26
25
{
27
- var cmdSet = CreateInstance ( ) ;
28
- SqlCommand cmd = new SqlCommand ( "" ) ;
29
- Exception ex = CallMethod_Throws ( cmdSet , "Append" , cmd ) ;
30
- VerifyException < InvalidOperationException > ( ex , "CommandText property has not been initialized" ) ;
26
+ SqlCommandSet cmdSet = new ( ) ;
27
+ SqlCommand cmd = new ( "" ) ;
28
+
29
+ InvalidOperationException ioe = Assert . Throws < InvalidOperationException > ( ( ) => cmdSet . Append ( cmd ) ) ;
30
+ Assert . Contains ( "CommandText property has not been initialized" , ioe . Message , StringComparison . OrdinalIgnoreCase ) ;
31
31
}
32
32
33
33
public static IEnumerable < object [ ] > CommandTypeData ( )
@@ -52,37 +52,38 @@ public static IEnumerable<object[]> CommandTypeData()
52
52
) ]
53
53
public void AppendBadCommandType_Throws ( CommandType commandType )
54
54
{
55
- var cmdSet = CreateInstance ( ) ;
55
+ SqlCommandSet cmdSet = new ( ) ;
56
56
SqlCommand cmd = GenerateBadCommand ( commandType ) ;
57
- Exception ex = CallMethod_Throws ( cmdSet , "Append" , cmd ) ;
58
- VerifyException < ArgumentOutOfRangeException > ( ex , "CommandType" ) ;
57
+
58
+ ArgumentOutOfRangeException aoore = Assert . Throws < ArgumentOutOfRangeException > ( ( ) => cmdSet . Append ( cmd ) ) ;
59
+ Assert . Contains ( "CommandType" , aoore . Message , StringComparison . OrdinalIgnoreCase ) ;
59
60
}
60
61
61
62
[ Fact ]
62
63
public void AppendBadParameterName_Throws ( )
63
64
{
64
- var cmdSet = CreateInstance ( ) ;
65
- SqlCommand cmd = new SqlCommand ( "Test" ) ;
65
+ SqlCommandSet cmdSet = new ( ) ;
66
+ SqlCommand cmd = new ( "Test" ) ;
66
67
cmd . CommandType = CommandType . Text ;
67
68
cmd . Parameters . Add ( new SqlParameter ( "Test1;=" , "1" ) ) ;
68
- Exception ex = CallMethod_Throws ( cmdSet , "Append" , cmd ) ;
69
- VerifyException < ArgumentException > ( ex , "not valid" ) ;
69
+
70
+ ArgumentException ae = Assert . Throws < ArgumentException > ( ( ) => cmdSet . Append ( cmd ) ) ;
71
+ Assert . Contains ( "not valid" , ae . Message , StringComparison . OrdinalIgnoreCase ) ;
70
72
}
71
73
72
74
[ Theory ]
73
75
[ InlineData ( new byte [ ] { 1 , 2 , 3 } ) ]
74
76
[ InlineData ( new char [ ] { '1' , '2' , '3' } ) ]
75
77
public void AppendParameterArrayWithSize ( object array )
76
78
{
77
- var cmdSet = CreateInstance ( ) ;
78
- SqlCommand cmd = new SqlCommand ( "Test" ) ;
79
+ SqlCommandSet cmdSet = new ( ) ;
80
+ SqlCommand cmd = new ( "Test" ) ;
79
81
cmd . CommandType = CommandType . StoredProcedure ;
80
- SqlParameter parameter = new SqlParameter ( "@array" , array ) ;
82
+ SqlParameter parameter = new ( "@array" , array ) ;
81
83
parameter . Size = 2 ;
82
84
cmd . Parameters . Add ( parameter ) ;
83
- CallMethod ( cmdSet , "Append" , cmd ) ;
84
- object p = CallMethod ( cmdSet , "GetParameter" , 0 , 0 ) ;
85
- SqlParameter result = p as SqlParameter ;
85
+ cmdSet . Append ( cmd ) ;
86
+ SqlParameter result = cmdSet . GetParameter ( 0 , 0 ) ;
86
87
Assert . NotNull ( result ) ;
87
88
Assert . Equal ( "@array" , result . ParameterName ) ;
88
89
Assert . Equal ( 2 , result . Size ) ;
@@ -91,13 +92,12 @@ public void AppendParameterArrayWithSize(object array)
91
92
[ Fact ]
92
93
public void GetParameter ( )
93
94
{
94
- var cmdSet = CreateInstance ( ) ;
95
- SqlCommand cmd = new SqlCommand ( "Test" ) ;
95
+ SqlCommandSet cmdSet = new ( ) ;
96
+ SqlCommand cmd = new ( "Test" ) ;
96
97
cmd . CommandType = CommandType . Text ;
97
98
cmd . Parameters . Add ( new SqlParameter ( "@text" , "value" ) ) ;
98
- CallMethod ( cmdSet , "Append" , cmd ) ;
99
- object p = CallMethod ( cmdSet , "GetParameter" , 0 , 0 ) ;
100
- SqlParameter result = p as SqlParameter ;
99
+ cmdSet . Append ( cmd ) ;
100
+ SqlParameter result = cmdSet . GetParameter ( 0 , 0 ) ;
101
101
Assert . NotNull ( result ) ;
102
102
Assert . Equal ( "@text" , result . ParameterName ) ;
103
103
Assert . Equal ( "value" , ( string ) result . Value ) ;
@@ -106,95 +106,76 @@ public void GetParameter()
106
106
[ Fact ]
107
107
public void GetParameterCount ( )
108
108
{
109
- var commandSetType = mds . GetType ( "Microsoft.Data.SqlClient.SqlCommandSet" ) ;
110
- var cmdSet = Activator . CreateInstance ( commandSetType , true ) ;
111
- SqlCommand cmd = new SqlCommand ( "Test" ) ;
109
+ SqlCommandSet cmdSet = new ( ) ;
110
+ SqlCommand cmd = new ( "Test" ) ;
112
111
cmd . CommandType = CommandType . Text ;
113
112
cmd . Parameters . Add ( new SqlParameter ( "@abc" , "1" ) ) ;
114
113
cmd . Parameters . Add ( new SqlParameter ( "@test" , "2" ) ) ;
115
- commandSetType . GetMethod ( " Append" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) . Invoke ( cmdSet , new object [ ] { cmd } ) ;
114
+ cmdSet . Append ( cmd ) ;
116
115
int index = 0 ;
117
- int count = ( int ) commandSetType . GetMethod ( " GetParameterCount" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) . Invoke ( cmdSet , new object [ ] { index } ) ;
116
+ int count = cmdSet . GetParameterCount ( index ) ;
118
117
Assert . Equal ( 2 , count ) ;
119
118
}
120
119
121
120
[ Fact ]
122
121
public void InvalidCommandBehaviorValidateCommandBehavior_Throws ( )
123
122
{
124
- var cmdSet = CreateInstance ( ) ;
125
- Exception ex = CallMethod_Throws ( cmdSet , "ValidateCommandBehavior" , "ExecuteNonQuery" , ( CommandBehavior ) 64 ) ;
126
- VerifyException < ArgumentOutOfRangeException > ( ex , "CommandBehavior" ) ;
123
+ SqlCommandSet cmdSet = new ( ) ;
124
+
125
+ ArgumentOutOfRangeException aoore = InvokeMethod_Throws < ArgumentOutOfRangeException > ( cmdSet , "ValidateCommandBehavior" , "ExecuteNonQuery" , ( CommandBehavior ) 64 ) ;
126
+ Assert . Contains ( "CommandBehavior" , aoore . Message , StringComparison . OrdinalIgnoreCase ) ;
127
127
}
128
128
129
129
[ Fact ]
130
130
public void NotSupportedCommandBehaviorValidateCommandBehavior_Throws ( )
131
131
{
132
- var cmdSet = CreateInstance ( ) ;
133
- Exception ex = CallMethod_Throws ( cmdSet , "ValidateCommandBehavior" , "ExecuteNonQuery" , CommandBehavior . KeyInfo ) ;
134
- VerifyException < ArgumentOutOfRangeException > ( ex , "not supported" ) ;
135
- }
136
-
137
- #region private methods
138
-
139
- private object CallMethod ( object instance , string methodName , params object [ ] values )
140
- {
141
- var commandSetType = mds . GetType ( "Microsoft.Data.SqlClient.SqlCommandSet" ) ;
142
- object returnValue = commandSetType . GetMethod ( methodName , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) . Invoke ( instance , values ) ;
143
- return returnValue ;
144
- }
145
-
146
- private object CallMethod ( object instance , string methodName )
147
- {
148
- var commandSetType = mds . GetType ( "Microsoft.Data.SqlClient.SqlCommandSet" ) ;
149
- object returnValue = commandSetType . GetMethod ( methodName , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) . Invoke ( instance , new object [ ] { } ) ;
150
- return returnValue ;
151
- }
132
+ SqlCommandSet cmdSet = new ( ) ;
152
133
153
- private Exception CallMethod_Throws ( object instance , string methodName , params object [ ] values )
154
- {
155
- var commandSetType = mds . GetType ( "Microsoft.Data.SqlClient.SqlCommandSet" ) ;
156
- Exception ex = Assert . ThrowsAny < Exception > ( ( ) =>
157
- {
158
- commandSetType . GetMethod ( methodName , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) . Invoke ( instance , values ) ;
159
- } ) ;
160
- return ex ;
134
+ ArgumentOutOfRangeException aoore = InvokeMethod_Throws < ArgumentOutOfRangeException > ( cmdSet , "ValidateCommandBehavior" , "ExecuteNonQuery" , CommandBehavior . KeyInfo ) ;
135
+ Assert . Contains ( "not supported" , aoore . Message , StringComparison . OrdinalIgnoreCase ) ;
161
136
}
162
137
163
- private object CreateInstance ( )
164
- {
165
- var commandSetType = mds . GetType ( "Microsoft.Data.SqlClient.SqlCommandSet" ) ;
166
- object cmdSet = Activator . CreateInstance ( commandSetType , true ) ;
167
- return cmdSet ;
168
- }
138
+ #region private methods
169
139
170
- private Exception GetProperty_Throws ( object instance , string propertyName )
171
- {
172
- var commandSetType = mds . GetType ( "Microsoft.Data.SqlClient.SqlCommandSet" ) ;
173
- var cmdSet = instance ;
174
- Exception ex = Assert . ThrowsAny < Exception > ( ( ) =>
140
+ private static T GetProperty_Throws < T > ( SqlCommandSet instance , string propertyName )
141
+ where T : Exception
142
+ => InvokeMethod_Throws < T > ( instance ,
143
+ typeof ( SqlCommandSet )
144
+ . GetProperty ( propertyName , BindingFlags . NonPublic | BindingFlags . Instance )
145
+ . GetGetMethod ( true ) ,
146
+ [ ] ) ;
147
+
148
+ private static T InvokeMethod_Throws < T > ( SqlCommandSet instance , string methodName , params object [ ] values )
149
+ where T : Exception
150
+ => InvokeMethod_Throws < T > ( instance ,
151
+ typeof ( SqlCommandSet )
152
+ . GetMethod ( methodName , BindingFlags . NonPublic | BindingFlags . Instance ) ,
153
+ values ) ;
154
+
155
+ private static T InvokeMethod_Throws < T > ( SqlCommandSet instance , MethodInfo methodInfo , params object [ ] values )
156
+ where T : Exception
157
+ {
158
+ return Assert . Throws < T > ( ( ) =>
175
159
{
176
- commandSetType . GetProperty ( propertyName , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) . GetGetMethod ( true ) . Invoke ( cmdSet , new object [ ] { } ) ;
160
+ try
161
+ {
162
+ methodInfo . Invoke ( instance , values ) ;
163
+ }
164
+ catch ( TargetInvocationException e )
165
+ {
166
+ throw e . InnerException ;
167
+ }
177
168
} ) ;
178
-
179
- return ex ;
180
169
}
181
170
182
171
private SqlCommand GenerateBadCommand ( CommandType cType )
183
172
{
184
- SqlCommand cmd = new SqlCommand ( "Test" ) ;
185
- Type sqlCommandType = cmd . GetType ( ) ;
173
+ SqlCommand cmd = new ( "Test" ) ;
186
174
// There's validation done on the CommandType property, but we need to create one that avoids the check for the test case.
187
- sqlCommandType . GetField ( "_commandType" , BindingFlags . NonPublic | BindingFlags . Instance ) . SetValue ( cmd , cType ) ;
175
+ typeof ( SqlCommand ) . GetField ( "_commandType" , BindingFlags . NonPublic | BindingFlags . Instance ) . SetValue ( cmd , cType ) ;
188
176
189
177
return cmd ;
190
178
}
191
-
192
- private void VerifyException < T > ( Exception ex , string contains )
193
- {
194
- Assert . NotNull ( ex ) ;
195
- Assert . IsType < T > ( ex . InnerException ) ;
196
- Assert . Contains ( contains , ex . InnerException . Message , StringComparison . OrdinalIgnoreCase ) ;
197
- }
198
179
#endregion
199
180
}
200
181
}
0 commit comments