From 0a908cb5181a430b4b3bc32ef3122dd0411e1d56 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 17 Mar 2011 18:20:50 -0500 Subject: [PATCH] HHH-5942 - Migrate to JUnit 4 --- .../test/hql/ASTParserLoadingOrderByTest.java | 11 +++----- .../junit4/BaseCoreFunctionalTestCase.java | 28 +++++++++---------- .../testing/junit4/CustomRunner.java | 16 +++-------- ...voker.java => FailureExpectedHandler.java} | 15 +++++++--- 4 files changed, 32 insertions(+), 38 deletions(-) rename hibernate-testing/src/main/java/org/hibernate/testing/junit4/{TestMethodInvoker.java => FailureExpectedHandler.java} (83%) diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingOrderByTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingOrderByTest.java index 698061ae59..3d0ae5a8de 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingOrderByTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingOrderByTest.java @@ -34,6 +34,8 @@ import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.hql.ast.ASTQueryTranslatorFactory; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.hibernate.testing.FailureExpected; @@ -75,12 +77,7 @@ public class ASTParserLoadingOrderByTest extends BaseCoreFunctionalTestCase { cfg.setProperty( Environment.QUERY_TRANSLATOR, ASTQueryTranslatorFactory.class.getName() ); } - @Override - protected void prepareTest() { - cleanupData(); - } - - protected void createData() { + private void createData() { stateProvince = new StateProvince(); stateProvince.setName( "IL" ); @@ -156,7 +153,7 @@ public class ASTParserLoadingOrderByTest extends BaseCoreFunctionalTestCase { zoosWithSameAddress.add( zoo2 ); } - protected void cleanupData() { + private void cleanupData() { Session s = openSession(); Transaction t = s.beginTransaction(); if ( zoo1 != null ) { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java index a4cc77c16b..3d43a69787 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java @@ -61,7 +61,6 @@ import org.hibernate.testing.OnExpectedFailure; import org.hibernate.testing.OnFailure; import org.hibernate.testing.SkipLog; -import static org.hibernate.testing.TestLogger.LOG; import static org.junit.Assert.fail; /** @@ -110,7 +109,6 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { @BeforeClassOnce private void buildSessionFactory() { - LOG.trace( "Building session factory" ); configuration = buildConfiguration(); serviceRegistry = buildServiceRegistry( configuration ); sessionFactory = (SessionFactoryImplementor) configuration.buildSessionFactory( serviceRegistry ); @@ -273,7 +271,6 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { if ( sessionFactory == null ) { return; } - LOG.trace( "Releasing session factory" ); sessionFactory.close(); sessionFactory = null; configuration = null; @@ -282,12 +279,11 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { @OnFailure @OnExpectedFailure public void onFailure() { - LOG.trace( "Processing failure-expected ignore" ); - if ( ! rebuildSessionFactoryOnError() ) { - return; - } +// cleanupSession(); - rebuildSessionFactory(); + if ( rebuildSessionFactoryOnError() ) { + rebuildSessionFactory(); + } } protected void rebuildSessionFactory() { @@ -307,6 +303,7 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { @Before public final void beforeTest() throws Exception { + System.out.println( " IN @Before CALLBACK!" ); prepareTest(); } @@ -315,21 +312,22 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { @After public final void afterTest() throws Exception { + System.out.println( " IN @After CALLBACK!" ); cleanupTest(); + cleanupSession(); + + assertAllDataRemoved(); + } + + private void cleanupSession() { if ( session != null && ! ( (SessionImplementor) session ).isClosed() ) { if ( session.isConnected() ) { session.doWork( new RollbackWork() ); } session.close(); - session = null; - LOG.debug( "unclosed session" ); } - else { - session = null; - } - - assertAllDataRemoved(); + session = null; } public class RollbackWork implements Work { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java index 6efd925966..5e9363d41b 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java @@ -90,20 +90,12 @@ public class CustomRunner extends BlockJUnit4ClassRunner { ); } + @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 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 ) ); - } + return new FailureExpectedHandler( originalMethodBlock, testClassMetadata, extendedFrameworkMethod, testInstance ); } private Object testInstance; diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/TestMethodInvoker.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/FailureExpectedHandler.java similarity index 83% rename from hibernate-testing/src/main/java/org/hibernate/testing/junit4/TestMethodInvoker.java rename to hibernate-testing/src/main/java/org/hibernate/testing/junit4/FailureExpectedHandler.java index ce2fda5b0e..40d6bc808d 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/TestMethodInvoker.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/FailureExpectedHandler.java @@ -23,6 +23,7 @@ */ package org.hibernate.testing.junit4; +import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; import org.hibernate.testing.FailureExpected; @@ -31,13 +32,13 @@ import org.hibernate.testing.TestLogger; /** * @author Steve Ebersole */ -class TestMethodInvoker extends Statement { +class FailureExpectedHandler extends Statement { private final TestClassMetadata testClassMetadata; private final ExtendedFrameworkMethod extendedFrameworkMethod; private final Statement realInvoker; private final Object testInstance; - public TestMethodInvoker( + public FailureExpectedHandler( Statement realInvoker, TestClassMetadata testClassMetadata, ExtendedFrameworkMethod extendedFrameworkMethod, @@ -55,10 +56,10 @@ class TestMethodInvoker extends Statement { realInvoker.evaluate(); // reaching here is expected, unless the test is marked as an expected failure if ( failureExpected != null ) { - throw new CustomRunner.FailureExpectedTestPassedException( extendedFrameworkMethod ); + throw new FailureExpectedTestPassedException( extendedFrameworkMethod ); } } - catch (CustomRunner.FailureExpectedTestPassedException e) { + catch (FailureExpectedTestPassedException e) { // just pass this along throw e; } @@ -81,4 +82,10 @@ class TestMethodInvoker extends Statement { } } } + + public static class FailureExpectedTestPassedException extends Exception { + public FailureExpectedTestPassedException(FrameworkMethod frameworkMethod) { + super( "Test marked as FailureExpected, but did not fail : " + Helper.extractTestName( frameworkMethod ) ); + } + } }