📄 abstractcollectioneventtest.java
字号:
public void testMoveChildToDifferentParent() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); ParentWithCollection otherParent = createParentWithOneChild( "otherParent", "otherChild" ); Child child = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); otherParent = ( ParentWithCollection ) s.get( otherParent.getClass(), otherParent.getId() ); if ( child instanceof Entity ) { child = ( Child ) s.get( child.getClass(), ( ( Entity ) child ).getId() ); } parent.removeChild( child ); otherParent.addChild( child ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getInitializeCollectionListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } if ( ( ( PersistentCollection ) otherParent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), otherParent, index++ ); } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPreCollectionUpdateListener(), otherParent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), otherParent, index++ ); if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkNumberOfResults( listeners, index ); } public void testMoveAllChildrenToDifferentParent() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); ParentWithCollection otherParent = createParentWithOneChild( "otherParent", "otherChild" ); Child child = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); otherParent = ( ParentWithCollection ) s.get( otherParent.getClass(), otherParent.getId() ); if ( child instanceof Entity ) { child = ( Child ) s.get( child.getClass(), ( ( Entity ) child ).getId() ); } otherParent.addAllChildren( parent.getChildren() ); parent.clearChildren(); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } if ( ( ( PersistentCollection ) otherParent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), otherParent, index++ ); } if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getInitializeCollectionListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPreCollectionUpdateListener(), otherParent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), otherParent, index++ ); if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkNumberOfResults( listeners, index ); } public void testMoveCollectionToDifferentParent() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); ParentWithCollection otherParent = createParentWithOneChild( "otherParent", "otherChild" ); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); otherParent = ( ParentWithCollection ) s.get( otherParent.getClass(), otherParent.getId() ); Collection otherCollectionOrig = otherParent.getChildren(); otherParent.newChildren( parent.getChildren() ); parent.newChildren( null ); tx.commit(); s.close(); int index = 0; Child otherChildOrig = null; if ( ( ( PersistentCollection ) otherCollectionOrig ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), otherParent, otherCollectionOrig, index++ ); otherChildOrig = ( Child ) otherCollectionOrig.iterator().next(); if ( otherChildOrig instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getInitializeCollectionListener(), ( ChildWithBidirectionalManyToMany ) otherChildOrig, index++ ); } } checkResult( listeners, listeners.getInitializeCollectionListener(), parent, otherParent.getChildren(), index++ ); Child otherChild = ( Child ) otherParent.getChildren().iterator().next(); if ( otherChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getInitializeCollectionListener(), ( ChildWithBidirectionalManyToMany ) otherChild, index++ ); } checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, otherParent.getChildren(), index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, otherParent.getChildren(), index++ ); checkResult( listeners, listeners.getPreCollectionRemoveListener(), otherParent, otherCollectionOrig, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), otherParent, otherCollectionOrig, index++ ); if ( otherChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherChildOrig, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherChildOrig, index++ ); checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherChild, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherChild, index++ ); } checkResult( listeners, listeners.getPreCollectionRecreateListener(), otherParent, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), otherParent, index++ ); // there should also be pre- and post-recreate collection events for parent, but thats broken now; // this is covered in BrokenCollectionEventTest checkNumberOfResults( listeners, index ); } public void testMoveCollectionToDifferentParentFlushMoveToDifferentParent() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); ParentWithCollection otherParent = createParentWithOneChild( "otherParent", "otherChild" ); ParentWithCollection otherOtherParent = createParentWithNoChildren( "otherParent" ); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); otherParent = ( ParentWithCollection ) s.get( otherParent.getClass(), otherParent.getId() ); otherOtherParent = ( ParentWithCollection ) s.get( otherOtherParent.getClass(), otherOtherParent.getId() ); Collection otherCollectionOrig = otherParent.getChildren(); Collection otherOtherCollectionOrig = otherOtherParent.getChildren(); otherParent.newChildren( parent.getChildren() ); parent.newChildren( null ); s.flush(); otherOtherParent.newChildren( otherParent.getChildren() ); otherParent.newChildren( null ); tx.commit(); s.close(); int index = 0; Child otherChildOrig = null; if ( ( ( PersistentCollection ) otherCollectionOrig ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), otherParent, otherCollectionOrig, index++ ); otherChildOrig = ( Child ) otherCollectionOrig.iterator().next(); if ( otherChildOrig instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getInitializeCollectionListener(), ( ChildWithBidirectionalManyToMany ) otherChildOrig, index++ ); } } checkResult( listeners, listeners.getInitializeCollectionListener(), parent, otherOtherParent.getChildren(), index++ ); Child otherOtherChild = ( Child ) otherOtherParent.getChildren().iterator().next(); if ( otherOtherChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getInitializeCollectionListener(), ( ChildWithBidirectionalManyToMany ) otherOtherChild, index++ ); } checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, otherOtherParent.getChildren(), index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, otherOtherParent.getChildren(), index++ ); checkResult( listeners, listeners.getPreCollectionRemoveListener(), otherParent, otherCollectionOrig, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), otherParent, otherCollectionOrig, index++ ); if ( otherOtherChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherChildOrig, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherChildOrig, index++ ); checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherOtherChild, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherOtherChild, index++ ); } checkResult( listeners, listeners.getPreCollectionRecreateListener(), otherParent, otherOtherParent.getChildren(), index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), otherParent, otherOtherParent.getChildren(), index++ ); if ( ( ( PersistentCollection ) otherOtherCollectionOrig ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), otherOtherParent, otherOtherCollectionOrig, index++ ); } checkResult( listeners, listeners.getPreCollectionRemoveListener(), otherParent, otherOtherParent.getChildren(), index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), otherParent, otherOtherParent.getChildren(), index++ ); checkResult( listeners, listeners.getPreCollectionRemoveListener(), otherOtherParent, otherOtherCollectionOrig, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), otherOtherParent, otherOtherCollectionOrig, index++ ); if ( otherOtherChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherOtherChild, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) otherOtherChild, index++ ); } checkResult( listeners, listeners.getPreCollectionRecreateListener(), otherOtherParent, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), 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 ); } protected ParentWithCollection createParentWithNullChildren(String parentName) { Session s = openSession(); Transaction tx = s.beginTransaction(); ParentWithCollection parent = createParent( parentName ); s.save( parent ); tx.commit(); s.close(); return parent; } protected ParentWithCollection createParentWithNoChildren(String parentName) { Session s = openSession(); Transaction tx = s.beginTransaction(); ParentWithCollection parent = createParent( parentName ); parent.newChildren( createCollection() ); s.save( parent ); tx.commit(); s.close(); return parent; } protected ParentWithCollection createParentWithOneChild(String parentName, String ChildName) { Session s = openSession(); Transaction tx = s.beginTransaction(); ParentWithCollection parent = createParent( parentName ); parent.newChildren( createCollection() ); parent.addChild( ChildName ); s.save( parent ); tx.commit(); s.close(); return parent; } protected void checkResult(CollectionListeners listeners, CollectionListeners.Listener listenerExpected, ParentWithCollection parent, int index) { checkResult( listeners, listenerExpected, parent, parent.getChildren(), index ); } protected void checkResult(CollectionListeners listeners, CollectionListeners.Listener listenerExpected, ChildWithBidirectionalManyToMany child, int index) { checkResult( listeners, listenerExpected, child, child.getParents(), index ); } protected void checkResult(CollectionListeners listeners, CollectionListeners.Listener listenerExpected, Entity ownerExpected, Collection collExpected, int index) { assertSame( listenerExpected, listeners.getListenersCalled().get( index ) ); assertSame( ownerExpected, ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerOrNull() ); assertEquals( ownerExpected.getId(), ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerIdOrNull() ); assertEquals( ownerExpected.getClass().getName(), ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerEntityName() ); assertSame( collExpected, ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getCollection() ); } protected void checkNumberOfResults(CollectionListeners listeners, int nEventsExpected) { assertEquals( nEventsExpected, listeners.getListenersCalled().size() ); assertEquals( nEventsExpected, listeners.getEvents().size() ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -