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.database.AmbiguousTableNameException;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  /**
30   * Simple implementation of a dataset backed by {@link ITable} objects which can
31   * be added dynamically.
32   * 
33   * @author Manuel Laflamme
34   * @author Last changed by: $Author$
35   * @version $Revision$ $Date$
36   * @since 1.0 (Feb 18, 2002)
37   */
38  public class DefaultDataSet extends AbstractDataSet
39  {
40  
41      /**
42       * Logger for this class
43       */
44      private static final Logger logger = LoggerFactory.getLogger(DefaultDataSet.class);
45  
46      public DefaultDataSet()
47      {
48      	super();
49      }
50  
51      /**
52       * Creates a default dataset which is empty initially
53       * @param caseSensitiveTableNames
54       * @since 2.4.2
55       */
56      public DefaultDataSet(boolean caseSensitiveTableNames)
57      {
58          super(caseSensitiveTableNames);
59      }
60  
61      public DefaultDataSet(ITable table) throws AmbiguousTableNameException
62      {
63          this(new ITable[]{table});
64      }
65  
66      public DefaultDataSet(ITable table1, ITable table2) throws AmbiguousTableNameException
67      {
68          this(new ITable[] {table1, table2});
69      }
70  
71      public DefaultDataSet(ITable[] tables) throws AmbiguousTableNameException
72      {
73          this(tables, false);
74      }
75      
76      /**
77       * Creates a default dataset which consists of the given tables
78       * @param caseSensitiveTableNames
79       * @since 2.4.2
80       */
81      public DefaultDataSet(ITable[] tables, boolean caseSensitiveTableNames) throws AmbiguousTableNameException
82      {
83          super(caseSensitiveTableNames);
84          
85          for (int i = 0; i < tables.length; i++)
86          {
87              addTable(tables[i]);
88          }
89      }
90  
91      /**
92       * Add a new table in this dataset.
93       * @throws AmbiguousTableNameException 
94       */
95      public void addTable(ITable table) throws AmbiguousTableNameException
96      {
97          logger.debug("addTable(table={}) - start", table);
98          
99          this.initialize();
100         
101         super._orderedTableNameMap.add(table.getTableMetaData().getTableName(), table);
102     }
103 
104     /**
105      * Initializes the {@link _orderedTableNameMap} of the parent class if it is not initialized yet.
106      * @throws DataSetException
107      * @since 2.4.6
108      */
109     protected void initialize()
110     {
111         logger.debug("initialize() - start");
112         
113         if(_orderedTableNameMap != null)
114         {
115             logger.debug("The table name map has already been initialized.");
116             // already initialized
117             return;
118         }
119        
120         // Gather all tables in the OrderedTableNameMap which also makes the duplicate check
121         _orderedTableNameMap = this.createTableNameMap();
122 
123     }
124 
125     ////////////////////////////////////////////////////////////////////////////
126     // AbstractDataSet class
127 
128     protected ITableIterator createIterator(boolean reversed)
129             throws DataSetException
130     {
131         logger.debug("createIterator(reversed={}) - start", Boolean.toString(reversed));
132         
133         this.initialize();
134         
135         ITable[] tables = (ITable[])_orderedTableNameMap.orderedValues().toArray(new ITable[0]);
136         return new DefaultTableIterator(tables, reversed);
137     }
138 }
139 
140 
141 
142 
143 
144