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  
40      /**
41       * Logger for this class
42       */
43      private static final Logger logger = LoggerFactory.getLogger(CachedDataSet.class);
44  
45      private OrderedTableNameMap _tables;
46  
47      private DefaultTable _activeTable;
48      
49  
50      /**
51       * Default constructor.
52       */
53      public CachedDataSet()
54      {
55          super();
56          initialize();
57      }
58  
59      /**
60       * Creates a copy of the specified dataset.
61       */
62      public CachedDataSet(IDataSet dataSet) throws DataSetException
63      {
64          super(dataSet.isCaseSensitiveTableNames());
65  
66          initialize();
67  
68          ITableIterator iterator = dataSet.iterator();
69          while (iterator.next())
70          {
71              ITable table = iterator.getTable();
72              _tables.add(table.getTableMetaData().getTableName(), new CachedTable(table));
73          }
74      }
75  
76      /**
77       * Creates a CachedDataSet that synchronously consume the specified producer.
78       */
79      public CachedDataSet(IDataSetProducer producer) throws DataSetException
80      {
81          this(producer, false);
82      }
83  
84      /**
85       * Creates a CachedDataSet that synchronously consume the specified producer.
86       * @param producer
87       * @param caseSensitiveTableNames Whether or not case sensitive table names should be used
88       * @throws DataSetException
89       */
90      public CachedDataSet(IDataSetProducer producer, boolean caseSensitiveTableNames) throws DataSetException
91      {
92          super(caseSensitiveTableNames);
93          
94          initialize();
95  
96          producer.setConsumer(this);
97          producer.produce();
98      }
99  
100     private void initialize()
101     {
102         _tables = super.createTableNameMap();
103     }
104     
105     ////////////////////////////////////////////////////////////////////////////
106     // AbstractDataSet class
107 
108     protected ITableIterator createIterator(boolean reversed)
109             throws DataSetException
110     {
111         if(logger.isDebugEnabled())
112             logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
113         
114         ITable[] tables = (ITable[])_tables.orderedValues().toArray(new ITable[0]);
115         return new DefaultTableIterator(tables, reversed);
116     }
117 
118     ////////////////////////////////////////////////////////////////////////
119     // IDataSetConsumer interface
120 
121     public void startDataSet() throws DataSetException
122     {
123         logger.debug("startDataSet() - start");
124         _tables = super.createTableNameMap();
125     }
126 
127     public void endDataSet() throws DataSetException
128     {
129         logger.debug("endDataSet() - start");
130         logger.debug("endDataSet() - the final tableMap is: " + _tables);
131     }
132 
133     public void startTable(ITableMetaData metaData) throws DataSetException
134     {
135         logger.debug("startTable(metaData={}) - start", metaData);
136         _activeTable = new DefaultTable(metaData);
137     }
138 
139     public void endTable() throws DataSetException
140     {
141         logger.debug("endTable() - start");
142         String tableName = _activeTable.getTableMetaData().getTableName();
143         // Check whether the table appeared once before
144         if(_tables.containsTable(tableName))
145         {
146             DefaultTable existingTable = (DefaultTable)_tables.get(tableName);
147             // Add all newly collected rows to the existing table
148             existingTable.addTableRows(_activeTable);
149         }
150         else
151         {
152             _tables.add(tableName, _activeTable);
153         }
154         _activeTable = null;
155     }
156 
157     public void row(Object[] values) throws DataSetException
158     {
159         logger.debug("row(values={}) - start", values);
160         _activeTable.addRow(values);
161     }
162 }