Skip to content

Commit

Permalink
Fix and re-enable outerloop test on OLEDB (dotnet/corefx#38024)
Browse files Browse the repository at this point in the history


Commit migrated from dotnet/corefx@477abf1
  • Loading branch information
maryamariyan authored Jul 4, 2019
1 parent a7ce624 commit 57db927
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public static class AssertExtensions
{
private static bool IsFullFramework => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework");

public static void Throws<T>(Action action, string message)
public static void Throws<T>(Action action, string expectedMessage)
where T : Exception
{
Assert.Equal(Assert.Throws<T>(action).Message, message);
Assert.Equal(expectedMessage, Assert.Throws<T>(action).Message);
}

public static void Throws<T>(string netCoreParamName, string netFxParamName, Action action)
Expand Down Expand Up @@ -57,12 +57,12 @@ public static void Throws<T>(string netCoreParamName, string netFxParamName, Fun
Assert.Equal(expectedParamName, exception.ParamName);
}

public static T Throws<T>(string paramName, Action action)
public static T Throws<T>(string expectedParamName, Action action)
where T : ArgumentException
{
T exception = Assert.Throws<T>(action);

Assert.Equal(paramName, exception.ParamName);
Assert.Equal(expectedParamName, exception.ParamName);

return exception;
}
Expand All @@ -75,27 +75,27 @@ public static T Throws<T>(Action action)
return exception;
}

public static T Throws<T>(string paramName, Func<object> testCode)
public static T Throws<T>(string expectedParamName, Func<object> testCode)
where T : ArgumentException
{
T exception = Assert.Throws<T>(testCode);

Assert.Equal(paramName, exception.ParamName);
Assert.Equal(expectedParamName, exception.ParamName);

return exception;
}

public static async Task<T> ThrowsAsync<T>(string paramName, Func<Task> testCode)
public static async Task<T> ThrowsAsync<T>(string expectedParamName, Func<Task> testCode)
where T : ArgumentException
{
T exception = await Assert.ThrowsAsync<T>(testCode);

Assert.Equal(paramName, exception.ParamName);
Assert.Equal(expectedParamName, exception.ParamName);

return exception;
}

public static void Throws<TNetCoreExceptionType, TNetFxExceptionType>(string paramName, Action action)
public static void Throws<TNetCoreExceptionType, TNetFxExceptionType>(string expectedParamName, Action action)
where TNetCoreExceptionType : ArgumentException
where TNetFxExceptionType : Exception
{
Expand All @@ -106,7 +106,7 @@ public static void Throws<TNetCoreExceptionType, TNetFxExceptionType>(string par
if (typeof(ArgumentException).IsAssignableFrom(typeof(TNetFxExceptionType)))
{
Exception exception = Assert.Throws(typeof(TNetFxExceptionType), action);
Assert.Equal(paramName, ((ArgumentException)exception).ParamName);
Assert.Equal(expectedParamName, ((ArgumentException)exception).ParamName);
}
else
{
Expand All @@ -115,7 +115,7 @@ public static void Throws<TNetCoreExceptionType, TNetFxExceptionType>(string par
}
else
{
AssertExtensions.Throws<TNetCoreExceptionType>(paramName, action);
AssertExtensions.Throws<TNetCoreExceptionType>(expectedParamName, action);
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/libraries/System.Data.OleDb/tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Globalization;

namespace System.Data.OleDb.Tests
{
Expand All @@ -20,6 +21,7 @@ private class Nested
public static readonly string ProviderName;
public static Nested Instance => s_instance;
private static readonly Nested s_instance = new Nested();
private const string ExpectedProviderName = @"Microsoft.ACE.OLEDB.12.0";
private Nested() { }
static Nested()
{
Expand All @@ -29,13 +31,15 @@ static Nested()
List<object> providerNames = new List<object>();
foreach (DataRow row in table.Rows)
{
providerNames.Add(row[providersRegistered]);
providerNames.Add((string)row[providersRegistered]);
}
string providerName = PlatformDetection.Is32BitProcess ?
@"Microsoft.Jet.OLEDB.4.0" :
@"Microsoft.ACE.OLEDB.12.0";
IsAvailable = false; // ActiveIssue #37823 // providerNames.Contains(providerName);
ProviderName = IsAvailable ? providerName : null;
// skip if x86 or if the expected driver not available
IsAvailable = !PlatformDetection.Is32BitProcess && providerNames.Contains(ExpectedProviderName);
if (!CultureInfo.CurrentCulture.Name.Equals("en-US", StringComparison.OrdinalIgnoreCase))
{
IsAvailable = false; // ActiveIssue: https://github.com/dotnet/corefx/issues/38737
}
ProviderName = IsAvailable ? ExpectedProviderName : null;
}
}
}
Expand Down
46 changes: 38 additions & 8 deletions src/libraries/System.Data.OleDb/tests/OleDbCommandBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;

namespace System.Data.OleDb.Tests
Expand Down Expand Up @@ -90,13 +92,26 @@ public void QuoteUnquoteIdentifier_Null_Throws()
command.CommandText = @"SELECT * FROM " + tableName;
using (var builder = (OleDbCommandBuilder)OleDbFactory.Instance.CreateCommandBuilder())
{
AssertExtensions.Throws<ArgumentNullException>(
() => builder.QuoteIdentifier(null, command.Connection),
$"Value cannot be null.\r\nParameter name: unquotedIdentifier");

AssertExtensions.Throws<ArgumentNullException>(
() => builder.UnquoteIdentifier(null, command.Connection),
$"Value cannot be null.\r\nParameter name: quotedIdentifier");
if (PlatformDetection.IsFullFramework)
{
AssertExtensions.Throws<ArgumentNullException>(
() => builder.QuoteIdentifier(null, command.Connection),
$"Value cannot be null.\r\nParameter name: unquotedIdentifier");

AssertExtensions.Throws<ArgumentNullException>(
() => builder.UnquoteIdentifier(null, command.Connection),
$"Value cannot be null.\r\nParameter name: quotedIdentifier");
}
else
{
AssertExtensions.Throws<ArgumentNullException>(
() => builder.QuoteIdentifier(null, command.Connection),
$"Value cannot be null. (Parameter \'unquotedIdentifier\')");

AssertExtensions.Throws<ArgumentNullException>(
() => builder.UnquoteIdentifier(null, command.Connection),
$"Value cannot be null. (Parameter \'quotedIdentifier\')");
}
}
command.CommandType = CommandType.Text;
});
Expand Down Expand Up @@ -154,7 +169,22 @@ private void RunTest(Action<OleDbCommand, string> testAction, [CallerMemberName]
Firstname NVARCHAR(5),
Lastname NVARCHAR(40),
Nickname NVARCHAR(30))";
command.ExecuteNonQuery();
try
{
command.ExecuteNonQuery();
}
catch (SEHException sehEx)
{
Console.WriteLine($"Code: {sehEx.ErrorCode}");
Exception baseException = sehEx.GetBaseException();
Debug.Assert(baseException != null);
Console.WriteLine($"Base Exception error code : {baseException.HResult}");
Console.WriteLine($"Base Exception message : {baseException}");
Console.WriteLine($"Base Inner Exception: {sehEx.InnerException}");

// This exception is not expected. So rethrow to indicate test failure.
throw;
}
Assert.True(File.Exists(Path.Combine(TestDirectory, tableName)));

command.CommandText =
Expand Down
72 changes: 56 additions & 16 deletions src/libraries/System.Data.OleDb/tests/OleDbCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ public void UpdatedRowSource_SetInvalidValue_Throws()
Assert.Equal(UpdateRowSource.Both, cmd.UpdatedRowSource);
cmd.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
Assert.Equal(UpdateRowSource.FirstReturnedRecord, cmd.UpdatedRowSource);
AssertExtensions.Throws<ArgumentOutOfRangeException>(
() => cmd.UpdatedRowSource = (UpdateRowSource)InvalidValue,
$"The {nameof(UpdateRowSource)} enumeration value, {InvalidValue}, is invalid.\r\nParameter name: {nameof(UpdateRowSource)}"
);
if (PlatformDetection.IsFullFramework)
{
AssertExtensions.Throws<ArgumentOutOfRangeException>(
() => cmd.UpdatedRowSource = (UpdateRowSource)InvalidValue,
$"The {nameof(UpdateRowSource)} enumeration value, {InvalidValue}, is invalid.\r\nParameter name: {nameof(UpdateRowSource)}"
);
}
else
{
AssertExtensions.Throws<ArgumentOutOfRangeException>(
() => cmd.UpdatedRowSource = (UpdateRowSource)InvalidValue,
$"The {nameof(UpdateRowSource)} enumeration value, {InvalidValue}, is invalid. (Parameter \'{nameof(UpdateRowSource)}\')"
);
}
}
}

Expand All @@ -34,10 +44,20 @@ public void CommandTimeout_SetInvalidValue_Throws()
const int InvalidValue = -1;
using (var cmd = new OleDbCommand(default, connection, transaction))
{
AssertExtensions.Throws<ArgumentException>(
() => cmd.CommandTimeout = InvalidValue,
$"Invalid CommandTimeout value {InvalidValue}; the value must be >= 0.\r\nParameter name: {nameof(cmd.CommandTimeout)}"
);
if (PlatformDetection.IsFullFramework)
{
AssertExtensions.Throws<ArgumentException>(
() => cmd.CommandTimeout = InvalidValue,
$"Invalid CommandTimeout value {InvalidValue}; the value must be >= 0.\r\nParameter name: {nameof(cmd.CommandTimeout)}"
);
}
else
{
AssertExtensions.Throws<ArgumentException>(
() => cmd.CommandTimeout = InvalidValue,
$"Invalid CommandTimeout value {InvalidValue}; the value must be >= 0. (Parameter \'{nameof(cmd.CommandTimeout)}\')"
);
}
}
}

Expand All @@ -61,10 +81,20 @@ public void CommandType_SetInvalidValue_Throws()
const int InvalidValue = 0;
using (var cmd = (OleDbCommand)OleDbFactory.Instance.CreateCommand())
{
AssertExtensions.Throws<ArgumentOutOfRangeException>(
() => cmd.CommandType = (CommandType)InvalidValue,
$"The CommandType enumeration value, {InvalidValue}, is invalid.\r\nParameter name: {nameof(cmd.CommandType)}"
);
if (PlatformDetection.IsFullFramework)
{
AssertExtensions.Throws<ArgumentOutOfRangeException>(
() => cmd.CommandType = (CommandType)InvalidValue,
$"The CommandType enumeration value, {InvalidValue}, is invalid.\r\nParameter name: {nameof(cmd.CommandType)}"
);
}
else
{
AssertExtensions.Throws<ArgumentOutOfRangeException>(
() => cmd.CommandType = (CommandType)InvalidValue,
$"The CommandType enumeration value, {InvalidValue}, is invalid. (Parameter \'{nameof(cmd.CommandType)}\')"
);
}
}
}

Expand Down Expand Up @@ -149,10 +179,20 @@ public void Prepare_InsertMultipleItems_UseTableDirectToVerify()
public void Parameters_AddNullParameter_Throws()
{
RunTest((command, tableName) => {
AssertExtensions.Throws<ArgumentNullException>(
() => command.Parameters.Add(null),
$"The {nameof(OleDbParameterCollection)} only accepts non-null {nameof(OleDbParameter)} type objects.\r\nParameter name: value"
);
if (PlatformDetection.IsFullFramework)
{
AssertExtensions.Throws<ArgumentNullException>(
() => command.Parameters.Add(null),
$"The {nameof(OleDbParameterCollection)} only accepts non-null {nameof(OleDbParameter)} type objects.\r\nParameter name: value"
);
}
else
{
AssertExtensions.Throws<ArgumentNullException>(
() => command.Parameters.Add(null),
$"The {nameof(OleDbParameterCollection)} only accepts non-null {nameof(OleDbParameter)} type objects. (Parameter \'value\')"
);
}
command.CommandText = "SELECT * FROM " + tableName + " WHERE NumPlants = ?";
command.Parameters.Add(new OleDbParameter("@p1", 7));
using (OleDbDataReader reader = command.ExecuteReader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void SignerIdentifierType_InvalidValues(SubjectIdentifierType inva
CmsSigner signer = new CmsSigner();

AssertExtensions.Throws<ArgumentException>(
paramName: null,
expectedParamName: null,
() => signer.SignerIdentifierType = invalidType);
}
}
Expand Down

0 comments on commit 57db927

Please sign in to comment.