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  package org.dbunit.dataset;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  /**
30   * Decorator that replace configured values from the decorated dataset
31   * with replacement values.
32   *
33   * @author Manuel Laflamme
34   * @since Mar 17, 2003
35   * @version $Revision$
36   */
37  public class ReplacementDataSet extends AbstractDataSet
38  {
39  
40      /**
41       * Logger for this class
42       */
43      private static final Logger logger = LoggerFactory.getLogger(ReplacementDataSet.class);
44  
45      private final IDataSet _dataSet;
46      private final Map _objectMap;
47      private final Map _substringMap;
48      private String _startDelim;
49      private String _endDelim;
50      private boolean _strictReplacement;
51  
52  
53      /**
54       * Create a new ReplacementDataSet object that decorates the specified dataset.
55       *
56       * @param dataSet the decorated table
57       */
58      public ReplacementDataSet(IDataSet dataSet)
59      {
60          this(dataSet, new HashMap(), new HashMap());
61      }
62  
63      /**
64       * Create a new ReplacementDataSet object that decorates the specified dataset.
65       *
66       * @param dataSet the decorated dataset
67       * @param objectMap the replacement objects mapping
68       * @param substringMap the replacement substrings mapping
69       */
70      public ReplacementDataSet(IDataSet dataSet, Map objectMap, Map substringMap)
71      {
72          super(dataSet.isCaseSensitiveTableNames());
73          _dataSet = dataSet;
74          _objectMap = objectMap == null ? new HashMap() : objectMap;
75          _substringMap = substringMap == null ? new HashMap() : substringMap;
76      }
77  
78      /**
79       * Setting this property to true indicates that when no replacement
80       * is found for a delimited substring the replacement will fail fast.
81       * 
82       * @param strictReplacement true if replacement should be strict
83       */
84      public void setStrictReplacement(boolean strictReplacement) {
85          this._strictReplacement = strictReplacement;
86      }
87      
88      /**
89       * Add a new Object replacement mapping.
90       *
91       * @param originalObject the object to replace
92       * @param replacementObject the replacement object
93       */
94      public void addReplacementObject(Object originalObject, Object replacementObject)
95      {
96          logger.debug("addReplacementObject(originalObject={}, replacementObject={}) - start", originalObject, replacementObject);
97  
98          _objectMap.put(originalObject, replacementObject);
99      }
100 
101     /**
102      * Add a new substring replacement mapping.
103      *
104      * @param originalSubstring the substring to replace
105      * @param replacementSubstring the replacement substring
106      */
107     public void addReplacementSubstring(String originalSubstring,
108             String replacementSubstring)
109     {
110         logger.debug("addReplacementSubstring(originalSubstring={}, replacementSubstring={}) - start", originalSubstring, replacementSubstring);
111 
112         if (originalSubstring == null || replacementSubstring == null)
113         {
114             throw new NullPointerException();
115         }
116 
117         _substringMap.put(originalSubstring, replacementSubstring);
118     }
119 
120     /**
121      * Sets substring delimiters.
122      */
123     public void setSubstringDelimiters(String startDelimiter, String endDelimiter)
124     {
125         logger.debug("setSubstringDelimiters(startDelimiter={}, endDelimiter={}) - start", startDelimiter, endDelimiter);
126 
127         if (startDelimiter == null || endDelimiter == null)
128         {
129             throw new NullPointerException();
130         }
131 
132         _startDelim = startDelimiter;
133         _endDelim = endDelimiter;
134     }
135 
136     private ReplacementTable createReplacementTable(ITable table)
137     {
138         logger.debug("createReplacementTable(table={}) - start", table);
139         
140         ReplacementTable replacementTable = new ReplacementTable(
141                 table, _objectMap, _substringMap, _startDelim, _endDelim);
142         replacementTable.setStrictReplacement(_strictReplacement);
143         return replacementTable;
144     }
145 
146     ////////////////////////////////////////////////////////////////////////////
147     // AbstractDataSet class
148 
149     protected ITableIterator createIterator(boolean reversed)
150             throws DataSetException
151     {
152     	if(logger.isDebugEnabled())
153     		logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
154 
155         return new ReplacementIterator(reversed ?
156                 _dataSet.reverseIterator() : _dataSet.iterator());
157     }
158 
159     ////////////////////////////////////////////////////////////////////////////
160     // IDataSet interface
161 
162     public String[] getTableNames() throws DataSetException
163     {
164         logger.debug("getTableNames() - start");
165 
166         return _dataSet.getTableNames();
167     }
168 
169     public ITableMetaData getTableMetaData(String tableName)
170             throws DataSetException
171     {
172         logger.debug("getTableMetaData(tableName={}) - start", tableName);
173 
174         return _dataSet.getTableMetaData(tableName);
175     }
176 
177     public ITable getTable(String tableName) throws DataSetException
178     {
179         logger.debug("getTable(tableName={}) - start", tableName);
180 
181         return createReplacementTable(_dataSet.getTable(tableName));
182     }
183 
184     ////////////////////////////////////////////////////////////////////////////
185     // ReplacementIterator class
186 
187     private class ReplacementIterator implements ITableIterator
188     {
189 
190         /**
191          * Logger for this class
192          */
193         private final Logger logger = LoggerFactory.getLogger(ReplacementIterator.class);
194 
195         private final ITableIterator _iterator;
196 
197         public ReplacementIterator(ITableIterator iterator)
198         {
199             _iterator = iterator;
200         }
201 
202         ////////////////////////////////////////////////////////////////////////
203         // ITableIterator interface
204 
205         public boolean next() throws DataSetException
206         {
207             logger.debug("next() - start");
208 
209             return _iterator.next();
210         }
211 
212         public ITableMetaData getTableMetaData() throws DataSetException
213         {
214             logger.debug("getTableMetaData() - start");
215 
216             return _iterator.getTableMetaData();
217         }
218 
219         public ITable getTable() throws DataSetException
220         {
221             logger.debug("getTable() - start");
222 
223             return createReplacementTable(_iterator.getTable());
224         }
225     }
226 }