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: package org.dbunit.dataset.filter;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.database.AmbiguousTableNameException;
027: import org.dbunit.dataset.*;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: /**
033: * This filter expose a specified table sequence and can be used to reorder
034: * tables in a dataset. This implementation do not support duplicate table names.
035: * Thus you cannot specify the same table name more than once in this filter
036: * and the filtered dataset must not contains duplicate table names. This is
037: * the default filter used by the {@link org.dbunit.dataset.FilteredDataSet}.
038: *
039: * @author Manuel Laflamme
040: * @since Mar 7, 2003
041: * @version $Revision: 554 $
042: */
043: public class SequenceTableFilter implements ITableFilter {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger logger = LoggerFactory
049: .getLogger(SequenceTableFilter.class);
050:
051: private final String[] _tableNames;
052:
053: /**
054: * Creates a new SequenceTableFilter with specified table names sequence.
055: */
056: public SequenceTableFilter(String[] tableNames) {
057: _tableNames = tableNames;
058: }
059:
060: private boolean accept(String tableName, String[] tableNames,
061: boolean verifyDuplicate) throws AmbiguousTableNameException {
062: logger.debug("accept(tableName=" + tableName + ", tableNames="
063: + tableNames + ", verifyDuplicate=" + verifyDuplicate
064: + ") - start");
065:
066: boolean found = false;
067: for (int i = 0; i < tableNames.length; i++) {
068: if (tableName.equalsIgnoreCase(tableNames[i])) {
069: if (!verifyDuplicate) {
070: return true;
071: }
072:
073: if (found) {
074: throw new AmbiguousTableNameException(tableName);
075: }
076: found = true;
077: }
078: }
079:
080: return found;
081: }
082:
083: ////////////////////////////////////////////////////////////////////////////
084: // ITableFilter interface
085:
086: public boolean accept(String tableName) throws DataSetException {
087: logger.debug("accept(tableName=" + tableName + ") - start");
088:
089: return accept(tableName, _tableNames, true);
090: }
091:
092: public String[] getTableNames(IDataSet dataSet)
093: throws DataSetException {
094: logger.debug("getTableNames(dataSet=" + dataSet + ") - start");
095:
096: List nameList = new ArrayList();
097: for (int i = 0; i < _tableNames.length; i++) {
098: try {
099: // Use the table name from the filtered dataset. This ensure
100: // that table names are having the same case (lower/upper) from
101: // getTableNames() and getTables() methods.
102: ITableMetaData metaData = dataSet
103: .getTableMetaData(_tableNames[i]);
104: nameList.add(metaData.getTableName());
105: } catch (NoSuchTableException e) {
106: logger.error("getTableNames()", e);
107:
108: // Skip this table name because the filtered dataset does not
109: // contains it.
110: }
111: }
112:
113: return (String[]) nameList.toArray(new String[0]);
114: }
115:
116: public ITableIterator iterator(IDataSet dataSet, boolean reversed)
117: throws DataSetException {
118: logger.debug("iterator(dataSet=" + dataSet + ", reversed="
119: + reversed + ") - start");
120:
121: String[] tableNames = getTableNames(dataSet);
122: return new SequenceTableIterator(reversed ? DataSetUtils
123: .reverseStringArray(tableNames) : tableNames, dataSet);
124: }
125: }
|