|
From: <de...@us...> - 2002-12-11 15:53:55
|
Update of /cvsroot/dbunit/dbunit/src/java/org/dbunit/ant
In directory sc8-pr-cvs1:/tmp/cvs-serv28874
Modified Files:
Export.java Table.java
Added Files:
Query.java
Log Message:
New Ant support for <query> element
--- NEW FILE: Query.java ---
/*
* Query.java Jun 10, 2002
*
* The DbUnit Database Testing Framework
* Copyright (C)2002, Timothy Ruppert && Ben Cox
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package org.dbunit.ant;
/**
* The <code>Query</code> class is just a step placeholder for a table name
* within an <code>Export</code>.
*
* @author Timothy Ruppert && Ben Cox
* @version $Revision: 1.1 $
*/
public class Query
{
private String name;
private String sql;
public Query()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
StringBuffer result = new StringBuffer();
result.append("Query: ");
result.append(" name=" + name);
result.append(" sql=" + sql);
return result.toString();
}
public String getSql()
{
return sql;
}
public void setSql(String sql)
{
this.sql = sql;
}
}
Index: Export.java
===================================================================
RCS file: /cvsroot/dbunit/dbunit/src/java/org/dbunit/ant/Export.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Export.java 3 Aug 2002 14:49:49 -0000 1.4
--- Export.java 11 Dec 2002 15:53:50 -0000 1.5
***************
*** 26,29 ****
--- 26,31 ----
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
+ import org.dbunit.dataset.QueryDataSet;
+ import org.dbunit.dataset.CompositeDataSet;
import org.dbunit.dataset.xml.*;
***************
*** 49,52 ****
--- 51,55 ----
private String format = "flat";
private List tables = new ArrayList();
+ private List queries = new ArrayList();
public Export()
***************
*** 74,77 ****
--- 77,85 ----
}
+ public List getQueries()
+ {
+ return queries;
+ }
+
public void setDest(File dest)
{
***************
*** 98,141 ****
}
public void execute(IDatabaseConnection connection) throws DatabaseUnitException
{
! IDataSet dataset;
try
{
! if (tables.size() == 0)
! {
! dataset = connection.createDataSet();
! }
! else
! {
! dataset = connection.createDataSet(getTableArray());
! }
if (dest == null)
{
throw new DatabaseUnitException("'dest' is a required attribute of the <export> step.");
}
else
{
! OutputStream out = new FileOutputStream(dest);
! try
! {
! if (format.equalsIgnoreCase("flat"))
! {
! FlatXmlDataSet.write(dataset, out);
}
! else if (format.equalsIgnoreCase("xml"))
! {
! XmlDataSet.write(dataset, out);
}
! else if (format.equalsIgnoreCase("dtd"))
! {
! FlatDtdDataSet.write(dataset, out);
}
}
! finally
{
! out.close();
}
}
}
--- 106,175 ----
}
+ public void addQuery(Query query)
+ {
+ queries.add(query);
+ }
+
public void execute(IDatabaseConnection connection) throws DatabaseUnitException
{
! IDataSet dataset = null;
try
{
!
if (dest == null)
{
throw new DatabaseUnitException("'dest' is a required attribute of the <export> step.");
}
+ // retrieve the dataset if no tables or queries specifedid.
+ if (tables.size() == 0 && queries.size()==0)
+ {
+ dataset = connection.createDataSet();
+ }
else
{
! if (tables.size() > 0) {
! dataset = connection.createDataSet(getTableArray());
! }
! if (queries.size() > 0) {
! QueryDataSet queryDataSet = new QueryDataSet(connection);
! for (int i = 0; i < queries.size(); i++){
! Query query = (Query)queries.get(i);
! if (query.getSql() == null){
! throw new DatabaseUnitException("'sql' is a required attribute of the <query> step.");
! }
! queryDataSet.addTable(query.getName(),query.getSql());
!
}
! //crummy merge!
! if(dataset != null) {
! dataset = new CompositeDataSet(queryDataSet,dataset);
}
! else {
! dataset = queryDataSet;
}
}
!
! }
! // save the dataset
! OutputStream out = new FileOutputStream(dest);
! try
! {
! if (format.equalsIgnoreCase("flat"))
{
! FlatXmlDataSet.write(dataset, out);
! }
! else if (format.equalsIgnoreCase("xml"))
! {
! XmlDataSet.write(dataset, out);
! }
! else if (format.equalsIgnoreCase("dtd"))
! {
! FlatDtdDataSet.write(dataset, out);
}
}
+ finally
+ {
+ out.close();
+ }
}
***************
*** 161,164 ****
--- 195,208 ----
}
+ private String[] convertListToStringArray(List list){
+ String []strArray = new String[list.size()];
+ for (int i = 0; i < list.size(); i++)
+ {
+ Table table = (Table)list.get(i);
+ strArray[i] = table.getName();
+ }
+ return strArray;
+ }
+
public String getLogMessage()
{
***************
*** 174,179 ****
result.append("Export: ");
result.append(" dest=" + getAbsolutePath(dest));
! result.append(", format= " + tables);
result.append(", tables= " + tables);
return result.toString();
--- 218,224 ----
result.append("Export: ");
result.append(" dest=" + getAbsolutePath(dest));
! result.append(", format= " + format);
result.append(", tables= " + tables);
+ result.append(", queries= " + queries);
return result.toString();
Index: Table.java
===================================================================
RCS file: /cvsroot/dbunit/dbunit/src/java/org/dbunit/ant/Table.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Table.java 13 Jun 2002 17:24:55 -0000 1.2
--- Table.java 11 Dec 2002 15:53:50 -0000 1.3
***************
*** 34,37 ****
--- 34,38 ----
private String name;
+ private String query;
public Table()
***************
*** 55,60 ****
--- 56,73 ----
result.append("Table: ");
result.append(" name=" + name);
+ result.append(" query=" + query);
+
return result.toString();
+ }
+
+ public String getQuery()
+ {
+ return query;
+ }
+
+ public void setQuery(String query)
+ {
+ this.query = query;
}
}
|