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
22 package org.dbunit.dataset;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28 * This abstract class provides the basic implementation of the IDataSet
29 * interface. Subclass are only required to implement the {@link #createIterator}
30 * method.
31 *
32 * @author Manuel Laflamme
33 * @author Last changed by: $Author$
34 * @version $Revision$ $Date$
35 * @since 1.0 (Feb 22, 2002)
36 */
37 public abstract class AbstractDataSet implements IDataSet
38 {
39 //TODO (matthias) Use a DataSetBuilder PLUS IDataSet to avoid this ugly lazy initialization with loads of protected internals a user must know...
40
41 protected OrderedTableNameMap _orderedTableNameMap;
42
43 /**
44 * Whether or not table names of this dataset are case sensitive.
45 * By default case-sensitivity is set to false for datasets
46 */
47 private boolean _caseSensitiveTableNames = false;
48
49 /**
50 * Logger for this class
51 */
52 private static final Logger logger = LoggerFactory.getLogger(AbstractDataSet.class);
53
54 /**
55 * Default constructor
56 */
57 public AbstractDataSet()
58 {
59 }
60
61 /**
62 * Constructor
63 * @param caseSensitiveTableNames Whether or not table names should be case sensitive
64 * @since 2.4
65 */
66 public AbstractDataSet(boolean caseSensitiveTableNames)
67 {
68 _caseSensitiveTableNames = caseSensitiveTableNames;
69 }
70
71 /**
72 * @return <code>true</code> if the case sensitivity of table names is used in this dataset.
73 * @since 2.4
74 */
75 public boolean isCaseSensitiveTableNames()
76 {
77 return this._caseSensitiveTableNames;
78 }
79
80 /**
81 * Creates and returns a new instance of the table names container.
82 * Implementors should use this method to retrieve a map which stores
83 * table names which can be linked with arbitrary objects.
84 * @return a new empty instance of the table names container
85 * @since 2.4
86 */
87 protected OrderedTableNameMap createTableNameMap()
88 {
89 return new OrderedTableNameMap(this._caseSensitiveTableNames);
90 }
91
92 /**
93 * Initializes the tables of this dataset
94 * @throws DataSetException
95 * @since 2.4
96 */
97 protected void initialize() throws DataSetException
98 {
99 logger.debug("initialize() - start");
100
101 if(_orderedTableNameMap != null)
102 {
103 logger.debug("The table name map has already been initialized.");
104 // already initialized
105 return;
106 }
107
108 // Gather all tables in the OrderedTableNameMap which also makes the duplicate check
109 _orderedTableNameMap = this.createTableNameMap();
110 ITableIterator iterator = createIterator(false);
111 while (iterator.next())
112 {
113 ITable table = iterator.getTable();
114 _orderedTableNameMap.add(table.getTableMetaData().getTableName(), table);
115 }
116 }
117
118
119 // protected ITable[] cloneTables(ITable[] tables)
120 // {
121 // logger.debug("cloneTables(tables={}) - start", tables);
122 //
123 // ITable[] clones = new ITable[tables.length];
124 // for (int i = 0; i < tables.length; i++)
125 // {
126 // clones[i] = tables[i];
127 // }
128 // return clones;
129 // }
130
131 /**
132 * Creates an iterator which provides access to all tables of this dataset
133 * @param reversed Whether the created iterator should be a reversed one or not
134 * @return The created {@link ITableIterator}
135 * @throws DataSetException
136 */
137 protected abstract ITableIterator createIterator(boolean reversed)
138 throws DataSetException;
139
140 ////////////////////////////////////////////////////////////////////////////
141 // IDataSet interface
142
143 public String[] getTableNames() throws DataSetException
144 {
145 logger.debug("getTableNames() - start");
146
147 initialize();
148
149 return this._orderedTableNameMap.getTableNames();
150 }
151
152 public ITableMetaData getTableMetaData(String tableName) throws DataSetException
153 {
154 logger.debug("getTableMetaData(tableName={}) - start", tableName);
155
156 return getTable(tableName).getTableMetaData();
157 }
158
159 public ITable getTable(String tableName) throws DataSetException
160 {
161 logger.debug("getTable(tableName={}) - start", tableName);
162
163 initialize();
164
165 ITable found = (ITable) _orderedTableNameMap.get(tableName);
166 if (found != null)
167 {
168 return found;
169 }
170 else
171 {
172 throw new NoSuchTableException(tableName);
173 }
174 }
175
176 public ITable[] getTables() throws DataSetException
177 {
178 logger.debug("getTables() - start");
179
180 initialize();
181
182 return (ITable[]) this._orderedTableNameMap.orderedValues().toArray(new ITable[0]);
183 }
184
185 public ITableIterator iterator() throws DataSetException
186 {
187 logger.debug("iterator() - start");
188
189 return createIterator(false);
190 }
191
192 public ITableIterator reverseIterator() throws DataSetException
193 {
194 logger.debug("reverseIterator() - start");
195
196 return createIterator(true);
197 }
198
199 ////////////////////////////////////////////////////////////////////////////
200 // Object class
201
202 public String toString()
203 {
204 final StringBuilder sb = new StringBuilder();
205 sb.append("AbstractDataSet[");
206 sb.append("_orderedTableNameMap=").append(_orderedTableNameMap);
207 sb.append("]");
208 return sb.toString();
209 }
210
211 }
212
213
214
215
216
217