1 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 ; 40 41 import java.util.List ; 42 import java.util.Iterator ; 43 import java.sql.SQLException ; 44 import java.io.File ; 45 import java.io.IOException ; 46 47 52 public abstract class AbstractStep implements DbUnitTaskStep 53 { 54 public static final String FORMAT_FLAT = "flat"; 55 public static final String FORMAT_XML = "xml"; 56 public static final String FORMAT_DTD = "dtd"; 57 public static final String FORMAT_CSV = "csv"; 58 59 protected IDataSet getDatabaseDataSet(IDatabaseConnection connection, 60 List tables, boolean forwardonly) throws DatabaseUnitException 61 { 62 try 63 { 64 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 if (tables.size() == 0) 80 { 81 return connection.createDataSet(); 82 } 83 84 QueryDataSet queryDataset = new QueryDataSet(connection); 85 for (Iterator it = tables.iterator(); it.hasNext();) 86 { 87 Object 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 e) 103 { 104 throw new DatabaseUnitException(e); 105 } 106 } 107 108 protected IDataSet getSrcDataSet(File src, String 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 (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 (src.toURL().toString())); 125 } 126 else if (format.equalsIgnoreCase(FORMAT_DTD)) 127 { 128 producer = new FlatDtdProducer(new InputSource (src.toURL().toString())); 129 } 130 else 131 { 132 throw new IllegalArgumentException ("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 e) 142 { 143 throw new DatabaseUnitException(e); 144 } 145 } 146 } 147 | Popular Tags |