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  
60      /**
61       * Creates a CachedDataSet that synchronously consume the specified producer.
62       */
63      public CachedDataSet(IDataSetProducer producer) throws DataSetException
64      {
65          this(producer, false);
66      }
67  
68      /**
69       * Creates a CachedDataSet that synchronously consume the specified producer.
70       * @param producer
71       * @param caseSensitiveTableNames Whether or not case sensitive table names should be used
72       * @throws DataSetException
73       */
74      public CachedDataSet(IDataSetProducer producer, boolean caseSensitiveTableNames) throws DataSetException
75      {
76          super(caseSensitiveTableNames);
77          initialize();
78  
79          producer.setConsumer(this);
80          producer.produce();
81      }
82  
83      ////////////////////////////////////////////////////////////////////////////
84      // AbstractDataSet class
85  
86      protected ITableIterator createIterator(boolean reversed)
87              throws DataSetException
88      {
89          if(logger.isDebugEnabled())
90              logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
91  
92          ITable[] tables = (ITable[])_orderedTableNameMap.orderedValues().toArray(new ITable[0]);
93          return new DefaultTableIterator(tables, reversed);
94      }
95  
96      ////////////////////////////////////////////////////////////////////////
97      // IDataSetConsumer interface
98  
99      public void startDataSet() throws DataSetException
100     {
101         logger.debug("startDataSet() - start");
102         _orderedTableNameMap = super.createTableNameMap();
103     }
104 
105     public void endDataSet() throws DataSetException
106     {
107         logger.debug("endDataSet() - start");
108         logger.debug("endDataSet() - the final tableMap is: " + _orderedTableNameMap);
109     }
110 
111     public void startTable(ITableMetaData metaData) throws DataSetException
112     {
113         logger.debug("startTable(metaData={}) - start", metaData);
114         _activeTable = new DefaultTable(metaData);
115     }
116 
117     public void endTable() throws DataSetException
118     {
119         logger.debug("endTable() - start");
120         String tableName = _activeTable.getTableMetaData().getTableName();
121         // Check whether the table appeared once before
122         if(_orderedTableNameMap.containsTable(tableName))
123         {
124             DefaultTable existingTable = (DefaultTable)_orderedTableNameMap.get(tableName);
125             // Add all newly collected rows to the existing table
126             existingTable.addTableRows(_activeTable);
127         }
128         else
129         {
130             _orderedTableNameMap.add(tableName, _activeTable);
131         }
132         _activeTable = null;
133     }
134 
135     public void row(Object[] values) throws DataSetException
136     {
137         logger.debug("row(values={}) - start", values);
138         _activeTable.addRow(values);
139     }
140 }