Skip to content

Commit 0b12045

Browse files
committed
Test for NH-2828
SVN: trunk@6002
1 parent 99406a1 commit 0b12045

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Iesi.Collections.Generic;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH2828
6+
{
7+
public class Company
8+
{
9+
public virtual Guid Id { get; protected set; }
10+
11+
/// <summary>
12+
/// The addresses.
13+
/// </summary>
14+
private readonly Iesi.Collections.Generic.ISet<Address> addresses;
15+
16+
/// <summary>
17+
/// The bank accounts.
18+
/// </summary>
19+
private readonly Iesi.Collections.Generic.ISet<BankAccount> bankAccounts;
20+
21+
/// <summary>
22+
/// Gets or sets Name.
23+
/// </summary>
24+
public virtual string Name { get; set; }
25+
26+
public Company(){
27+
this.addresses = new HashedSet<Address>();
28+
this.bankAccounts = new HashedSet<BankAccount>();
29+
}
30+
31+
/// <summary>
32+
/// Gets or sets Addresses.
33+
/// </summary>
34+
public virtual IEnumerable<Address> Addresses
35+
{
36+
get
37+
{
38+
return this.addresses;
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Gets or sets BankAccounts.
44+
/// </summary>
45+
public virtual IEnumerable<BankAccount> BankAccounts
46+
{
47+
get
48+
{
49+
return this.bankAccounts;
50+
}
51+
}
52+
public virtual bool AddBank(BankAccount bankAccount)
53+
{
54+
if (bankAccount == null)
55+
{
56+
return false;
57+
}
58+
59+
if (this.bankAccounts.Add(bankAccount))
60+
{
61+
bankAccount.Company = this;
62+
return true;
63+
}
64+
return false;
65+
}
66+
67+
public virtual bool AddAddress(Address address)
68+
{
69+
if (address == null)
70+
{
71+
return false;
72+
}
73+
74+
if (this.addresses.Add(address))
75+
{
76+
address.AddCompany(this);
77+
return true;
78+
}
79+
return false;
80+
}
81+
82+
public virtual bool RemoveAddress(Address address)
83+
{
84+
if (address == null)
85+
{
86+
return false;
87+
}
88+
if (this.addresses.Remove(address))
89+
{
90+
address.RemoveCompany();
91+
return true;
92+
}
93+
return false;
94+
}
95+
96+
}
97+
98+
public class Address
99+
{
100+
public virtual Guid Id { get; protected set; }
101+
102+
public virtual string Name { get; set; }
103+
104+
public virtual Company Company { get; set; }
105+
106+
public virtual bool AddCompany(Company company)
107+
{
108+
if (company == null)
109+
{
110+
return false;
111+
}
112+
113+
this.Company = company;
114+
if (company.AddAddress(this)) return true;
115+
return false;
116+
}
117+
118+
public virtual void RemoveCompany()
119+
{
120+
this.Company = null;
121+
}
122+
123+
}
124+
125+
public class BankAccount
126+
{
127+
public virtual Guid Id { get; protected set; }
128+
129+
public virtual string Name { get; set; }
130+
131+
public virtual Company Company { get; set; }
132+
133+
}
134+
135+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using SharpTestsEx;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH2828
7+
{
8+
public class Fixture : BugTestCase
9+
{
10+
[Test, Ignore("Not fixed yet")]
11+
public void WhenPersistShouldNotFetchUninitializedCollection()
12+
{
13+
var companyId = CreateScenario();
14+
15+
//Now in a second transaction i remove the address and persist Company: for a cascade option the Address will be removed
16+
using (var sl = new SqlLogSpy())
17+
{
18+
using (ISession session = sessions.OpenSession())
19+
{
20+
using (ITransaction tx = session.BeginTransaction())
21+
{
22+
var company = session.Get<Company>(companyId);
23+
company.Addresses.Count().Should().Be.EqualTo(1);
24+
company.RemoveAddress(company.Addresses.First()).Should().Be.EqualTo(true);
25+
26+
//now this company will be saved and deleting the address.
27+
//BUT it should not try to load the BanckAccound collection!
28+
session.Persist(company);
29+
tx.Commit();
30+
}
31+
}
32+
var wholeMessage = sl.GetWholeLog();
33+
wholeMessage.Should().Not.Contain("BankAccount");
34+
}
35+
36+
Cleanup(companyId);
37+
}
38+
39+
private void Cleanup(Guid companyId)
40+
{
41+
using (ISession session = sessions.OpenSession())
42+
{
43+
using (ITransaction tx = session.BeginTransaction())
44+
{
45+
session.Delete(session.Get<Company>(companyId));
46+
tx.Commit();
47+
}
48+
}
49+
}
50+
51+
private Guid CreateScenario()
52+
{
53+
var company = new Company() {Name = "Company test"};
54+
var address = new Address() {Name = "Address test"};
55+
var bankAccount = new BankAccount() {Name = "Bank test"};
56+
company.AddAddress(address);
57+
company.AddBank(bankAccount);
58+
using (ISession session = sessions.OpenSession())
59+
{
60+
using (ITransaction tx = session.BeginTransaction())
61+
{
62+
session.Persist(company);
63+
tx.Commit();
64+
}
65+
}
66+
return company.Id;
67+
}
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.NH2828"
4+
assembly="NHibernate.Test">
5+
<class name="Company">
6+
<id name="Id" type="Guid">
7+
<generator class="guid" />
8+
</id>
9+
<set name="Addresses" access="field.camelcase" inverse="true" cascade="all,delete-orphan">
10+
<key column="CompanyId" on-delete="cascade" />
11+
<one-to-many class="Address" />
12+
</set>
13+
<set name="BankAccounts" access="field.camelcase" inverse="true" cascade="all,delete-orphan">
14+
<key column="CompanyId" on-delete="cascade" not-null="true" />
15+
<one-to-many class="BankAccount" />
16+
</set>
17+
<property name="Name" />
18+
</class>
19+
20+
<class name="BankAccount">
21+
<id name="Id" type="Guid">
22+
<generator class="guid" />
23+
</id>
24+
<property name="Name" />
25+
<many-to-one name="Company" column="CompanyId" />
26+
</class>
27+
28+
<class name="Address">
29+
<id name="Id" type="Guid">
30+
<generator class="guid" />
31+
</id>
32+
<property name="Name" />
33+
<many-to-one name="Company" column="CompanyId" />
34+
</class>
35+
36+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,8 @@
898898
<Compile Include="NHSpecificTest\SqlConverterAndMultiQuery\Fixture.cs" />
899899
<Compile Include="NHSpecificTest\SqlConverterAndMultiQuery\Model.cs" />
900900
<Compile Include="NHSpecificTest\SqlConverterAndMultiQuery\SqlConverter.cs" />
901+
<Compile Include="NHSpecificTest\NH2828\Entities.cs" />
902+
<Compile Include="NHSpecificTest\NH2828\Fixture.cs" />
901903
<Compile Include="Parameters\NamedParameterSpecificationTest.cs" />
902904
<Compile Include="PolymorphicGetAndLoad\Domain.cs" />
903905
<Compile Include="PolymorphicGetAndLoad\PolymorphicGetAndLoadTest.cs" />
@@ -2734,6 +2736,7 @@
27342736
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
27352737
</ItemGroup>
27362738
<ItemGroup>
2739+
<EmbeddedResource Include="NHSpecificTest\NH2828\Mappings.hbm.xml" />
27372740
<EmbeddedResource Include="NHSpecificTest\NH2761\A.hbm.xml" />
27382741
<EmbeddedResource Include="NHSpecificTest\NH2761\B.hbm.xml" />
27392742
<EmbeddedResource Include="NHSpecificTest\NH2761\C.hbm.xml" />

0 commit comments

Comments
 (0)