1 21 22 package org.dbunit.ant; 23 24 import org.dbunit.DatabaseUnitException; 25 import org.dbunit.database.IDatabaseConnection; 26 import org.dbunit.dataset.IDataSet; 27 import org.dbunit.ext.mssql.InsertIdentityOperation; 28 import org.dbunit.operation.DatabaseOperation; 29 30 import java.io.File ; 31 import java.sql.SQLException ; 32 33 44 public class Operation extends AbstractStep 45 { 46 private static final String DEFAULT_FORMAT = FORMAT_FLAT; 47 48 protected String _type = "CLEAN_INSERT"; 49 private String _format; 50 private File _src; 51 private DatabaseOperation _operation; 52 private boolean _forwardOperation = true; 53 54 public String getType() 55 { 56 return _type; 57 } 58 59 public File getSrc() 60 { 61 return _src; 62 } 63 64 public DatabaseOperation getDbOperation() 65 { 66 return _operation; 67 } 68 69 public String getFormat() 70 { 71 return _format != null ? _format : DEFAULT_FORMAT; 72 } 73 74 public void setType(String type) 75 { 76 if ("UPDATE".equals(type)) 77 { 78 _operation = DatabaseOperation.UPDATE; 79 _forwardOperation = true; 80 } 81 else if ("INSERT".equals(type)) 82 { 83 _operation = DatabaseOperation.INSERT; 84 _forwardOperation = true; 85 } 86 else if ("REFRESH".equals(type)) 87 { 88 _operation = DatabaseOperation.REFRESH; 89 _forwardOperation = true; 90 } 91 else if ("DELETE".equals(type)) 92 { 93 _operation = DatabaseOperation.DELETE; 94 _forwardOperation = false; 95 } 96 else if ("DELETE_ALL".equals(type)) 97 { 98 _operation = DatabaseOperation.DELETE_ALL; 99 _forwardOperation = false; 100 } 101 else if ("CLEAN_INSERT".equals(type)) 102 { 103 _operation = DatabaseOperation.CLEAN_INSERT; 104 _forwardOperation = false; 105 } 106 else if ("NONE".equals(type)) 107 { 108 _operation = DatabaseOperation.NONE; 109 _forwardOperation = true; 110 } 111 else if ("MSSQL_CLEAN_INSERT".equals(type)) 112 { 113 _operation = InsertIdentityOperation.CLEAN_INSERT; 114 _forwardOperation = false; 115 } 116 else if ("MSSQL_INSERT".equals(type)) 117 { 118 _operation = InsertIdentityOperation.INSERT; 119 _forwardOperation = true; 120 } 121 else if ("MSSQL_REFRESH".equals(type)) 122 { 123 _operation = InsertIdentityOperation.REFRESH; 124 _forwardOperation = true; 125 } 126 else 127 { 128 throw new IllegalArgumentException ("Type must be one of: UPDATE, INSERT," 129 + " REFRESH, DELETE, DELETE_ALL, CLEAN_INSERT, MSSQL_INSERT, " 130 + " or MSSQL_REFRESH but was: " + type); 131 } 132 _type = type; 133 } 134 135 public void setSrc(File src) 136 { 137 _src = src; 138 } 139 140 public void setFormat(String format) 141 { 142 if (format.equalsIgnoreCase(FORMAT_FLAT) 143 || format.equalsIgnoreCase(FORMAT_XML) 144 || format.equalsIgnoreCase(FORMAT_CSV) 145 ) 146 { 147 _format = format; 148 } 149 else 150 { 151 throw new IllegalArgumentException ("Type must be either 'flat'(default), 'xml' or 'csv' but was: " + format); 152 } 153 } 154 155 public void execute(IDatabaseConnection connection) throws DatabaseUnitException 156 { 157 if (_operation == null) 158 { 159 throw new DatabaseUnitException("Operation.execute(): setType(String) must be called before execute()!"); 160 } 161 162 if (_operation == DatabaseOperation.NONE) 163 { 164 return; 165 } 166 167 try 168 { 169 IDataSet dataset = getSrcDataSet(getSrc(), 170 getFormat(), _forwardOperation); 171 _operation.execute(connection, dataset); 172 } 173 catch (SQLException e) 174 { 175 throw new DatabaseUnitException(e); 176 } 177 } 178 179 public String getLogMessage() 180 { 181 return "Executing operation: " + _type 182 + "\n on file: " + ((_src == null) ? null : _src.getAbsolutePath()) 183 + "\n with format: " + _format; 184 } 185 186 187 public String toString() 188 { 189 StringBuffer result = new StringBuffer (); 190 result.append("Operation: "); 191 result.append(" type=" + _type); 192 result.append(", format=" + _format); 193 result.append(", SRC=" + _src == null ? null : _src.getAbsolutePath()); 194 result.append(", operation = " + _operation); 195 196 return result.toString(); 197 } 198 } 199 200 | Popular Tags |