Operation.java

  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. import org.apache.tools.ant.DirectoryScanner;
  23. import org.apache.tools.ant.types.FileSet;
  24. import org.dbunit.DatabaseUnitException;
  25. import org.dbunit.database.IDatabaseConnection;
  26. import org.dbunit.database.DatabaseSequenceFilter;
  27. import org.dbunit.dataset.CompositeDataSet;
  28. import org.dbunit.dataset.IDataSet;
  29. import org.dbunit.dataset.ReplacementDataSet;
  30. import org.dbunit.dataset.FilteredDataSet;
  31. import org.dbunit.ext.mssql.InsertIdentityOperation;
  32. import org.dbunit.operation.DatabaseOperation;
  33. import org.dbunit.operation.TransactionOperation;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;

  36. import java.io.File;
  37. import java.sql.SQLException;
  38. import java.util.ArrayList;
  39. import java.util.Arrays;
  40. import java.util.List;

  41. /**
  42.  * The <code>Operation</code> class is the step that defines which
  43.  * operation will be performed in the execution of the <code>DbUnitTask</code>
  44.  * task.
  45.  *
  46.  * @author Timothy Ruppert
  47.  * @author Ben Cox
  48.  * @version $Revision$
  49.  * @since Jun 10, 2002
  50.  */
  51. public class Operation extends AbstractStep
  52. {

  53.     /**
  54.      * Logger for this class
  55.      */
  56.     private static final Logger logger = LoggerFactory.getLogger(Operation.class);

  57.     private static final String DEFAULT_FORMAT = FORMAT_FLAT;

  58.     protected String _type = "CLEAN_INSERT";
  59.     private String _format;
  60.     private List<File> _sources = new ArrayList<>();
  61.     private boolean _combine = false;
  62.     private boolean _transaction = false;
  63.     private DatabaseOperation _operation;
  64.     private boolean _forwardOperation = true;
  65.     private String _nullToken;

  66.     public File[] getSrc()
  67.     {
  68.         return _sources.toArray(new File[_sources.size()]);
  69.     }

  70.     public void setSrc(File[] sources)
  71.     {
  72.         _sources.clear();
  73.         _sources.addAll(Arrays.asList(sources));
  74.     }

  75.     public void setSrc(File src)
  76.     {
  77.         _sources.clear();
  78.         _sources.add(src);
  79.     }

  80.     public void addConfiguredFileset(FileSet fileSet)
  81.     {
  82.         DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
  83.         for (String file : scanner.getIncludedFiles()) {
  84.             _sources.add(new File(scanner.getBasedir(), file));
  85.         }
  86.     }

  87.     public String getFormat()
  88.     {
  89.         return _format != null ? _format : DEFAULT_FORMAT;
  90.     }

  91.     public void setFormat(String format)
  92.     {
  93.         logger.debug("setFormat(format={}) - start", format);

  94.         // Check if the given format is accepted
  95.         checkDataFormat(format);
  96.         // If we get here the given format is a valid data format
  97.         _format = format;
  98.     }

  99.     public boolean isCombine()
  100.     {
  101.         return _combine;
  102.     }

  103.     public void setCombine(boolean combine)
  104.     {
  105.         _combine = combine;
  106.     }

  107.     public boolean isTransaction()
  108.     {
  109.         return _transaction;
  110.     }

  111.     public void setTransaction(boolean transaction)
  112.     {
  113.         _transaction = transaction;
  114.     }

  115.     public String getNullToken()
  116.     {
  117.         return _nullToken;
  118.     }

  119.     public void setNullToken(final String nullToken)
  120.     {
  121.         this._nullToken = nullToken;
  122.     }

  123.     public DatabaseOperation getDbOperation()
  124.     {
  125.         return _operation;
  126.     }

  127.     public String getType()
  128.     {
  129.         return _type;
  130.     }

  131.     public void setType(String type)
  132.     {
  133.         logger.debug("setType(type={}) - start", type);

  134.         if ("UPDATE".equals(type)) {
  135.             _operation = DatabaseOperation.UPDATE;
  136.             _forwardOperation = true;
  137.         } else if ("INSERT".equals(type)) {
  138.             _operation = DatabaseOperation.INSERT;
  139.             _forwardOperation = true;
  140.         } else if ("REFRESH".equals(type)) {
  141.             _operation = DatabaseOperation.REFRESH;
  142.             _forwardOperation = true;
  143.         } else if ("DELETE".equals(type)) {
  144.             _operation = DatabaseOperation.DELETE;
  145.             _forwardOperation = false;
  146.         } else if ("DELETE_ALL".equals(type)) {
  147.             _operation = DatabaseOperation.DELETE_ALL;
  148.             _forwardOperation = false;
  149.         } else if ("CLEAN_INSERT".equals(type)) {
  150.             _operation = DatabaseOperation.CLEAN_INSERT;
  151.             _forwardOperation = false;
  152.         } else if ("NONE".equals(type)) {
  153.             _operation = DatabaseOperation.NONE;
  154.             _forwardOperation = true;
  155.         } else if ("MSSQL_CLEAN_INSERT".equals(type)) {
  156.             _operation = InsertIdentityOperation.CLEAN_INSERT;
  157.             _forwardOperation = false;
  158.         } else if ("MSSQL_INSERT".equals(type)) {
  159.             _operation = InsertIdentityOperation.INSERT;
  160.             _forwardOperation = true;
  161.         } else if ("MSSQL_REFRESH".equals(type)) {
  162.             _operation = InsertIdentityOperation.REFRESH;
  163.             _forwardOperation = true;
  164.         } else {
  165.             throw new IllegalArgumentException("Type must be one of: UPDATE, INSERT,"
  166.                     + " REFRESH, DELETE, DELETE_ALL, CLEAN_INSERT, MSSQL_INSERT, "
  167.                     + " or MSSQL_REFRESH but was: " + type);
  168.         }
  169.         _type = type;
  170.     }

  171.     public void execute(IDatabaseConnection connection) throws DatabaseUnitException
  172.     {
  173.         logger.debug("execute(connection={}) - start", connection);
  174.         if (_operation == null)
  175.         {
  176.             throw new DatabaseUnitException("Operation.execute(): setType(String) must be called before execute()!");
  177.         }

  178.         if (_operation == DatabaseOperation.NONE)
  179.         {
  180.             return;
  181.         }

  182.         if (_sources.size() == 0)
  183.         {
  184.             throw new DatabaseUnitException("Operation.execute(): must call setSrc(File), addSrc(File), or setSources(File[]) before execute()!");
  185.         }

  186.         try {
  187.             DatabaseOperation operation = (_transaction ? new TransactionOperation(_operation) : _operation);
  188.             // TODO This is not very nice and the design should be reviewed but it works for now (gommma)
  189.             boolean useForwardOnly = _forwardOperation && ! isOrdered();
  190.             IDataSet dataset;
  191.             if (_sources.size() > 1) {
  192.                 IDataSet[] datasets = new IDataSet[_sources.size()];
  193.                 for (int i = 0; i < _sources.size(); i++) {
  194.                     datasets[i] = getSrcDataSet(_sources.get(i), getFormat(), useForwardOnly);
  195.                 }
  196.                 dataset = new CompositeDataSet(datasets, _combine);
  197.             } else {
  198.                 dataset = getSrcDataSet(_sources.get(0), getFormat(), useForwardOnly);
  199.             }
  200.             if (_nullToken != null) {
  201.                 dataset = new ReplacementDataSet(dataset);
  202.                 ((ReplacementDataSet)dataset).addReplacementObject(_nullToken, null);
  203.             }
  204.             if(isOrdered())
  205.             {
  206.                 DatabaseSequenceFilter databaseSequenceFilter = new DatabaseSequenceFilter(connection);
  207.                 dataset = new FilteredDataSet(databaseSequenceFilter, dataset);
  208.             }
  209.             operation.execute(connection, dataset);
  210.         }
  211.         catch (SQLException e)
  212.         {
  213.             throw new DatabaseUnitException(e);
  214.         }
  215.     }

  216.     public String getLogMessage()
  217.     {
  218.         final StringBuilder result = new StringBuilder();
  219.         result.append("Executing operation: " + _type);
  220.         result.append("\n          on   files: [ ");
  221.         for (File f : _sources) {
  222.             result.append(f.getAbsolutePath() + " ");
  223.         }
  224.         result.append("]");
  225.         result.append("\n          with format: " + _format);
  226.         return result.toString();
  227.     }

  228.     public String toString()
  229.     {
  230.         final StringBuilder result = new StringBuilder();
  231.         result.append("Operation: ");
  232.         result.append(" type=").append(_type);
  233.         result.append(", format=").append(_format);
  234.         result.append(", sources=[ ");
  235.         for (File f : _sources) {
  236.             result.append(f.getAbsolutePath() + " ");
  237.         }
  238.         result.append("]");
  239.         result.append(", operation=").append(_operation);
  240.         result.append(", nullToken=").append(_nullToken);
  241.         result.append(", ordered=").append(super.isOrdered());
  242.         return result.toString();
  243.     }
  244. }