KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > CaseInsensitiveDataSet


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
26 /**
27  * Allows access to a decorated dataset in a case insensitive way. Dataset
28  * implementations provided by the framework are case sensitive. This class
29  * allows using them in situation where case sensitiveness is not desirable.
30  *
31  * @author Manuel Laflamme
32  * @version $Revision: 1.8 $
33  * @deprecated All IDataSet implementations are case insensitive since DbUnit 1.5
34  */

35 public class CaseInsensitiveDataSet extends AbstractDataSet
36 {
37     private final IDataSet _dataSet;
38
39     public CaseInsensitiveDataSet(IDataSet dataSet)
40     {
41         _dataSet = dataSet;
42     }
43
44     private String JavaDoc getInternalTableName(String JavaDoc tableName) throws DataSetException
45     {
46         String JavaDoc found = null;
47         String JavaDoc[] names = _dataSet.getTableNames();
48         for (int i = 0; i < names.length; i++)
49         {
50             if (tableName.equalsIgnoreCase(names[i]))
51             {
52                 if (found != null)
53                 {
54                     throw new AmbiguousTableNameException(tableName);
55                 }
56                 found = names[i];
57             }
58         }
59
60         if (found != null)
61         {
62             return found;
63         }
64
65         throw new NoSuchTableException(tableName);
66     }
67
68     ////////////////////////////////////////////////////////////////////////////
69
// AbstractDataSet class
70

71     protected ITableIterator createIterator(boolean reversed)
72             throws DataSetException
73     {
74         return new CaseInsensitiveIterator(reversed ?
75                 _dataSet.reverseIterator() : _dataSet.iterator());
76     }
77
78     ////////////////////////////////////////////////////////////////////////////
79
// IDataSet interface
80

81     public String JavaDoc[] getTableNames() throws DataSetException
82     {
83         return _dataSet.getTableNames();
84     }
85
86     public ITableMetaData getTableMetaData(String JavaDoc tableName)
87             throws DataSetException
88     {
89         return _dataSet.getTableMetaData(getInternalTableName(tableName));
90     }
91
92     public ITable getTable(String JavaDoc tableName) throws DataSetException
93     {
94         ITable table = _dataSet.getTable(getInternalTableName(tableName));
95         return new CaseInsensitiveTable(table);
96     }
97
98     ////////////////////////////////////////////////////////////////////////////
99
// CaseInsensitiveIterator class
100

101     private class CaseInsensitiveIterator implements ITableIterator
102     {
103         private final ITableIterator _iterator;
104
105         public CaseInsensitiveIterator(ITableIterator iterator)
106         {
107             _iterator = iterator;
108         }
109
110         ////////////////////////////////////////////////////////////////////////
111
// ITableIterator interface
112

113         public boolean next() throws DataSetException
114         {
115             return _iterator.next();
116         }
117
118         public ITableMetaData getTableMetaData() throws DataSetException
119         {
120             return _iterator.getTableMetaData();
121         }
122
123         public ITable getTable() throws DataSetException
124         {
125             return new CaseInsensitiveTable(_iterator.getTable());
126         }
127     }
128 }
129
130
131
132
Popular Tags