CachedDataSet.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.dataset.stream.IDataSetConsumer;
  23. import org.dbunit.dataset.stream.IDataSetProducer;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;

  26. /**
  27.  * Hold copy of another dataset or a consumed provider content.
  28.  *
  29.  * @author Manuel Laflamme
  30.  * @author Last changed by: $Author$
  31.  * @version $Revision$ $Date$
  32.  * @since 1.x (Apr 18, 2003)
  33.  */
  34. public class CachedDataSet extends AbstractDataSet implements IDataSetConsumer
  35. {
  36.     private static final Logger logger = LoggerFactory.getLogger(CachedDataSet.class);

  37.     private DefaultTable _activeTable;

  38.     /**
  39.      * Default constructor.
  40.      */
  41.     public CachedDataSet() throws DataSetException {
  42.         super();
  43.         initialize();
  44.     }

  45.     /**
  46.      * Creates a copy of the specified dataset.
  47.      */
  48.     public CachedDataSet(IDataSet dataSet) throws DataSetException
  49.     {
  50.         super(dataSet.isCaseSensitiveTableNames());
  51.         initialize();

  52.         final ITableIterator iterator = dataSet.iterator();
  53.         while (iterator.next())
  54.          {
  55.             final ITable table = iterator.getTable();
  56.             _orderedTableNameMap.add(table.getTableMetaData().getTableName(),
  57.                     new CachedTable(table));
  58.         }
  59.     }

  60.     /**
  61.      * Creates a CachedDataSet that synchronously consume the specified producer.
  62.      */
  63.     public CachedDataSet(IDataSetProducer producer) throws DataSetException
  64.     {
  65.         this(producer, false);
  66.     }

  67.     /**
  68.      * Creates a CachedDataSet that synchronously consume the specified producer.
  69.      * @param producer
  70.      * @param caseSensitiveTableNames Whether or not case sensitive table names should be used
  71.      * @throws DataSetException
  72.      */
  73.     public CachedDataSet(IDataSetProducer producer, boolean caseSensitiveTableNames) throws DataSetException
  74.     {
  75.         super(caseSensitiveTableNames);
  76.         initialize();

  77.         producer.setConsumer(this);
  78.         producer.produce();
  79.     }

  80.     ////////////////////////////////////////////////////////////////////////////
  81.     // AbstractDataSet class

  82.     protected ITableIterator createIterator(boolean reversed)
  83.             throws DataSetException
  84.     {
  85.         if(logger.isDebugEnabled())
  86.             logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));

  87.         ITable[] tables = (ITable[])_orderedTableNameMap.orderedValues().toArray(new ITable[0]);
  88.         return new DefaultTableIterator(tables, reversed);
  89.     }

  90.     ////////////////////////////////////////////////////////////////////////
  91.     // IDataSetConsumer interface

  92.     public void startDataSet() throws DataSetException
  93.     {
  94.         logger.debug("startDataSet() - start");
  95.         _orderedTableNameMap = super.createTableNameMap();
  96.     }

  97.     public void endDataSet() throws DataSetException
  98.     {
  99.         logger.debug("endDataSet() - start");
  100.         logger.debug("endDataSet() - the final tableMap is: " + _orderedTableNameMap);
  101.     }

  102.     public void startTable(ITableMetaData metaData) throws DataSetException
  103.     {
  104.         logger.debug("startTable(metaData={}) - start", metaData);
  105.         _activeTable = new DefaultTable(metaData);
  106.     }

  107.     public void endTable() throws DataSetException
  108.     {
  109.         logger.debug("endTable() - start");
  110.         String tableName = _activeTable.getTableMetaData().getTableName();
  111.         // Check whether the table appeared once before
  112.         if(_orderedTableNameMap.containsTable(tableName))
  113.         {
  114.             DefaultTable existingTable = (DefaultTable)_orderedTableNameMap.get(tableName);
  115.             // Add all newly collected rows to the existing table
  116.             existingTable.addTableRows(_activeTable);
  117.         }
  118.         else
  119.         {
  120.             _orderedTableNameMap.add(tableName, _activeTable);
  121.         }
  122.         _activeTable = null;
  123.     }

  124.     public void row(Object[] values) throws DataSetException
  125.     {
  126.         logger.debug("row(values={}) - start", values);
  127.         _activeTable.addRow(values);
  128.     }
  129. }