View Javadoc
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  
22  package org.dbunit.dataset;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  
28  /**
29   * Specialized IDataSet decorator that convert the table name and
30   * column names to lower case. Used in DbUnit own test suite to verify that
31   * operations are case insensitive.
32   *
33   * @author Manuel Laflamme
34   * @version $Revision$
35   * @since Feb 14, 2003
36   */
37  public class LowerCaseDataSet extends AbstractDataSet
38  {
39  
40      /**
41       * Logger for this class
42       */
43      private static final Logger logger = LoggerFactory.getLogger(LowerCaseDataSet.class);
44  
45      private final IDataSet _dataSet;
46  
47      public LowerCaseDataSet(ITable table) throws DataSetException
48      {
49          this(new DefaultDataSet(table));
50      }
51  
52      public LowerCaseDataSet(ITable[] tables) throws DataSetException
53      {
54          this(new DefaultDataSet(tables));
55      }
56  
57      public LowerCaseDataSet(IDataSet dataSet) throws DataSetException
58      {
59          _dataSet = dataSet;
60      }
61  
62      private ITable createLowerTable(ITable table) throws DataSetException
63      {
64          logger.debug("createLowerTable(table={}) - start", table);
65  
66          return new CompositeTable(
67                  new LowerCaseTableMetaData(table.getTableMetaData()), table);
68      }
69  
70      ////////////////////////////////////////////////////////////////////////////
71      // AbstractDataSet class
72  
73      protected ITableIterator createIterator(boolean reversed)
74              throws DataSetException
75      {
76          logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
77  
78          return new LowerCaseIterator(reversed ?
79                  _dataSet.reverseIterator() : _dataSet.iterator());
80      }
81  
82      ////////////////////////////////////////////////////////////////////////////
83      // IDataSet interface
84  
85      public String[] getTableNames() throws DataSetException
86      {
87          logger.debug("getTableNames() - start");
88  
89          String[] tableNames = super.getTableNames();
90          for (int i = 0; i < tableNames.length; i++)
91          {
92              tableNames[i] = tableNames[i].toLowerCase();
93          }
94          return tableNames;
95      }
96  
97      public ITableMetaData getTableMetaData(String tableName) throws DataSetException
98      {
99          logger.debug("getTableMetaData(tableName={}) - start", tableName);
100         return new LowerCaseTableMetaData(super.getTableMetaData(tableName));
101     }
102 
103     public ITable getTable(String tableName) throws DataSetException
104     {
105         logger.debug("getTable(tableName={}) - start", tableName);
106         return createLowerTable(super.getTable(tableName));
107     }
108 
109     ////////////////////////////////////////////////////////////////////////////
110     // LowerCaseIterator class
111 
112     private class LowerCaseIterator implements ITableIterator
113     {
114 
115         private final ITableIterator _iterator;
116 
117         public LowerCaseIterator(ITableIterator iterator)
118         {
119             _iterator = iterator;
120         }
121 
122         ////////////////////////////////////////////////////////////////////////
123         // ITableIterator interface
124 
125         public boolean next() throws DataSetException
126         {
127             return _iterator.next();
128         }
129 
130         public ITableMetaData getTableMetaData() throws DataSetException
131         {
132             return new LowerCaseTableMetaData(_iterator.getTableMetaData());
133         }
134 
135         public ITable getTable() throws DataSetException
136         {
137             return createLowerTable(_iterator.getTable());
138         }
139     }
140 }