Skip to content

Commit af251dc

Browse files
committed
Remove all internal reflection on SqlCommandSetTest
1 parent 95b1dcc commit af251dc

File tree

4 files changed

+14
-51
lines changed

4 files changed

+14
-51
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected override void AfterCleared(SqlCommand owner)
114114
}
115115

116116
private string _commandText;
117-
private CommandType _commandType;
117+
internal CommandType _commandType;
118118
private int? _commandTimeout;
119119
private UpdateRowSource _updatedRowSource = UpdateRowSource.Both;
120120
private bool _designTimeInvisible;

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected override void AfterCleared(SqlCommand owner)
119119
}
120120

121121
private string _commandText;
122-
private CommandType _commandType;
122+
internal CommandType _commandType;
123123
private int? _commandTimeout;
124124
private UpdateRowSource _updatedRowSource = UpdateRowSource.Both;
125125
private bool _designTimeInvisible;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommandSet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal SqlCommandSet() : base()
3030
_commandList = new List<SqlBatchCommand>();
3131
}
3232

33-
private SqlCommand BatchCommand
33+
internal SqlCommand BatchCommand
3434
{
3535
get
3636
{
@@ -45,7 +45,7 @@ private SqlCommand BatchCommand
4545

4646
internal int CommandCount => CommandList.Count;
4747

48-
private List<SqlBatchCommand> CommandList
48+
internal List<SqlBatchCommand> CommandList
4949
{
5050
get
5151
{
@@ -279,7 +279,7 @@ internal bool GetBatchedAffected(int commandIdentifier, out int recordsAffected,
279279
internal int GetParameterCount(int commandIndex)
280280
=> CommandList[commandIndex].Parameters.Count;
281281

282-
private void ValidateCommandBehavior(string method, CommandBehavior behavior)
282+
internal void ValidateCommandBehavior(string method, CommandBehavior behavior)
283283
{
284284
if (0 != (behavior & ~(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)))
285285
{

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

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Data;
7-
using System.Reflection;
87
using Xunit;
98

109
namespace Microsoft.Data.SqlClient.UnitTests;
@@ -17,18 +16,16 @@ public class SqlCommandSetTest
1716
/// <summary>
1817
/// Verifies that key properties throw an ObjectDisposedException after the SqlCommandSet has been disposed.
1918
/// </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()
2721
{
2822
SqlCommandSet cmdSet = new();
2923
cmdSet.Dispose();
3024

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);
3229
Assert.Contains("disposed", ex.Message, StringComparison.OrdinalIgnoreCase);
3330
}
3431

@@ -152,7 +149,7 @@ public void InvalidCommandBehaviorValidateCommandBehavior_Throws()
152149
{
153150
SqlCommandSet cmdSet = new();
154151

155-
ArgumentOutOfRangeException ex = InvokeMethod_Throws<ArgumentOutOfRangeException>(cmdSet, "ValidateCommandBehavior", "ExecuteNonQuery", (CommandBehavior)64);
152+
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => cmdSet.ValidateCommandBehavior("ExecuteNonQuery", (CommandBehavior)64));
156153
Assert.Contains("CommandBehavior", ex.Message, StringComparison.OrdinalIgnoreCase);
157154
}
158155

@@ -164,50 +161,16 @@ public void NotSupportedCommandBehaviorValidateCommandBehavior_Throws()
164161
{
165162
SqlCommandSet cmdSet = new();
166163

167-
ArgumentOutOfRangeException ex = InvokeMethod_Throws<ArgumentOutOfRangeException>(cmdSet, "ValidateCommandBehavior", "ExecuteNonQuery", CommandBehavior.KeyInfo);
164+
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => cmdSet.ValidateCommandBehavior("ExecuteNonQuery", CommandBehavior.KeyInfo));
168165
Assert.Contains("not supported", ex.Message, StringComparison.OrdinalIgnoreCase);
169166
}
170167

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-
204168
private static SqlCommand GenerateBadCommand(CommandType cType)
205169
{
206170
SqlCommand cmd = new("Test");
207171
// 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;
209173

210174
return cmd;
211175
}
212-
#endregion
213176
}

0 commit comments

Comments
 (0)