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.dbunit.database.AmbiguousTableNameException;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * Allows access to a decorated dataset in a case insensitive way. Dataset
30   * implementations provided by the framework are case sensitive. This class
31   * allows using them in situation where case sensitiveness is not desirable.
32   *
33   * @author Manuel Laflamme
34   * @author Last changed by: $Author$
35   * @version $Revision$ $Date$
36   * @deprecated All IDataSet implementations are case insensitive since DbUnit 1.5 - may change again since tablenames on RDBMSes can be case sensitive
37   * @since 1.0
38   */
39  public class CaseInsensitiveDataSet extends AbstractDataSet
40  {
41  
42      /**
43       * Logger for this class
44       */
45      private static final Logger logger = LoggerFactory.getLogger(CaseInsensitiveDataSet.class);
46  
47      private final IDataSet _dataSet;
48      private OrderedTableNameMap orderedTableMap;
49  
50      public CaseInsensitiveDataSet(IDataSet dataSet) throws AmbiguousTableNameException, DataSetException
51      {
52          _dataSet = dataSet;
53          
54          // Check for duplicates using the OrderedTableNameMap as helper
55          orderedTableMap = new OrderedTableNameMap(false);
56          ITableIterator tableIterator = _dataSet.iterator();
57          while(tableIterator.next()) {
58              ITable table = (ITable) tableIterator.getTable();
59              String tableName = table.getTableMetaData().getTableName();
60              orderedTableMap.add(tableName.toUpperCase(), tableName);
61          }
62      }
63  
64      private String getInternalTableName(String tableName) throws DataSetException
65      {
66          logger.debug("getInternalTableName(tableName={}) - start", tableName);
67  
68          String originalTableName = (String)orderedTableMap.get(tableName.toUpperCase());
69          if(originalTableName==null){
70              throw new NoSuchTableException(tableName);
71          }
72          else {
73              return originalTableName;
74          }
75      }
76  
77      ////////////////////////////////////////////////////////////////////////////
78      // AbstractDataSet class
79  
80      protected ITableIterator createIterator(boolean reversed)
81              throws DataSetException
82      {
83          if(logger.isDebugEnabled())
84              logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
85          
86          return new CaseInsensitiveIterator(reversed ?
87                  _dataSet.reverseIterator() : _dataSet.iterator());
88      }
89  
90      ////////////////////////////////////////////////////////////////////////////
91      // IDataSet interface
92  
93      public String[] getTableNames() throws DataSetException
94      {
95          return _dataSet.getTableNames();
96      }
97  
98      public ITableMetaData getTableMetaData(String tableName)
99              throws DataSetException
100     {
101         logger.debug("getTableMetaData(tableName={}) - start", tableName);
102         return _dataSet.getTableMetaData(getInternalTableName(tableName));
103     }
104 
105     public ITable getTable(String tableName) throws DataSetException
106     {
107         logger.debug("getTable(tableName={}) - start", tableName);
108         ITable table = _dataSet.getTable(getInternalTableName(tableName));
109         return new CaseInsensitiveTable(table);
110     }
111 
112     ////////////////////////////////////////////////////////////////////////////
113     // CaseInsensitiveIterator class
114 
115     private class CaseInsensitiveIterator implements ITableIterator
116     {
117         private final ITableIterator _iterator;
118 
119         public CaseInsensitiveIterator(ITableIterator iterator)
120         {
121             _iterator = iterator;
122         }
123 
124         ////////////////////////////////////////////////////////////////////////
125         // ITableIterator interface
126 
127         public boolean next() throws DataSetException
128         {
129             return _iterator.next();
130         }
131 
132         public ITableMetaData getTableMetaData() throws DataSetException
133         {
134             return _iterator.getTableMetaData();
135         }
136 
137         public ITable getTable() throws DataSetException
138         {
139             return new CaseInsensitiveTable(_iterator.getTable());
140         }
141     }
142 }