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  
22  package org.dbunit.dataset;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Default table implementation backed by a simple java in-memory list.
32   * 
33   * @author Manuel Laflamme
34   * @version $Revision$
35   * @since Feb 17, 2002
36   */
37  public class DefaultTable extends AbstractTable
38  {
39  
40      /**
41       * Logger for this class
42       */
43      private static final Logger logger = LoggerFactory.getLogger(DefaultTable.class);
44  
45      private final ITableMetaData _metaData;
46      private final List _rowList;
47  
48      /**
49       * Creates a new empty table with specified metadata and values.
50       * @deprecated Use public mutators to initialize table values instead
51       */
52      public DefaultTable(ITableMetaData metaData, List list)
53      {
54          _metaData = metaData;
55          _rowList = list;
56      }
57  
58      /**
59       * Creates a new empty table having the specified name.
60       */
61      public DefaultTable(String tableName)
62      {
63          _metaData = new DefaultTableMetaData(tableName, new Column[0]);
64          _rowList = new ArrayList();
65      }
66  
67      /**
68       * Creates a new empty table with specified metadata and values.
69       * @deprecated Use public mutators to initialize table values instead
70       */
71      public DefaultTable(String tableName, Column[] columns, List list)
72      {
73          _metaData = new DefaultTableMetaData(tableName, columns);
74          _rowList = list;
75      }
76  
77      /**
78       * Creates a new empty table with specified metadata.
79       */
80      public DefaultTable(String tableName, Column[] columns)
81      {
82          _metaData = new DefaultTableMetaData(tableName, columns);
83          _rowList = new ArrayList();
84      }
85  
86      public DefaultTable(ITableMetaData metaData)
87      {
88          _metaData = metaData;
89          _rowList = new ArrayList();
90      }
91  
92      /**
93       * Inserts a new empty row. You can add values with {@link #setValue}.
94       */
95      public void addRow() throws DataSetException
96      {
97          logger.debug("addRow() - start");
98  
99          int columnCount = _metaData.getColumns().length;
100         _rowList.add(new Object[columnCount]);
101     }
102 
103     /**
104      * Inserts a new row initialized with specified array of values.
105      * @param values The array of values. Each value correspond to the column at the
106      * same index from {@link ITableMetaData#getColumns}.
107      * @see #getTableMetaData
108      */
109     public void addRow(Object[] values) throws DataSetException
110     {
111         logger.debug("addRow(values={}) - start", values);
112 
113         _rowList.add(values);
114     }
115 
116     /**
117      * Inserts all rows from the specified table.
118      * @param table The source table.
119      */
120     public void addTableRows(ITable table) throws DataSetException
121     {
122         logger.debug("addTableRows(table={}) - start", table);
123 
124         try
125         {
126             Column[] columns = _metaData.getColumns();
127             if (columns.length <= 0)
128             {
129             	logger.warn("The table '" + table + "' does not have any columns. Cannot add table rows. This should never happen...");
130             	return;
131             }
132             
133             for (int i = 0; ; i++)
134             {
135                 Object[] rowValues = new Object[columns.length];
136                 for (int j = 0; j < columns.length; j++)
137                 {
138                     Column column = columns[j];
139                     rowValues[j] = table.getValue(i, column.getColumnName());
140                 }
141                 _rowList.add(rowValues);
142             }
143         }
144         catch(RowOutOfBoundsException e)
145         {
146             // end of table
147         	// ignore error.
148         }
149     }
150 
151     /**
152      * Replaces the value at the specified position in this table with the specified value.
153      * @param row The row index
154      * @param column The column name
155      * @param value The value to store at the specified location
156      * @return the value previously at the specified location
157      * @throws RowOutOfBoundsException if the row index is out of range
158      * @throws NoSuchColumnException if the column does not exist
159      * @throws DataSetException if an unexpected error occurs
160      */
161     public Object setValue(int row, String column, Object value)
162             throws RowOutOfBoundsException, NoSuchColumnException, DataSetException
163     {
164         if(logger.isDebugEnabled())
165             logger.debug("setValue(row={}, column={}, value={}) - start", new Object[] {Integer.toString(row), column, value});
166 
167         assertValidRowIndex(row);
168 
169         Object[] rowValues = (Object[])_rowList.get(row);
170         int columnIndex = getColumnIndex(column);
171         Object oldValue = rowValues[columnIndex];
172         rowValues[columnIndex] = value;
173         return oldValue;
174     }
175 
176     ////////////////////////////////////////////////////////////////////////////
177     // ITable interface
178 
179     public ITableMetaData getTableMetaData()
180     {
181         return _metaData;
182     }
183 
184     public int getRowCount()
185     {
186         return _rowList.size();
187     }
188 
189     public Object getValue(int row, String column) throws DataSetException
190     {
191         if(logger.isDebugEnabled())
192             logger.debug("getValue(row={}, column={}) - start", Integer.toString(row), column);
193 
194         assertValidRowIndex(row);
195 
196         Object[] rowValues = (Object[])_rowList.get(row);
197         return rowValues[getColumnIndex(column)];
198     }
199 
200     public String toString()
201     {
202     	StringBuffer sb = new StringBuffer();
203     	sb.append(getClass().getName()).append("[");
204     	sb.append("_metaData=").append(this._metaData == null ? "null" : this._metaData.toString());
205     	sb.append(", _rowList.size()=").append(this._rowList == null ? "null" : ""+this._rowList.size());
206     	sb.append("]");
207     	return sb.toString();
208     }
209 }
210 
211 
212 
213 
214