Made sure that the the superclass dialect is checked.
Did not apply provided patch though.

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17692 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Hardy Ferentschik 2009-10-12 14:08:37 +00:00
parent ea91bf00a0
commit a8047fe21a
2 changed files with 126 additions and 90 deletions

View File

@ -11,6 +11,9 @@ import java.util.Arrays;
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,18 +24,16 @@ import org.hibernate.cfg.Environment;
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;
@ -46,27 +47,34 @@ public abstract class TestCase extends junit.framework.TestCase {
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 ( String aPackage : packages ) {
getCfg().addPackage( aPackage );
@ -78,9 +86,10 @@ public abstract class TestCase extends junit.framework.TestCase {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
getCfg().addInputStream( is );
}
setDialect(Dialect.getDialect());
setSessions(getCfg().buildSessionFactory( /* new TestInterceptor() */));
} catch ( Exception e ) {
setDialect( Dialect.getDialect() );
setSessions( getCfg().buildSessionFactory( /* new TestInterceptor() */ ) );
}
catch ( Exception e ) {
e.printStackTrace();
throw e;
}
@ -88,63 +97,85 @@ public abstract class TestCase extends junit.framework.TestCase {
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())) && appliesTo(Dialect.getDialect()) ) {
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;
}
}
protected boolean appliesTo( Dialect dialect ) {
return true;
}
private void runTestMethod( Method runMethod ) throws Throwable {
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;
}
@ -152,46 +183,52 @@ public abstract class TestCase extends junit.framework.TestCase {
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;
}
@ -201,22 +238,22 @@ public abstract class TestCase extends junit.framework.TestCase {
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;
}
@ -224,7 +261,7 @@ public abstract class TestCase extends junit.framework.TestCase {
return sessions;
}
private void setDialect( Dialect dialect ) {
private void setDialect(Dialect dialect) {
TestCase.dialect = dialect;
}
@ -232,7 +269,7 @@ public abstract class TestCase extends junit.framework.TestCase {
return dialect;
}
protected static void setCfg( AnnotationConfiguration cfg ) {
protected static void setCfg(AnnotationConfiguration cfg) {
TestCase.cfg = cfg;
}
@ -240,7 +277,7 @@ public abstract class TestCase extends junit.framework.TestCase {
return cfg;
}
protected void configure( Configuration cfg ) {
protected void configure(Configuration cfg) {
}
protected boolean recreateSchema() {
@ -248,13 +285,13 @@ public abstract class TestCase extends junit.framework.TestCase {
}
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();
}
}

View File

@ -3,7 +3,6 @@ package org.hibernate.test.annotations.lob;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.dialect.Dialect;
import org.hibernate.test.annotations.TestCase;
/**
@ -15,7 +14,7 @@ public class LobTest extends TestCase {
Editor editor = new Editor();
editor.setName( "O'Reilly" );
book.setEditor( editor );
book.setCode2( new char[]{'r'} );
book.setCode2( new char[] { 'r' } );
Session s;
Transaction tx;
s = openSession();
@ -25,7 +24,7 @@ public class LobTest extends TestCase {
s.close();
s = openSession();
tx = s.beginTransaction();
Book loadedBook = (Book) s.get( Book.class, book.getId() );
Book loadedBook = ( Book ) s.get( Book.class, book.getId() );
assertNotNull( loadedBook.getEditor() );
assertEquals( book.getEditor().getName(), loadedBook.getEditor().getName() );
loadedBook.setEditor( null );
@ -33,7 +32,7 @@ public class LobTest extends TestCase {
s.close();
s = openSession();
tx = s.beginTransaction();
loadedBook = (Book) s.get( Book.class, book.getId() );
loadedBook = ( Book ) s.get( Book.class, book.getId() );
assertNull( loadedBook.getEditor() );
tx.commit();
s.close();
@ -48,15 +47,15 @@ public class LobTest extends TestCase {
Book b = new Book();
b.setShortDescription( "Hibernate Bible" );
b.setFullText( "Hibernate in Action aims to..." );
b.setCode( new Character[]{'a', 'b', 'c'} );
b.setCode2( new char[]{'a', 'b', 'c'} );
b.setCode( new Character[] { 'a', 'b', 'c' } );
b.setCode2( new char[] { 'a', 'b', 'c' } );
s.persist( b );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
Book b2 = (Book) s.get( Book.class, b.getId() );
Book b2 = ( Book ) s.get( Book.class, b.getId() );
assertNotNull( b2 );
assertEquals( b2.getFullText(), b.getFullText() );
assertEquals( b2.getCode()[1].charValue(), b.getCode()[1].charValue() );
@ -72,13 +71,13 @@ public class LobTest extends TestCase {
tx = s.beginTransaction();
CompiledCode cc = new CompiledCode();
Byte[] header = new Byte[2];
header[0] = new Byte( (byte) 3 );
header[1] = new Byte( (byte) 0 );
header[0] = new Byte( ( byte ) 3 );
header[1] = new Byte( ( byte ) 0 );
cc.setHeader( header );
int codeSize = 5;
byte[] full = new byte[codeSize];
for ( int i = 0; i < codeSize ; i++ ) {
full[i] = (byte) ( 1 + i );
for ( int i = 0; i < codeSize; i++ ) {
full[i] = ( byte ) ( 1 + i );
}
cc.setFullCode( full );
s.persist( cc );
@ -86,7 +85,7 @@ public class LobTest extends TestCase {
s.close();
s = openSession();
tx = s.beginTransaction();
CompiledCode recompiled = (CompiledCode) s.get( CompiledCode.class, cc.getId() );
CompiledCode recompiled = ( CompiledCode ) s.get( CompiledCode.class, cc.getId() );
assertEquals( recompiled.getHeader()[1], cc.getHeader()[1] );
assertEquals( recompiled.getFullCode()[codeSize - 1], cc.getFullCode()[codeSize - 1] );
tx.commit();
@ -100,15 +99,15 @@ public class LobTest extends TestCase {
tx = s.beginTransaction();
CompiledCode cc = new CompiledCode();
byte[] metadata = new byte[2];
metadata[0] = (byte) 3;
metadata[1] = (byte) 0;
metadata[0] = ( byte ) 3;
metadata[1] = ( byte ) 0;
cc.setMetadata( metadata );
s.persist( cc );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
CompiledCode recompiled = (CompiledCode) s.get( CompiledCode.class, cc.getId() );
CompiledCode recompiled = ( CompiledCode ) s.get( CompiledCode.class, cc.getId() );
assertEquals( recompiled.getMetadata()[1], cc.getMetadata()[1] );
tx.commit();
s.close();
@ -117,14 +116,14 @@ public class LobTest extends TestCase {
public LobTest(String x) {
super( x );
}
@Override
protected boolean appliesTo(Dialect dialect) {
return dialect.supportsExpectedLobUsagePattern();
protected boolean runForCurrentDialect() {
return super.runForCurrentDialect() && getDialect().supportsExpectedLobUsagePattern();
}
protected Class[] getMappings() {
return new Class[]{
return new Class[] {
Book.class,
CompiledCode.class
};