📄 abstractcollectioneventtest.java
字号:
//$Id: $/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as * indicated by the @author tags or express copyright attribution statements * applied by the authors. * * All third-party contributions are distributed under license by Red Hat * Middleware LLC. This copyrighted material is made available to anyone * wishing to use, modify, copy, or redistribute it subject to the terms * and conditions of the GNU Lesser General Public License, as published by * the Free Software Foundation. This program is distributed in the hope * that it will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU Lesser General Public License for more details. You should * have received a copy of the GNU Lesser General Public License along with * this distribution; if not, write to: Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA */package org.hibernate.test.event.collection;import java.util.Collection;import java.util.Iterator;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.collection.PersistentCollection;import org.hibernate.collection.PersistentSet;import org.hibernate.event.AbstractCollectionEvent;import org.hibernate.junit.functional.FunctionalTestCase;import org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany;/** * * @author Gail Badner */public abstract class AbstractCollectionEventTest extends FunctionalTestCase { public AbstractCollectionEventTest(String string) { super( string ); } public abstract String[] getMappings(); public abstract ParentWithCollection createParent(String name); public abstract Collection createCollection(); protected void cleanupTest() { ParentWithCollection dummyParent = createParent( "dummyParent" ); dummyParent.newChildren( createCollection() ); Child dummyChild = dummyParent.addChild( "dummyChild" ); Session s = openSession(); Transaction tx = s.beginTransaction(); List children = s.createCriteria( dummyChild.getClass() ).list(); List parents = s.createCriteria( dummyParent.getClass() ).list(); for ( Iterator it = parents.iterator(); it.hasNext(); ) { ParentWithCollection parent = ( ParentWithCollection ) it.next(); parent.clearChildren(); s.delete( parent ); } for ( Iterator it = children.iterator(); it.hasNext(); ) { s.delete( it.next() ); } tx.commit(); s.close(); } public void testSaveParentEmptyChildren() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithNoChildren( "parent" ); assertEquals( 0, parent.getChildren().size() ); int index = 0; checkResult( listeners, listeners.getPreCollectionRecreateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), parent, index++ ); checkNumberOfResults( listeners, index ); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); tx.commit(); s.close(); assertNotNull( parent.getChildren() ); checkNumberOfResults( listeners, 0 ); } public void testSaveParentOneChild() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); int index = 0; checkResult( listeners, listeners.getPreCollectionRecreateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), parent, index++ ); Child child = ( Child ) parent.getChildren().iterator().next(); if ( child instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) child, index++ ); } checkNumberOfResults( listeners, index ); } public void testUpdateParentNullToOneChild() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithNullChildren( "parent" ); listeners.clear(); assertNull( parent.getChildren() ); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); assertNotNull( parent.getChildren() ); Child newChild = parent.addChild( "new" ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); if ( newChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); } checkNumberOfResults( listeners, index ); } public void testUpdateParentNoneToOneChild() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithNoChildren( "parent" ); listeners.clear(); assertEquals( 0, parent.getChildren().size() ); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); Child newChild = parent.addChild( "new" ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); if ( newChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); } checkNumberOfResults( listeners, index ); } public void testUpdateParentOneToTwoChildren() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); assertEquals( 1, parent.getChildren().size() ); listeners.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); Child newChild = parent.addChild( "new2" ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); if ( newChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); checkResult( listeners, listeners.getPostCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ ); } checkNumberOfResults( listeners, index ); } public void testUpdateParentOneToTwoSameChildren() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithOneChild( "parent", "child" ); Child child = ( Child ) parent.getChildren().iterator().next(); assertEquals( 1, parent.getChildren().size() ); 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.addChild( child ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) parent.getChildren() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, index++ ); } ChildWithBidirectionalManyToMany childWithManyToMany = null; if ( child instanceof ChildWithBidirectionalManyToMany ) { childWithManyToMany = ( ChildWithBidirectionalManyToMany ) child; if ( ( ( PersistentCollection ) childWithManyToMany.getParents() ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++ ); } } if ( !( parent.getChildren() instanceof PersistentSet ) ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), parent, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), parent, index++ ); } if ( childWithManyToMany != null && !( childWithManyToMany.getParents() instanceof PersistentSet ) ) { checkResult( listeners, listeners.getPreCollectionUpdateListener(), childWithManyToMany, index++ ); checkResult( listeners, listeners.getPostCollectionUpdateListener(), childWithManyToMany, index++ ); } checkNumberOfResults( listeners, index ); } public void testUpdateParentNullToOneChildDiffCollection() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithNullChildren( "parent" ); listeners.clear(); assertNull( parent.getChildren() ); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); Collection collectionOrig = parent.getChildren(); parent.newChildren( createCollection() ); Child newChild = parent.addChild( "new" ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) collectionOrig ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, collectionOrig, index++ ); } checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, collectionOrig, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, collectionOrig, index++ ); if ( newChild instanceof ChildWithBidirectionalManyToMany ) { 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 testUpdateParentNoneToOneChildDiffCollection() { CollectionListeners listeners = new CollectionListeners( getSessions() ); ParentWithCollection parent = createParentWithNoChildren( "parent" ); listeners.clear(); assertEquals( 0, parent.getChildren().size() ); Session s = openSession(); Transaction tx = s.beginTransaction(); parent = ( ParentWithCollection ) s.get( parent.getClass(), parent.getId() ); Collection oldCollection = parent.getChildren(); parent.newChildren( createCollection() ); Child newChild = parent.addChild( "new" ); tx.commit(); s.close(); int index = 0; if ( ( ( PersistentCollection ) oldCollection ).wasInitialized() ) { checkResult( listeners, listeners.getInitializeCollectionListener(), parent, oldCollection, index++ ); } checkResult( listeners, listeners.getPreCollectionRemoveListener(), parent, oldCollection, index++ ); checkResult( listeners, listeners.getPostCollectionRemoveListener(), parent, oldCollection, index++ ); if ( newChild instanceof ChildWithBidirectionalManyToMany ) { checkResult( listeners, listeners.getPreCollectionRecreateListener(), ( ChildWithBidirectionalManyToMany ) newChild, index++ );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -