public void MoveChildToDifferentParent() { CollectionListeners listeners = new CollectionListeners(Sfi); IParentWithCollection parent = CreateParentWithOneChild("parent", "child"); IParentWithCollection otherParent = CreateParentWithOneChild("otherParent", "otherChild"); IChild child = GetFirstChild(parent.Children); listeners.Clear(); ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id); otherParent = (IParentWithCollection)s.Get(otherParent.GetType(), otherParent.Id); IEntity e = child as IEntity; if (e != null) { child = (IChild)s.Get(child.GetType(), e.Id); } parent.RemoveChild(child); otherParent.AddChild(child); tx.Commit(); s.Close(); int index = 0; if (((IPersistentCollection)parent.Children).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, parent, index++); } ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany; if (childWithManyToMany != null) { CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++); } if (((IPersistentCollection)otherParent.Children).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, otherParent, index++); } CheckResult(listeners, listeners.PreCollectionUpdate, parent, index++); CheckResult(listeners, listeners.PostCollectionUpdate, parent, index++); CheckResult(listeners, listeners.PreCollectionUpdate, otherParent, index++); CheckResult(listeners, listeners.PostCollectionUpdate, otherParent, index++); if (childWithManyToMany != null) { CheckResult(listeners, listeners.PreCollectionUpdate, childWithManyToMany, index++); CheckResult(listeners, listeners.PostCollectionUpdate, childWithManyToMany, index++); } CheckNumberOfResults(listeners, index); }
public virtual void UpdateParentOneToTwoSameChildren() { CollectionListeners listeners = new CollectionListeners(Sfi); IParentWithCollection parent = CreateParentWithOneChild("parent", "child"); IChild child = GetFirstChild(parent.Children); Assert.That(parent.Children.Count, Is.EqualTo(1)); listeners.Clear(); using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id); IEntity e = child as IEntity; if (e != null) { child = (IChild)s.Get(child.GetType(), e.Id); } parent.AddChild(child); tx.Commit(); } int index = 0; if (((IPersistentCollection)parent.Children).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, parent, index++); } ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany; if (childWithManyToMany != null) { if (((IPersistentCollection)childWithManyToMany.Parents).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++); } } if (!(parent.Children is PersistentGenericSet <IChild>)) { CheckResult(listeners, listeners.PreCollectionUpdate, parent, index++); CheckResult(listeners, listeners.PostCollectionUpdate, parent, index++); } if (childWithManyToMany != null && !(childWithManyToMany.Parents is PersistentGenericSet <ParentWithBidirectionalManyToMany>)) { CheckResult(listeners, listeners.PreCollectionUpdate, childWithManyToMany, index++); CheckResult(listeners, listeners.PostCollectionUpdate, childWithManyToMany, index++); } CheckNumberOfResults(listeners, index); }
public void UpdateParentOneChildToNoneByRemove() { CollectionListeners listeners = new CollectionListeners(Sfi); IParentWithCollection parent = CreateParentWithOneChild("parent", "child"); Assert.That(parent.Children.Count, Is.EqualTo(1)); IChild child = GetFirstChild(parent.Children); listeners.Clear(); ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id); IEntity e = child as IEntity; if (e != null) { child = (IChild)s.Get(child.GetType(), e.Id); } parent.RemoveChild(child); tx.Commit(); s.Close(); int index = 0; if (((IPersistentCollection)parent.Children).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, parent, index++); } ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany; if (childWithManyToMany != null) { if (((IPersistentCollection)childWithManyToMany.Parents).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++); } } CheckResult(listeners, listeners.PreCollectionUpdate, parent, index++); CheckResult(listeners, listeners.PostCollectionUpdate, parent, index++); if (childWithManyToMany != null) { CheckResult(listeners, listeners.PreCollectionUpdate, childWithManyToMany, index++); CheckResult(listeners, listeners.PostCollectionUpdate, childWithManyToMany, index++); } CheckNumberOfResults(listeners, index); }
public virtual void SaveParentOneChild() { CollectionListeners listeners = new CollectionListeners(Sfi); IParentWithCollection parent = CreateParentWithOneChild("parent", "child"); int index = 0; CheckResult(listeners, listeners.PreCollectionRecreate, parent, index++); CheckResult(listeners, listeners.PostCollectionRecreate, parent, index++); ChildWithBidirectionalManyToMany child = GetFirstChild(parent.Children) as ChildWithBidirectionalManyToMany; if (child != null) { CheckResult(listeners, listeners.PreCollectionRecreate, child, index++); CheckResult(listeners, listeners.PostCollectionRecreate, child, index++); } CheckNumberOfResults(listeners, index); }
public void DeleteParentAndChild() { CollectionListeners listeners = new CollectionListeners(Sfi); IParentWithCollection parent = CreateParentWithOneChild("parent", "child"); IChild child = GetFirstChild(parent.Children); listeners.Clear(); ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id); IEntity e = child as IEntity; if (e != null) { child = (IChild)s.Get(child.GetType(), e.Id); } parent.RemoveChild(child); if (e != null) { s.Delete(child); } s.Delete(parent); tx.Commit(); s.Close(); int index = 0; CheckResult(listeners, listeners.InitializeCollection, parent, index++); ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany; if (childWithManyToMany != null) { CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++); } CheckResult(listeners, listeners.PreCollectionRemove, parent, index++); CheckResult(listeners, listeners.PostCollectionRemove, parent, index++); if (childWithManyToMany != null) { CheckResult(listeners, listeners.PreCollectionRemove, childWithManyToMany, index++); CheckResult(listeners, listeners.PostCollectionRemove, childWithManyToMany, index++); } CheckNumberOfResults(listeners, index); }
public void UpdateParentNoneToOneChildDiffCollection() { CollectionListeners listeners = new CollectionListeners(Sfi); IParentWithCollection parent = CreateParentWithNoChildren("parent"); listeners.Clear(); Assert.That(parent.Children.Count, Is.EqualTo(0)); ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id); ICollection <IChild> oldCollection = parent.Children; parent.NewChildren(CreateCollection()); ChildWithBidirectionalManyToMany newChild = parent.AddChild("new") as ChildWithBidirectionalManyToMany; tx.Commit(); s.Close(); int index = 0; if (((IPersistentCollection)oldCollection).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, parent, oldCollection, index++); } CheckResult(listeners, listeners.PreCollectionRemove, parent, oldCollection, index++); CheckResult(listeners, listeners.PostCollectionRemove, parent, oldCollection, index++); if (newChild != null) { CheckResult(listeners, listeners.PreCollectionRecreate, newChild, index++); CheckResult(listeners, listeners.PostCollectionRecreate, newChild, index++); } CheckResult(listeners, listeners.PreCollectionRecreate, parent, index++); CheckResult(listeners, listeners.PostCollectionRecreate, parent, index++); CheckNumberOfResults(listeners, index); }
protected void CheckResult(CollectionListeners listeners, CollectionListeners.IListener listenerExpected, ChildWithBidirectionalManyToMany child, int index) { CheckResult(listeners, listenerExpected, child, child.Parents, index); }
public void MoveCollectionToDifferentParentFlushMoveToDifferentParent() { CollectionListeners listeners = new CollectionListeners(Sfi); IParentWithCollection parent = CreateParentWithOneChild("parent", "child"); IParentWithCollection otherParent = CreateParentWithOneChild("otherParent", "otherChild"); IParentWithCollection otherOtherParent = CreateParentWithNoChildren("otherParent"); listeners.Clear(); ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id); otherParent = (IParentWithCollection)s.Get(otherParent.GetType(), otherParent.Id); otherOtherParent = (IParentWithCollection)s.Get(otherOtherParent.GetType(), otherOtherParent.Id); ICollection <IChild> otherCollectionOrig = otherParent.Children; ICollection <IChild> otherOtherCollectionOrig = otherOtherParent.Children; otherParent.NewChildren(parent.Children); parent.NewChildren(null); s.Flush(); otherOtherParent.NewChildren(otherParent.Children); otherParent.NewChildren(null); tx.Commit(); s.Close(); int index = 0; ChildWithBidirectionalManyToMany otherChildOrig = null; if (((IPersistentCollection)otherCollectionOrig).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, otherParent, otherCollectionOrig, index++); otherChildOrig = GetFirstChild(otherCollectionOrig) as ChildWithBidirectionalManyToMany; if (otherChildOrig != null) { CheckResult(listeners, listeners.InitializeCollection, otherChildOrig, index++); } } CheckResult(listeners, listeners.InitializeCollection, parent, otherOtherParent.Children, index++); ChildWithBidirectionalManyToMany otherOtherChild = GetFirstChild(otherOtherParent.Children) as ChildWithBidirectionalManyToMany; if (otherOtherChild != null) { CheckResult(listeners, listeners.InitializeCollection, otherOtherChild, index++); } CheckResult(listeners, listeners.PreCollectionRemove, parent, otherOtherParent.Children, index++); CheckResult(listeners, listeners.PostCollectionRemove, parent, otherOtherParent.Children, index++); CheckResult(listeners, listeners.PreCollectionRemove, otherParent, otherCollectionOrig, index++); CheckResult(listeners, listeners.PostCollectionRemove, otherParent, otherCollectionOrig, index++); if (otherOtherChild != null) { CheckResult(listeners, listeners.PreCollectionUpdate, otherChildOrig, index++); CheckResult(listeners, listeners.PostCollectionUpdate, otherChildOrig, index++); CheckResult(listeners, listeners.PreCollectionUpdate, otherOtherChild, index++); CheckResult(listeners, listeners.PostCollectionUpdate, otherOtherChild, index++); } CheckResult(listeners, listeners.PreCollectionRecreate, otherParent, otherOtherParent.Children, index++); CheckResult(listeners, listeners.PostCollectionRecreate, otherParent, otherOtherParent.Children, index++); if (((IPersistentCollection)otherOtherCollectionOrig).WasInitialized) { CheckResult(listeners, listeners.InitializeCollection, otherOtherParent, otherOtherCollectionOrig, index++); } CheckResult(listeners, listeners.PreCollectionRemove, otherParent, otherOtherParent.Children, index++); CheckResult(listeners, listeners.PostCollectionRemove, otherParent, otherOtherParent.Children, index++); CheckResult(listeners, listeners.PreCollectionRemove, otherOtherParent, otherOtherCollectionOrig, index++); CheckResult(listeners, listeners.PostCollectionRemove, otherOtherParent, otherOtherCollectionOrig, index++); if (otherOtherChild != null) { CheckResult(listeners, listeners.PreCollectionUpdate, otherOtherChild, index++); CheckResult(listeners, listeners.PostCollectionUpdate, otherOtherChild, index++); } CheckResult(listeners, listeners.PreCollectionRecreate, otherOtherParent, index++); CheckResult(listeners, listeners.PostCollectionRecreate, otherOtherParent, index++); // there should also be pre- and post-recreate collection events for parent, and otherParent // but thats broken now; this is covered in BrokenCollectionEventTest CheckNumberOfResults(listeners, index); }