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: import java.util.ArrayList;
030: import java.util.Arrays;
031: import java.util.List;
032:
033: /**
034: * This abstract class provides the basic implementation of the IDataSet
035: * interface. Subclass are only required to implement the {@link #createIterator}
036: * method.
037: *
038: * @author Manuel Laflamme
039: * @version $Revision: 554 $
040: * @since Feb 22, 2002
041: */
042: public abstract class AbstractDataSet implements IDataSet {
043:
044: /**
045: * Logger for this class
046: */
047: private static final Logger logger = LoggerFactory
048: .getLogger(AbstractDataSet.class);
049:
050: protected ITable[] cloneTables(ITable[] tables) {
051: logger.debug("cloneTables(tables=" + tables + ") - start");
052:
053: ITable[] clones = new ITable[tables.length];
054: for (int i = 0; i < tables.length; i++) {
055: clones[i] = tables[i];
056: }
057: return clones;
058: }
059:
060: protected abstract ITableIterator createIterator(boolean reversed)
061: throws DataSetException;
062:
063: ////////////////////////////////////////////////////////////////////////////
064: // IDataSet interface
065:
066: public String[] getTableNames() throws DataSetException {
067: logger.debug("getTableNames() - start");
068:
069: List tableNameList = new ArrayList();
070: ITableIterator iterator = createIterator(false);
071: while (iterator.next()) {
072: tableNameList.add(iterator.getTableMetaData()
073: .getTableName());
074: }
075: return (String[]) tableNameList.toArray(new String[0]);
076: }
077:
078: public ITableMetaData getTableMetaData(String tableName)
079: throws DataSetException {
080: logger.debug("getTableMetaData(tableName=" + tableName
081: + ") - start");
082:
083: return getTable(tableName).getTableMetaData();
084: }
085:
086: public ITable getTable(String tableName) throws DataSetException {
087: logger.debug("getTable(tableName=" + tableName + ") - start");
088:
089: ITable found = null;
090: ITableIterator iterator = createIterator(false);
091: while (iterator.next()) {
092: ITable table = iterator.getTable();
093: if (tableName.equalsIgnoreCase(table.getTableMetaData()
094: .getTableName())) {
095: if (found != null) {
096: throw new AmbiguousTableNameException(tableName);
097: }
098:
099: found = table;
100: }
101: }
102:
103: if (found != null) {
104: return found;
105: }
106:
107: throw new NoSuchTableException(tableName);
108: }
109:
110: public ITable[] getTables() throws DataSetException {
111: logger.debug("getTables() - start");
112:
113: List tableList = new ArrayList();
114: ITableIterator iterator = createIterator(false);
115: while (iterator.next()) {
116: tableList.add(iterator.getTable());
117: }
118: return (ITable[]) tableList.toArray(new ITable[0]);
119: }
120:
121: public ITableIterator iterator() throws DataSetException {
122: logger.debug("iterator() - start");
123:
124: return createIterator(false);
125: }
126:
127: public ITableIterator reverseIterator() throws DataSetException {
128: logger.debug("reverseIterator() - start");
129:
130: return createIterator(true);
131: }
132:
133: ////////////////////////////////////////////////////////////////////////////
134: // Object class
135:
136: public String toString() {
137: logger.debug("toString() - start");
138:
139: try {
140: return Arrays.asList(getTableNames()).toString();
141: } catch (DataSetException e) {
142: logger.error("toString()", e);
143:
144: return super.toString();
145: }
146: }
147: }
|