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.stream;
22  
23  import org.dbunit.dataset.Column;
24  import org.dbunit.dataset.DataSetException;
25  import org.dbunit.dataset.IDataSet;
26  import org.dbunit.dataset.ITable;
27  import org.dbunit.dataset.ITableIterator;
28  import org.dbunit.dataset.ITableMetaData;
29  import org.dbunit.dataset.RowOutOfBoundsException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * Implementation of {@link IDataSetProducer} based on a given {@link IDataSet} or a
36   * {@link ITableIterator}.
37   * 
38   * @author Manuel Laflamme
39   * @author Last changed by: $Author$
40   * @version $Revision$ $Date$
41   * @since Apr 17, 2003
42   */
43  public class DataSetProducerAdapter implements IDataSetProducer
44  {
45  
46      /**
47       * Logger for this class
48       */
49      private static final Logger logger = LoggerFactory.getLogger(DataSetProducerAdapter.class);
50  
51      private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
52  
53      private final ITableIterator _iterator;
54      private IDataSetConsumer _consumer = EMPTY_CONSUMER;
55  
56      public DataSetProducerAdapter(ITableIterator iterator)
57      {
58          _iterator = iterator;
59      }
60  
61      public DataSetProducerAdapter(IDataSet dataSet) throws DataSetException
62      {
63          _iterator = dataSet.iterator();
64      }
65  
66      ////////////////////////////////////////////////////////////////////////////
67      // IDataSetProducer interface
68  
69      public void setConsumer(IDataSetConsumer consumer) throws DataSetException
70      {
71          logger.debug("setConsumer(consumer) - start");
72  
73          _consumer = consumer;
74      }
75  
76      public void produce() throws DataSetException
77      {
78          logger.debug("produce() - start");
79  
80          _consumer.startDataSet();
81          while(_iterator.next())
82          {
83              ITable table = _iterator.getTable();
84              ITableMetaData metaData = table.getTableMetaData();
85  
86              _consumer.startTable(metaData);
87              try
88              {
89                  Column[] columns = metaData.getColumns();
90                  if (columns.length == 0)
91                  {
92                      _consumer.endTable();
93                      continue;
94                  }
95  
96                  for (int i = 0; ; i++)
97                  {
98                      Object[] values = new Object[columns.length];
99                      for (int j = 0; j < columns.length; j++)
100                     {
101                         Column column = columns[j];
102                         values[j] = table.getValue(i, column.getColumnName());
103                     }
104                     _consumer.row(values);
105                 }
106             }
107             catch (RowOutOfBoundsException e)
108             {
109             	// This exception occurs when records are exhausted
110             	// and we reach the end of the table.  Ignore this error
111             	// and close table.
112 
113                 // end of table
114                 _consumer.endTable();
115             }
116         }
117         _consumer.endDataSet();
118     }
119 }
120