DatabaseTestCase.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit;

  22. import static org.junit.jupiter.api.Assertions.assertNotNull;

  23. import org.dbunit.database.DatabaseConfig;
  24. import org.dbunit.database.IDatabaseConnection;
  25. import org.dbunit.dataset.IDataSet;
  26. import org.dbunit.operation.DatabaseOperation;
  27. import org.junit.jupiter.api.extension.InvocationInterceptor;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;

  30. /**
  31.  * Convenience class for writing JUnit tests with dbunit easily.
  32.  * <br />
  33.  * Note that there are some even more convenient classes available such
  34.  * as {@link DBTestCase}.
  35.  *
  36.  * @author Manuel Laflamme
  37.  * @version $Revision$
  38.  * @since 1.0 (Feb 17, 2002)
  39.  */
  40. public abstract class DatabaseTestCase implements InvocationInterceptor {
  41.     private static final Logger logger = LoggerFactory.getLogger(DatabaseTestCase.class);

  42.     private IDatabaseTester tester;

  43.     private IOperationListener operationListener;

  44.     private final String name;

  45.     protected DatabaseTestCase() {
  46.         this.name = null;
  47.     }

  48.     protected DatabaseTestCase(final String name) {
  49.         this.name = name;
  50.     }

  51.     public String getName() {
  52.         return this.name;
  53.     }

  54.     /**
  55.      * Returns the test database connection.
  56.      */
  57.     protected abstract IDatabaseConnection getConnection() throws Exception;

  58.     /**
  59.      * Returns the test dataset.
  60.      */
  61.     protected abstract IDataSet getDataSet() throws Exception;

  62.     /**
  63.      * Creates a IDatabaseTester for this testCase.<br>
  64.      *
  65.      * A {@link DefaultDatabaseTester} is used by default.
  66.      * @throws Exception
  67.      */
  68.     protected IDatabaseTester newDatabaseTester() throws Exception{
  69.         logger.debug("newDatabaseTester() - start");

  70.         final IDatabaseConnection connection = getConnection();
  71.         getOperationListener().connectionRetrieved(connection);
  72.         final IDatabaseTester tester = new DefaultDatabaseTester(connection);
  73.         return tester;
  74.     }

  75.     /**
  76.      * Designed to be overridden by subclasses in order to set additional configuration
  77.      * parameters for the {@link IDatabaseConnection}.
  78.      * @param config The settings of the current {@link IDatabaseConnection} to be configured
  79.      */
  80.     protected void setUpDatabaseConfig(final DatabaseConfig config)
  81.     {
  82.         // Designed to be overridden.
  83.     }

  84.     /**
  85.      * Gets the IDatabaseTester for this testCase.<br>
  86.      * If the IDatabaseTester is not set yet, this method calls
  87.      * newDatabaseTester() to obtain a new instance.
  88.      * @throws Exception
  89.      */
  90.     protected IDatabaseTester getDatabaseTester() throws Exception {
  91.         if ( this.tester == null ) {
  92.             this.tester = newDatabaseTester();
  93.         }
  94.         return this.tester;
  95.     }

  96.     /**
  97.      * Close the specified connection. Override this method of you want to
  98.      * keep your connection alive between tests.
  99.      * @deprecated since 2.4.4 define a user defined {@link #getOperationListener()} in advance
  100.      */
  101.     @Deprecated
  102.     protected void closeConnection(final IDatabaseConnection connection) throws Exception
  103.     {
  104.         logger.debug("closeConnection(connection={}) - start", connection);

  105.         assertNotNull(getDatabaseTester(), "DatabaseTester is not set" );
  106.         getDatabaseTester().closeConnection( connection );
  107.     }

  108.     /**
  109.      * Returns the database operation executed in test setup.
  110.      */
  111.     protected DatabaseOperation getSetUpOperation() throws Exception
  112.     {
  113.         return DatabaseOperation.CLEAN_INSERT;
  114.     }

  115.     /**
  116.      * Returns the database operation executed in test cleanup.
  117.      */
  118.     protected DatabaseOperation getTearDownOperation() throws Exception
  119.     {
  120.         return DatabaseOperation.NONE;
  121.     }

  122.     ////////////////////////////////////////////////////////////////////////////
  123.     // TestCase class

  124.     protected void setUp() throws Exception
  125.     {
  126.         logger.debug("setUp() - start");

  127.         final IDatabaseTester databaseTester = getDatabaseTester();
  128.         assertNotNull(databaseTester, "DatabaseTester is not set");
  129.         databaseTester.setSetUpOperation(getSetUpOperation());
  130.         databaseTester.setDataSet(getDataSet());
  131.         databaseTester.setOperationListener(getOperationListener());
  132.         databaseTester.onSetup();
  133.     }

  134.     protected void tearDown() throws Exception
  135.     {
  136.         logger.debug("tearDown() - start");

  137.         try {
  138.             final IDatabaseTester databaseTester = getDatabaseTester();
  139.             assertNotNull(databaseTester, "DatabaseTester is not set");
  140.             databaseTester.setTearDownOperation(getTearDownOperation());
  141.             databaseTester.setDataSet(getDataSet());
  142.             databaseTester.setOperationListener(getOperationListener());
  143.             databaseTester.onTearDown();
  144.         } finally {
  145.             tester = null;
  146.         }
  147.     }

  148.     /**
  149.      * @return The {@link IOperationListener} to be used by the {@link IDatabaseTester}.
  150.      * @since 2.4.4
  151.      */
  152.     protected IOperationListener getOperationListener()
  153.     {
  154.         logger.debug("getOperationListener() - start");
  155.         if(this.operationListener==null){
  156.             this.operationListener = new DefaultOperationListener(){
  157.                 @Override
  158.                 public void connectionRetrieved(final IDatabaseConnection connection) {
  159.                     super.connectionRetrieved(connection);
  160.                     // When a new connection has been created then invoke the setUp method
  161.                     // so that user defined DatabaseConfig parameters can be set.
  162.                     setUpDatabaseConfig(connection.getConfig());
  163.                 }
  164.             };
  165.         }
  166.         return this.operationListener;
  167.     }
  168. }