LowerCaseDataSet.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.  * Specialized IDataSet decorator that convert the table name and
  26.  * column names to lower case. Used in DbUnit own test suite to verify that
  27.  * operations are case insensitive.
  28.  *
  29.  * @author Manuel Laflamme
  30.  * @version $Revision$
  31.  * @since Feb 14, 2003
  32.  */
  33. public class LowerCaseDataSet extends AbstractDataSet
  34. {

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

  39.     private final IDataSet _dataSet;

  40.     public LowerCaseDataSet(ITable table) throws DataSetException
  41.     {
  42.         this(new DefaultDataSet(table));
  43.     }

  44.     public LowerCaseDataSet(ITable[] tables) throws DataSetException
  45.     {
  46.         this(new DefaultDataSet(tables));
  47.     }

  48.     public LowerCaseDataSet(IDataSet dataSet) throws DataSetException
  49.     {
  50.         _dataSet = dataSet;
  51.     }

  52.     private ITable createLowerTable(ITable table) throws DataSetException
  53.     {
  54.         logger.debug("createLowerTable(table={}) - start", table);

  55.         return new CompositeTable(
  56.                 new LowerCaseTableMetaData(table.getTableMetaData()), table);
  57.     }

  58.     ////////////////////////////////////////////////////////////////////////////
  59.     // AbstractDataSet class

  60.     protected ITableIterator createIterator(boolean reversed)
  61.             throws DataSetException
  62.     {
  63.         logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));

  64.         return new LowerCaseIterator(reversed ?
  65.                 _dataSet.reverseIterator() : _dataSet.iterator());
  66.     }

  67.     ////////////////////////////////////////////////////////////////////////////
  68.     // IDataSet interface

  69.     public String[] getTableNames() throws DataSetException
  70.     {
  71.         logger.debug("getTableNames() - start");

  72.         String[] tableNames = super.getTableNames();
  73.         for (int i = 0; i < tableNames.length; i++)
  74.         {
  75.             tableNames[i] = tableNames[i].toLowerCase();
  76.         }
  77.         return tableNames;
  78.     }

  79.     public ITableMetaData getTableMetaData(String tableName) throws DataSetException
  80.     {
  81.         logger.debug("getTableMetaData(tableName={}) - start", tableName);
  82.         return new LowerCaseTableMetaData(super.getTableMetaData(tableName));
  83.     }

  84.     public ITable getTable(String tableName) throws DataSetException
  85.     {
  86.         logger.debug("getTable(tableName={}) - start", tableName);
  87.         return createLowerTable(super.getTable(tableName));
  88.     }

  89.     ////////////////////////////////////////////////////////////////////////////
  90.     // LowerCaseIterator class

  91.     private class LowerCaseIterator implements ITableIterator
  92.     {

  93.         private final ITableIterator _iterator;

  94.         public LowerCaseIterator(ITableIterator iterator)
  95.         {
  96.             _iterator = iterator;
  97.         }

  98.         ////////////////////////////////////////////////////////////////////////
  99.         // ITableIterator interface

  100.         public boolean next() throws DataSetException
  101.         {
  102.             return _iterator.next();
  103.         }

  104.         public ITableMetaData getTableMetaData() throws DataSetException
  105.         {
  106.             return new LowerCaseTableMetaData(_iterator.getTableMetaData());
  107.         }

  108.         public ITable getTable() throws DataSetException
  109.         {
  110.             return createLowerTable(_iterator.getTable());
  111.         }
  112.     }
  113. }