DefaultTable.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 java.util.ArrayList;
  23. import java.util.List;

  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;

  26. /**
  27.  * Default table implementation backed by a simple java in-memory list.
  28.  *
  29.  * @author Manuel Laflamme
  30.  * @version $Revision$
  31.  * @since Feb 17, 2002
  32.  */
  33. public class DefaultTable extends AbstractTable
  34. {

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

  39.     private final ITableMetaData _metaData;
  40.     private final List _rowList;

  41.     /**
  42.      * Creates a new empty table with specified metadata and values.
  43.      * @deprecated Use public mutators to initialize table values instead
  44.      */
  45.     public DefaultTable(ITableMetaData metaData, List list)
  46.     {
  47.         _metaData = metaData;
  48.         _rowList = list;
  49.     }

  50.     /**
  51.      * Creates a new empty table having the specified name.
  52.      */
  53.     public DefaultTable(String tableName)
  54.     {
  55.         _metaData = new DefaultTableMetaData(tableName, new Column[0]);
  56.         _rowList = new ArrayList();
  57.     }

  58.     /**
  59.      * Creates a new empty table with specified metadata and values.
  60.      * @deprecated Use public mutators to initialize table values instead
  61.      */
  62.     public DefaultTable(String tableName, Column[] columns, List list)
  63.     {
  64.         _metaData = new DefaultTableMetaData(tableName, columns);
  65.         _rowList = list;
  66.     }

  67.     /**
  68.      * Creates a new empty table with specified metadata.
  69.      */
  70.     public DefaultTable(String tableName, Column[] columns)
  71.     {
  72.         _metaData = new DefaultTableMetaData(tableName, columns);
  73.         _rowList = new ArrayList();
  74.     }

  75.     public DefaultTable(ITableMetaData metaData)
  76.     {
  77.         _metaData = metaData;
  78.         _rowList = new ArrayList();
  79.     }

  80.     /**
  81.      * Inserts a new empty row. You can add values with {@link #setValue}.
  82.      */
  83.     public void addRow() throws DataSetException
  84.     {
  85.         logger.debug("addRow() - start");

  86.         int columnCount = _metaData.getColumns().length;
  87.         _rowList.add(new Object[columnCount]);
  88.     }

  89.     /**
  90.      * Inserts a new row initialized with specified array of values.
  91.      * @param values The array of values. Each value correspond to the column at the
  92.      * same index from {@link ITableMetaData#getColumns}.
  93.      * @see #getTableMetaData
  94.      */
  95.     public void addRow(Object[] values) throws DataSetException
  96.     {
  97.         logger.debug("addRow(values={}) - start", values);

  98.         _rowList.add(values);
  99.     }

  100.     /**
  101.      * Inserts all rows from the specified table.
  102.      * @param table The source table.
  103.      */
  104.     public void addTableRows(ITable table) throws DataSetException
  105.     {
  106.         logger.debug("addTableRows(table={}) - start", table);

  107.         try
  108.         {
  109.             Column[] columns = _metaData.getColumns();
  110.             if (columns.length <= 0)
  111.             {
  112.                 logger.warn("The table '" + table + "' does not have any columns. Cannot add table rows. This should never happen...");
  113.                 return;
  114.             }
  115.            
  116.             for (int i = 0; ; i++)
  117.             {
  118.                 Object[] rowValues = new Object[columns.length];
  119.                 for (int j = 0; j < columns.length; j++)
  120.                 {
  121.                     Column column = columns[j];
  122.                     rowValues[j] = table.getValue(i, column.getColumnName());
  123.                 }
  124.                 _rowList.add(rowValues);
  125.             }
  126.         }
  127.         catch(RowOutOfBoundsException e)
  128.         {
  129.             // end of table
  130.             // ignore error.
  131.         }
  132.     }

  133.     /**
  134.      * Replaces the value at the specified position in this table with the specified value.
  135.      * @param row The row index
  136.      * @param column The column name
  137.      * @param value The value to store at the specified location
  138.      * @return the value previously at the specified location
  139.      * @throws RowOutOfBoundsException if the row index is out of range
  140.      * @throws NoSuchColumnException if the column does not exist
  141.      * @throws DataSetException if an unexpected error occurs
  142.      */
  143.     public Object setValue(int row, String column, Object value)
  144.             throws RowOutOfBoundsException, NoSuchColumnException, DataSetException
  145.     {
  146.         if(logger.isDebugEnabled())
  147.             logger.debug("setValue(row={}, column={}, value={}) - start", row, column, value);

  148.         assertValidRowIndex(row);

  149.         Object[] rowValues = (Object[])_rowList.get(row);
  150.         int columnIndex = getColumnIndex(column);
  151.         Object oldValue = rowValues[columnIndex];
  152.         rowValues[columnIndex] = value;
  153.         return oldValue;
  154.     }

  155.     ////////////////////////////////////////////////////////////////////////////
  156.     // ITable interface

  157.     public ITableMetaData getTableMetaData()
  158.     {
  159.         return _metaData;
  160.     }

  161.     public int getRowCount()
  162.     {
  163.         return _rowList.size();
  164.     }

  165.     public Object getValue(int row, String column) throws DataSetException
  166.     {
  167.         if(logger.isDebugEnabled())
  168.             logger.debug("getValue(row={}, column={}) - start", Integer.toString(row), column);

  169.         assertValidRowIndex(row);

  170.         Object[] rowValues = (Object[])_rowList.get(row);
  171.         return rowValues[getColumnIndex(column)];
  172.     }

  173.     public String toString()
  174.     {
  175.         final StringBuilder sb = new StringBuilder();
  176.         sb.append(getClass().getName()).append("[");
  177.         sb.append("_metaData=").append(this._metaData == null ? "null" : this._metaData.toString());
  178.         sb.append(", _rowList.size()=").append(this._rowList == null ? "null" : ""+this._rowList.size());
  179.         sb.append("]");
  180.         return sb.toString();
  181.     }
  182. }