[hibernate-commits] Hibernate SVN: r17716 - in annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations: id and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Oct 13 09:23:38 EDT 2009


Author: stliu
Date: 2009-10-13 09:23:37 -0400 (Tue, 13 Oct 2009)
New Revision: 17716

Modified:
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytoone/referencedcolumnname/ManyToOneReferencedColumnNameTest.java
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmTest.java
Log:
JBPAPP-2910 HHH-4415 : TestCase could check for superclass of Dialect before skipping it

Modified: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java	2009-10-13 12:25:56 UTC (rev 17715)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java	2009-10-13 13:23:37 UTC (rev 17716)
@@ -2,24 +2,60 @@
 package org.hibernate.test.annotations;
 
 import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 
+
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hibernate.HibernateException;
+import org.hibernate.Interceptor;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
-import org.hibernate.Interceptor;
 import org.hibernate.cfg.AnnotationConfiguration;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Environment;
 import org.hibernate.dialect.Dialect;
+import org.hibernate.tool.hbm2ddl.SchemaExport;
 
+/**
+ * A base class for all tests.
+ *
+ * @author Emmnauel Bernand
+ * @author Hardy Ferentschik
+ */
 public abstract class TestCase extends junit.framework.TestCase {
 
+	public static final Log log = LogFactory.getLog( TestCase.class );
+
 	private static SessionFactory sessions;
 	private static AnnotationConfiguration cfg;
-	private static Dialect dialect;
-	private static Class lastTestClass;
+	private static Class<?> lastTestClass;
 	private Session session;
 
+	/**
+	 * The test method.
+	 */
+	private Method runMethod = null;
+
+	/**
+	 * Flag indicating whether the test should be run or skipped.
+	 */
+	private boolean runTest = true;
+
+	/**
+	 * List of required dialect for the current {@code runMethod}. If the list is empty any dialect is allowed.
+	 * Otherwise the current dialect or a superclass of the current dialect must be in the list.
+	 */
+	private final Set<Class<? extends Dialect>> requiredDialectList = new HashSet<Class<? extends Dialect>>();
+
 	public TestCase() {
 		super();
 	}
@@ -28,73 +64,171 @@
 		super( x );
 	}
 
-	protected void buildSessionFactory(Class[] classes, String[] packages, String[] xmlFiles) throws Exception {
+	protected void buildSessionFactory(Class<?>[] classes, String[] packages, String[] xmlFiles) throws Exception {
 
-		if ( getSessions() != null ) getSessions().close();
+		if ( getSessions() != null ) {
+			getSessions().close();
+		}
 		try {
 			setCfg( new AnnotationConfiguration() );
 			configure( cfg );
 			if ( recreateSchema() ) {
 				cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
 			}
-			for ( int i = 0; i < packages.length ; i++ ) {
-				getCfg().addPackage( packages[i] );
+			for ( String aPackage : packages ) {
+				getCfg().addPackage( aPackage );
 			}
-			for ( int i = 0; i < classes.length ; i++ ) {
-				getCfg().addAnnotatedClass( classes[i] );
+			for ( Class<?> aClass : classes ) {
+				getCfg().addAnnotatedClass( aClass );
 			}
-			for ( int i = 0; i < xmlFiles.length ; i++ ) {
-				InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFiles[i] );
+			for ( String xmlFile : xmlFiles ) {
+				InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
 				getCfg().addInputStream( is );
 			}
-			setDialect( Dialect.getDialect() );
-			setSessions( getCfg().buildSessionFactory( /*new TestInterceptor()*/ ) );
+			setSessions( getCfg().buildSessionFactory( /* new TestInterceptor() */ ) );
 		}
-		catch (Exception e) {
+		catch ( Exception e ) {
 			e.printStackTrace();
 			throw e;
 		}
 	}
 
 	protected void setUp() throws Exception {
-		if ( getSessions() == null || lastTestClass != getClass() ) {
-			buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
-			lastTestClass = getClass();
+		runMethod = findTestMethod();
+		System.out.println(Dialect.getDialect());
+		setRunTestFlag( runMethod );
+		if ( runTest ) {
+			if ( getSessions() == null || lastTestClass != getClass() ) {
+				buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
+				lastTestClass = getClass();
+			}
+			else {
+				runSchemaGeneration();
+			}
 		}
 	}
 
 	protected void runTest() throws Throwable {
 		try {
-			super.runTest();
-			if ( session != null && session.isOpen() ) {
-				if ( session.isConnected() ) session.connection().rollback();
-				session.close();
-				session = null;
-				fail( "unclosed session" );
+			if ( runTest ) {
+				runTestMethod( runMethod );
+				handleUnclosedSession();
 			}
-			else {
-				session = null;
-			}
 		}
-		catch (Throwable e) {
-			try {
-				if ( session != null && session.isOpen() ) {
-					if ( session.isConnected() ) session.connection().rollback();
-					session.close();
+		catch ( Throwable e ) {
+			closeSession( e );
+		}
+	}
+
+	private void setRunTestFlag(Method runMethod) {
+		updateRequiredDialectList( runMethod );
+
+		if ( runForCurrentDialect() ) {
+			runTest = true;
+		}
+		else {
+			log.warn(
+					"Skipping test { "+runMethod.getName()+" }, because test does not apply for dialect { "+Dialect.getDialect().getClass()+" }");
+
+			runTest = false;
+		}
+	}
+
+	private void updateRequiredDialectList(Method runMethod) {
+		requiredDialectList.clear();
+
+		RequiresDialect requiresDialectMethodAnn = runMethod.getAnnotation( RequiresDialect.class );
+		if ( requiresDialectMethodAnn != null ) {
+			Class<? extends Dialect>[] requiredDialects = requiresDialectMethodAnn.value();
+			requiredDialectList.addAll( Arrays.asList( requiredDialects ) );
+		}
+
+		RequiresDialect requiresDialectClassAnn = getClass().getAnnotation( RequiresDialect.class );
+		if ( requiresDialectClassAnn != null ) {
+			Class<? extends Dialect>[] requiredDialects = requiresDialectClassAnn.value();
+			requiredDialectList.addAll( Arrays.asList( requiredDialects ) );
+		}
+	}
+
+	protected boolean runForCurrentDialect() {
+		if ( requiredDialectList.isEmpty() ) {
+			return true;
+		}
+		else {
+			// check whether the current dialect is assignableFrom from any of the specified required dialects.
+			for ( Class<? extends Dialect> dialect : requiredDialectList ) {
+				if ( dialect.isAssignableFrom( Dialect.getDialect().getClass() ) ) {
+					return true;
 				}
 			}
-			catch (Exception ignore) {
+			return false;
+		}
+	}
+
+	private void runTestMethod(Method runMethod) throws Throwable {
+		try {
+			runMethod.invoke( this, new Class[0] );
+		}
+		catch ( InvocationTargetException e ) {
+			e.fillInStackTrace();
+			throw e.getTargetException();
+		}
+		catch ( IllegalAccessException e ) {
+			e.fillInStackTrace();
+			throw e;
+		}
+	}
+
+	private Method findTestMethod() {
+		String fName = getName();
+		assertNotNull( fName );
+		Method runMethod = null;
+		try {
+			runMethod = getClass().getMethod( fName, null );
+		}
+		catch ( NoSuchMethodException e ) {
+			fail( "Method \"" + fName + "\" not found" );
+		}
+		if ( !Modifier.isPublic( runMethod.getModifiers() ) ) {
+			fail( "Method \"" + fName + "\" should be public" );
+		}
+		return runMethod;
+	}
+
+	private void handleUnclosedSession() throws Exception {
+		if ( session != null && session.isOpen() ) {
+			if ( session.isConnected() ) {
+				session.connection().rollback();
 			}
-			try {
-				if ( sessions != null ) {
-					sessions.close();
-					sessions = null;
+			session.close();
+			session = null;
+			fail( "unclosed session" );
+		}
+		else {
+			session = null;
+		}
+	}
+
+	private void closeSession(Throwable e) throws Throwable {
+		try {
+			if ( session != null && session.isOpen() ) {
+				if ( session.isConnected() ) {
+					session.connection().rollback();
 				}
+				session.close();
 			}
-			catch (Exception ignore) {
+		}
+		catch ( Exception ignore ) {
+		}
+		try {
+			if ( sessions != null ) {
+				sessions.close();
+				sessions = null;
 			}
-			throw e;
 		}
+		catch ( Exception ignore ) {
+		}
+		throw e;
 	}
 
 	public Session openSession() throws HibernateException {
@@ -103,18 +237,18 @@
 	}
 
 	public Session openSession(Interceptor interceptor) throws HibernateException {
-		session = getSessions().openSession(interceptor);
+		session = getSessions().openSession( interceptor );
 		return session;
 	}
 
-	protected abstract Class[] getMappings();
+	protected abstract Class<?>[] getMappings();
 
 	protected String[] getAnnotatedPackages() {
-		return new String[]{};
+		return new String[] { };
 	}
 
 	protected String[] getXmlFiles() {
-		return new String[]{};
+		return new String[] { };
 	}
 
 	private void setSessions(SessionFactory sessions) {
@@ -125,12 +259,9 @@
 		return sessions;
 	}
 
-	private void setDialect(Dialect dialect) {
-		TestCase.dialect = dialect;
-	}
 
 	protected Dialect getDialect() {
-		return dialect;
+		return Dialect.getDialect();
 	}
 
 	protected static void setCfg(AnnotationConfiguration cfg) {
@@ -142,13 +273,15 @@
 	}
 
 	protected void configure(Configuration cfg) {
-		//cfg.setNamingStrategy( AlternativeNamingStrategy.INSTANCE );
-		//cfg.getSessionEventListenerConfig().setFlushEventListener( new EJB3FlushEventListener() );
-		//cfg.getSessionEventListenerConfig().setAutoFlushEventListener( new EJB3AutoFlushEventListener() );
 	}
 
 	protected boolean recreateSchema() {
 		return true;
 	}
 
+	protected void runSchemaGeneration() {
+		SchemaExport export = new SchemaExport( cfg );
+		export.create( true, true );
+	}
+
 }

Modified: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java	2009-10-13 12:25:56 UTC (rev 17715)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java	2009-10-13 13:23:37 UTC (rev 17716)
@@ -293,6 +293,11 @@
 				"org.hibernate.test.annotations.id"
 		};
 	}
+	//fix for https://jira.jboss.org/jira/browse/JBPAPP-1061
+	@Override
+	protected boolean runForCurrentDialect() {
+		return super.runForCurrentDialect() && getDialect().supportsSequences();
+	}
 
 	@Override
 	protected String[] getXmlFiles() {

Modified: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java	2009-10-13 12:25:56 UTC (rev 17715)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java	2009-10-13 13:23:37 UTC (rev 17716)
@@ -113,6 +113,11 @@
 		s.close();
 	}
 
+	@Override
+	protected boolean runForCurrentDialect() {
+		return super.runForCurrentDialect() && getDialect().supportsExpectedLobUsagePattern();
+	}
+
 	public LobTest(String x) {
 		super( x );
 	}

Modified: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytoone/referencedcolumnname/ManyToOneReferencedColumnNameTest.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytoone/referencedcolumnname/ManyToOneReferencedColumnNameTest.java	2009-10-13 12:25:56 UTC (rev 17715)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytoone/referencedcolumnname/ManyToOneReferencedColumnNameTest.java	2009-10-13 13:23:37 UTC (rev 17716)
@@ -34,6 +34,16 @@
 		s.close();
 		
 	}
+	
+	
+	//fix for https://jira.jboss.org/jira/browse/JBPAPP-1125
+	@Override
+	protected boolean runForCurrentDialect() {
+		return super.runForCurrentDialect() && getDialect().supportsIdentityColumns();
+	}
+
+
+
 	protected Class[] getMappings() {
 		return new Class[] {
 				Item.class,

Modified: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmTest.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmTest.java	2009-10-13 12:25:56 UTC (rev 17715)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmTest.java	2009-10-13 13:23:37 UTC (rev 17716)
@@ -63,19 +63,6 @@
 		s.close();
 	}
 
-	public void testManyToOneAndInterface() throws Exception {
-		Session s = openSession();
-		s.getTransaction().begin();
-		B b = new BImpl();
-		b.setBId( 1 );
-		s.persist( b );
-		Z z = new ZImpl();
-		z.setB( b );
-		s.persist( z );
-		s.flush();
-		s.getTransaction().rollback();
-		s.close();
-	}
 
 	@Override
 	protected void configure(Configuration cfg) {
@@ -87,7 +74,6 @@
 		return new Class[]{
 				PrimeMinister.class,
 				Sky.class,
-				ZImpl.class
 
 		};
 	}
@@ -97,8 +83,6 @@
 		return new String[]{
 				"org/hibernate/test/annotations/xml/hbm/Government.hbm.xml",
 				"org/hibernate/test/annotations/xml/hbm/CloudType.hbm.xml",
-				"org/hibernate/test/annotations/xml/hbm/A.hbm.xml",
-				"org/hibernate/test/annotations/xml/hbm/B.hbm.xml"
 		};
 	}
 }



More information about the hibernate-commits mailing list