From: dion g. <dio...@us...> - 2004-08-01 13:17:32
|
diongillard 04/08/01 06:17:23 Modified: dbunit/src/main/net/sourceforge/mavenplugins/dbunit DbUnitTool.java DataSetTool.java dbunit plugin.jelly Added: dbunit/src/main/net/sourceforge/mavenplugins/dbunit DataSetConverter.java Log: Add support for converting datasets Revision Changes Path 1.2 +80 -10 maven-plugins/dbunit/src/main/net/sourceforge/mavenplugins/dbunit/DbUnitTool.java Index: DbUnitTool.java =================================================================== RCS file: /cvsroot/maven-plugins/maven-plugins/dbunit/src/main/net/sourceforge/mavenplugins/dbunit/DbUnitTool.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DbUnitTool.java 1 Aug 2004 11:14:35 -0000 1.1 +++ DbUnitTool.java 1 Aug 2004 13:17:22 -0000 1.2 @@ -55,17 +55,28 @@ * * ==================================================================== */ -import org.dbunit.database.IDatabaseConnection; +import org.dbunit.database.IDatabaseConnection; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; -import org.dbunit.database.DatabaseConnection; - +import org.dbunit.database.DatabaseConnection; +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.excel.XlsDataSet; +import org.dbunit.dataset.xml.FlatXmlDataSet; +import org.dbunit.dataset.xml.XmlDataSet; + /** * Base class for working with DbUnit * * @author Dion Gillard - */ + */ public class DbUnitTool { private String driverClassName; @@ -78,12 +89,12 @@ public String getDriverClassName() { return driverClassName; - } + } public void setDriverClassName(String name) { driverClassName = name; } - + public String getUrl() { return url; @@ -92,7 +103,7 @@ { url = newURL; } - + public String getUser() { return user; @@ -101,16 +112,16 @@ { user = newUser; } - + public String getPassword() - { + { return password; } public void setPassword(String newPassword) { password = newPassword; } - + public String getFileName() { return fileName; @@ -129,6 +140,11 @@ this.schema = schema; } + /** + * @return a database connection from the url, user and password + * @throws SQLException if some other jdbc error happens + * @throws ClassNotFoundException if the jdbc driver can't be found + */ protected IDatabaseConnection getConnection() throws SQLException, ClassNotFoundException { Class driverClass = Class.forName(getDriverClassName()); @@ -145,4 +161,58 @@ } return connection; } + + /** + * @return a dataset from the file of the given format + * @param fileName the name of the file + * @param format the dbunit format, e.g. excel, xml, flat etc + */ + protected IDataSet getDataSetFromFile(String fileName, String format) throws DataSetException, IOException + { + IDataSet dataset = null; + if ("EXCEL".equalsIgnoreCase(format)) + { + dataset = new XlsDataSet(new File(fileName)); + } + else if ("XML".equalsIgnoreCase(format)) + { + dataset = new XmlDataSet(new FileReader(fileName)); + } + else if ("FLAT".equalsIgnoreCase(format)) + { + dataset = new FlatXmlDataSet(new File(fileName)); + } + else + { + throw new IllegalArgumentException("dataset of format '" + format + "' is not supported."); + } + return dataset; + } + + /** + * Write the dataset to the file in that format. + * @param fileName the name of the file + * @param format the dbunit format, e.g. excel, xml, flat etc + * @param the data set to write + * @throws DataSetException + * @throws FileNotFoundException + * @throws IOException + */ + protected void writeDataSetToFile(String fileName, String format, IDataSet dataset) throws DataSetException, FileNotFoundException, IOException + { + FileOutputStream stream = new FileOutputStream(fileName); + if ("EXCEL".equalsIgnoreCase(format)) + { + XlsDataSet.write(dataset, stream); + } + else if ("XML".equalsIgnoreCase(format)) + { + XmlDataSet.write(dataset, stream); + } + else if ("FLAT".equalsIgnoreCase(format)) + { + FlatXmlDataSet.write(dataset, stream); + } + stream.close(); + } } \ No newline at end of file 1.3 +2 -17 maven-plugins/dbunit/src/main/net/sourceforge/mavenplugins/dbunit/DataSetTool.java Index: DataSetTool.java =================================================================== RCS file: /cvsroot/maven-plugins/maven-plugins/dbunit/src/main/net/sourceforge/mavenplugins/dbunit/DataSetTool.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DataSetTool.java 1 Aug 2004 11:43:10 -0000 1.2 +++ DataSetTool.java 1 Aug 2004 13:17:22 -0000 1.3 @@ -161,27 +161,12 @@ // database connection - close? IDatabaseConnection connection = getConnection(); // write file - if (getDataSetFormat().equalsIgnoreCase("EXCEL")) - { - XlsDataSet.write(getDataSet(connection), new FileOutputStream(getFileName())); - } - else - { - throw new IllegalArgumentException("operation of '" + getOperation() + "' is not supported."); - } + writeDataSetToFile(getFileName(), getDataSetFormat(), getDataSet(connection)); } else if (dbOperation != null) { // TODO create a dataset of the right type - IDataSet dataset = null; - if ("EXCEL".equalsIgnoreCase(getDataSetFormat())) - { - dataset = new XlsDataSet(new File(getFileName())); - } - else - { - throw new IllegalArgumentException("dataSetFormat of '" + getDataSetFormat() + "' is not supported."); - } + IDataSet dataset = getDataSetFromFile(getFileName(), getDataSetFormat()); dbOperation.execute(getConnection(), dataset); } } 1.1 maven-plugins/dbunit/src/main/net/sourceforge/mavenplugins/dbunit/DataSetConverter.java Index: DataSetConverter.java =================================================================== package net.sourceforge.mavenplugins.dbunit; import org.dbunit.DatabaseUnitException; import org.dbunit.dataset.DataSetException; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.SQLException; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Maven" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact ap...@ap.... * * 5. Products derived from this software may not be called "Apache", * "Apache Maven", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * ==================================================================== */ /** * @author Dion Gillard */ public class DataSetConverter extends DbUnitTool { /** format of the input dataset */ private String inputFormat; /** format of the output dataset */ private String outputFormat; /** file name for input */ private String input; /** file name for output */ private String output; /** * Convert the input dataset to the output dataset * @throws IOException * @throws FileNotFoundException * @throws DataSetException */ public void process() throws DataSetException, FileNotFoundException, IOException { writeDataSetToFile(getOutput(), getOutputFormat(), getDataSetFromFile(getInput(), getInputFormat())); } /** * @return Returns the input. */ public String getInput() { return input; } /** * @param input The input to set. */ public void setInput(String input) { this.input = input; } /** * @return Returns the inputFormat. */ public String getInputFormat() { return inputFormat; } /** * @param inputFormat The inputFormat to set. */ public void setInputFormat(String inputFormat) { this.inputFormat = inputFormat; } /** * @return Returns the output. */ public String getOutput() { return output; } /** * @param output The output to set. */ public void setOutput(String output) { this.output = output; } /** * @return Returns the outputFormat. */ public String getOutputFormat() { return outputFormat; } /** * @param outputFormat The outputFormat to set. */ public void setOutputFormat(String outputFormat) { this.outputFormat = outputFormat; } } 1.13 +9 -0 maven-plugins/dbunit/plugin.jelly Index: plugin.jelly =================================================================== RCS file: /cvsroot/maven-plugins/maven-plugins/dbunit/plugin.jelly,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- plugin.jelly 1 Aug 2004 11:43:10 -0000 1.12 +++ plugin.jelly 1 Aug 2004 13:17:22 -0000 1.13 @@ -119,4 +119,13 @@ <ant:echo>Spreadsheet imported from ${file}</ant:echo> </goal> + <goal name="dbunit:xml2xls"> + <j:useBean var="tool" class="net.sourceforge.mavenplugins.dbunit.DataSetConverter" + input="${input}" + inputFormat="xml" + output="${output}" + outputFormat="excel" /> + ${tool.process()} + <ant:echo>Spreadsheet '${output}' created from '${input}'</ant:echo> + </goal> </project> |