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:
022: package org.dbunit.dataset;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: /**
031: * @author Manuel Laflamme
032: * @version $Revision: 554 $
033: * @since Feb 17, 2002
034: */
035: public class DefaultTable extends AbstractTable {
036:
037: /**
038: * Logger for this class
039: */
040: private static final Logger logger = LoggerFactory
041: .getLogger(DefaultTable.class);
042:
043: private final ITableMetaData _metaData;
044: private final List _rowList;
045:
046: /**
047: * Creates a new empty table with specified metadata and values.
048: * @deprecated Use public mutators to initialize table values instead
049: */
050: public DefaultTable(ITableMetaData metaData, List list) {
051: _metaData = metaData;
052: _rowList = list;
053: }
054:
055: /**
056: * Creates a new empty table having the specified name.
057: */
058: public DefaultTable(String tableName) {
059: _metaData = new DefaultTableMetaData(tableName, new Column[0]);
060: _rowList = new ArrayList();
061: }
062:
063: /**
064: * Creates a new empty table with specified metadata and values.
065: * @deprecated Use public mutators to initialize table values instead
066: */
067: public DefaultTable(String tableName, Column[] columns, List list) {
068: _metaData = new DefaultTableMetaData(tableName, columns);
069: _rowList = list;
070: }
071:
072: /**
073: * Creates a new empty table with specified metadata.
074: */
075: public DefaultTable(String tableName, Column[] columns) {
076: _metaData = new DefaultTableMetaData(tableName, columns);
077: _rowList = new ArrayList();
078: }
079:
080: public DefaultTable(ITableMetaData metaData) {
081: _metaData = metaData;
082: _rowList = new ArrayList();
083: }
084:
085: /**
086: * Inserts a new empty row. You can add values with {@link #setValue}.
087: */
088: public void addRow() throws DataSetException {
089: logger.debug("addRow() - start");
090:
091: int columnCount = _metaData.getColumns().length;
092: _rowList.add(new Object[columnCount]);
093: }
094:
095: /**
096: * Inserts a new row initialized with specified array of values.
097: * @param values The array of values. Each value correspond to the column at the
098: * same index from {@link ITableMetaData#getColumns}.
099: * @see #getTableMetaData
100: */
101: public void addRow(Object[] values) throws DataSetException {
102: logger.debug("addRow(values=" + values + ") - start");
103:
104: _rowList.add(values);
105: }
106:
107: /**
108: * Inserts all rows from the specified table.
109: * @param table The source table.
110: */
111: public void addTableRows(ITable table) throws DataSetException {
112: logger.debug("addTableRows(table=" + table + ") - start");
113:
114: try {
115: Column[] columns = _metaData.getColumns();
116: if (columns.length > 0) {
117: for (int i = 0;; i++) {
118: Object[] rowValues = new Object[columns.length];
119: for (int j = 0; j < columns.length; j++) {
120: Column column = columns[j];
121: rowValues[j] = table.getValue(i, column
122: .getColumnName());
123: }
124: _rowList.add(rowValues);
125: }
126: }
127: } catch (RowOutOfBoundsException e) {
128: logger.error("addTableRows()", e);
129:
130: // end of table
131: }
132: }
133:
134: /**
135: * Replaces the value at the specified position in this table with the specified value.
136: * @param row The row index
137: * @param column The column name
138: * @param value The value to store at the specified location
139: * @return the value previously at the specified location
140: * @throws RowOutOfBoundsException if the row index is out of range
141: * @throws NoSuchColumnException if the column does not exist
142: * @throws DataSetException if an unexpected error occurs
143: */
144: public Object setValue(int row, String column, Object value)
145: throws RowOutOfBoundsException, NoSuchColumnException,
146: DataSetException {
147: logger.debug("setValue(row=" + row + ", column=" + column
148: + ", value=" + value + ") - start");
149:
150: assertValidRowIndex(row);
151:
152: Object[] rowValues = (Object[]) _rowList.get(row);
153: int columnIndex = getColumnIndex(column);
154: Object oldValue = rowValues[columnIndex];
155: rowValues[columnIndex] = value;
156: return oldValue;
157: }
158:
159: ////////////////////////////////////////////////////////////////////////////
160: // ITable interface
161:
162: public ITableMetaData getTableMetaData() {
163: logger.debug("getTableMetaData() - start");
164:
165: return _metaData;
166: }
167:
168: public int getRowCount() {
169: logger.debug("getRowCount() - start");
170:
171: return _rowList.size();
172: }
173:
174: public Object getValue(int row, String column)
175: throws DataSetException {
176: logger.debug("getValue(row=" + row + ", column=" + column
177: + ") - start");
178:
179: assertValidRowIndex(row);
180:
181: Object[] rowValues = (Object[]) _rowList.get(row);
182: return rowValues[getColumnIndex(column)];
183: }
184:
185: }
|