KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > ant > AbstractStep


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.ant;
22
23 import org.dbunit.dataset.IDataSet;
24 import org.dbunit.dataset.CachedDataSet;
25 import org.dbunit.dataset.csv.CsvProducer;
26 import org.dbunit.dataset.xml.XmlProducer;
27 import org.dbunit.dataset.xml.FlatXmlProducer;
28 import org.dbunit.dataset.xml.FlatDtdProducer;
29 import org.dbunit.dataset.stream.IDataSetProducer;
30 import org.dbunit.dataset.stream.StreamingDataSet;
31 import org.dbunit.database.IDatabaseConnection;
32 import org.dbunit.database.IResultSetTableFactory;
33 import org.dbunit.database.ForwardOnlyResultSetTableFactory;
34 import org.dbunit.database.CachedResultSetTableFactory;
35 import org.dbunit.database.DatabaseConfig;
36 import org.dbunit.database.QueryDataSet;
37 import org.dbunit.DatabaseUnitException;
38
39 import org.xml.sax.InputSource JavaDoc;
40
41 import java.util.List JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 /**
48  * @author Manuel Laflamme
49  * @since Apr 3, 2004
50  * @version $Revision: 1.2 $
51  */

52 public abstract class AbstractStep implements DbUnitTaskStep
53 {
54     public static final String JavaDoc FORMAT_FLAT = "flat";
55     public static final String JavaDoc FORMAT_XML = "xml";
56     public static final String JavaDoc FORMAT_DTD = "dtd";
57     public static final String JavaDoc FORMAT_CSV = "csv";
58
59     protected IDataSet getDatabaseDataSet(IDatabaseConnection connection,
60             List JavaDoc tables, boolean forwardonly) throws DatabaseUnitException
61     {
62         try
63         {
64             // Setup the ResultSet table factory
65
IResultSetTableFactory factory = null;
66             if (forwardonly)
67             {
68                 factory = new ForwardOnlyResultSetTableFactory();
69             }
70             else
71             {
72                 factory = new CachedResultSetTableFactory();
73             }
74             DatabaseConfig config = connection.getConfig();
75             config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY,
76                     factory);
77
78             // Retrieve the complete database if no tables or queries specified.
79
if (tables.size() == 0)
80             {
81                 return connection.createDataSet();
82             }
83
84             QueryDataSet queryDataset = new QueryDataSet(connection);
85             for (Iterator JavaDoc it = tables.iterator(); it.hasNext();)
86             {
87                 Object JavaDoc item = it.next();
88                 if (item instanceof Query)
89                 {
90                     Query queryItem = (Query)item;
91                     queryDataset.addTable(queryItem.getName(), queryItem.getSql());
92                 }
93                 else
94                 {
95                     Table tableItem = (Table)item;
96                     queryDataset.addTable(tableItem.getName());
97                 }
98             }
99
100             return queryDataset;
101         }
102         catch (SQLException JavaDoc e)
103         {
104             throw new DatabaseUnitException(e);
105         }
106     }
107
108     protected IDataSet getSrcDataSet(File JavaDoc src, String JavaDoc format,
109             boolean forwardonly) throws DatabaseUnitException
110     {
111         try
112         {
113             IDataSetProducer producer = null;
114             if (format.equalsIgnoreCase(FORMAT_XML))
115             {
116                 producer = new XmlProducer(new InputSource JavaDoc(src.toURL().toString()));
117             }
118             else if (format.equalsIgnoreCase(FORMAT_CSV))
119             {
120                 producer = new CsvProducer(src);
121             }
122             else if (format.equalsIgnoreCase(FORMAT_FLAT))
123             {
124                 producer = new FlatXmlProducer(new InputSource JavaDoc(src.toURL().toString()));
125             }
126             else if (format.equalsIgnoreCase(FORMAT_DTD))
127             {
128                 producer = new FlatDtdProducer(new InputSource JavaDoc(src.toURL().toString()));
129             }
130             else
131             {
132                 throw new IllegalArgumentException JavaDoc("Type must be either 'flat'(default), 'xml', 'csv' or 'dtd' but was: " + format);
133             }
134
135             if (forwardonly)
136             {
137                 return new StreamingDataSet(producer);
138             }
139             return new CachedDataSet(producer);
140         }
141         catch (IOException JavaDoc e)
142         {
143             throw new DatabaseUnitException(e);
144         }
145     }
146 }
147
Popular Tags