AbstractDataSet.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.dataset;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. /**
  25.  * This abstract class provides the basic implementation of the IDataSet
  26.  * interface. Subclass are only required to implement the {@link #createIterator}
  27.  * method.
  28.  *
  29.  * @author Manuel Laflamme
  30.  * @author Last changed by: $Author$
  31.  * @version $Revision$ $Date$
  32.  * @since 1.0 (Feb 22, 2002)
  33.  */
  34. public abstract class AbstractDataSet implements IDataSet
  35. {
  36.     //TODO (matthias) Use a DataSetBuilder PLUS IDataSet to avoid this ugly lazy initialization with loads of protected internals a user must know...

  37.     protected OrderedTableNameMap _orderedTableNameMap;

  38.     /**
  39.      * Whether or not table names of this dataset are case sensitive.
  40.      * By default case-sensitivity is set to false for datasets
  41.      */
  42.     private boolean _caseSensitiveTableNames = false;

  43.     /**
  44.      * Logger for this class
  45.      */
  46.     private static final Logger logger = LoggerFactory.getLogger(AbstractDataSet.class);

  47.     /**
  48.      * Default constructor
  49.      */
  50.     public AbstractDataSet()
  51.     {
  52.     }

  53.     /**
  54.      * Constructor
  55.      * @param caseSensitiveTableNames Whether or not table names should be case sensitive
  56.      * @since 2.4
  57.      */
  58.     public AbstractDataSet(boolean caseSensitiveTableNames)
  59.     {
  60.         _caseSensitiveTableNames = caseSensitiveTableNames;
  61.     }

  62.     /**
  63.      * @return <code>true</code> if the case sensitivity of table names is used in this dataset.
  64.      * @since 2.4
  65.      */
  66.     public boolean isCaseSensitiveTableNames()
  67.     {
  68.         return this._caseSensitiveTableNames;
  69.     }

  70.     /**
  71.      * Creates and returns a new instance of the table names container.
  72.      * Implementors should use this method to retrieve a map which stores
  73.      * table names which can be linked with arbitrary objects.
  74.      * @return a new empty instance of the table names container
  75.      * @since 2.4
  76.      */
  77.     protected OrderedTableNameMap createTableNameMap()
  78.     {
  79.         return new OrderedTableNameMap(this._caseSensitiveTableNames);
  80.     }

  81.     /**
  82.      * Initializes the tables of this dataset
  83.      * @throws DataSetException
  84.      * @since 2.4
  85.      */
  86.     protected void initialize() throws DataSetException
  87.     {
  88.         logger.debug("initialize() - start");

  89.         if(_orderedTableNameMap != null)
  90.         {
  91.             logger.debug("The table name map has already been initialized.");
  92.             // already initialized
  93.             return;
  94.         }

  95.         // Gather all tables in the OrderedTableNameMap which also makes the duplicate check
  96.         _orderedTableNameMap = this.createTableNameMap();
  97.         ITableIterator iterator = createIterator(false);
  98.         while (iterator.next())
  99.         {
  100.             ITable table = iterator.getTable();
  101.             _orderedTableNameMap.add(table.getTableMetaData().getTableName(), table);
  102.         }
  103.     }


  104. //    protected ITable[] cloneTables(ITable[] tables)
  105. //    {
  106. //        logger.debug("cloneTables(tables={}) - start", tables);
  107. //
  108. //        ITable[] clones = new ITable[tables.length];
  109. //        for (int i = 0; i < tables.length; i++)
  110. //        {
  111. //            clones[i] = tables[i];
  112. //        }
  113. //        return clones;
  114. //    }

  115.     /**
  116.      * Creates an iterator which provides access to all tables of this dataset
  117.      * @param reversed Whether the created iterator should be a reversed one or not
  118.      * @return The created {@link ITableIterator}
  119.      * @throws DataSetException
  120.      */
  121.     protected abstract ITableIterator createIterator(boolean reversed)
  122.             throws DataSetException;

  123.     ////////////////////////////////////////////////////////////////////////////
  124.     // IDataSet interface

  125.     public String[] getTableNames() throws DataSetException
  126.     {
  127.         logger.debug("getTableNames() - start");

  128.         initialize();

  129.         return this._orderedTableNameMap.getTableNames();
  130.     }

  131.     public ITableMetaData getTableMetaData(String tableName) throws DataSetException
  132.     {
  133.         logger.debug("getTableMetaData(tableName={}) - start", tableName);

  134.         return getTable(tableName).getTableMetaData();
  135.     }

  136.     public ITable getTable(String tableName) throws DataSetException
  137.     {
  138.         logger.debug("getTable(tableName={}) - start", tableName);

  139.         initialize();

  140.         ITable found = (ITable) _orderedTableNameMap.get(tableName);
  141.         if (found != null)
  142.         {
  143.             return found;
  144.         }
  145.         else
  146.         {
  147.             throw new NoSuchTableException(tableName);
  148.         }
  149.     }

  150.     public ITable[] getTables() throws DataSetException
  151.     {
  152.         logger.debug("getTables() - start");

  153.         initialize();

  154.         return (ITable[]) this._orderedTableNameMap.orderedValues().toArray(new ITable[0]);
  155.     }

  156.     public ITableIterator iterator() throws DataSetException
  157.     {
  158.         logger.debug("iterator() - start");

  159.         return createIterator(false);
  160.     }

  161.     public ITableIterator reverseIterator() throws DataSetException
  162.     {
  163.         logger.debug("reverseIterator() - start");

  164.         return createIterator(true);
  165.     }

  166.     ////////////////////////////////////////////////////////////////////////////
  167.     // Object class

  168.     public String toString()
  169.     {
  170.         final StringBuilder sb = new StringBuilder();
  171.         sb.append("AbstractDataSet[");
  172.         sb.append("_orderedTableNameMap=").append(_orderedTableNameMap);
  173.         sb.append("]");
  174.         return sb.toString();
  175.     }

  176. }