Skip to content

Commit a875d1b

Browse files
committed
End porting of new events for collections (need some "more" investigation of tests for ManyToMany)
SVN: trunk@3721
1 parent e2c72cc commit a875d1b

File tree

39 files changed

+2262
-8
lines changed

39 files changed

+2262
-8
lines changed

src/NHibernate.Test/Events/Collections/AbstractCollectionEventFixture.cs

+907
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.Events.Collections
4+
{
5+
public abstract class AbstractParentWithCollection : IParentWithCollection
6+
{
7+
private ICollection<IChild> children;
8+
private long id;
9+
private string name;
10+
protected AbstractParentWithCollection() {}
11+
12+
protected AbstractParentWithCollection(string name)
13+
{
14+
this.name = name;
15+
}
16+
17+
#region IParentWithCollection Members
18+
19+
public virtual long Id
20+
{
21+
get { return id; }
22+
set { id = value; }
23+
}
24+
25+
public virtual void NewChildren(ICollection<IChild> collection)
26+
{
27+
Children = collection;
28+
}
29+
30+
public abstract IChild CreateChild(string name);
31+
32+
public virtual string Name
33+
{
34+
get { return name; }
35+
set { name = value; }
36+
}
37+
38+
public virtual ICollection<IChild> Children
39+
{
40+
get { return children; }
41+
set { children = value; }
42+
}
43+
44+
public virtual IChild AddChild(string childName)
45+
{
46+
IChild c = CreateChild(childName);
47+
AddChild(c);
48+
return c;
49+
}
50+
51+
public virtual void AddChild(IChild child)
52+
{
53+
if (child != null)
54+
{
55+
children.Add(child);
56+
}
57+
}
58+
59+
public virtual void AddAllChildren(ICollection<IChild> children)
60+
{
61+
foreach (IChild child in children)
62+
{
63+
this.children.Add(child);
64+
}
65+
}
66+
67+
public virtual void RemoveChild(IChild child)
68+
{
69+
children.Remove(child);
70+
}
71+
72+
public virtual void RemoveAllChildren(ICollection<IChild> children)
73+
{
74+
foreach (IChild child in children)
75+
{
76+
this.children.Remove(child);
77+
}
78+
}
79+
80+
public virtual void ClearChildren()
81+
{
82+
if (children != null)
83+
{
84+
children.Clear();
85+
}
86+
}
87+
88+
#endregion
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using NHibernate.Test.Events.Collections.Association.Bidirectional.ManyToMany;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.Events.Collections.Association
5+
{
6+
public abstract class AbstractAssociationCollectionEventFixture : AbstractCollectionEventFixture
7+
{
8+
[Test]
9+
public void DeleteParentButNotChild()
10+
{
11+
CollectionListeners listeners = new CollectionListeners(sessions);
12+
IParentWithCollection parent = CreateParentWithOneChild("parent", "child");
13+
ChildEntity child = (ChildEntity) GetFirstChild(parent.Children);
14+
listeners.Clear();
15+
ISession s = OpenSession();
16+
ITransaction tx = s.BeginTransaction();
17+
parent = (IParentWithCollection) s.Get(parent.GetType(), parent.Id);
18+
child = (ChildEntity) s.Get(child.GetType(), child.Id);
19+
parent.RemoveChild(child);
20+
s.Delete(parent);
21+
tx.Commit();
22+
s.Close();
23+
int index = 0;
24+
CheckResult(listeners, listeners.InitializeCollection, parent, index++);
25+
if (child is ChildWithBidirectionalManyToMany)
26+
{
27+
CheckResult(listeners, listeners.InitializeCollection, (ChildWithBidirectionalManyToMany) child, index++);
28+
}
29+
CheckResult(listeners, listeners.PreCollectionRemove, parent, index++);
30+
CheckResult(listeners, listeners.PostCollectionRemove, parent, index++);
31+
if (child is ChildWithBidirectionalManyToMany)
32+
{
33+
CheckResult(listeners, listeners.PreCollectionUpdate, (ChildWithBidirectionalManyToMany) child, index++);
34+
CheckResult(listeners, listeners.PostCollectionUpdate, (ChildWithBidirectionalManyToMany) child, index++);
35+
}
36+
CheckNumberOfResults(listeners, index);
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.Events.Collections.Association.Bidirectional.ManyToMany
6+
{
7+
[TestFixture, Ignore("Need some more check for timeouts.")]
8+
public class BidirectionalManyToManyBagToSetCollectionEventFixture : AbstractAssociationCollectionEventFixture
9+
{
10+
protected override IList Mappings
11+
{
12+
get { return new string[] { "Events.Collections.Association.Bidirectional.ManyToMany.BidirectionalManyToManyBagToSetMapping.hbm.xml" }; }
13+
}
14+
15+
public override IParentWithCollection CreateParent(string name)
16+
{
17+
return new ParentWithBidirectionalManyToMany(name);
18+
}
19+
20+
public override ICollection<IChild> CreateCollection()
21+
{
22+
return new List<IChild>();
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.Events.Collections.Association.Bidirectional.ManyToMany">
5+
6+
<class name="ParentWithBidirectionalManyToMany" table="PARENT">
7+
<id name="Id" column="ID" type="long">
8+
<generator class="native"/>
9+
</id>
10+
<bag name="Children" table="PARENT_CHILD" inverse="false" cascade="all">
11+
<key column="parent_id"/>
12+
<many-to-many column="child_id" class="ChildWithBidirectionalManyToMany"/>
13+
</bag>
14+
</class>
15+
16+
<class name="ChildWithBidirectionalManyToMany" table="CHILD">
17+
<id name="Id" column="ID" type="long">
18+
<generator class="native"/>
19+
</id>
20+
<property name="Name" column="NAME" type="string"/>
21+
<set name="Parents" table="PARENT_CHILD" inverse="true">
22+
<key column="child_id"/>
23+
<many-to-many column="parent_id" class="ParentWithBidirectionalManyToMany"/>
24+
</set>
25+
</class>
26+
27+
</hibernate-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Iesi.Collections.Generic;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.Events.Collections.Association.Bidirectional.ManyToMany
7+
{
8+
[TestFixture]
9+
public class BidirectionalManyToManySetToSetCollectionEventFixture : AbstractAssociationCollectionEventFixture
10+
{
11+
protected override IList Mappings
12+
{
13+
get { return new string[] { "Events.Collections.Association.Bidirectional.ManyToMany.BidirectionalManyToManySetToSetMapping.hbm.xml" }; }
14+
}
15+
16+
public override IParentWithCollection CreateParent(string name)
17+
{
18+
return new ParentWithBidirectionalManyToMany(name);
19+
}
20+
21+
public override ICollection<IChild> CreateCollection()
22+
{
23+
return new HashedSet<IChild>();
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.Events.Collections.Association.Bidirectional.ManyToMany">
5+
6+
<class name="ParentWithBidirectionalManyToMany" table="PARENT">
7+
<id name="Id" column="ID" type="long">
8+
<generator class="native"/>
9+
</id>
10+
<set name="Children" table="PARENT_CHILD" inverse="false" cascade="all">
11+
<key column="parent_id"/>
12+
<many-to-many column="child_id" class="ChildWithBidirectionalManyToMany"/>
13+
</set>
14+
</class>
15+
16+
<class name="ChildWithBidirectionalManyToMany" table="CHILD">
17+
<id name="Id" column="ID" type="long">
18+
<generator class="native"/>
19+
</id>
20+
<property name="Name" column="NAME" type="string"/>
21+
<set name="Parents" table="PARENT_CHILD" inverse="true">
22+
<key column="child_id"/>
23+
<many-to-many column="parent_id" class="ParentWithBidirectionalManyToMany"/>
24+
</set>
25+
</class>
26+
27+
</hibernate-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.Events.Collections.Association.Bidirectional.ManyToMany
4+
{
5+
public class ChildWithBidirectionalManyToMany : ChildEntity
6+
{
7+
private ICollection<ParentWithBidirectionalManyToMany> parents;
8+
public ChildWithBidirectionalManyToMany() {}
9+
10+
public ChildWithBidirectionalManyToMany(string name, ICollection<ParentWithBidirectionalManyToMany> parents)
11+
: base(name)
12+
{
13+
this.parents = parents;
14+
}
15+
16+
public virtual ICollection<ParentWithBidirectionalManyToMany> Parents
17+
{
18+
get { return parents; }
19+
set { parents = value; }
20+
}
21+
22+
public virtual void AddParent(ParentWithBidirectionalManyToMany parent)
23+
{
24+
if (parent != null)
25+
{
26+
parents.Add(parent);
27+
}
28+
}
29+
30+
public virtual void RemoveParent(ParentWithBidirectionalManyToMany parent)
31+
{
32+
if (parent != null)
33+
{
34+
parents.Remove(parent);
35+
}
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections.Generic;
2+
using Iesi.Collections.Generic;
3+
4+
namespace NHibernate.Test.Events.Collections.Association.Bidirectional.ManyToMany
5+
{
6+
public class ParentWithBidirectionalManyToMany : AbstractParentWithCollection
7+
{
8+
public ParentWithBidirectionalManyToMany() {}
9+
10+
public ParentWithBidirectionalManyToMany(string name) : base(name) {}
11+
12+
public override IChild CreateChild(string name)
13+
{
14+
return new ChildWithBidirectionalManyToMany(name, new HashedSet<ParentWithBidirectionalManyToMany>());
15+
}
16+
17+
public override void NewChildren(ICollection<IChild> children)
18+
{
19+
if (children == Children)
20+
{
21+
return;
22+
}
23+
if (Children != null)
24+
{
25+
foreach (ChildWithBidirectionalManyToMany child in Children)
26+
{
27+
child.RemoveParent(this);
28+
}
29+
}
30+
if (children != null)
31+
{
32+
foreach (ChildWithBidirectionalManyToMany child in children)
33+
{
34+
child.AddParent(this);
35+
}
36+
}
37+
if (children == Children)
38+
{
39+
return;
40+
}
41+
base.NewChildren(children);
42+
}
43+
44+
public override void AddChild(IChild child)
45+
{
46+
base.AddChild(child);
47+
((ChildWithBidirectionalManyToMany) child).AddParent(this);
48+
}
49+
50+
public override void AddAllChildren(ICollection<IChild> children)
51+
{
52+
base.AddAllChildren(children);
53+
foreach (ChildWithBidirectionalManyToMany child in children)
54+
{
55+
child.AddParent(this);
56+
}
57+
}
58+
59+
public override void RemoveChild(IChild child)
60+
{
61+
// Note: if the collection is a bag, the same child can be in the collection more than once
62+
base.RemoveChild(child);
63+
// only remove the parent from the child's set if child is no longer in the collection
64+
if (!Children.Contains(child))
65+
{
66+
((ChildWithBidirectionalManyToMany) child).RemoveParent(this);
67+
}
68+
}
69+
70+
public override void RemoveAllChildren(ICollection<IChild> children)
71+
{
72+
base.RemoveAllChildren(children);
73+
foreach (ChildWithBidirectionalManyToMany child in children)
74+
{
75+
child.RemoveParent(this);
76+
}
77+
}
78+
79+
public override void ClearChildren()
80+
{
81+
if (Children != null)
82+
{
83+
foreach (ChildWithBidirectionalManyToMany child in Children)
84+
{
85+
child.RemoveParent(this);
86+
}
87+
}
88+
base.ClearChildren();
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)