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;
22  
23  import org.dbunit.database.QueryTableIterator;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Decorator that allows forward only access to decorated dataset.
29   *
30   * @author Manuel Laflamme
31   * @since Apr 9, 2003
32   * @version $Revision$
33   */
34  public class ForwardOnlyDataSet extends AbstractDataSet
35  {
36  
37      /**
38       * Logger for this class
39       */
40      private static final Logger logger = LoggerFactory.getLogger(ForwardOnlyDataSet.class);
41  
42      private final IDataSet _dataSet;
43      private int _iteratorCount;
44  
45      public ForwardOnlyDataSet(IDataSet dataSet)
46      {
47          _dataSet = dataSet;
48      }
49  
50      ////////////////////////////////////////////////////////////////////////////
51      // AbstractDataSet class
52  
53      protected ITableIterator createIterator(boolean reversed)
54              throws DataSetException
55      {
56          logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
57  
58          if (reversed)
59          {
60              throw new UnsupportedOperationException("Reverse iterator not supported!");
61          }
62  
63          if (_iteratorCount > 0)
64          {
65              throw new UnsupportedOperationException("Only one iterator allowed!");
66          }
67  
68          return new ForwardOnlyIterator(_dataSet.iterator());
69      }
70  
71      ////////////////////////////////////////////////////////////////////////////
72      // IDataSet interface
73  
74      public String[] getTableNames() throws DataSetException
75      {
76          throw new UnsupportedOperationException();
77      }
78  
79      public ITableMetaData getTableMetaData(String tableName) throws DataSetException
80      {
81          throw new UnsupportedOperationException();
82      }
83  
84      public ITable getTable(String tableName) throws DataSetException
85      {
86          throw new UnsupportedOperationException();
87      }
88  
89      ////////////////////////////////////////////////////////////////////////////
90      // ForwardOnlyIterator class
91  
92      private class ForwardOnlyIterator implements ITableIterator
93      {
94          private final ITableIterator _iterator;
95  
96          public ForwardOnlyIterator(ITableIterator iterator)
97          {
98              _iterator = iterator;
99              _iteratorCount++;
100         }
101 
102         ////////////////////////////////////////////////////////////////////////////
103         // ITableIterator interface
104 
105         public boolean next() throws DataSetException
106         {
107             if(_iterator instanceof QueryTableIterator) {
108                 return ((QueryTableIterator) _iterator).nextWithoutClosing();
109             } else {
110                 return _iterator.next();
111             }
112         }
113 
114         public ITableMetaData getTableMetaData() throws DataSetException
115         {
116             return _iterator.getTableMetaData();
117         }
118 
119         public ITable getTable() throws DataSetException
120         {
121             return new ForwardOnlyTable(_iterator.getTable());
122         }
123     }
124 }