* 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
This commit is contained in:
Hardy Ferentschik 2010-07-13 10:40:39 +00:00
parent e197d208b6
commit 22131bd2e5
7 changed files with 149 additions and 482 deletions

View File

@ -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} );
}
}

View File

@ -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;
}
}

View File

@ -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"
};
}
}

View File

@ -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;
}
}

View File

@ -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 );
}
}

View File

@ -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;
}
}

View File

@ -1,25 +0,0 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.ejb.test.ops">
<class name="Parent">
<id name="name"/>
<list name="children" cascade="persist,merge">
<key column="parentName" not-null="true"/>
<list-index column="sibling"/>
<one-to-many class="Child"/>
</list>
</class>
<class name="Child">
<id name="name"/>
<property name="age" not-null="true"/>
<many-to-one name="parent" column="parentName"
not-null="true" insert="false" update="false"/>
</class>
</hibernate-mapping>