Skip to content

Commit ec7da1c

Browse files
darrenkopphazzik
authored andcommitted
Fix NH-3316
Tests covering NH3316 Fix some other bad unit tests
1 parent 69db287 commit ec7da1c

File tree

5 files changed

+170
-9
lines changed

5 files changed

+170
-9
lines changed

src/NHibernate.Test/MappingByCode/ExpliticMappingTests/BagOfNestedComponentsWithParentTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void WhenMapClasByClassThenAutodiscoverParent()
4949
cm.Bag(x => x.Addresses, cp => { }, cr => { });
5050
});
5151
HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Person) });
52-
VerifyMapping(mapping);
52+
VerifyMapping(mapping, "Street", "Number", "Owner");
5353
}
5454

5555
[Test]
@@ -72,7 +72,7 @@ public void WhenMapClassWithWrongElementsThenAutodiscoverParent()
7272
}));
7373
});
7474
HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Person) });
75-
VerifyMapping(mapping);
75+
VerifyMapping(mapping, "Street", "Number", "Owner");
7676
}
7777

7878
[Test]
@@ -94,10 +94,10 @@ public void WhenMapClassElementsThenMapParent()
9494
}));
9595
});
9696
HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Person) });
97-
VerifyMapping(mapping);
97+
VerifyMapping(mapping, "Street", "Number");
9898
}
9999

100-
private void VerifyMapping(HbmMapping mapping)
100+
private void VerifyMapping(HbmMapping mapping, params string[] properties)
101101
{
102102
HbmClass rc = mapping.RootClasses.First(r => r.Name.Contains("Person"));
103103
var relation = rc.Properties.First(p => p.Name == "Addresses");
@@ -108,8 +108,8 @@ private void VerifyMapping(HbmMapping mapping)
108108

109109
// This test was modified because when the "owner" is an entity it can be mapped as many-to-one or as parent and without an explicit
110110
// definition of the property representing the bidiretional-relation we can't know is the mapping element (many-to-one or parent)
111-
elementRelation.Properties.Should().Have.Count.EqualTo(3);
112-
elementRelation.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Street", "Number", "Owner");
111+
elementRelation.Properties.Should().Have.Count.EqualTo(properties.Length);
112+
elementRelation.Properties.Select(p => p.Name).Should().Have.SameValuesAs(properties);
113113
//elementRelation.Parent.Should().Not.Be.Null();
114114
//elementRelation.Parent.name.Should().Be.EqualTo("Owner");
115115

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NHibernate.Cfg.MappingSchema;
6+
using NHibernate.Mapping.ByCode;
7+
using NUnit.Framework;
8+
9+
namespace NHibernate.Test.NHSpecificTest.NH3316
10+
{
11+
[TestFixture]
12+
public class ByCodeFixture : TestCaseMappingByCode
13+
{
14+
protected override HbmMapping GetMappings()
15+
{
16+
var mapper = new ModelMapper();
17+
mapper.Class<Entity>(e =>
18+
{
19+
e.Id(x => x.Id, id => id.Generator(Generators.GuidComb));
20+
e.Set(x => x.Children, c =>
21+
{
22+
c.Key(key => key.Column("EntityId"));
23+
c.Cascade(NHibernate.Mapping.ByCode.Cascade.All | NHibernate.Mapping.ByCode.Cascade.DeleteOrphans);
24+
},
25+
r =>
26+
{
27+
r.Component(c =>
28+
{
29+
c.Parent(x => x.Parent);
30+
c.Property(x => x.Value);
31+
});
32+
});
33+
});
34+
35+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
36+
}
37+
38+
protected override void OnTearDown()
39+
{
40+
using (ISession session = OpenSession())
41+
using (ITransaction transaction = session.BeginTransaction())
42+
{
43+
session.Delete("from System.Object");
44+
45+
session.Flush();
46+
transaction.Commit();
47+
}
48+
}
49+
50+
[Test]
51+
public void Test_That_Parent_Property_Can_Be_Persisted_And_Retrieved()
52+
{
53+
Guid id = Guid.Empty;
54+
using (ISession session = OpenSession())
55+
using (ITransaction transaction = session.BeginTransaction())
56+
{
57+
Entity e = new Entity();
58+
e.AddChild(1);
59+
e.AddChild(2);
60+
61+
session.Save(e);
62+
session.Flush();
63+
transaction.Commit();
64+
id = e.Id;
65+
}
66+
67+
using (ISession session = OpenSession())
68+
using (ITransaction transaction = session.BeginTransaction())
69+
{
70+
Entity e = session.Get<Entity>(id);
71+
Assert.AreEqual(2, e.Children.Count());
72+
foreach (ChildComponent c in e.Children)
73+
Assert.AreEqual(e, c.Parent);
74+
75+
session.Flush();
76+
transaction.Commit();
77+
}
78+
}
79+
}
80+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH3316
7+
{
8+
class Entity
9+
{
10+
public Entity()
11+
{
12+
Children = new HashSet<ChildComponent>();
13+
}
14+
15+
public virtual Guid Id { get; set; }
16+
17+
public virtual ICollection<ChildComponent> Children { get; protected set; }
18+
19+
public virtual void AddChild(int value)
20+
{
21+
Children.Add(new ChildComponent(this, value));
22+
}
23+
24+
public override bool Equals(object obj)
25+
{
26+
Entity other = obj as Entity;
27+
if (ReferenceEquals(null, other)) return false;
28+
if (ReferenceEquals(this, other)) return true;
29+
30+
return Id == other.Id;
31+
}
32+
33+
public override int GetHashCode()
34+
{
35+
return Id.GetHashCode();
36+
}
37+
}
38+
39+
class ChildComponent
40+
{
41+
protected ChildComponent()
42+
{
43+
}
44+
45+
public ChildComponent(Entity parent, int value)
46+
{
47+
Parent = parent;
48+
Value = value;
49+
}
50+
51+
public virtual Entity Parent { get; protected set; }
52+
53+
public virtual int Value { get; protected set; }
54+
55+
public override bool Equals(object obj)
56+
{
57+
ChildComponent other = obj as ChildComponent;
58+
if (ReferenceEquals(null, other)) return false;
59+
if (ReferenceEquals(this, other)) return true;
60+
61+
return Parent == other.Parent && Value == other.Value;
62+
}
63+
64+
public override int GetHashCode()
65+
{
66+
int hash = 17;
67+
unchecked
68+
{
69+
hash = (hash * 23) + Parent.GetHashCode();
70+
hash = (hash * 23) + Value.GetHashCode();
71+
}
72+
73+
return hash;
74+
}
75+
}
76+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@
683683
<Compile Include="MappingByCode\IntegrationTests\NH3140\Fixture.cs" />
684684
<Compile Include="NHSpecificTest\NH3428\Entity.cs" />
685685
<Compile Include="NHSpecificTest\NH3428\Fixture.cs" />
686+
<Compile Include="NHSpecificTest\NH3316\ByCodeFixture.cs" />
687+
<Compile Include="NHSpecificTest\NH3316\Entity.cs" />
686688
<Compile Include="NHSpecificTest\NH3408\Fixture.cs" />
687689
<Compile Include="NHSpecificTest\NH3408\Model.cs" />
688690
<Compile Include="NHSpecificTest\NH2297\CustomCompositeUserType.cs" />

src/NHibernate/Mapping/ByCode/ModelMapper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,9 +1130,12 @@ private void MapComponent(MemberInfo member, PropertyPath memberPath, System.Typ
11301130

11311131
protected MemberInfo GetComponentParentReferenceProperty(IEnumerable<MemberInfo> persistentProperties, System.Type propertiesContainerType)
11321132
{
1133-
return modelInspector.IsComponent(propertiesContainerType)
1134-
? persistentProperties.FirstOrDefault(pp => pp.GetPropertyOrFieldType() == propertiesContainerType)
1135-
: null;
1133+
// if container is component, then all properties referencing container are assumed parent reference
1134+
if (modelInspector.IsComponent(propertiesContainerType))
1135+
return persistentProperties.FirstOrDefault(pp => pp.GetPropertyOrFieldType() == propertiesContainerType);
1136+
1137+
// return the first non-many-to-one property
1138+
return persistentProperties.Where(pp => !modelInspector.IsManyToOne(pp)).FirstOrDefault(pp => pp.GetPropertyOrFieldType() == propertiesContainerType);
11361139
}
11371140

11381141
private void MapBag(MemberInfo member, PropertyPath propertyPath, System.Type propertyType, ICollectionPropertiesContainerMapper propertiesContainer,

0 commit comments

Comments
 (0)