CompositeDataSet.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.  * Combines multiple datasets into a single logical dataset.
  27.  *
  28.  * @author Manuel Laflamme
  29.  * @version $Revision$
  30.  * @since Feb 19, 2002
  31.  */
  32. public class CompositeDataSet extends AbstractDataSet
  33. {

  34.     /**
  35.      * Logger for this class
  36.      */
  37.     private static final Logger logger = LoggerFactory.getLogger(CompositeDataSet.class);

  38.     private final ITable[] _tables;

  39.     /**
  40.      * Creates a composite dataset that combines specified datasets.
  41.      * Tables having the same name are merged into one table.
  42.      */
  43.     public CompositeDataSet(IDataSet[] dataSets) throws DataSetException
  44.     {
  45.         this(dataSets, true);
  46.     }

  47.     /**
  48.      * Creates a composite dataset that combines specified datasets.
  49.      *
  50.      * @param dataSets
  51.      *      list of datasets
  52.      * @param combine
  53.      *      if <code>true</code>, tables having the same name are merged into
  54.      *      one table.
  55.      */
  56.     public CompositeDataSet(IDataSet[] dataSets, boolean combine)
  57.             throws DataSetException
  58.     {
  59.         this(dataSets, combine, false);
  60.     }
  61.    
  62.     /**
  63.      * Creates a composite dataset that combines specified datasets.
  64.      *
  65.      * @param dataSets
  66.      *      list of datasets
  67.      * @param combine
  68.      *      if <code>true</code>, tables having the same name are merged into
  69.      *      one table.
  70.      * @param caseSensitiveTableNames Whether or not table names are handled in a case sensitive
  71.      * way over all datasets.
  72.      * @since 2.4.2
  73.      */
  74.     public CompositeDataSet(IDataSet[] dataSets, boolean combine, boolean caseSensitiveTableNames)
  75.             throws DataSetException
  76.     {
  77.         super(caseSensitiveTableNames);
  78.        
  79.         // Check for duplicates using the OrderedTableNameMap as helper
  80.         OrderedTableNameMap orderedTableMap = super.createTableNameMap();
  81.         for (int i = 0; i < dataSets.length; i++)
  82.         {
  83.             IDataSet dataSet = dataSets[i];
  84.             ITableIterator iterator = dataSet.iterator();
  85.             while(iterator.next())
  86.             {
  87.                 addTable(iterator.getTable(), orderedTableMap, combine);
  88.             }
  89.         }

  90.         _tables = (ITable[]) orderedTableMap.orderedValues().toArray(new ITable[0]);
  91.     }

  92.     /**
  93.      * Creates a composite dataset that combines the two specified datasets.
  94.      * Tables having the same name are merged into one table.
  95.      */
  96.     public CompositeDataSet(IDataSet dataSet1, IDataSet dataSet2)
  97.             throws DataSetException
  98.     {
  99.         this(new IDataSet[]{dataSet1, dataSet2});
  100.     }

  101.     /**
  102.      * Creates a composite dataset that combines the two specified datasets.
  103.      *
  104.      * @param dataSet1
  105.      *      first dataset
  106.      * @param dataSet2
  107.      *      second dataset
  108.      * @param combine
  109.      *      if <code>true</code>, tables having the same name are merged into
  110.      *      one table.
  111.      */
  112.     public CompositeDataSet(IDataSet dataSet1, IDataSet dataSet2, boolean combine)
  113.             throws DataSetException
  114.     {
  115.         this(new IDataSet[]{dataSet1, dataSet2}, combine);
  116.     }

  117.     /**
  118.      * Creates a composite dataset that combines duplicate tables of the specified dataset.
  119.      *
  120.      * @param dataSet
  121.      *      the dataset
  122.      * @param combine
  123.      *      if <code>true</code>, tables having the same name are merged into
  124.      *      one table.
  125.      * @deprecated This constructor is useless when the combine parameter is
  126.      * <code>false</code>. Use overload that doesn't have the combine argument.
  127.      */
  128.     public CompositeDataSet(IDataSet dataSet, boolean combine)
  129.             throws DataSetException
  130.     {
  131.         this(new IDataSet[]{dataSet}, combine);
  132.     }

  133.     /**
  134.      * Creates a composite dataset that combines duplicate tables of the specified dataset.
  135.      *
  136.      * @param dataSet
  137.      *      the dataset
  138.      */
  139.     public CompositeDataSet(IDataSet dataSet) throws DataSetException
  140.     {
  141.         this(new IDataSet[]{dataSet}, true);
  142.     }

  143.     /**
  144.      * Creates a composite dataset that combines tables having identical name.
  145.      * Tables having the same name are merged into one table.
  146.      */
  147.     public CompositeDataSet(ITable[] tables) throws DataSetException
  148.     {
  149.         this(tables, false);
  150.     }
  151.    
  152.     /**
  153.      * Creates a composite dataset that combines tables having identical name.
  154.      * Tables having the same name are merged into one table.
  155.      * @param tables The tables to merge to one dataset
  156.      * @param caseSensitiveTableNames Whether or not table names are handled in a case sensitive
  157.      * way over all datasets.
  158.      * @since 2.4.2
  159.      */
  160.     public CompositeDataSet(ITable[] tables, boolean caseSensitiveTableNames) throws DataSetException
  161.     {
  162.         super(caseSensitiveTableNames);
  163.        
  164.         OrderedTableNameMap orderedTableMap = super.createTableNameMap();
  165.         for (int i = 0; i < tables.length; i++)
  166.         {
  167.             addTable(tables[i], orderedTableMap, true);
  168.         }

  169.         _tables = (ITable[]) orderedTableMap.orderedValues().toArray(new ITable[0]);
  170.     }

  171.    
  172.     /**
  173.      * @param newTable
  174.      * @param tableMap
  175.      * @param combine
  176.      * @throws AmbiguousTableNameException Can only occur when the combine flag is set to <code>false</code>.
  177.      */
  178.     private void addTable(ITable newTable, OrderedTableNameMap tableMap, boolean combine)
  179.     throws AmbiguousTableNameException
  180.     {
  181.         if (logger.isDebugEnabled())
  182.         {
  183.             logger.debug("addTable(newTable={}, tableList={}, combine={}) - start",
  184.                     new Object[] { newTable, tableMap, String.valueOf(combine) });
  185.         }

  186.         String tableName = newTable.getTableMetaData().getTableName();
  187.        
  188.         // No merge required, simply add new table at then end of the list
  189.         if (!combine)
  190.         {
  191.             tableMap.add(tableName, newTable);
  192.             return;
  193.         }

  194.         // Merge required, search for existing table with the same name
  195.         ITable existingTable = (ITable) tableMap.get(tableName);
  196.         if(existingTable != null) {
  197.             // Found existing table, merge existing and new tables together
  198.             tableMap.update(tableName, new CompositeTable(existingTable, newTable));
  199.             return;
  200.         }
  201.         else {
  202.             // No existing table found, add new table at the end of the list
  203.             tableMap.add(tableName, newTable);
  204.         }
  205.     }

  206.     ////////////////////////////////////////////////////////////////////////////
  207.     // AbstractDataSet class

  208.     protected ITableIterator createIterator(boolean reversed)
  209.             throws DataSetException
  210.     {
  211.         if(logger.isDebugEnabled())
  212.             logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
  213.        
  214.         return new DefaultTableIterator(_tables, reversed);
  215.     }
  216. }