From 22131bd2e52e26544c27c905a5177156c5990e6d Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Tue, 13 Jul 2010 10:40:39 +0000 Subject: [PATCH] HHH-4945 * Removed obsolete testcase EJB3TestCase. * Removed obsolete FlushTest (not clear what it really tested) * Updated GetLoadTest and MergeTest to depend on ejb TestCase git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19941 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/ejb/test/EJB3TestCase.java | 225 ------------------ .../org/hibernate/ejb/test/ops/Child.java | 43 ---- .../org/hibernate/ejb/test/ops/FlushTest.java | 49 ---- .../hibernate/ejb/test/ops/GetLoadTest.java | 137 ++++++----- .../org/hibernate/ejb/test/ops/MergeTest.java | 116 +++++---- .../org/hibernate/ejb/test/ops/Parent.java | 36 --- .../ejb/test/ops/ParentChild.hbm.xml | 25 -- 7 files changed, 149 insertions(+), 482 deletions(-) delete mode 100755 entitymanager/src/test/java/org/hibernate/ejb/test/EJB3TestCase.java delete mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/ops/Child.java delete mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/ops/FlushTest.java delete mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/ops/Parent.java delete mode 100644 entitymanager/src/test/resources/org/hibernate/ejb/test/ops/ParentChild.hbm.xml diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/EJB3TestCase.java b/entitymanager/src/test/java/org/hibernate/ejb/test/EJB3TestCase.java deleted file mode 100755 index 9fc51bfe70..0000000000 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/EJB3TestCase.java +++ /dev/null @@ -1,225 +0,0 @@ -//$Id$ -package org.hibernate.ejb.test; - -import java.sql.Blob; -import java.sql.Clob; -import java.util.Iterator; -import java.util.Properties; - -import org.hibernate.HibernateException; -import org.hibernate.Interceptor; -import org.hibernate.SessionFactory; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; -import org.hibernate.dialect.Dialect; -import org.hibernate.ejb.event.EJB3AutoFlushEventListener; -import org.hibernate.ejb.event.EJB3FlushEventListener; -import org.hibernate.engine.SessionFactoryImplementor; -import org.hibernate.event.AutoFlushEventListener; -import org.hibernate.mapping.Collection; -import org.hibernate.mapping.PersistentClass; -import org.hibernate.mapping.Property; -import org.hibernate.mapping.SimpleValue; - -/** - * @author Gavin King - */ -public abstract class EJB3TestCase extends junit.framework.TestCase { - private static SessionFactory sessions; - private static Configuration cfg; - private static Dialect dialect; - private static Class lastTestClass; - private org.hibernate.classic.Session session; - - protected boolean recreateSchema() { - return true; - } - - public EJB3TestCase(String x) { - super( x ); - } - - private void buildSessionFactory(String[] files) throws Exception { - - if ( getSessions() != null ) getSessions().close(); - - try { - - setCfg( new Configuration() ); - - cfg.addProperties( getExtraProperties() ); - - if ( recreateSchema() ) { - cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); - } - - for ( int i = 0; i < files.length ; i++ ) { - if ( !files[i].startsWith( "net/" ) ) files[i] = getBaseForMappings() + files[i]; - getCfg().addResource( files[i], TestCase.class.getClassLoader() ); - } - - setDialect( Dialect.getDialect() ); - - configure( cfg ); - - if ( getCacheConcurrencyStrategy() != null ) { - - Iterator iter = cfg.getClassMappings(); - while ( iter.hasNext() ) { - PersistentClass clazz = (PersistentClass) iter.next(); - Iterator props = clazz.getPropertyClosureIterator(); - boolean hasLob = false; - while ( props.hasNext() ) { - Property prop = (Property) props.next(); - if ( prop.getValue().isSimpleValue() ) { - String type = ( (SimpleValue) prop.getValue() ).getTypeName(); - if ( "blob".equals( type ) || "clob".equals( type ) ) hasLob = true; - if ( Blob.class.getName().equals( type ) || Clob.class.getName().equals( type ) ) { - hasLob = true; - } - } - } - if ( !hasLob && !clazz.isInherited() ) { - cfg.setCacheConcurrencyStrategy( - clazz.getEntityName(), - getCacheConcurrencyStrategy() - ); - } - } - - iter = cfg.getCollectionMappings(); - while ( iter.hasNext() ) { - Collection coll = (Collection) iter.next(); - cfg.setCollectionCacheConcurrencyStrategy( - coll.getRole(), - getCacheConcurrencyStrategy() - ); - } - - } - - setSessions( getCfg().buildSessionFactory( /*new TestInterceptor()*/ ) ); - - } - catch (Exception e) { - e.printStackTrace(); - throw e; - } - - } - - public String getCacheConcurrencyStrategy() { - return "nonstrict-read-write"; - } - - protected void setUp() throws Exception { - if ( getSessions() == null || lastTestClass != getClass() || getSessions().isClosed() ) { - buildSessionFactory( getMappings() ); - lastTestClass = getClass(); - } - } - - protected void tearDown() throws Exception { - if (getSessions() != null && !getSessions().isClosed()) { - getSessions().close(); - } - } - - protected void runTest() throws Throwable { - final boolean stats = ( (SessionFactoryImplementor) sessions ).getStatistics().isStatisticsEnabled(); - try { - if ( stats ) sessions.getStatistics().clear(); - - super.runTest(); - - if ( stats ) sessions.getStatistics().logSummary(); - - if ( session != null && session.isOpen() ) { - if ( session.isConnected() ) session.connection().rollback(); - session.close(); - session = null; - fail( "unclosed session" ); - } - else { - session = null; - } - } - catch (Throwable e) { - try { - if ( session != null && session.isOpen() ) { - if ( session.isConnected() ) session.connection().rollback(); - session.close(); - } - } - catch (Exception ignore) { - } - try { - if ( dropAfterFailure() && sessions != null ) { - sessions.close(); - sessions = null; - } - } - catch (Exception ignore) { - } - throw e; - } - } - - protected boolean dropAfterFailure() { - return true; - } - - public org.hibernate.classic.Session openSession() throws HibernateException { - session = getSessions().openSession(); - return session; - } - - public org.hibernate.classic.Session openSession(Interceptor interceptor) - throws HibernateException { - session = getSessions().openSession( interceptor ); - return session; - } - - protected abstract String[] getMappings(); - - private void setSessions(SessionFactory sessions) { - EJB3TestCase.sessions = sessions; - } - - protected SessionFactory getSessions() { - return sessions; - } - - private void setDialect(Dialect dialect) { - EJB3TestCase.dialect = dialect; - } - - protected Dialect getDialect() { - return dialect; - } - - protected static void setCfg(Configuration cfg) { - EJB3TestCase.cfg = cfg; - } - - protected static Configuration getCfg() { - return cfg; - } - - /** - * @deprecated - */ - public Properties getExtraProperties() { - return new Properties(); - } - - protected String getBaseForMappings() { - return "org/hibernate/ejb/test/"; - } - - protected void configure(Configuration cfg) { - cfg.setListener( "flush", EJB3FlushEventListener.INSTANCE ); - cfg.setListeners( "auto-flush", new AutoFlushEventListener[]{EJB3AutoFlushEventListener.INSTANCE} ); - } - -} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Child.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Child.java deleted file mode 100644 index 8675306e3f..0000000000 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Child.java +++ /dev/null @@ -1,43 +0,0 @@ -//$Id$ -package org.hibernate.ejb.test.ops; - -/** - * @author Emmanuel Bernard - */ -public class Child { - private String name; - private int age; - - Child() { - } - - public Child(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - private Parent parent; - - public Parent getParent() { - return parent; - } - - public void setParent(Parent parent) { - this.parent = parent; - } -} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/FlushTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/FlushTest.java deleted file mode 100644 index 847825a91a..0000000000 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/FlushTest.java +++ /dev/null @@ -1,49 +0,0 @@ -//$Id$ -package org.hibernate.ejb.test.ops; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.ejb.test.EJB3TestCase; - -/** - * @author Emmanuel Bernard - */ -public class FlushTest extends EJB3TestCase { - - public void testPersistCascasde() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - Parent p = new Parent( "Marc" ); - Parent p2 = new Parent( "Nathalie" ); - - // FAILS - s.persist( p ); - s.persist( p2 ); - - Child c = new Child( "Elvira" ); - Child c2 = new Child( "Blase" ); - p.getChildren().add( c ); - c.setParent( p ); - p.getChildren().add( c2 ); - c2.setParent( p ); - - // WORKS - //s.persist(p); - //s.persist(p2); - - t.commit(); - s.close(); - - } - - public FlushTest(String x) { - super( x ); - } - - protected String[] getMappings() { - return new String[]{ - "ops/ParentChild.hbm.xml" - }; - } -} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/GetLoadTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/GetLoadTest.java index a2e3290ac7..c5061e90aa 100755 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/GetLoadTest.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/GetLoadTest.java @@ -1,114 +1,139 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat Inc. 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 Inc. + * + * 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 + */ + //$Id$ package org.hibernate.ejb.test.ops; -import junit.framework.Test; -import junit.framework.TestSuite; +import java.util.Map; +import javax.persistence.EntityManager; + import org.hibernate.Hibernate; import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.ejb.test.EJB3TestCase; +import org.hibernate.ejb.EntityManagerFactoryImpl; +import org.hibernate.ejb.test.TestCase; /** * @author Gavin King + * @author Hardy Ferentschik */ -public class GetLoadTest extends EJB3TestCase { - - public GetLoadTest(String str) { - super( str ); - } +public class GetLoadTest extends TestCase { public void testGetLoad() { clearCounts(); - Session s = openSession(); - Transaction tx = s.beginTransaction(); + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + Session s = ( Session ) em.getDelegate(); + Employer emp = new Employer(); s.persist( emp ); Node node = new Node( "foo" ); Node parent = new Node( "bar" ); parent.addChild( node ); s.persist( parent ); - tx.commit(); - s.close(); + em.getTransaction().commit(); + em.close(); - s = openSession(); - tx = s.beginTransaction(); - emp = (Employer) s.get( Employer.class, emp.getId() ); + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + s = ( Session ) em.getDelegate(); + emp = ( Employer ) s.get( Employer.class, emp.getId() ); assertTrue( Hibernate.isInitialized( emp ) ); assertFalse( Hibernate.isInitialized( emp.getEmployees() ) ); - node = (Node) s.get( Node.class, node.getName() ); + node = ( Node ) s.get( Node.class, node.getName() ); assertTrue( Hibernate.isInitialized( node ) ); assertFalse( Hibernate.isInitialized( node.getChildren() ) ); assertFalse( Hibernate.isInitialized( node.getParent() ) ); assertNull( s.get( Node.class, "xyz" ) ); - tx.commit(); - s.close(); + em.getTransaction().commit(); + em.close(); - s = openSession(); - tx = s.beginTransaction(); - emp = (Employer) s.load( Employer.class, emp.getId() ); + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + s = ( Session ) em.getDelegate(); + emp = ( Employer ) s.load( Employer.class, emp.getId() ); emp.getId(); assertFalse( Hibernate.isInitialized( emp ) ); - node = (Node) s.load( Node.class, node.getName() ); + node = ( Node ) s.load( Node.class, node.getName() ); assertEquals( node.getName(), "foo" ); assertFalse( Hibernate.isInitialized( node ) ); - tx.commit(); - s.close(); + em.getTransaction().commit(); + em.close(); - s = openSession(); - tx = s.beginTransaction(); - emp = (Employer) s.get( "org.hibernate.ejb.test.ops.Employer", emp.getId() ); + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + s = ( Session ) em.getDelegate(); + emp = ( Employer ) s.get( "org.hibernate.ejb.test.ops.Employer", emp.getId() ); assertTrue( Hibernate.isInitialized( emp ) ); - node = (Node) s.get( "org.hibernate.ejb.test.ops.Node", node.getName() ); + node = ( Node ) s.get( "org.hibernate.ejb.test.ops.Node", node.getName() ); assertTrue( Hibernate.isInitialized( node ) ); - tx.commit(); - s.close(); + em.getTransaction().commit(); + em.close(); - s = openSession(); - tx = s.beginTransaction(); - emp = (Employer) s.load( "org.hibernate.ejb.test.ops.Employer", emp.getId() ); + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + s = ( Session ) em.getDelegate(); + emp = ( Employer ) s.load( "org.hibernate.ejb.test.ops.Employer", emp.getId() ); emp.getId(); assertFalse( Hibernate.isInitialized( emp ) ); - node = (Node) s.load( "org.hibernate.ejb.test.ops.Node", node.getName() ); + node = ( Node ) s.load( "org.hibernate.ejb.test.ops.Node", node.getName() ); assertEquals( node.getName(), "foo" ); assertFalse( Hibernate.isInitialized( node ) ); - tx.commit(); - s.close(); + em.getTransaction().commit(); + em.close(); assertFetchCount( 0 ); } private void clearCounts() { - getSessions().getStatistics().clear(); + ( ( EntityManagerFactoryImpl ) factory ).getSessionFactory().getStatistics().clear(); } private void assertFetchCount(int count) { - int fetches = (int) getSessions().getStatistics().getEntityFetchCount(); + int fetches = ( int ) ( ( EntityManagerFactoryImpl ) factory ).getSessionFactory() + .getStatistics() + .getEntityFetchCount(); assertEquals( count, fetches ); } - protected void configure(Configuration cfg) { - super.configure( cfg ); - cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); - cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" ); + @Override + protected void addConfigOptions(Map options) { + options.put( Environment.GENERATE_STATISTICS, "true" ); + options.put( Environment.STATEMENT_BATCH_SIZE, "0" ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[0]; } protected String[] getMappings() { - return new String[]{ - "ops/Node.hbm.xml", - "ops/Employer.hbm.xml" + return new String[] { + "org/hibernate/ejb/test/ops/Node.hbm.xml", + "org/hibernate/ejb/test/ops/Employer.hbm.xml" }; } - - public static Test suite() { - return new TestSuite( GetLoadTest.class ); - } - - public String getCacheConcurrencyStrategy() { - return null; - } - } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/MergeTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/MergeTest.java index 94df6e2956..59ecb53593 100755 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/MergeTest.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/MergeTest.java @@ -1,35 +1,53 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat Inc. 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 Inc. + * + * 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 + */ //$Id$ package org.hibernate.ejb.test.ops; -import junit.framework.Test; -import junit.framework.TestSuite; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; +import java.util.Map; +import javax.persistence.EntityManager; + import org.hibernate.cfg.Environment; -import org.hibernate.ejb.test.EJB3TestCase; +import org.hibernate.ejb.EntityManagerFactoryImpl; +import org.hibernate.ejb.test.TestCase; /** * @author Gavin King + * @author Hardy Ferentschik */ -public class MergeTest extends EJB3TestCase { - - public MergeTest(String str) { - super( str ); - } +public class MergeTest extends TestCase { public void testMergeTree() { - clearCounts(); - Session s = openSession(); - Transaction tx = s.beginTransaction(); + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); Node root = new Node( "root" ); Node child = new Node( "child" ); root.addChild( child ); - s.persist( root ); - tx.commit(); - s.close(); + em.persist( root ); + em.getTransaction().commit(); + em.close(); assertInsertCount( 2 ); clearCounts(); @@ -41,29 +59,27 @@ public class MergeTest extends EJB3TestCase { root.addChild( secondChild ); - s = openSession(); - tx = s.beginTransaction(); - s.merge( root ); - tx.commit(); - s.close(); + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.merge( root ); + em.getTransaction().commit(); + em.close(); assertInsertCount( 1 ); assertUpdateCount( 2 ); - } public void testMergeTreeWithGeneratedId() { - clearCounts(); - Session s = openSession(); - Transaction tx = s.beginTransaction(); + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); NumberedNode root = new NumberedNode( "root" ); NumberedNode child = new NumberedNode( "child" ); root.addChild( child ); - s.persist( root ); - tx.commit(); - s.close(); + em.persist( root ); + em.getTransaction().commit(); + em.close(); assertInsertCount( 2 ); clearCounts(); @@ -75,44 +91,48 @@ public class MergeTest extends EJB3TestCase { root.addChild( secondChild ); - s = openSession(); - tx = s.beginTransaction(); - s.merge( root ); - tx.commit(); - s.close(); + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.merge( root ); + em.getTransaction().commit(); + em.close(); assertInsertCount( 1 ); assertUpdateCount( 2 ); - } private void clearCounts() { - getSessions().getStatistics().clear(); + ( ( EntityManagerFactoryImpl ) factory ).getSessionFactory().getStatistics().clear(); } private void assertInsertCount(int count) { - int inserts = (int) getSessions().getStatistics().getEntityInsertCount(); + int inserts = ( int ) ( ( EntityManagerFactoryImpl ) factory ).getSessionFactory() + .getStatistics() + .getEntityInsertCount(); assertEquals( count, inserts ); } private void assertUpdateCount(int count) { - int updates = (int) getSessions().getStatistics().getEntityUpdateCount(); + int updates = ( int ) ( ( EntityManagerFactoryImpl ) factory ).getSessionFactory() + .getStatistics() + .getEntityUpdateCount(); assertEquals( count, updates ); } - protected void configure(Configuration cfg) { - super.configure( cfg ); - cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); - cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" ); + @Override + protected void addConfigOptions(Map options) { + options.put( Environment.GENERATE_STATISTICS, "true" ); + options.put( Environment.STATEMENT_BATCH_SIZE, "0" ); } + @Override + protected Class[] getAnnotatedClasses() { + return new Class[0]; + } + + @Override protected String[] getMappings() { - return new String[]{"ops/Node.hbm.xml"}; + return new String[] { "org/hibernate/ejb/test/ops/Node.hbm.xml" }; } - - public static Test suite() { - return new TestSuite( MergeTest.class ); - } - } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Parent.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Parent.java deleted file mode 100644 index 6dba99a0a2..0000000000 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Parent.java +++ /dev/null @@ -1,36 +0,0 @@ -//$Id$ -package org.hibernate.ejb.test.ops; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Emmanuel Bernard - */ -public class Parent { - private String name; - private List children = new ArrayList(); - - Parent() { - } - - public Parent(String name) { - this.name = name; - } - - public List getChildren() { - return children; - } - - public void setChildren(List children) { - this.children = children; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/entitymanager/src/test/resources/org/hibernate/ejb/test/ops/ParentChild.hbm.xml b/entitymanager/src/test/resources/org/hibernate/ejb/test/ops/ParentChild.hbm.xml deleted file mode 100644 index 4db90c8165..0000000000 --- a/entitymanager/src/test/resources/org/hibernate/ejb/test/ops/ParentChild.hbm.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file