Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0"></remove>
<add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</DbProviderFactories>
</system.data>
</configuration>

View File

@@ -0,0 +1,92 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Moq;
using WebMatrix.Data.Test.Mocks;
using Xunit;
namespace WebMatrix.Data.Test
{
public class ConfigurationManagerWrapperTest
{
[Fact]
public void GetConnectionGetsConnectionFromConfig()
{
// Arrange
var configManager = new ConfigurationManagerWrapper(new Dictionary<string, IDbFileHandler>(), "DataDirectory");
Func<string, bool> fileExists = path => false;
Func<string, IConnectionConfiguration> getFromConfig = name => new MockConnectionConfiguration("connection string");
// Act
IConnectionConfiguration configuration = configManager.GetConnection("foo", getFromConfig, fileExists);
// Assert
Assert.NotNull(configuration);
Assert.Equal("connection string", configuration.ConnectionString);
}
[Fact]
public void GetConnectionGetsConnectionFromDataDirectoryIfFileWithSupportedExtensionExists()
{
// Arrange
var mockHandler = new Mock<MockDbFileHandler>();
mockHandler.Setup(m => m.GetConnectionConfiguration(@"DataDirectory\Bar.foo")).Returns(new MockConnectionConfiguration("some file based connection"));
var handlers = new Dictionary<string, IDbFileHandler>
{
{ ".foo", mockHandler.Object }
};
var configManager = new ConfigurationManagerWrapper(handlers, "DataDirectory");
Func<string, bool> fileExists = path => path.Equals(@"DataDirectory\Bar.foo");
Func<string, IConnectionConfiguration> getFromConfig = name => null;
// Act
IConnectionConfiguration configuration = configManager.GetConnection("Bar", getFromConfig, fileExists);
// Assert
Assert.NotNull(configuration);
Assert.Equal("some file based connection", configuration.ConnectionString);
}
[Fact]
public void GetConnectionSdfAndMdfFile_MdfFileWins()
{
// Arrange
var mockSdfHandler = new Mock<MockDbFileHandler>();
mockSdfHandler.Setup(m => m.GetConnectionConfiguration(@"DataDirectory\Bar.sdf")).Returns(new MockConnectionConfiguration("sdf connection"));
var mockMdfHandler = new Mock<MockDbFileHandler>();
mockMdfHandler.Setup(m => m.GetConnectionConfiguration(@"DataDirectory\Bar.mdf")).Returns(new MockConnectionConfiguration("mdf connection"));
var handlers = new Dictionary<string, IDbFileHandler>
{
{ ".sdf", mockSdfHandler.Object },
{ ".mdf", mockMdfHandler.Object },
};
var configManager = new ConfigurationManagerWrapper(handlers, "DataDirectory");
Func<string, bool> fileExists = path => path.Equals(@"DataDirectory\Bar.mdf") ||
path.Equals(@"DataDirectory\Bar.sdf");
Func<string, IConnectionConfiguration> getFromConfig = name => null;
// Act
IConnectionConfiguration configuration = configManager.GetConnection("Bar", getFromConfig, fileExists);
// Assert
Assert.NotNull(configuration);
Assert.Equal("mdf connection", configuration.ConnectionString);
}
[Fact]
public void GetConnectionReturnsNullIfNoConnectionFound()
{
// Act
var configManager = new ConfigurationManagerWrapper(new Dictionary<string, IDbFileHandler>(), "DataDirectory");
Func<string, bool> fileExists = path => false;
Func<string, IConnectionConfiguration> getFromConfig = name => null;
// Act
IConnectionConfiguration configuration = configManager.GetConnection("test", getFromConfig, fileExists);
// Assert
Assert.Null(configuration);
}
}
}

View File

@@ -0,0 +1,90 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Data.Common;
using Moq;
using WebMatrix.Data.Test.Mocks;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace WebMatrix.Data.Test
{
public class DatabaseTest
{
[Fact]
public void OpenWithNullConnectionStringNameThrowsException()
{
Assert.ThrowsArgumentNullOrEmptyString(() => Database.Open(null), "name");
}
[Fact]
public void OpenConnectionStringWithNullConnectionStringThrowsException()
{
Assert.ThrowsArgumentNullOrEmptyString(() => Database.OpenConnectionString(null), "connectionString");
}
[Fact]
public void OpenConnectionStringWithEmptyConnectionStringThrowsException()
{
Assert.ThrowsArgumentNullOrEmptyString(() => Database.OpenConnectionString(String.Empty), "connectionString");
}
[Fact]
public void OpenNamedConnectionUsesConnectionStringFromConfigurationIfExists()
{
// Arrange
MockConfigurationManager mockConfigurationManager = new MockConfigurationManager();
Mock<DbConnection> mockConnection = new Mock<DbConnection>();
mockConnection.Setup(m => m.ConnectionString).Returns("connection string");
Mock<MockDbProviderFactory> mockProviderFactory = new Mock<MockDbProviderFactory>();
mockProviderFactory.Setup(m => m.CreateConnection("connection string")).Returns(mockConnection.Object);
mockConfigurationManager.AddConnection("foo", new ConnectionConfiguration(mockProviderFactory.Object, "connection string"));
// Act
Database db = Database.OpenNamedConnection("foo", mockConfigurationManager);
// Assert
Assert.Equal("connection string", db.Connection.ConnectionString);
}
[Fact]
public void OpenNamedConnectionThrowsIfNoConnectionFound()
{
// Arrange
IConfigurationManager mockConfigurationManager = new MockConfigurationManager();
// Act & Assert
Assert.Throws<InvalidOperationException>(() => Database.OpenNamedConnection("foo", mockConfigurationManager), "Connection string \"foo\" was not found.");
}
[Fact]
public void GetConnectionConfigurationGetConnectionForFileHandlersIfRegistered()
{
// Arrange
var mockHandler = new Mock<MockDbFileHandler>();
mockHandler.Setup(m => m.GetConnectionConfiguration("filename.foo")).Returns(new MockConnectionConfiguration("some file based connection"));
var handlers = new Dictionary<string, IDbFileHandler>
{
{ ".foo", mockHandler.Object }
};
// Act
IConnectionConfiguration configuration = Database.GetConnectionConfiguration("filename.foo", handlers);
// Assert
Assert.NotNull(configuration);
Assert.Equal("some file based connection", configuration.ConnectionString);
}
[Fact]
public void GetConnectionThrowsIfNoHandlersRegisteredForExtension()
{
// Arrange
var handlers = new Dictionary<string, IDbFileHandler>();
// Act
Assert.Throws<InvalidOperationException>(() => Database.GetConnectionConfiguration("filename.foo", handlers), "Unable to determine the provider for the database file \"filename.foo\".");
}
}
}

View File

@@ -0,0 +1,152 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace WebMatrix.Data.Test
{
public class DynamicRecordTest
{
[Fact]
public void GetFieldValueByNameAccessesUnderlyingRecordForValue()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
mockRecord.SetupGet(m => m["A"]).Returns(1);
mockRecord.SetupGet(m => m["B"]).Returns(2);
dynamic record = new DynamicRecord(new[] { "A", "B" }, mockRecord.Object);
// Assert
Assert.Equal(1, record.A);
Assert.Equal(2, record.B);
}
[Fact]
public void GetFieldValueByIndexAccessesUnderlyingRecordForValue()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
mockRecord.SetupGet(m => m[0]).Returns(1);
mockRecord.SetupGet(m => m[1]).Returns(2);
dynamic record = new DynamicRecord(new[] { "A", "B" }, mockRecord.Object);
// Assert
Assert.Equal(1, record[0]);
Assert.Equal(2, record[1]);
}
[Fact]
public void GetFieldValueByNameReturnsNullIfValueIsDbNull()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
mockRecord.SetupGet(m => m["A"]).Returns(DBNull.Value);
dynamic record = new DynamicRecord(new[] { "A" }, mockRecord.Object);
// Assert
Assert.Null(record.A);
}
[Fact]
public void GetFieldValueByIndexReturnsNullIfValueIsDbNull()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
mockRecord.SetupGet(m => m[0]).Returns(DBNull.Value);
dynamic record = new DynamicRecord(new[] { "A" }, mockRecord.Object);
// Assert
Assert.Null(record[0]);
}
[Fact]
public void GetInvalidFieldValueThrows()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
dynamic record = new DynamicRecord(Enumerable.Empty<string>(), mockRecord.Object);
// Assert
Assert.Throws<InvalidOperationException>(() => { var value = record.C; }, "Invalid column name \"C\".");
}
[Fact]
public void VerfiyCustomTypeDescriptorMethods()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
mockRecord.SetupGet(m => m["A"]).Returns(1);
mockRecord.SetupGet(m => m["B"]).Returns(2);
// Act
ICustomTypeDescriptor record = new DynamicRecord(new[] { "A", "B" }, mockRecord.Object);
// Assert
Assert.Equal(AttributeCollection.Empty, record.GetAttributes());
Assert.Null(record.GetClassName());
Assert.Null(record.GetConverter());
Assert.Null(record.GetDefaultEvent());
Assert.Null(record.GetComponentName());
Assert.Null(record.GetDefaultProperty());
Assert.Null(record.GetEditor(null));
Assert.Equal(EventDescriptorCollection.Empty, record.GetEvents());
Assert.Equal(EventDescriptorCollection.Empty, record.GetEvents(null));
Assert.Same(record, record.GetPropertyOwner(null));
Assert.Equal(2, record.GetProperties().Count);
Assert.Equal(2, record.GetProperties(null).Count);
Assert.NotNull(record.GetProperties()["A"]);
Assert.NotNull(record.GetProperties()["B"]);
}
[Fact]
public void VerifyPropertyDescriptorProperties()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
mockRecord.SetupGet(m => m["A"]).Returns(1);
mockRecord.Setup(m => m.GetOrdinal("A")).Returns(0);
mockRecord.Setup(m => m.GetFieldType(0)).Returns(typeof(string));
// Act
ICustomTypeDescriptor record = new DynamicRecord(new[] { "A" }, mockRecord.Object);
// Assert
var aDescriptor = record.GetProperties().Find("A", ignoreCase: false);
Assert.NotNull(aDescriptor);
Assert.Null(aDescriptor.GetValue(null));
Assert.Equal(1, aDescriptor.GetValue(record));
Assert.True(aDescriptor.IsReadOnly);
Assert.Equal(typeof(string), aDescriptor.PropertyType);
Assert.Equal(typeof(DynamicRecord), aDescriptor.ComponentType);
Assert.False(aDescriptor.ShouldSerializeValue(record));
Assert.False(aDescriptor.CanResetValue(record));
}
[Fact]
public void SetAndResetValueOnPropertyDescriptorThrows()
{
// Arrange
var mockRecord = new Mock<IDataRecord>();
mockRecord.SetupGet(m => m["A"]).Returns(1);
// Act
ICustomTypeDescriptor record = new DynamicRecord(new[] { "A" }, mockRecord.Object);
// Assert
var aDescriptor = record.GetProperties().Find("A", ignoreCase: false);
Assert.NotNull(aDescriptor);
Assert.Throws<InvalidOperationException>(() => aDescriptor.SetValue(record, 1), "Unable to modify the value of column \"A\" because the record is read only.");
Assert.Throws<InvalidOperationException>(() => aDescriptor.ResetValue(record), "Unable to modify the value of column \"A\" because the record is read only.");
}
}
}

View File

@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using Xunit;
namespace WebMatrix.Data.Test
{
public class FileHandlerTest
{
[Fact]
public void SqlCeFileHandlerReturnsDataDirectoryRelativeConnectionStringIfPathIsNotRooted()
{
// Act
string connectionString = SqlCeDbFileHandler.GetConnectionString("foo.sdf");
// Assert
Assert.NotNull(connectionString);
Assert.Equal(@"Data Source=|DataDirectory|\foo.sdf;File Access Retry Timeout=10", connectionString);
}
[Fact]
public void SqlCeFileHandlerReturnsFullPathConnectionStringIfPathIsNotRooted()
{
// Act
string connectionString = SqlCeDbFileHandler.GetConnectionString(@"c:\foo.sdf");
// Assert
Assert.NotNull(connectionString);
Assert.Equal(@"Data Source=c:\foo.sdf;File Access Retry Timeout=10", connectionString);
}
[Fact]
public void SqlServerFileHandlerReturnsDataDirectoryRelativeConnectionStringIfPathIsNotRooted()
{
// Act
string connectionString = SqlServerDbFileHandler.GetConnectionString("foo.mdf", "datadir");
// Assert
Assert.NotNull(connectionString);
Assert.Equal(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\foo.mdf;Initial Catalog=datadir\foo.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True",
connectionString);
}
[Fact]
public void SqlServerFileHandlerReturnsFullPathConnectionStringIfPathIsNotRooted()
{
// Act
string connectionString = SqlServerDbFileHandler.GetConnectionString(@"c:\foo.mdf", "datadir");
// Assert
Assert.NotNull(connectionString);
Assert.Equal(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\foo.mdf;Initial Catalog=c:\foo.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True", connectionString);
}
}
}

View File

@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace WebMatrix.Data.Test.Mocks
{
internal class MockConfigurationManager : IConfigurationManager
{
private Dictionary<string, IConnectionConfiguration> _connectionStrings = new Dictionary<string, IConnectionConfiguration>();
public MockConfigurationManager()
{
AppSettings = new Dictionary<string, string>();
}
public IDictionary<string, string> AppSettings { get; private set; }
public void AddConnection(string name, IConnectionConfiguration configuration)
{
_connectionStrings.Add(name, configuration);
}
public IConnectionConfiguration GetConnection(string name)
{
IConnectionConfiguration configuration;
_connectionStrings.TryGetValue(name, out configuration);
return configuration;
}
}
}

View File

@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
namespace WebMatrix.Data.Test.Mocks
{
public class MockConnectionConfiguration : IConnectionConfiguration
{
public MockConnectionConfiguration(string connectionString)
{
ConnectionString = connectionString;
}
public string ConnectionString { get; private set; }
string IConnectionConfiguration.ConnectionString
{
get { return ConnectionString; }
}
IDbProviderFactory IConnectionConfiguration.ProviderFactory
{
get { return null; }
}
}
}

View File

@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
namespace WebMatrix.Data.Test.Mocks
{
public abstract class MockDbFileHandler : IDbFileHandler
{
IConnectionConfiguration IDbFileHandler.GetConnectionConfiguration(string fileName)
{
return GetConnectionConfiguration(fileName);
}
public abstract MockConnectionConfiguration GetConnectionConfiguration(string fileName);
}
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Data.Common;
namespace WebMatrix.Data.Test.Mocks
{
// Needs to be public for Moq to work
public abstract class MockDbProviderFactory : IDbProviderFactory
{
public abstract DbConnection CreateConnection(string connectionString);
}
}

View File

@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Reflection;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WebMatrix.Data.Test")]
[assembly: AssemblyDescription("")]

View File

@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory),Runtime.sln))\tools\WebStack.settings.targets" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{E2D008A9-4D1D-4F6B-8325-4ED717D6EA0A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WebMatrix.Data.Test</RootNamespace>
<AssemblyName>WebMatrix.Data.Test</AssemblyName>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>$(WebStackRootPath)\bin\Debug\Test\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>$(WebStackRootPath)\bin\Release\Test\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'CodeCoverage' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>$(WebStackRootPath)\bin\CodeCoverage\Test\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="xunit">
<HintPath>..\..\packages\xunit.1.9.0.1566\lib\xunit.dll</HintPath>
</Reference>
<Reference Include="xunit.extensions">
<HintPath>..\..\packages\xunit.extensions.1.9.0.1566\lib\xunit.extensions.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
<Visible>False</Visible>
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WebMatrix.Data\WebMatrix.Data.csproj">
<Project>{4D39BAAF-8A96-473E-AB79-C8A341885137}</Project>
<Name>WebMatrix.Data</Name>
</ProjectReference>
<ProjectReference Include="..\Microsoft.TestCommon\Microsoft.TestCommon.csproj">
<Project>{FCCC4CB7-BAF7-4A57-9F89-E5766FE536C0}</Project>
<Name>Microsoft.TestCommon</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="ConfigurationManagerWrapperTest.cs" />
<Compile Include="DynamicRecordTest.cs" />
<Compile Include="FileHandlerTest.cs" />
<Compile Include="Mocks\MockConfigurationManager.cs" />
<Compile Include="Mocks\MockConnectionConfiguration.cs" />
<Compile Include="Mocks\MockDbFileHandler.cs" />
<Compile Include="Mocks\MockDbProviderFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DatabaseTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Moq" version="4.0.10827" />
<package id="xunit" version="1.9.0.1566" />
<package id="xunit.extensions" version="1.9.0.1566" />
</packages>