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;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * Decorator that replace configured values from the decorated dataset
031: * with replacement values.
032: *
033: * @author Manuel Laflamme
034: * @since Mar 17, 2003
035: * @version $Revision: 554 $
036: */
037: public class ReplacementDataSet extends AbstractDataSet {
038:
039: /**
040: * Logger for this class
041: */
042: private static final Logger logger = LoggerFactory
043: .getLogger(ReplacementDataSet.class);
044:
045: private final IDataSet _dataSet;
046: private final Map _objectMap;
047: private final Map _substringMap;
048: private String _startDelim;
049: private String _endDelim;
050:
051: /**
052: * Create a new ReplacementDataSet object that decorates the specified dataset.
053: *
054: * @param dataSet the decorated table
055: */
056: public ReplacementDataSet(IDataSet dataSet) {
057: _dataSet = dataSet;
058: _objectMap = new HashMap();
059: _substringMap = new HashMap();
060: }
061:
062: /**
063: * Create a new ReplacementDataSet object that decorates the specified dataset.
064: *
065: * @param dataSet the decorated dataset
066: * @param objectMap the replacement objects mapping
067: * @param substringMap the replacement substrings mapping
068: */
069: public ReplacementDataSet(IDataSet dataSet, Map objectMap,
070: Map substringMap) {
071: _dataSet = dataSet;
072: _objectMap = objectMap == null ? new HashMap() : objectMap;
073: _substringMap = substringMap == null ? new HashMap()
074: : substringMap;
075: }
076:
077: /**
078: * Add a new Object replacement mapping.
079: *
080: * @param originalObject the object to replace
081: * @param replacementObject the replacement object
082: */
083: public void addReplacementObject(Object originalObject,
084: Object replacementObject) {
085: logger.debug("addReplacementObject(originalObject="
086: + originalObject + ", replacementObject="
087: + replacementObject + ") - start");
088:
089: _objectMap.put(originalObject, replacementObject);
090: }
091:
092: /**
093: * Add a new substring replacement mapping.
094: *
095: * @param originalSubstring the substring to replace
096: * @param replacementSubstring the replacement substring
097: */
098: public void addReplacementSubstring(String originalSubstring,
099: String replacementSubstring) {
100: logger.debug("addReplacementSubstring(originalSubstring="
101: + originalSubstring + ", replacementSubstring="
102: + replacementSubstring + ") - start");
103:
104: if (originalSubstring == null || replacementSubstring == null) {
105: throw new NullPointerException();
106: }
107:
108: _substringMap.put(originalSubstring, replacementSubstring);
109: }
110:
111: /**
112: * Sets substring delimiters.
113: */
114: public void setSubstringDelimiters(String startDelimiter,
115: String endDelimiter) {
116: logger.debug("setSubstringDelimiters(startDelimiter="
117: + startDelimiter + ", endDelimiter=" + endDelimiter
118: + ") - start");
119:
120: if (startDelimiter == null || endDelimiter == null) {
121: throw new NullPointerException();
122: }
123:
124: _startDelim = startDelimiter;
125: _endDelim = endDelimiter;
126: }
127:
128: private ReplacementTable createReplacementTable(ITable table) {
129: logger.debug("createReplacementTable(table=" + table
130: + ") - start");
131:
132: return new ReplacementTable(table, _objectMap, _substringMap,
133: _startDelim, _endDelim);
134: }
135:
136: ////////////////////////////////////////////////////////////////////////////
137: // AbstractDataSet class
138:
139: protected ITableIterator createIterator(boolean reversed)
140: throws DataSetException {
141: logger.debug("createIterator(reversed=" + reversed
142: + ") - start");
143:
144: return new ReplacementIterator(reversed ? _dataSet
145: .reverseIterator() : _dataSet.iterator());
146: }
147:
148: ////////////////////////////////////////////////////////////////////////////
149: // IDataSet interface
150:
151: public String[] getTableNames() throws DataSetException {
152: logger.debug("getTableNames() - start");
153:
154: return _dataSet.getTableNames();
155: }
156:
157: public ITableMetaData getTableMetaData(String tableName)
158: throws DataSetException {
159: logger.debug("getTableMetaData(tableName=" + tableName
160: + ") - start");
161:
162: return _dataSet.getTableMetaData(tableName);
163: }
164:
165: public ITable getTable(String tableName) throws DataSetException {
166: logger.debug("getTable(tableName=" + tableName + ") - start");
167:
168: return createReplacementTable(_dataSet.getTable(tableName));
169: }
170:
171: ////////////////////////////////////////////////////////////////////////////
172: // ReplacementIterator class
173:
174: private class ReplacementIterator implements ITableIterator {
175:
176: /**
177: * Logger for this class
178: */
179: private final Logger logger = LoggerFactory
180: .getLogger(ReplacementIterator.class);
181:
182: private final ITableIterator _iterator;
183:
184: public ReplacementIterator(ITableIterator iterator) {
185: _iterator = iterator;
186: }
187:
188: ////////////////////////////////////////////////////////////////////////
189: // ITableIterator interface
190:
191: public boolean next() throws DataSetException {
192: logger.debug("next() - start");
193:
194: return _iterator.next();
195: }
196:
197: public ITableMetaData getTableMetaData()
198: throws DataSetException {
199: logger.debug("getTableMetaData() - start");
200:
201: return _iterator.getTableMetaData();
202: }
203:
204: public ITable getTable() throws DataSetException {
205: logger.debug("getTable() - start");
206:
207: return createReplacementTable(_iterator.getTable());
208: }
209: }
210: }
|