Skip to content

Commit

Permalink
Added Tests for mappings from resources
Browse files Browse the repository at this point in the history
Added Tests for Load/Save entities with NH
  • Loading branch information
TheEmidee committed Jun 20, 2011
1 parent 9542721 commit 6365b9b
Show file tree
Hide file tree
Showing 24 changed files with 697 additions and 7 deletions.
27 changes: 27 additions & 0 deletions NHStaticProxy.Resources.Tests/Entities/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Iesi.Collections.Generic;

namespace NHStaticProxy.Resources.Tests.Entities
{
[StaticProxy]
public class Customer
{
public Customer()
{
Orders = new HashedSet<Order>();
}

public int Id { get; set; }

public string Name { get; set; }

public string NotMapped { get; set; }

public ISet<Order> Orders { get; set; }

public void AddOrder(Order order)
{
Orders.Add(order);
order.Customer = this;
}
}
}
12 changes: 12 additions & 0 deletions NHStaticProxy.Resources.Tests/Entities/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NHStaticProxy.Resources.Tests.Entities
{
[StaticProxy]
public class Order
{
public string Name { get; set; }

public Customer Customer { get; set; }

public int Id { get; set; }
}
}
22 changes: 22 additions & 0 deletions NHStaticProxy.Resources.Tests/MappingDocumentParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.IO;
using Microsoft.Xml.Serialization.GeneratedAssembly;
using NHibernate.Cfg.MappingSchema;

namespace NHStaticProxy.Resources.Tests
{
public class MappingDocumentParser
{
private readonly HbmMappingSerializer serializer = new HbmMappingSerializer();
private readonly HbmMapping mapping;

public MappingDocumentParser(Stream stream)
{
mapping = (HbmMapping)serializer.Deserialize(stream);
}

public HbmMapping Mapping
{
get { return mapping; }
}
}
}
14 changes: 14 additions & 0 deletions NHStaticProxy.Resources.Tests/Mappings/Customer.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Application" assembly="Application">
<class name="Customer">
<id name="Id" column="CustomerId">
<generator class="native"/>
</id>
<property name="Name"/>
<set name="Orders" inverse="true">
<key column="CustomerId" />
<one-to-many class="Order" />
</set>

</class>
</hibernate-mapping>
10 changes: 10 additions & 0 deletions NHStaticProxy.Resources.Tests/Mappings/Order.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Application" assembly="Application">
<class name="Order" table="Orders">
<id name="Id" column="OrderId">
<generator class="native"/>
</id>
<property name="Name"/>
<many-to-one name="Customer" column="CustomerId" not-null="true" />
</class>
</hibernate-mapping>
91 changes: 91 additions & 0 deletions NHStaticProxy.Resources.Tests/NHStaticProxy.Resources.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{19D30CF6-5BB5-4E60-A18E-E3511775F2B8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NHStaticProxy.Resources.Tests</RootNamespace>
<AssemblyName>NHStaticProxy.Resources.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<DontImportPostSharp>True</DontImportPostSharp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Iesi.Collections">
<HintPath>..\packages\Iesi.Collections.3.2.0.2001\lib\Net35\Iesi.Collections.dll</HintPath>
</Reference>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="NHibernate">
<HintPath>..\packages\NHibernate.3.2.0.2001\lib\Net35\NHibernate.dll</HintPath>
</Reference>
<Reference Include="NHibernate.XmlSerializers">
<HintPath>lib\NHibernate.XmlSerializers.dll</HintPath>
</Reference>
<Reference Include="PostSharp">
<HintPath>..\packages\PostSharp.2.1.1.5\lib\net20\PostSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>..\packages\xunit.1.8.0.1545\lib\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MappingDocumentParser.cs" />
<Compile Include="Entities\Customer.cs" />
<Compile Include="Entities\Order.cs" />
<Compile Include="ResourcesStaticProxyConfigurationAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WeaverTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NHStaticProxy\NHStaticProxy.csproj">
<Project>{51623325-C1BC-4EF4-A2B0-9A10F8258F0E}</Project>
<Name>NHStaticProxy</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Mappings\Order.hbm.xml" />
<EmbeddedResource Include="Mappings\Customer.hbm.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\PostSharp.2.1.1.5\tools\PostSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
39 changes: 39 additions & 0 deletions NHStaticProxy.Resources.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NHStaticProxy.Resources.Tests;

// 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("NHStaticProxy.Resources.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NHStaticProxy.Resources.Tests")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c3c99b81-f723-4724-b961-53bd62ccf8f8")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: ResourcesStaticProxyConfiguration]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHStaticProxy.Resources.Tests.Entities;

namespace NHStaticProxy.Resources.Tests
{
public class ResourcesStaticProxyConfigurationAttribute : StaticProxyConfigurationAttribute
{
public override IEnumerable<HbmMapping> HbmMappings
{
get
{
var assembly = typeof(Customer).Assembly;

return from ressource in assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hbm.xml"))
let stream = assembly.GetManifestResourceStream(ressource)
let parser = new MappingDocumentParser(stream)
select parser.Mapping;
}
}
}
}
88 changes: 88 additions & 0 deletions NHStaticProxy.Resources.Tests/WeaverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// ReSharper disable InconsistentNaming

using Moq;
using NHStaticProxy.Resources.Tests.Entities;
using PostSharp;
using PostSharp.Aspects;
using Xunit;

namespace NHStaticProxy.Resources.Tests
{
public class WeaverTests
{
[Fact]
public void Entities_Implement_IPostSharpNHibernateProxy()
{
Assert.Contains(typeof(IPostSharpNHibernateProxy), typeof(Customer).GetInterfaces());
Assert.Contains(typeof(IPostSharpNHibernateProxy), typeof(Order).GetInterfaces());
}

[Fact]
public void MappedPropertiesSetters_AreIntercepted_WhenEntityIsProxy()
{
var cust = new Customer();

var stub = new Mock<IStaticProxyLazyInitializer>();

var nhProxy = Post.Cast<Customer, IPostSharpNHibernateProxy>(cust);

nhProxy.SetInterceptor(stub.Object);

cust.Name = "Zoubi";

stub.Verify(x => x.InterceptSet(It.IsAny<LocationInterceptionArgs>()), Times.Once());
}

[Fact]
public void MappedPropertiesGetters_AreIntercepted_WhenEntityIsProxy()
{
var cust = new Customer();
cust.Name = "Zoubi";

var stub = new Mock<IStaticProxyLazyInitializer>();

var nhProxy = Post.Cast<Customer, IPostSharpNHibernateProxy>(cust);

nhProxy.SetInterceptor(stub.Object);

var str = cust.Name;

stub.Verify(x => x.InterceptGet(It.IsAny<LocationInterceptionArgs>()), Times.Once());
}

[Fact]
public void NotMappedPropertiesSetters_AreNotIntercepted_WhenEntityIsProxy()
{
var cust = new Customer();

var stub = new Mock<IStaticProxyLazyInitializer>();

var nhProxy = Post.Cast<Customer, IPostSharpNHibernateProxy>(cust);

nhProxy.SetInterceptor(stub.Object);

cust.NotMapped = "Zoubi";

stub.Verify(x => x.InterceptSet(It.IsAny<LocationInterceptionArgs>()), Times.Never());
}

[Fact]
public void NotMappedPropertiesGetters_AreNotIntercepted_WhenEntityIsProxy()
{
var cust = new Customer();
cust.NotMapped = "Zoubi";

var stub = new Mock<IStaticProxyLazyInitializer>();

var nhProxy = Post.Cast<Customer, IPostSharpNHibernateProxy>(cust);

nhProxy.SetInterceptor(stub.Object);

var str = cust.NotMapped;

stub.Verify(x => x.InterceptGet(It.IsAny<LocationInterceptionArgs>()), Times.Never());
}
}
}

// ReSharper restore InconsistentNaming
Binary file not shown.
8 changes: 8 additions & 0 deletions NHStaticProxy.Resources.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="1.8.0.1545" />
<package id="PostSharp" version="2.1.1.5" />
<package id="Iesi.Collections" version="3.2.0.2001" />
<package id="NHibernate" version="3.2.0.2001" />
<package id="Moq" version="4.0.10827" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NHibernate.Cfg;
using NHStaticProxy.Tests.Config;
using Xunit;

namespace NHStaticProxy.Tests
{
public class CanCreateNHibernateSessionFactoryWithLazyLoadingAndNoVirtuals : NHTestsBase<HbmMappingProvider>
{
[Fact]
public void SessionFactoryCreatedSuccessfully()
{
Assert.NotNull(Session);
}

[Fact]
public void ProxyFactoryFactoryIsSetProperly()
{
Assert.IsType<ProxyFactoryFactory>(Environment.BytecodeProvider.ProxyFactoryFactory);
}

[Fact]
public void ProxyValidatorIsSetProperly()
{
Assert.IsType<ProxyValidator>(Environment.BytecodeProvider.ProxyFactoryFactory.ProxyValidator);
}
}
}
Loading

0 comments on commit 6365b9b

Please sign in to comment.