DefaultDataTypeFactory.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.datatype;

  22. import java.sql.Types;
  23. import java.util.Arrays;
  24. import java.util.Collection;

  25. import org.dbunit.dataset.datatype.ToleratedDeltaMap.ToleratedDelta;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;

  28. /**
  29.  * Generic factory that handle standard JDBC types.
  30.  *
  31.  * @author Manuel Laflamme
  32.  * @since May 17, 2003
  33.  * @version $Revision$
  34.  */
  35. public class DefaultDataTypeFactory
  36.         implements IDataTypeFactory, IDbProductRelatable
  37. {

  38.     private ToleratedDeltaMap _toleratedDeltaMap = new ToleratedDeltaMap();

  39.     /**
  40.      * Logger for this class
  41.      */
  42.     private static final Logger logger =
  43.             LoggerFactory.getLogger(DefaultDataTypeFactory.class);
  44.     /**
  45.      * Database product names supported.
  46.      */
  47.     private static final Collection DATABASE_PRODUCTS =
  48.             Arrays.asList(new String[] {"derby"});

  49.     /**
  50.      * @see IDbProductRelatable#getValidDbProducts()
  51.      */
  52.     public Collection getValidDbProducts()
  53.     {
  54.         return DATABASE_PRODUCTS;
  55.     }

  56.     /**
  57.      * @see org.dbunit.dataset.datatype.IDataTypeFactory#createDataType(int,
  58.      *      java.lang.String)
  59.      */
  60.     public DataType createDataType(int sqlType, String sqlTypeName)
  61.             throws DataTypeException
  62.     {
  63.         logger.debug("createDataType(sqlType={}, sqlTypeName={}) - start",
  64.                 sqlType, sqlTypeName);

  65.         DataType dataType = DataType.UNKNOWN;
  66.         if (sqlType != Types.OTHER)
  67.         {
  68.             dataType = DataType.forSqlType(sqlType);
  69.         } else
  70.         {
  71.             // Necessary for compatibility with DbUnit 1.5 and older
  72.             // BLOB
  73.             if ("BLOB".equals(sqlTypeName))
  74.             {
  75.                 return DataType.BLOB;
  76.             }

  77.             // CLOB
  78.             if ("CLOB".equals(sqlTypeName))
  79.             {
  80.                 return DataType.CLOB;
  81.             }
  82.         }
  83.         return dataType;
  84.     }

  85.     /**
  86.      * @see org.dbunit.dataset.datatype.IDataTypeFactory#createDataType(int,
  87.      *      java.lang.String, java.lang.String, java.lang.String)
  88.      */
  89.     public DataType createDataType(int sqlType, String sqlTypeName,
  90.             String tableName, String columnName) throws DataTypeException
  91.     {
  92.         logger.debug(
  93.                 "createDataType(sqlType={} , sqlTypeName={}, tableName={}, columnName={}) - start",
  94.                 sqlType, sqlTypeName, tableName, columnName);

  95.         if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL)
  96.         {
  97.             // Check if the user has set a tolerance delta for this floating
  98.             // point field
  99.             ToleratedDelta delta = _toleratedDeltaMap
  100.                     .findToleratedDelta(tableName, columnName);
  101.             // Found a toleratedDelta object
  102.             if (delta != null)
  103.             {
  104.                 if (logger.isDebugEnabled())
  105.                     logger.debug(
  106.                             "Creating NumberTolerantDataType for table={}, column={}, toleratedDelta={}",
  107.                             tableName, columnName, delta.getToleratedDelta());

  108.                 // Use a special data type to implement the tolerance for
  109.                 // numbers (floating point things)
  110.                 NumberTolerantDataType type = new NumberTolerantDataType(
  111.                         "NUMERIC_WITH_TOLERATED_DELTA", sqlType,
  112.                         delta.getToleratedDelta());
  113.                 return type;
  114.             }
  115.         }

  116.         // In all other cases (default) use the default data type creation
  117.         return this.createDataType(sqlType, sqlTypeName);
  118.     }

  119.     /**
  120.      * @return The whole map of tolerated delta objects that have been set until
  121.      *         now
  122.      * @since 2.3.0
  123.      */
  124.     public ToleratedDeltaMap getToleratedDeltaMap()
  125.     {
  126.         return _toleratedDeltaMap;
  127.     }

  128.     /**
  129.      * Adds a tolerated delta to this data type factory to be used for numeric
  130.      * comparisons
  131.      *
  132.      * @param delta
  133.      *            The new tolerated delta object
  134.      * @since 2.3.0
  135.      */
  136.     public void addToleratedDelta(ToleratedDelta delta)
  137.     {
  138.         this._toleratedDeltaMap.addToleratedDelta(delta);
  139.     }

  140.     /**
  141.      * Returns a string representation of this {@link DefaultDataTypeFactory}
  142.      * instance
  143.      *
  144.      * @since 2.4.6
  145.      */
  146.     public String toString()
  147.     {
  148.         final StringBuilder sb = new StringBuilder();
  149.         sb.append(getClass().getName()).append("[");
  150.         sb.append("_toleratedDeltaMap=").append(_toleratedDeltaMap);
  151.         sb.append("]");
  152.         return sb.toString();
  153.     }
  154. }