HHH-5942 - Migrate to JUnit 4

This commit is contained in:
Steve Ebersole 2011-03-17 18:20:50 -05:00
parent a4562f4da1
commit 0a908cb518
4 changed files with 32 additions and 38 deletions

View File

@ -34,6 +34,8 @@
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.hql.ast.ASTQueryTranslatorFactory; import org.hibernate.hql.ast.ASTQueryTranslatorFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.hibernate.testing.FailureExpected; import org.hibernate.testing.FailureExpected;
@ -75,12 +77,7 @@ public void configure(Configuration cfg) {
cfg.setProperty( Environment.QUERY_TRANSLATOR, ASTQueryTranslatorFactory.class.getName() ); cfg.setProperty( Environment.QUERY_TRANSLATOR, ASTQueryTranslatorFactory.class.getName() );
} }
@Override private void createData() {
protected void prepareTest() {
cleanupData();
}
protected void createData() {
stateProvince = new StateProvince(); stateProvince = new StateProvince();
stateProvince.setName( "IL" ); stateProvince.setName( "IL" );
@ -156,7 +153,7 @@ protected void createData() {
zoosWithSameAddress.add( zoo2 ); zoosWithSameAddress.add( zoo2 );
} }
protected void cleanupData() { private void cleanupData() {
Session s = openSession(); Session s = openSession();
Transaction t = s.beginTransaction(); Transaction t = s.beginTransaction();
if ( zoo1 != null ) { if ( zoo1 != null ) {

View File

@ -61,7 +61,6 @@
import org.hibernate.testing.OnFailure; import org.hibernate.testing.OnFailure;
import org.hibernate.testing.SkipLog; import org.hibernate.testing.SkipLog;
import static org.hibernate.testing.TestLogger.LOG;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -110,7 +109,6 @@ protected org.hibernate.classic.Session openSession(Interceptor interceptor) thr
@BeforeClassOnce @BeforeClassOnce
private void buildSessionFactory() { private void buildSessionFactory() {
LOG.trace( "Building session factory" );
configuration = buildConfiguration(); configuration = buildConfiguration();
serviceRegistry = buildServiceRegistry( configuration ); serviceRegistry = buildServiceRegistry( configuration );
sessionFactory = (SessionFactoryImplementor) configuration.buildSessionFactory( serviceRegistry ); sessionFactory = (SessionFactoryImplementor) configuration.buildSessionFactory( serviceRegistry );
@ -273,7 +271,6 @@ private void releaseSessionFactory() {
if ( sessionFactory == null ) { if ( sessionFactory == null ) {
return; return;
} }
LOG.trace( "Releasing session factory" );
sessionFactory.close(); sessionFactory.close();
sessionFactory = null; sessionFactory = null;
configuration = null; configuration = null;
@ -282,12 +279,11 @@ private void releaseSessionFactory() {
@OnFailure @OnFailure
@OnExpectedFailure @OnExpectedFailure
public void onFailure() { public void onFailure() {
LOG.trace( "Processing failure-expected ignore" ); // cleanupSession();
if ( ! rebuildSessionFactoryOnError() ) {
return;
}
rebuildSessionFactory(); if ( rebuildSessionFactoryOnError() ) {
rebuildSessionFactory();
}
} }
protected void rebuildSessionFactory() { protected void rebuildSessionFactory() {
@ -307,6 +303,7 @@ protected void rebuildSessionFactory() {
@Before @Before
public final void beforeTest() throws Exception { public final void beforeTest() throws Exception {
System.out.println( " IN @Before CALLBACK!" );
prepareTest(); prepareTest();
} }
@ -315,21 +312,22 @@ protected void prepareTest() throws Exception {
@After @After
public final void afterTest() throws Exception { public final void afterTest() throws Exception {
System.out.println( " IN @After CALLBACK!" );
cleanupTest(); cleanupTest();
cleanupSession();
assertAllDataRemoved();
}
private void cleanupSession() {
if ( session != null && ! ( (SessionImplementor) session ).isClosed() ) { if ( session != null && ! ( (SessionImplementor) session ).isClosed() ) {
if ( session.isConnected() ) { if ( session.isConnected() ) {
session.doWork( new RollbackWork() ); session.doWork( new RollbackWork() );
} }
session.close(); session.close();
session = null;
LOG.debug( "unclosed session" );
} }
else { session = null;
session = null;
}
assertAllDataRemoved();
} }
public class RollbackWork implements Work { public class RollbackWork implements Work {

View File

@ -90,20 +90,12 @@ protected Statement withAfterClasses(Statement statement) {
); );
} }
@Override @Override
protected Statement methodInvoker(FrameworkMethod method, final Object test) { protected Statement methodBlock(FrameworkMethod method) {
final Statement originalMethodBlock = super.methodBlock( method );
final ExtendedFrameworkMethod extendedFrameworkMethod = (ExtendedFrameworkMethod) method; final ExtendedFrameworkMethod extendedFrameworkMethod = (ExtendedFrameworkMethod) method;
return new FailureExpectedHandler( originalMethodBlock, testClassMetadata, extendedFrameworkMethod, testInstance );
final Statement invoker = super.methodInvoker( method, test );
return new TestMethodInvoker( invoker, testClassMetadata, extendedFrameworkMethod, test );
}
public static class FailureExpectedTestPassedException extends Exception {
public FailureExpectedTestPassedException(FrameworkMethod frameworkMethod) {
super( "Test marked as FailureExpected, but did not fail : " + Helper.extractTestName( frameworkMethod ) );
}
} }
private Object testInstance; private Object testInstance;

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.testing.junit4; package org.hibernate.testing.junit4;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement; import org.junit.runners.model.Statement;
import org.hibernate.testing.FailureExpected; import org.hibernate.testing.FailureExpected;
@ -31,13 +32,13 @@
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
class TestMethodInvoker extends Statement { class FailureExpectedHandler extends Statement {
private final TestClassMetadata testClassMetadata; private final TestClassMetadata testClassMetadata;
private final ExtendedFrameworkMethod extendedFrameworkMethod; private final ExtendedFrameworkMethod extendedFrameworkMethod;
private final Statement realInvoker; private final Statement realInvoker;
private final Object testInstance; private final Object testInstance;
public TestMethodInvoker( public FailureExpectedHandler(
Statement realInvoker, Statement realInvoker,
TestClassMetadata testClassMetadata, TestClassMetadata testClassMetadata,
ExtendedFrameworkMethod extendedFrameworkMethod, ExtendedFrameworkMethod extendedFrameworkMethod,
@ -55,10 +56,10 @@ public void evaluate() throws Throwable {
realInvoker.evaluate(); realInvoker.evaluate();
// reaching here is expected, unless the test is marked as an expected failure // reaching here is expected, unless the test is marked as an expected failure
if ( failureExpected != null ) { if ( failureExpected != null ) {
throw new CustomRunner.FailureExpectedTestPassedException( extendedFrameworkMethod ); throw new FailureExpectedTestPassedException( extendedFrameworkMethod );
} }
} }
catch (CustomRunner.FailureExpectedTestPassedException e) { catch (FailureExpectedTestPassedException e) {
// just pass this along // just pass this along
throw e; throw e;
} }
@ -81,4 +82,10 @@ public void evaluate() throws Throwable {
} }
} }
} }
public static class FailureExpectedTestPassedException extends Exception {
public FailureExpectedTestPassedException(FrameworkMethod frameworkMethod) {
super( "Test marked as FailureExpected, but did not fail : " + Helper.extractTestName( frameworkMethod ) );
}
}
} }