[hibernate-commits] Hibernate SVN: r19400 - in entitymanager/branches/v3_4_0_GA_CP: src/test/java/org/hibernate/ejb/test and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri May 7 03:23:22 EDT 2010


Author: stliu
Date: 2010-05-07 03:23:22 -0400 (Fri, 07 May 2010)
New Revision: 19400

Modified:
   entitymanager/branches/v3_4_0_GA_CP/pom.xml
   entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/PackagedEntityManagerTest.java
   entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/TestCase.java
Log:
JBPAPP-4235 HHH-4822 Add @FailureExpected annotation to annotations and entitymananger modules to allow the skipping of tests

Modified: entitymanager/branches/v3_4_0_GA_CP/pom.xml
===================================================================
--- entitymanager/branches/v3_4_0_GA_CP/pom.xml	2010-05-07 07:18:16 UTC (rev 19399)
+++ entitymanager/branches/v3_4_0_GA_CP/pom.xml	2010-05-07 07:23:22 UTC (rev 19400)
@@ -64,8 +64,13 @@
 			<artifactId>hibernate-core</artifactId>
 			<version>3.3.2.GA_CP01</version>
 		</dependency>
-
         <dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-testing</artifactId>
+			<version>3.3.2.GA_CP01</version>
+			<scope>test</scope>
+		</dependency>
+        <dependency>
 			<groupId>dom4j</groupId>
 			<artifactId>dom4j</artifactId>
 		</dependency>
@@ -168,6 +173,10 @@
           		</descriptors>
 			</configuration>
 		</plugin>
+	</plugins>
+	</build>
+	<reporting>
+		<plugins>
 		<plugin>
 			<groupId>org.apache.maven.plugins</groupId>
 			<artifactId>maven-javadoc-plugin</artifactId>
@@ -178,16 +187,7 @@
 				</links>
 				<stylesheetfile>${basedir}/src/main/javadoc/jdstyle.css</stylesheetfile>
 			</configuration>
-			<executions>
-				<execution>
-					<id>make-javadoc</id>
-					<phase>package</phase>
-					<goals>
-						<goal>javadoc</goal>
-					</goals>
-				</execution>
-			</executions>
 		</plugin>
 	</plugins>
-	</build>
+	</reporting>
 </project>
\ No newline at end of file

Modified: entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/PackagedEntityManagerTest.java
===================================================================
--- entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/PackagedEntityManagerTest.java	2010-05-07 07:18:16 UTC (rev 19399)
+++ entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/PackagedEntityManagerTest.java	2010-05-07 07:23:22 UTC (rev 19400)
@@ -40,11 +40,13 @@
 public class PackagedEntityManagerTest extends TestCase {
 
 	public Class[] getAnnotatedClasses() {
-		return new Class[] {
+		return new Class[] {Item.class,Distributor.class
 		};
 	}
 
-	public void setUp() {
+	@Override
+	protected void buildConfiguration() throws Exception {
+		super.buildConfiguration();
 		factory = Persistence.createEntityManagerFactory( "manager1" );
 	}
 

Modified: entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/TestCase.java
===================================================================
--- entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/TestCase.java	2010-05-07 07:18:16 UTC (rev 19399)
+++ entitymanager/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/ejb/test/TestCase.java	2010-05-07 07:23:22 UTC (rev 19400)
@@ -1,11 +1,11 @@
-//$Id$
+// $Id$
 /*
  * Hibernate, Relational Persistence for Idiomatic Java
  *
- * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
- * third-party contributors as indicated by either @author tags or express
- * copyright attribution statements applied by the authors.  All
- * third-party contributions are distributed under license by Red Hat Inc.
+ * 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
@@ -27,29 +27,39 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
-
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import org.hibernate.cfg.Environment;
 import org.hibernate.dialect.Dialect;
+import org.hibernate.ejb.Ejb3Configuration;
 import org.hibernate.ejb.HibernatePersistence;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.hibernate.test.annotations.HibernateTestCase;
 
-
 /**
+ * A base class for all ejb tests.
+ *
  * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
  */
-public abstract class TestCase extends junit.framework.TestCase {
-	protected EntityManagerFactory factory;
-	protected EntityManager em;
-	private static Logger log = LoggerFactory.getLogger( TestCase.class );
+public abstract class TestCase extends HibernateTestCase {
 
+	private static final Log log = LogFactory.getLog( TestCase.class );
+
+	protected static EntityManagerFactory factory;
+	private EntityManager em;
+	private ArrayList isolatedEms = new ArrayList();
+
+
 	public TestCase() {
 		super();
 	}
@@ -58,54 +68,90 @@
 		super( name );
 	}
 
-	public void setUp() {
-		factory = new HibernatePersistence().createEntityManagerFactory( getConfig() );
+
+	public void tearDown() throws Exception {
+		super.tearDown();
 	}
 
-	public void tearDown() {
-		factory.close();
-	}
-	
 	@Override
-	public void runTest() throws Throwable {
-		try {
-			em = getOrCreateEntityManager();
-			super.runTest();
-			if (em.getTransaction().isActive()) {
-				em.getTransaction().rollback();
-				fail("You left an open transaction! Fix your test case. For now, we are closing it for you.");
-			}
-		} catch (Throwable t) {
-			if (em.getTransaction().isActive())  
-				em.getTransaction().rollback();
-			throw t;
-		} finally {
-			if (em.isOpen()) {
-				em.close();
-				log.warn("The test case didn't closed the Entity Manager. Make sure you close it always!");
-			}
+	protected void buildConfiguration() throws Exception {
+		Ejb3Configuration ejbconfig = new Ejb3Configuration();
+		TestCase.cfg = ejbconfig.getHibernateConfiguration();
+		if ( recreateSchema() ) {
+			cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
 		}
+		//cfg.setProperty( AnnotationConfiguration.USE_NEW_ID_GENERATOR_MAPPINGS, "true" );
+
+		for ( String mappingFile : getMappings() ) {
+			cfg.addResource( mappingFile );
+		}
+
+		factory = ejbconfig.createEntityManagerFactory( getConfig() );
 	}
-	
+
+	private void cleanUnclosed(EntityManager em) {
+		if ( em == null ) {
+			return;
+		}
+		if ( em.getTransaction().isActive() ) {
+			em.getTransaction().rollback();
+			log.warn( "You left an open transaction! Fix your test case. For now, we are closing it for you." );
+		}
+		if ( em.isOpen() ) {
+			// as we open an EM before the test runs, it will still be open if the test uses a custom EM.
+			// or, the person may have forgotten to close. So, do not raise a "fail", but log the fact.
+			em.close();
+			log.warn( "The EntityManager is not closed. Closing it." );
+		}
+	}
+
+	protected void handleUnclosedResources() {
+		cleanUnclosed( this.em );
+		for ( Iterator iter = isolatedEms.iterator(); iter.hasNext(); ) {
+			cleanUnclosed( ( EntityManager ) iter.next() );
+		}
+
+		cfg = null;
+	}
+
+	protected void closeResources() {
+		if ( factory != null ) {
+			factory.close();
+		}
+	}
+
 	protected EntityManager getOrCreateEntityManager() {
-		if (em == null || !em.isOpen()) 
+		if ( em == null || !em.isOpen() ) {
 			em = factory.createEntityManager();
+		}
 		return em;
 	}
 
-	/** always reopen a new EM and close the existing one */
+	protected EntityManager createIsolatedEntityManager() {
+		EntityManager isolatedEm = factory.createEntityManager();
+		isolatedEms.add( isolatedEm );
+		return isolatedEm;
+	}
+
+	protected EntityManager createIsolatedEntityManager(Map props) {
+		EntityManager isolatedEm = factory.createEntityManager(props);
+		isolatedEms.add( isolatedEm );
+		return isolatedEm;
+	}
+
+	/**
+	 * always reopen a new EM and clse the existing one
+	 */
 	protected EntityManager createEntityManager(Map properties) {
-		if (em != null && em.isOpen() ) {
+		if ( em != null && em.isOpen() ) {
 			em.close();
 		}
-		em = factory.createEntityManager(properties);
+		em = factory.createEntityManager( properties );
 		return em;
 	}
 
-	public abstract Class[] getAnnotatedClasses();
-
 	public String[] getEjb3DD() {
-		return new String[] {};
+		return new String[] { };
 	}
 
 	public Map<Class, String> getCachedClasses() {
@@ -123,14 +169,14 @@
 			try {
 				props.load( stream );
 			}
-			catch (Exception e) {
+			catch ( Exception e ) {
 				throw new RuntimeException( "could not load hibernate.properties" );
 			}
 			finally {
 				try {
 					stream.close();
 				}
-				catch (IOException ioe) {
+				catch ( IOException ioe ) {
 				}
 			}
 		}
@@ -139,12 +185,9 @@
 	}
 
 	public Map getConfig() {
-		Map config = loadProperties();
+		Map<Object, Object> config = loadProperties();
 		ArrayList<Class> classes = new ArrayList<Class>();
-
-		for ( Class clazz : getAnnotatedClasses() ) {
-			classes.add( clazz );
-		}
+		classes.addAll( Arrays.asList( getAnnotatedClasses() ) );
 		config.put( HibernatePersistence.LOADED_CLASSES, classes );
 		for ( Map.Entry<Class, String> entry : getCachedClasses().entrySet() ) {
 			config.put(
@@ -160,40 +203,26 @@
 		}
 		if ( getEjb3DD().length > 0 ) {
 			ArrayList<String> dds = new ArrayList<String>();
-			for ( String dd : getEjb3DD() ) {
-				dds.add( dd );
-			}
+			dds.addAll( Arrays.asList( getEjb3DD() ) );
 			config.put( HibernatePersistence.XML_FILE_NAMES, dds );
 		}
+
+		addConfigOptions( config );
 		return config;
 	}
 
+	protected void addConfigOptions(Map options) {
+	}
+
 	@Override
 	public void runBare() throws Throwable {
-		
-		if (!appliesTo(Dialect.getDialect())) 
+		if ( !appliesTo( Dialect.getDialect() ) ) {
 			return;
-		
-		Throwable exception = null;
-		setUp();
-		try {
-			runTest();
-		} catch (Throwable running) {
-			exception = running;
-		} finally {
-			try {
-				tearDown();
-			} catch (Throwable tearingDown) {
-				if (exception == null)
-					exception = tearingDown;
-			}
 		}
-		if (exception != null)
-			throw exception;
+		super.runBare();
 	}
 
 	public boolean appliesTo(Dialect dialect) {
 		return true;
 	}
-
 }



More information about the hibernate-commits mailing list