Skip to content

Commit 7948fab

Browse files
committed
Remove unnecessary reflection from SqlCommandSetTest
1 parent b713b2a commit 7948fab

File tree

1 file changed

+69
-88
lines changed

1 file changed

+69
-88
lines changed

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlCommandSetTest.cs

Lines changed: 69 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ namespace Microsoft.Data.SqlClient.UnitTests
88
{
99
public class SqlCommandSetTest
1010
{
11-
private static Assembly mds = Assembly.GetAssembly(typeof(SqlConnection));
12-
1311
[Theory]
1412
[InlineData("BatchCommand")]
1513
[InlineData("CommandList")]
1614
public void GetDisposedProperty_Throws(string propertyName)
1715
{
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);
2221
}
2322

2423
[Fact]
2524
public void AppendCommandWithEmptyString_Throws()
2625
{
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);
3131
}
3232

3333
public static IEnumerable<object[]> CommandTypeData()
@@ -52,37 +52,38 @@ public static IEnumerable<object[]> CommandTypeData()
5252
)]
5353
public void AppendBadCommandType_Throws(CommandType commandType)
5454
{
55-
var cmdSet = CreateInstance();
55+
SqlCommandSet cmdSet = new();
5656
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);
5960
}
6061

6162
[Fact]
6263
public void AppendBadParameterName_Throws()
6364
{
64-
var cmdSet = CreateInstance();
65-
SqlCommand cmd = new SqlCommand("Test");
65+
SqlCommandSet cmdSet = new();
66+
SqlCommand cmd = new("Test");
6667
cmd.CommandType = CommandType.Text;
6768
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);
7072
}
7173

7274
[Theory]
7375
[InlineData(new byte[] { 1, 2, 3 })]
7476
[InlineData(new char[] { '1', '2', '3' })]
7577
public void AppendParameterArrayWithSize(object array)
7678
{
77-
var cmdSet = CreateInstance();
78-
SqlCommand cmd = new SqlCommand("Test");
79+
SqlCommandSet cmdSet = new();
80+
SqlCommand cmd = new("Test");
7981
cmd.CommandType = CommandType.StoredProcedure;
80-
SqlParameter parameter = new SqlParameter("@array", array);
82+
SqlParameter parameter = new("@array", array);
8183
parameter.Size = 2;
8284
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);
8687
Assert.NotNull(result);
8788
Assert.Equal("@array", result.ParameterName);
8889
Assert.Equal(2, result.Size);
@@ -91,13 +92,12 @@ public void AppendParameterArrayWithSize(object array)
9192
[Fact]
9293
public void GetParameter()
9394
{
94-
var cmdSet = CreateInstance();
95-
SqlCommand cmd = new SqlCommand("Test");
95+
SqlCommandSet cmdSet = new();
96+
SqlCommand cmd = new("Test");
9697
cmd.CommandType = CommandType.Text;
9798
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);
101101
Assert.NotNull(result);
102102
Assert.Equal("@text", result.ParameterName);
103103
Assert.Equal("value", (string)result.Value);
@@ -106,95 +106,76 @@ public void GetParameter()
106106
[Fact]
107107
public void GetParameterCount()
108108
{
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");
112111
cmd.CommandType = CommandType.Text;
113112
cmd.Parameters.Add(new SqlParameter("@abc", "1"));
114113
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);
116115
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);
118117
Assert.Equal(2, count);
119118
}
120119

121120
[Fact]
122121
public void InvalidCommandBehaviorValidateCommandBehavior_Throws()
123122
{
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);
127127
}
128128

129129
[Fact]
130130
public void NotSupportedCommandBehaviorValidateCommandBehavior_Throws()
131131
{
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();
152133

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);
161136
}
162137

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
169139

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>(() =>
175159
{
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+
}
177168
});
178-
179-
return ex;
180169
}
181170

182171
private SqlCommand GenerateBadCommand(CommandType cType)
183172
{
184-
SqlCommand cmd = new SqlCommand("Test");
185-
Type sqlCommandType = cmd.GetType();
173+
SqlCommand cmd = new("Test");
186174
// 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);
188176

189177
return cmd;
190178
}
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-
}
198179
#endregion
199180
}
200181
}

0 commit comments

Comments
 (0)