[hibernate-commits] Hibernate SVN: r17724 - in annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations: id and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Oct 13 11:48:00 EDT 2009


Author: stliu
Date: 2009-10-13 11:47:59 -0400 (Tue, 13 Oct 2009)
New Revision: 17724

Added:
   annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmWithIdentityTest.java
Modified:
   annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java
   annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java
   annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java
   annotations/branches/v3_4_0_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_4_0_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java	2009-10-13 15:30:21 UTC (rev 17723)
+++ annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/TestCase.java	2009-10-13 15:47:59 UTC (rev 17724)
@@ -11,6 +11,9 @@
 import java.util.HashSet;
 import java.util.Set;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import org.hibernate.HibernateException;
 import org.hibernate.Interceptor;
 import org.hibernate.Session;
@@ -21,22 +24,19 @@
 import org.hibernate.dialect.Dialect;
 import org.hibernate.jdbc.Work;
 import org.hibernate.tool.hbm2ddl.SchemaExport;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * A base class for all tests.
- * 
+ *
  * @author Emmnauel Bernand
  * @author Hardy Ferentschik
  */
 public abstract class TestCase extends junit.framework.TestCase {
 
-	public static final Logger log = LoggerFactory.getLogger(TestCase.class);
+	public static final Logger log = LoggerFactory.getLogger( TestCase.class );
 
 	private static SessionFactory sessions;
 	private static AnnotationConfiguration cfg;
-	private static Dialect dialect;
 	private static Class<?> lastTestClass;
 	private Session session;
 
@@ -46,41 +46,48 @@
 	private Method runMethod = null;
 
 	/**
-	 * Flag indicating whether the test should be skipped.
+	 * Flag indicating whether the test should be run or skipped.
 	 */
-	private boolean skip = false;
+	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();
 	}
 
 	public TestCase(String x) {
-		super(x);
+		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 )
+		if ( getSessions() != null ) {
 			getSessions().close();
+		}
 		try {
-			setCfg(new AnnotationConfiguration());
-			configure(cfg);
+			setCfg( new AnnotationConfiguration() );
+			configure( cfg );
 			if ( recreateSchema() ) {
-				cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
+				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]);
-				getCfg().addInputStream(is);
+			for ( String xmlFile : xmlFiles ) {
+				InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
+				getCfg().addInputStream( is );
 			}
-			setDialect(Dialect.getDialect());
-			setSessions(getCfg().buildSessionFactory( /* new TestInterceptor() */));
-		} catch ( Exception e ) {
+			setSessions( getCfg().buildSessionFactory( /* new TestInterceptor() */ ) );
+		}
+		catch ( Exception e ) {
 			e.printStackTrace();
 			throw e;
 		}
@@ -88,59 +95,85 @@
 
 	protected void setUp() throws Exception {
 		runMethod = findTestMethod();
-		checkSkip(runMethod);
-		if ( !skip ) {
+		setRunTestFlag( runMethod );
+		if ( runTest ) {
 			if ( getSessions() == null || lastTestClass != getClass() ) {
-				buildSessionFactory(getMappings(), getAnnotatedPackages(), getXmlFiles());
+				buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
 				lastTestClass = getClass();
-			} else {
+			}
+			else {
 				runSchemaGeneration();
 			}
 		}
 	}
-	
+
 	protected void runTest() throws Throwable {
 		try {
-			if ( !skip ) {
-				runTestMethod(runMethod);
+			if ( runTest ) {
+				runTestMethod( runMethod );
 				handleUnclosedSession();
 			}
-		} catch ( Throwable e ) {
-			closeSession(e);
 		}
+		catch ( Throwable e ) {
+			closeSession( e );
+		}
 	}
 
-	private void checkSkip( Method runMethod ) {
-		Set<Class<? extends Dialect>> dialectList = new HashSet<Class<? extends Dialect>>();
+	private void setRunTestFlag(Method runMethod) {
+		updateRequiredDialectList( runMethod );
 
-		RequiresDialect requiresDialectMethodAnn = runMethod.getAnnotation(RequiresDialect.class);
+		if ( runForCurrentDialect() ) {
+			runTest = true;
+		}
+		else {
+			log.warn(
+					"Skipping test {}, because test does not apply for dialect {}", runMethod.getName(), 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();
-			dialectList.addAll(Arrays.asList(requiredDialects));
+			requiredDialectList.addAll( Arrays.asList( requiredDialects ) );
 		}
 
-		RequiresDialect requiresDialectClassAnn = getClass().getAnnotation(RequiresDialect.class);
+		RequiresDialect requiresDialectClassAnn = getClass().getAnnotation( RequiresDialect.class );
 		if ( requiresDialectClassAnn != null ) {
 			Class<? extends Dialect>[] requiredDialects = requiresDialectClassAnn.value();
-			dialectList.addAll(Arrays.asList(requiredDialects));
+			requiredDialectList.addAll( Arrays.asList( requiredDialects ) );
 		}
+	}
 
-		if ( dialectList.isEmpty() || dialectList.contains(Dialect.getDialect().getClass()) ) {
-			skip = false;
-		} else {
-			log.warn("Skipping test {}, because test does not apply for dialect {}", runMethod.getName(), Dialect
-					.getDialect().getClass());
-			skip = true;
+	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;
+				}
+			}
+			return false;
+		}
 	}
 
-	private void runTestMethod( Method runMethod ) throws Throwable, IllegalAccessException {
+	private void runTestMethod(Method runMethod) throws Throwable {
 		try {
-			runMethod.invoke(this, new Class[0]);
-		} catch ( InvocationTargetException e ) {
+			runMethod.invoke( this, new Class[0] );
+		}
+		catch ( InvocationTargetException e ) {
 			e.fillInStackTrace();
 			throw e.getTargetException();
-		} catch ( IllegalAccessException e ) {
+		}
+		catch ( IllegalAccessException e ) {
 			e.fillInStackTrace();
 			throw e;
 		}
@@ -148,47 +181,53 @@
 
 	private Method findTestMethod() {
 		String fName = getName();
-		assertNotNull(fName);
+		assertNotNull( fName );
 		Method runMethod = null;
 		try {
-			runMethod = getClass().getMethod(fName, null);
-		} catch ( NoSuchMethodException e ) {
-			fail("Method \"" + fName + "\" not found");
+			runMethod = getClass().getMethod( fName, null );
 		}
-		if ( !Modifier.isPublic(runMethod.getModifiers()) ) {
-			fail("Method \"" + fName + "\" should be public");
+		catch ( NoSuchMethodException e ) {
+			fail( "Method \"" + fName + "\" not found" );
 		}
+		if ( !Modifier.isPublic( runMethod.getModifiers() ) ) {
+			fail( "Method \"" + fName + "\" should be public" );
+		}
 		return runMethod;
 	}
 
 	private void handleUnclosedSession() {
 		if ( session != null && session.isOpen() ) {
-			if ( session.isConnected() )
-				session.doWork(new RollbackWork());
+			if ( session.isConnected() ) {
+				session.doWork( new RollbackWork() );
+			}
 			session.close();
 			session = null;
-			fail("unclosed session");
-		} else {
+			fail( "unclosed session" );
+		}
+		else {
 			session = null;
 		}
 	}
 
-	private void closeSession( Throwable e ) throws Throwable {
+	private void closeSession(Throwable e) throws Throwable {
 		try {
 			if ( session != null && session.isOpen() ) {
-				if ( session.isConnected() )
-					session.doWork(new RollbackWork());
+				if ( session.isConnected() ) {
+					session.doWork( new RollbackWork() );
+				}
 				session.close();
 			}
-		} catch ( Exception ignore ) {
 		}
+		catch ( Exception ignore ) {
+		}
 		try {
 			if ( sessions != null ) {
 				sessions.close();
 				sessions = null;
 			}
-		} catch ( Exception ignore ) {
 		}
+		catch ( Exception ignore ) {
+		}
 		throw e;
 	}
 
@@ -197,22 +236,22 @@
 		return session;
 	}
 
-	public Session openSession( Interceptor interceptor ) throws HibernateException {
-		session = getSessions().openSession(interceptor);
+	public Session openSession(Interceptor interceptor) throws HibernateException {
+		session = getSessions().openSession( interceptor );
 		return session;
 	}
 
 	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 ) {
+	private void setSessions(SessionFactory sessions) {
 		TestCase.sessions = sessions;
 	}
 
@@ -220,15 +259,11 @@
 		return sessions;
 	}
 
-	private void setDialect( Dialect dialect ) {
-		TestCase.dialect = dialect;
-	}
-
 	protected Dialect getDialect() {
-		return dialect;
+		return Dialect.getDialect();
 	}
 
-	protected static void setCfg( AnnotationConfiguration cfg ) {
+	protected static void setCfg(AnnotationConfiguration cfg) {
 		TestCase.cfg = cfg;
 	}
 
@@ -236,7 +271,7 @@
 		return cfg;
 	}
 
-	protected void configure( Configuration cfg ) {
+	protected void configure(Configuration cfg) {
 	}
 
 	protected boolean recreateSchema() {
@@ -244,13 +279,13 @@
 	}
 
 	protected void runSchemaGeneration() {
-		SchemaExport export = new SchemaExport(cfg);
-		export.create(true, true);
+		SchemaExport export = new SchemaExport( cfg );
+		export.create( true, true );
 	}
 
 	public class RollbackWork implements Work {
 
-		public void execute( Connection connection ) throws SQLException {
+		public void execute(Connection connection) throws SQLException {
 			connection.rollback();
 		}
 	}

Modified: annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java	2009-10-13 15:30:21 UTC (rev 17723)
+++ annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/id/IdTest.java	2009-10-13 15:47:59 UTC (rev 17724)
@@ -3,9 +3,7 @@
 
 import org.hibernate.Session;
 import org.hibernate.Transaction;
-import org.hibernate.dialect.HSQLDialect;
 import org.hibernate.mapping.Column;
-import org.hibernate.test.annotations.RequiresDialect;
 import org.hibernate.test.annotations.TestCase;
 import org.hibernate.test.annotations.id.entities.Ball;
 import org.hibernate.test.annotations.id.entities.BreakDance;
@@ -29,7 +27,6 @@
  * @author Emmanuel Bernard
  */
 @SuppressWarnings("unchecked")
- at RequiresDialect(HSQLDialect.class)
 public class IdTest extends TestCase {
 	// FIXME split Sequence and Id tests to explicit the run failure on Oracle etc
 	public void testGenericGenerator() throws Exception {

Modified: annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java	2009-10-13 15:30:21 UTC (rev 17723)
+++ annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/lob/LobTest.java	2009-10-13 15:47:59 UTC (rev 17724)
@@ -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_4_0_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmTest.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmTest.java	2009-10-13 15:30:21 UTC (rev 17723)
+++ annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmTest.java	2009-10-13 15:47:59 UTC (rev 17724)
@@ -63,31 +63,11 @@
 		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) {
-		super.configure( cfg );
-		//cfg.addClass( Government.class );
-	}
-
 	protected Class[] getMappings() {
 		return new Class[]{
 				PrimeMinister.class,
 				Sky.class,
-				ZImpl.class
 
 		};
 	}
@@ -97,8 +77,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"
 		};
 	}
 }

Added: annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmWithIdentityTest.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmWithIdentityTest.java	                        (rev 0)
+++ annotations/branches/v3_4_0_GA_CP/src/test/org/hibernate/test/annotations/xml/hbm/HbmWithIdentityTest.java	2009-10-13 15:47:59 UTC (rev 17724)
@@ -0,0 +1,51 @@
+//$Id:HbmTest.java 9793 2006-04-26 02:20:18 -0400 (mer., 26 avr. 2006) epbernard $
+package org.hibernate.test.annotations.xml.hbm;
+
+import java.util.HashSet;
+
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class HbmWithIdentityTest extends TestCase {
+
+	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 boolean runForCurrentDialect() {
+		return super.runForCurrentDialect() && getDialect().supportsIdentityColumns();
+	}
+
+
+
+	protected Class[] getMappings() {
+		return new Class[]{
+				ZImpl.class
+		};
+	}
+
+	@Override
+	protected String[] getXmlFiles() {
+		return new String[]{
+				"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