KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * This abstract class provides the basic implementation of the IDataSet
32  * interface. Subclass are only required to implement the {@link #createIterator}
33  * method.
34  *
35  * @author Manuel Laflamme
36  * @version $Revision: 1.11 $
37  * @since Feb 22, 2002
38  */

39 public abstract class AbstractDataSet implements IDataSet
40 {
41     protected ITable[] cloneTables(ITable[] tables)
42     {
43         ITable[] clones = new ITable[tables.length];
44         for (int i = 0; i < tables.length; i++)
45         {
46             clones[i] = tables[i];
47         }
48         return clones;
49     }
50
51     protected abstract ITableIterator createIterator(boolean reversed)
52             throws DataSetException;
53
54     ////////////////////////////////////////////////////////////////////////////
55
// IDataSet interface
56

57     public String JavaDoc[] getTableNames() throws DataSetException
58     {
59         List JavaDoc tableNameList = new ArrayList JavaDoc();
60         ITableIterator iterator = createIterator(false);
61         while (iterator.next())
62         {
63             tableNameList.add(iterator.getTableMetaData().getTableName());
64         }
65         return (String JavaDoc[])tableNameList.toArray(new String JavaDoc[0]);
66     }
67
68     public ITableMetaData getTableMetaData(String JavaDoc tableName) throws DataSetException
69     {
70         return getTable(tableName).getTableMetaData();
71     }
72
73     public ITable getTable(String JavaDoc tableName) throws DataSetException
74     {
75         ITable found = null;
76         ITableIterator iterator = createIterator(false);
77         while (iterator.next())
78         {
79             ITable table = iterator.getTable();
80             if (tableName.equalsIgnoreCase(table.getTableMetaData().getTableName()))
81             {
82                 if (found != null)
83                 {
84                     throw new AmbiguousTableNameException(tableName);
85                 }
86
87                 found = table;
88             }
89         }
90
91         if (found != null)
92         {
93             return found;
94         }
95
96         throw new NoSuchTableException(tableName);
97     }
98
99     public ITable[] getTables() throws DataSetException
100     {
101         List JavaDoc tableList = new ArrayList JavaDoc();
102         ITableIterator iterator = createIterator(false);
103         while (iterator.next())
104         {
105             tableList.add(iterator.getTable());
106         }
107         return (ITable[])tableList.toArray(new ITable[0]);
108     }
109
110     public ITableIterator iterator() throws DataSetException
111     {
112         return createIterator(false);
113     }
114
115     public ITableIterator reverseIterator() throws DataSetException
116     {
117         return createIterator(true);
118     }
119
120     ////////////////////////////////////////////////////////////////////////////
121
// Object class
122

123     public String JavaDoc toString()
124     {
125         try
126         {
127             return Arrays.asList(getTableNames()).toString();
128         }
129         catch (DataSetException e)
130         {
131             return super.toString();
132         }
133     }
134 }
135
136
137
138
139
140
141
Popular Tags