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