📄 abstractcollectioneventtest.java
字号:
checkResult( listeners, listeners.getPostCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); } checkResult( listeners, listeners.getPreCollectionRecreateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), parent, index++ ); checkNumberOfResults( listeners, index ); } public void testUpdateParentOneChildDiffCollectionSameChild() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); Child child = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); assertEquals( 1, parent.getChildren().size() ); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); if ( child instanceof Entity ) { child = ( Child ) s.get( child.getClass(), ( ( Entity ) child).getId() ); } Collection oldCollection = parent.getChildren(); parent.newChildren( createCollection() ); parent.addChild( child ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) oldCollection ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, oldCollection, index++ ); } if ( child instanceof ChildWithBidirectionalManyToMany ) { ChildWithBidirectionalManyToMany childWithManyToMany = ( ChildWithBidirectionalManyToMany ) child; if ( ( ( PersistentCollection ) childWithManyToMany.getParents() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++ ); } } checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, oldCollection, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, oldCollection, index++ ); if ( child instanceof ChildWithBidirectionalManyToMany ) { // hmmm, the same parent was removed and re-added to the child's collection; // should this be considered an update? checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkResult( listeners, listeners.getPreCollectionRecreateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), parent, index++ ); checkNumberOfResults( listeners, index ); } public void testUpdateParentOneChildDiffCollectionDiffChild() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); Child oldChild = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); assertEquals( 1, parent.getChildren().size() ); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); if ( oldChild instanceof Entity ) { oldChild = ( Child ) s.get( oldChild.getClass(), ( ( Entity ) oldChild).getId() ); } Collection oldCollection = parent.getChildren(); parent.newChildren( createCollection() ); Child newChild = parent.addChild( "new1" ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) oldCollection ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, oldCollection, index++ ); } if ( oldChild instanceof ChildWithBidirectionalManyToMany ) { ChildWithBidirectionalManyToMany oldChildWithManyToMany = ( ChildWithBidirectionalManyToMany ) oldChild; if ( ( ( PersistentCollection ) oldChildWithManyToMany.getParents() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), oldChildWithManyToMany, index++ ); } } checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, oldCollection, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, oldCollection, index++ ); if ( oldChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) oldChild, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) oldChild, index++ ); checkResult( listeners, listeners.getPreCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); } checkResult( listeners, listeners.getPreCollectionRecreateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), parent, index++ ); checkNumberOfResults( listeners, index ); } public void testUpdateParentOneChildToNoneByRemove() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); assertEquals( 1, parent.getChildren().size() ); Child child = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); if ( child instanceof Entity ) { child = ( Child ) s.get( child.getClass(), ( ( Entity ) child ).getId() ); } parent.removeChild( child ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } if ( child instanceof ChildWithBidirectionalManyToMany ) { ChildWithBidirectionalManyToMany childWithManyToMany = ( ChildWithBidirectionalManyToMany ) child; if ( ( ( PersistentCollection ) childWithManyToMany.getParents( ) ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++ ); } } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkNumberOfResults( listeners, index ); } public void testUpdateParentOneChildToNoneByClear() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); assertEquals( 1, parent.getChildren().size() ); Child child = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); if ( child instanceof Entity ) { child = ( Child ) s.get( child.getClass(), ( ( Entity ) child ).getId() ); } parent.clearChildren(); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } if ( child instanceof ChildWithBidirectionalManyToMany ) { ChildWithBidirectionalManyToMany childWithManyToMany = ( ChildWithBidirectionalManyToMany ) child; if ( ( ( PersistentCollection ) childWithManyToMany.getParents() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++ ); } } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkNumberOfResults( listeners, index ); } public void testUpdateParentTwoChildrenToOne() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); assertEquals( 1, parent.getChildren().size() ); Child oldChild = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); parent.addChild( "new" ); tx.commit(); s.close(); listeners.clear(); s = openSession(); tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); if ( oldChild instanceof Entity ) { oldChild = ( Child ) s.get( oldChild.getClass(), ( ( Entity ) oldChild ).getId() ); } parent.removeChild( oldChild ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } if ( oldChild instanceof ChildWithBidirectionalManyToMany ) { ChildWithBidirectionalManyToMany oldChildWithManyToMany = ( ChildWithBidirectionalManyToMany ) oldChild; if ( ( ( PersistentCollection ) oldChildWithManyToMany.getParents() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), oldChildWithManyToMany, index++ ); } } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); if ( oldChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) oldChild, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), ( ChildWithBidirectionalManyToMany ) oldChild, index++ ); } checkNumberOfResults( listeners, index ); } public void testDeleteParentWithNullChildren() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithNullChildren( "parent" ); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); s.delete( parent ); tx.commit(); s.close(); int index = 0; checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, index++ ); checkNumberOfResults( listeners, index ); } public void testDeleteParentWithNoChildren() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithNoChildren( "parent" ); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); s.delete( parent ); tx.commit(); s.close(); int index = 0; checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, index++ ); checkNumberOfResults( listeners, index ); } public void testDeleteParentAndChild() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); Child child = ( Child ) parent.getChildren().iterator().next(); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); if ( child instanceof Entity ) { child = ( Child ) s.get( child.getClass(), ( ( Entity ) child ).getId() ); } parent.removeChild( child ); if ( child instanceof Entity ) { s.delete( child ); } s.delete( parent ); tx.commit(); s.close(); int index = 0; checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getInitializeCollectionListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, index++ ); if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionRemoveListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkNumberOfResults( listeners, index ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -