DefaultDataSet.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.dbunit.database.AmbiguousTableNameException;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;


  25. /**
  26.  * Simple implementation of a dataset backed by {@link ITable} objects which can
  27.  * be added dynamically.
  28.  *
  29.  * @author Manuel Laflamme
  30.  * @author Last changed by: $Author$
  31.  * @version $Revision$ $Date$
  32.  * @since 1.0 (Feb 18, 2002)
  33.  */
  34. public class DefaultDataSet extends AbstractDataSet
  35. {

  36.     /**
  37.      * Logger for this class
  38.      */
  39.     private static final Logger logger = LoggerFactory.getLogger(DefaultDataSet.class);

  40.     public DefaultDataSet()
  41.     {
  42.         super();
  43.     }

  44.     /**
  45.      * Creates a default dataset which is empty initially
  46.      * @param caseSensitiveTableNames
  47.      * @since 2.4.2
  48.      */
  49.     public DefaultDataSet(boolean caseSensitiveTableNames)
  50.     {
  51.         super(caseSensitiveTableNames);
  52.     }

  53.     public DefaultDataSet(ITable table) throws AmbiguousTableNameException
  54.     {
  55.         this(new ITable[]{table});
  56.     }

  57.     public DefaultDataSet(ITable table1, ITable table2) throws AmbiguousTableNameException
  58.     {
  59.         this(new ITable[] {table1, table2});
  60.     }

  61.     public DefaultDataSet(ITable[] tables) throws AmbiguousTableNameException
  62.     {
  63.         this(tables, false);
  64.     }
  65.    
  66.     /**
  67.      * Creates a default dataset which consists of the given tables
  68.      * @param caseSensitiveTableNames
  69.      * @since 2.4.2
  70.      */
  71.     public DefaultDataSet(ITable[] tables, boolean caseSensitiveTableNames) throws AmbiguousTableNameException
  72.     {
  73.         super(caseSensitiveTableNames);
  74.        
  75.         for (int i = 0; i < tables.length; i++)
  76.         {
  77.             addTable(tables[i]);
  78.         }
  79.     }

  80.     /**
  81.      * Add a new table in this dataset.
  82.      * @throws AmbiguousTableNameException
  83.      */
  84.     public void addTable(ITable table) throws AmbiguousTableNameException
  85.     {
  86.         logger.debug("addTable(table={}) - start", table);
  87.        
  88.         this.initialize();
  89.        
  90.         super._orderedTableNameMap.add(table.getTableMetaData().getTableName(), table);
  91.     }

  92.     /**
  93.      * Initializes the {@link _orderedTableNameMap} of the parent class if it is not initialized yet.
  94.      * @throws DataSetException
  95.      * @since 2.4.6
  96.      */
  97.     protected void initialize()
  98.     {
  99.         logger.debug("initialize() - start");
  100.        
  101.         if(_orderedTableNameMap != null)
  102.         {
  103.             logger.debug("The table name map has already been initialized.");
  104.             // already initialized
  105.             return;
  106.         }
  107.        
  108.         // Gather all tables in the OrderedTableNameMap which also makes the duplicate check
  109.         _orderedTableNameMap = this.createTableNameMap();

  110.     }

  111.     ////////////////////////////////////////////////////////////////////////////
  112.     // AbstractDataSet class

  113.     protected ITableIterator createIterator(boolean reversed)
  114.             throws DataSetException
  115.     {
  116.         logger.debug("createIterator(reversed={}) - start", Boolean.toString(reversed));
  117.        
  118.         this.initialize();
  119.        
  120.         ITable[] tables = (ITable[])_orderedTableNameMap.orderedValues().toArray(new ITable[0]);
  121.         return new DefaultTableIterator(tables, reversed);
  122.     }
  123. }