AbstractDatabaseTester.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 org.dbunit.assertion.DefaultFailureHandler;
  23. import org.dbunit.assertion.SimpleAssert;
  24. import org.dbunit.database.IDatabaseConnection;
  25. import org.dbunit.dataset.IDataSet;
  26. import org.dbunit.operation.DatabaseOperation;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;

  29. /**
  30.  * Basic implementation of IDatabaseTester.<br>
  31.  * Implementations of IDatabaseTester may use this class as a starting point.
  32.  *
  33.  * @author Andres Almiray (aalmiray@users.sourceforge.net)
  34.  * @author Last changed by: $Author$
  35.  * @version $Revision$ $Date$
  36.  * @since 2.2.0
  37.  */
  38. public abstract class AbstractDatabaseTester extends SimpleAssert implements IDatabaseTester
  39. {
  40.     /**
  41.      * Logger for this class
  42.      */
  43.     private static final Logger logger = LoggerFactory.getLogger(AbstractDatabaseTester.class);

  44.     /**
  45.      * Enumeration of the valid {@link OperationType}s
  46.      */
  47.     private static final class OperationType
  48.     {
  49.         public static final OperationType SET_UP = new OperationType("setUp");
  50.         public static final OperationType TEAR_DOWN = new OperationType("tearDown");

  51.         private final String key;

  52.         private OperationType(String key)
  53.         {
  54.             this.key = key;
  55.         }

  56.         @Override
  57.         public String toString()
  58.         {
  59.             return "OperationType: " + key;
  60.         }
  61.     }

  62.     private IDataSet dataSet;
  63.     private String schema;
  64.     private DatabaseOperation setUpOperation = DatabaseOperation.CLEAN_INSERT;
  65.     private DatabaseOperation tearDownOperation = DatabaseOperation.NONE;
  66.     private IOperationListener operationListener;

  67.     public AbstractDatabaseTester()
  68.     {
  69.         this(null);
  70.     }

  71.     /**
  72.      * @param schema
  73.      *            The schema to be tested. Can be <code>null</code>
  74.      * @since 2.4.3
  75.      */
  76.     public AbstractDatabaseTester(String schema)
  77.     {
  78.         super(new DefaultFailureHandler());
  79.         this.schema = schema;
  80.     }

  81.     public void closeConnection(IDatabaseConnection connection) throws Exception
  82.     {
  83.         logger.debug("closeConnection(connection={}) - start", connection);

  84.         connection.close();
  85.     }

  86.     public IDataSet getDataSet()
  87.     {
  88.         logger.debug("getDataSet() - start");

  89.         return dataSet;
  90.     }

  91.     public void onSetup() throws Exception
  92.     {
  93.         logger.debug("onSetup() - start");
  94.         executeOperation(getSetUpOperation(), OperationType.SET_UP);
  95.     }

  96.     public void onTearDown() throws Exception
  97.     {
  98.         logger.debug("onTearDown() - start");
  99.         executeOperation(getTearDownOperation(), OperationType.TEAR_DOWN);
  100.     }

  101.     public void setDataSet(IDataSet dataSet)
  102.     {
  103.         logger.debug("setDataSet(dataSet={}) - start", dataSet);

  104.         this.dataSet = dataSet;
  105.     }

  106.     public void setSchema(String schema)
  107.     {
  108.         logger.debug("setSchema(schema={}) - start", schema);

  109.         logger.warn("setSchema() should not be used anymore");
  110.         this.schema = schema;
  111.     }

  112.     public void setSetUpOperation(DatabaseOperation setUpOperation)
  113.     {
  114.         logger.debug("setSetUpOperation(setUpOperation={}) - start", setUpOperation);

  115.         this.setUpOperation = setUpOperation;
  116.     }

  117.     public void setTearDownOperation(DatabaseOperation tearDownOperation)
  118.     {
  119.         logger.debug("setTearDownOperation(tearDownOperation={}) - start", tearDownOperation);

  120.         this.tearDownOperation = tearDownOperation;
  121.     }

  122.     /**
  123.      * Returns the schema value.
  124.      */
  125.     protected String getSchema()
  126.     {
  127.         logger.trace("getSchema() - start");

  128.         return schema;
  129.     }

  130.     /**
  131.      * Returns the DatabaseOperation to call when starting the test.
  132.      */
  133.     public DatabaseOperation getSetUpOperation()
  134.     {
  135.         logger.trace("getSetUpOperation() - start");

  136.         return setUpOperation;
  137.     }

  138.     /**
  139.      * Returns the DatabaseOperation to call when ending the test.
  140.      */
  141.     public DatabaseOperation getTearDownOperation()
  142.     {
  143.         logger.trace("getTearDownOperation() - start");

  144.         return tearDownOperation;
  145.     }

  146.     /**
  147.      * Executes a DatabaseOperation with a IDatabaseConnection supplied by
  148.      * {@link #getConnection()} and the test dataset.
  149.      */
  150.     private void executeOperation(DatabaseOperation operation, OperationType type) throws Exception
  151.     {
  152.         logger.debug("executeOperation(operation={}) - start", operation);

  153.         if (operation != DatabaseOperation.NONE)
  154.         {
  155.             // Ensure that the operationListener is set
  156.             if (operationListener == null)
  157.             {
  158.                 logger.debug("OperationListener is null and will be defaulted.");
  159.                 operationListener = new DefaultOperationListener();
  160.             }

  161.             IDatabaseConnection connection = getConnection();
  162.             operationListener.connectionRetrieved(connection);

  163.             try
  164.             {
  165.                 operation.execute(connection, getDataSet());
  166.             } finally
  167.             {
  168.                 // Since 2.4.4 the OperationListener is responsible for closing
  169.                 // the connection at the right time
  170.                 if (type == OperationType.SET_UP)
  171.                 {
  172.                     operationListener.operationSetUpFinished(connection);
  173.                 } else if (type == OperationType.TEAR_DOWN)
  174.                 {
  175.                     operationListener.operationTearDownFinished(connection);
  176.                 } else
  177.                 {
  178.                     throw new DatabaseUnitRuntimeException("Cannot happen - unknown OperationType specified: " + type);
  179.                 }
  180.             }
  181.         }
  182.     }

  183.     public void setOperationListener(IOperationListener operationListener)
  184.     {
  185.         logger.debug("setOperationListener(operationListener={}) - start", operationListener);
  186.         this.operationListener = operationListener;
  187.     }

  188.     @Override
  189.     public String toString()
  190.     {
  191.         final StringBuilder sb = new StringBuilder();
  192.         sb.append(getClass().getName()).append("[");
  193.         sb.append("schema=").append(schema);
  194.         sb.append(", dataSet=").append(dataSet);
  195.         sb.append(", setUpOperation=").append(setUpOperation);
  196.         sb.append(", tearDownOperation=").append(tearDownOperation);
  197.         sb.append(", operationListener=").append(operationListener);
  198.         sb.append("]");
  199.         return sb.toString();
  200.     }
  201. }