DataSetUtils.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.dataset;

  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.StringTokenizer;

  25. import org.dbunit.Assertion;
  26. import org.dbunit.dataset.datatype.DataType;
  27. import org.dbunit.dataset.datatype.TypeCastException;
  28. import org.dbunit.util.QualifiedTableName;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * This class contains various methods for manipulating datasets.
  33.  *
  34.  * @author Manuel Laflamme
  35.  * @version $Revision$
  36.  * @since Feb 19, 2002
  37.  */
  38. public class DataSetUtils
  39. {

  40.     /**
  41.      * Logger for this class
  42.      */
  43.     private static final Logger logger = LoggerFactory.getLogger(DataSetUtils.class);

  44.     private DataSetUtils()
  45.     {
  46.     }

  47.     /**
  48.      * Asserts that the two specified dataset are equals. This method ignore
  49.      * the tables order.
  50.      *
  51.      * @deprecated Use Assertion.assertEquals
  52.      */
  53.     public static void assertEquals(IDataSet expectedDataSet,
  54.             IDataSet actualDataSet) throws Exception
  55.     {
  56.         logger.debug("assertEquals(expectedDataSet={}, actualDataSet={}) - start", expectedDataSet, actualDataSet);

  57.         Assertion.assertEquals(expectedDataSet, actualDataSet);
  58.     }


  59.     /**
  60.      * Asserts that the two specified tables are equals. This method ignore the
  61.      * table names, the columns order, the columns data type and the primary
  62.      * keys.
  63.      *
  64.      * @deprecated Use Assertion.assertEquals
  65.      */
  66.     public static void assertEquals(ITable expectedTable, ITable actualTable)
  67.             throws Exception
  68.     {
  69.         logger.debug("assertEquals(expectedTable={}, actualTable={}) - start", expectedTable, actualTable);

  70.         Assertion.assertEquals(expectedTable, actualTable);
  71.     }

  72.     /**
  73.      * Returns the specified name qualified with the specified prefix. The name
  74.      * is not modified if the prefix is <code>null</code> or if the name is
  75.      * already qualified.
  76.      * <p>
  77.      * Example: <br>
  78.      * <code>getQualifiedName(null, "NAME")</code> returns
  79.      * <code>"NAME"</code>. <code>getQualifiedName("PREFIX", "NAME")</code>
  80.      * returns <code>"PREFIX.NAME"</code> and
  81.      * <code>getQualifiedName("PREFIX2", "PREFIX1.NAME")</code>
  82.      * returns <code>"PREFIX1.NAME"</code>.
  83.      *
  84.      * @param prefix the prefix that qualifies the name and is prepended if the name is not qualified yet
  85.      * @param name the name The name to be qualified if it is not qualified already
  86.      * @return the qualified name
  87.      * @deprecated since 2.3.0. Prefer usage of {@link QualifiedTableName#getQualifiedName()} creating a new {@link QualifiedTableName} object
  88.      */
  89.     public static String getQualifiedName(String prefix, String name)
  90.     {
  91.         logger.debug("getQualifiedName(prefix={}, name={}) - start", prefix, name);

  92.         return new QualifiedTableName(name, prefix, (String)null).getQualifiedName();
  93.     }

  94.     /**
  95.      * @param prefix the prefix that qualifies the name and is prepended if the name is not qualified yet
  96.      * @param name the name The name to be qualified if it is not qualified already
  97.      * @param escapePattern The escape pattern to be applied on the prefix and the name. Can be null.
  98.      * @return The qualified name
  99.      * @deprecated since 2.3.0. Prefer usage of {@link QualifiedTableName#getQualifiedName()} creating a new {@link QualifiedTableName} object
  100.      */
  101.     public static String getQualifiedName(String prefix, String name,
  102.             String escapePattern)
  103.     {
  104.         if(logger.isDebugEnabled())
  105.             logger.debug("getQualifiedName(prefix={}, name={}, escapePattern={}) - start",
  106.                     prefix, name, escapePattern);
  107.        
  108.         return new QualifiedTableName(name, prefix, escapePattern).getQualifiedName();
  109.     }

  110.     /**
  111.      * @param name
  112.      * @param escapePattern
  113.      * @return The escaped name if the escape pattern is not null
  114.      * @deprecated since 2.3.0. Prefer usage of {@link QualifiedTableName#getQualifiedName()} creating a new {@link QualifiedTableName} object
  115.      */
  116.     public static String getEscapedName(String name, String escapePattern)
  117.     {
  118.         logger.debug("getEscapedName(name={}, escapePattern={}) - start", name, escapePattern);
  119.         return new QualifiedTableName(name, null, escapePattern).getQualifiedName();
  120.     }

  121.     /**
  122.      * Returns the specified value as a string to be use in an SQL Statement.
  123.      * For example the string <code>myValue</code> is returned as
  124.      * <code>'myValue'</code>.
  125.      *
  126.      * @param value the value
  127.      * @param dataType the value data type
  128.      * @return the SQL string value
  129.      */
  130.     public static String getSqlValueString(Object value, DataType dataType)
  131.             throws TypeCastException
  132.     {
  133.         logger.debug("getSqlValueString(value={}, dataType={}) - start", value, dataType);

  134.         if (value == null || value == ITable.NO_VALUE)
  135.         {
  136.             return "NULL";
  137.         }

  138.         String stringValue = DataType.asString(value);
  139.         if (dataType == DataType.DATE)
  140.         {
  141.             return "{d '" + stringValue + "'}";
  142.         }

  143.         if (dataType == DataType.TIME)
  144.         {
  145.             return "{t '" + stringValue + "'}";
  146.         }

  147.         if (dataType == DataType.TIMESTAMP)
  148.         {
  149.             return "{ts '" + stringValue + "'}";
  150.         }

  151.         if (!dataType.isNumber())
  152.         {
  153.             // no single quotes
  154.             if (!stringValue.contains("'"))
  155.             {
  156.                 return "'" + stringValue + "'";
  157.             }

  158.             // escaping single quotes
  159.             final StringBuilder buffer = new StringBuilder(stringValue.length() * 2);
  160.             StringTokenizer tokenizer = new StringTokenizer(stringValue, "'", true);

  161.             buffer.append("'");
  162.             while (tokenizer.hasMoreTokens())
  163.             {
  164.                 String token = tokenizer.nextToken();
  165.                 buffer.append(token);
  166.                 if (token.equals("'"))
  167.                 {
  168.                     buffer.append("'");
  169.                 }
  170.             }
  171.             buffer.append("'");
  172.             return buffer.toString();

  173.         }

  174.         return stringValue;
  175.     }

  176.     /**
  177.      * Search and returns the specified column from the specified column array.
  178.      *
  179.      * @param columnName the name of the column to search.
  180.      * @param columns the array of columns from which the column must be searched.
  181.      * @return the column or <code>null</code> if the column is not found
  182.      * @deprecated since 2.3.0 - prefer usage of {@link Columns#getColumn(String, Column[])}
  183.      */
  184.     public static Column getColumn(String columnName, Column[] columns)
  185.     {
  186.         logger.debug("getColumn(columnName={}, columns={}) - start", columnName, columns);
  187.         return Columns.getColumn(columnName, columns);
  188.     }

  189.     /**
  190.      * Search and returns the specified tables from the specified dataSet.
  191.      *
  192.      * @param names the names of the tables to search.
  193.      * @param dataSet the dataset from which the tables must be searched.
  194.      * @return the tables or an empty array if no tables are found.
  195.      */
  196.     public static ITable[] getTables(String[] names, IDataSet dataSet)
  197.             throws DataSetException
  198.     {
  199.         logger.debug("getTables(names={}, dataSet={}) - start", names, dataSet);

  200.         ITable[] tables = new ITable[names.length];
  201.         for (int i = 0; i < names.length; i++)
  202.         {
  203.             String name = names[i];
  204.             tables[i] = dataSet.getTable(name);
  205.         }

  206.         return tables;
  207.     }

  208.     /**
  209.      * Returns the tables from the specified dataset.
  210.      */
  211.     public static ITable[] getTables(IDataSet dataSet) throws DataSetException
  212.     {
  213.         logger.debug("getTables(dataSet={}) - start", dataSet);

  214.         return getTables(dataSet.iterator());
  215.     }

  216.     /**
  217.      * Returns the tables from the specified iterator.
  218.      */
  219.     public static ITable[] getTables(ITableIterator iterator) throws DataSetException
  220.     {
  221.         logger.debug("getTables(iterator={}) - start", iterator);

  222.         List tableList = new ArrayList();
  223.         while(iterator.next())
  224.         {
  225.             tableList.add(iterator.getTable());
  226.         }
  227.         return (ITable[])tableList.toArray(new ITable[0]);
  228.     }

  229.     /**
  230.      * Returns the table names from the specified dataset in reverse order.
  231.      */
  232.     public static String[] getReverseTableNames(IDataSet dataSet)
  233.             throws DataSetException
  234.     {
  235.         logger.debug("getReverseTableNames(dataSet={}) - start", dataSet);
  236.         return reverseStringArray(dataSet.getTableNames());
  237.     }

  238.     /**
  239.      * reverses a String array.
  240.      * @param array
  241.      * @return String[] - reversed array.
  242.      */
  243.     public static String[] reverseStringArray(String[] array)
  244.     {
  245.         logger.debug("reverseStringArray(array={}) - start", array);
  246.         String[] newArray = new String[array.length];
  247.         for (int i = 0; i < array.length; i++)
  248.         {
  249.             newArray[array.length - 1 - i] = array[i];
  250.         }
  251.         return newArray;
  252.     }

  253. }