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 org.dbunit.dataset.stream.IDataSetConsumer;
25  import org.dbunit.dataset.stream.IDataSetProducer;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Hold copy of another dataset or a consumed provider content.
31   *
32   * @author Manuel Laflamme
33   * @author Last changed by: $Author$
34   * @version $Revision$ $Date$
35   * @since 1.x (Apr 18, 2003)
36   */
37  public class CachedDataSet extends AbstractDataSet implements IDataSetConsumer
38  {
39      private static final Logger logger = LoggerFactory.getLogger(CachedDataSet.class);
40  
41      private DefaultTable _activeTable;
42  
43      /**
44       * Default constructor.
45       */
46      public CachedDataSet() throws DataSetException {
47          super();
48          initialize();
49      }
50  
51      /**
52       * Creates a copy of the specified dataset.
53       */
54      public CachedDataSet(IDataSet dataSet) throws DataSetException
55      {
56          super(dataSet.isCaseSensitiveTableNames());
57          initialize();
58  
59          final ITableIterator iterator = dataSet.iterator();
60          while (iterator.next())
61           {
62              final ITable table = iterator.getTable();
63              _orderedTableNameMap.add(table.getTableMetaData().getTableName(),
64                      new CachedTable(table));
65          }
66      }
67  
68      /**
69       * Creates a CachedDataSet that synchronously consume the specified producer.
70       */
71      public CachedDataSet(IDataSetProducer producer) throws DataSetException
72      {
73          this(producer, false);
74      }
75  
76      /**
77       * Creates a CachedDataSet that synchronously consume the specified producer.
78       * @param producer
79       * @param caseSensitiveTableNames Whether or not case sensitive table names should be used
80       * @throws DataSetException
81       */
82      public CachedDataSet(IDataSetProducer producer, boolean caseSensitiveTableNames) throws DataSetException
83      {
84          super(caseSensitiveTableNames);
85          initialize();
86  
87          producer.setConsumer(this);
88          producer.produce();
89      }
90  
91      ////////////////////////////////////////////////////////////////////////////
92      // AbstractDataSet class
93  
94      protected ITableIterator createIterator(boolean reversed)
95              throws DataSetException
96      {
97          if(logger.isDebugEnabled())
98              logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
99  
100         ITable[] tables = (ITable[])_orderedTableNameMap.orderedValues().toArray(new ITable[0]);
101         return new DefaultTableIterator(tables, reversed);
102     }
103 
104     ////////////////////////////////////////////////////////////////////////
105     // IDataSetConsumer interface
106 
107     public void startDataSet() throws DataSetException
108     {
109         logger.debug("startDataSet() - start");
110         _orderedTableNameMap = super.createTableNameMap();
111     }
112 
113     public void endDataSet() throws DataSetException
114     {
115         logger.debug("endDataSet() - start");
116         logger.debug("endDataSet() - the final tableMap is: " + _orderedTableNameMap);
117     }
118 
119     public void startTable(ITableMetaData metaData) throws DataSetException
120     {
121         logger.debug("startTable(metaData={}) - start", metaData);
122         _activeTable = new DefaultTable(metaData);
123     }
124 
125     public void endTable() throws DataSetException
126     {
127         logger.debug("endTable() - start");
128         String tableName = _activeTable.getTableMetaData().getTableName();
129         // Check whether the table appeared once before
130         if(_orderedTableNameMap.containsTable(tableName))
131         {
132             DefaultTable existingTable = (DefaultTable)_orderedTableNameMap.get(tableName);
133             // Add all newly collected rows to the existing table
134             existingTable.addTableRows(_activeTable);
135         }
136         else
137         {
138             _orderedTableNameMap.add(tableName, _activeTable);
139         }
140         _activeTable = null;
141     }
142 
143     public void row(Object[] values) throws DataSetException
144     {
145         logger.debug("row(values={}) - start", values);
146         _activeTable.addRow(values);
147     }
148 }