001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.database.AmbiguousTableNameException;
028:
029: /**
030: * Allows access to a decorated dataset in a case insensitive way. Dataset
031: * implementations provided by the framework are case sensitive. This class
032: * allows using them in situation where case sensitiveness is not desirable.
033: *
034: * @author Manuel Laflamme
035: * @version $Revision: 554 $
036: * @deprecated All IDataSet implementations are case insensitive since DbUnit 1.5
037: */
038: public class CaseInsensitiveDataSet extends AbstractDataSet {
039:
040: /**
041: * Logger for this class
042: */
043: private static final Logger logger = LoggerFactory
044: .getLogger(CaseInsensitiveDataSet.class);
045:
046: private final IDataSet _dataSet;
047:
048: public CaseInsensitiveDataSet(IDataSet dataSet) {
049: _dataSet = dataSet;
050: }
051:
052: private String getInternalTableName(String tableName)
053: throws DataSetException {
054: logger.debug("getInternalTableName(tableName=" + tableName
055: + ") - start");
056:
057: String found = null;
058: String[] names = _dataSet.getTableNames();
059: for (int i = 0; i < names.length; i++) {
060: if (tableName.equalsIgnoreCase(names[i])) {
061: if (found != null) {
062: throw new AmbiguousTableNameException(tableName);
063: }
064: found = names[i];
065: }
066: }
067:
068: if (found != null) {
069: return found;
070: }
071:
072: throw new NoSuchTableException(tableName);
073: }
074:
075: ////////////////////////////////////////////////////////////////////////////
076: // AbstractDataSet class
077:
078: protected ITableIterator createIterator(boolean reversed)
079: throws DataSetException {
080: logger.debug("createIterator(reversed=" + reversed
081: + ") - start");
082:
083: return new CaseInsensitiveIterator(reversed ? _dataSet
084: .reverseIterator() : _dataSet.iterator());
085: }
086:
087: ////////////////////////////////////////////////////////////////////////////
088: // IDataSet interface
089:
090: public String[] getTableNames() throws DataSetException {
091: logger.debug("getTableNames() - start");
092:
093: return _dataSet.getTableNames();
094: }
095:
096: public ITableMetaData getTableMetaData(String tableName)
097: throws DataSetException {
098: logger.debug("getTableMetaData(tableName=" + tableName
099: + ") - start");
100:
101: return _dataSet
102: .getTableMetaData(getInternalTableName(tableName));
103: }
104:
105: public ITable getTable(String tableName) throws DataSetException {
106: logger.debug("getTable(tableName=" + tableName + ") - start");
107:
108: ITable table = _dataSet
109: .getTable(getInternalTableName(tableName));
110: return new CaseInsensitiveTable(table);
111: }
112:
113: ////////////////////////////////////////////////////////////////////////////
114: // CaseInsensitiveIterator class
115:
116: private class CaseInsensitiveIterator implements ITableIterator {
117:
118: /**
119: * Logger for this class
120: */
121: private final Logger logger = LoggerFactory
122: .getLogger(CaseInsensitiveIterator.class);
123:
124: private final ITableIterator _iterator;
125:
126: public CaseInsensitiveIterator(ITableIterator iterator) {
127: _iterator = iterator;
128: }
129:
130: ////////////////////////////////////////////////////////////////////////
131: // ITableIterator interface
132:
133: public boolean next() throws DataSetException {
134: logger.debug("next() - start");
135:
136: return _iterator.next();
137: }
138:
139: public ITableMetaData getTableMetaData()
140: throws DataSetException {
141: logger.debug("getTableMetaData() - start");
142:
143: return _iterator.getTableMetaData();
144: }
145:
146: public ITable getTable() throws DataSetException {
147: logger.debug("getTable() - start");
148:
149: return new CaseInsensitiveTable(_iterator.getTable());
150: }
151: }
152: }
|