NumberTolerantDataType.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.math.BigDecimal;

  23. import org.dbunit.dataset.datatype.ToleratedDeltaMap.Precision;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;

  26. /**
  27.  * Extended version of the {@link NumberDataType}. Extends the
  28.  * {@link #compare(Object, Object)} method in order to respect precision
  29.  * tolerance. This is comparable to the JUnit method
  30.  * <code>assert(double val1, double val2, double toleratedDelta)</code>.
  31.  *
  32.  * @author gommma
  33.  * @author Last changed by: $Author$
  34.  * @version $Revision$ $Date$
  35.  * @since 2.3.0
  36.  */
  37. public class NumberTolerantDataType extends NumberDataType
  38. {

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

  44.     private static final BigDecimal C_100 = new BigDecimal("100");

  45.     /**
  46.      * The allowed/tolerated difference
  47.      */
  48.     private Precision toleratedDelta;

  49.     /**
  50.      * Creates a new number tolerant datatype
  51.      *
  52.      * @param name
  53.      * @param sqlType
  54.      * @param delta
  55.      *            The tolerated delta to be used for the comparison
  56.      */
  57.     NumberTolerantDataType(String name, int sqlType, Precision delta)
  58.     {
  59.         super(name, sqlType);

  60.         if (delta == null)
  61.         {
  62.             throw new NullPointerException(
  63.                     "The parameter 'delta' must not be null");
  64.         }
  65.         this.toleratedDelta = delta;
  66.     }

  67.     public Precision getToleratedDelta()
  68.     {
  69.         return toleratedDelta;
  70.     }

  71.     /**
  72.      * The only method overwritten from the base implementation to compare
  73.      * numbers allowing a tolerance
  74.      *
  75.      * @see org.dbunit.dataset.datatype.AbstractDataType#compareNonNulls(java.lang.Object,
  76.      *      java.lang.Object)
  77.      */
  78.     protected int compareNonNulls(Object value1cast, Object value2cast)
  79.             throws TypeCastException
  80.     {
  81.         logger.debug("compareNonNulls(value1={}, value2={}) - start",
  82.                 value1cast, value2cast);

  83.         try
  84.         {
  85.             // Start of special handling
  86.             if (value1cast instanceof BigDecimal
  87.                     && value2cast instanceof BigDecimal)
  88.             {
  89.                 BigDecimal bdValue1 = (BigDecimal) value1cast;
  90.                 BigDecimal bdValue2 = (BigDecimal) value2cast;
  91.                 BigDecimal diff = bdValue1.subtract(bdValue2);
  92.                 // Exact match
  93.                 if (isZero(diff))
  94.                 {
  95.                     return 0;
  96.                 }

  97.                 BigDecimal toleratedDeltaValue = this.toleratedDelta.getDelta();
  98.                 if (!this.toleratedDelta.isPercentage())
  99.                 {
  100.                     if (diff.abs().compareTo(toleratedDeltaValue) <= 0)
  101.                     {
  102.                         // within tolerance delta, so accept
  103.                         if (logger.isDebugEnabled())
  104.                             logger.debug(
  105.                                     "Values val1={}, val2={} differ but are within tolerated delta {}",
  106.                                     new Object[] {bdValue1, bdValue2,
  107.                                             toleratedDeltaValue});
  108.                         return 0;
  109.                     } else
  110.                     {
  111.                         // TODO it would be beautiful to report a precise
  112.                         // description about difference and tolerated delta
  113.                         // values in the assertion
  114.                         // Therefore think about introducing a method
  115.                         // "DataType.getCompareInfo()"
  116.                         return diff.signum();
  117.                     }
  118.                 } else
  119.                 {
  120.                     // percentage comparison
  121.                     int scale = toleratedDeltaValue.scale() + 2;
  122.                     BigDecimal toleratedValue =
  123.                             bdValue1.multiply(toleratedDeltaValue.divide(C_100,
  124.                                     scale, BigDecimal.ROUND_HALF_UP));
  125.                     if (diff.abs().compareTo(toleratedValue) <= 0)
  126.                     {
  127.                         // within tolerance delta, so accept
  128.                         if (logger.isDebugEnabled())
  129.                             logger.debug(
  130.                                     "Values val1={}, val2={} differ but are within tolerated delta {}",
  131.                                     new Object[] {bdValue1, bdValue2,
  132.                                             toleratedValue});
  133.                         return 0;
  134.                     } else
  135.                     {
  136.                         // TODO it would be beautiful to report a precise
  137.                         // description about difference and tolerated delta
  138.                         // values in the assertion
  139.                         // Therefore think about introducing a method
  140.                         // "DataType.getCompareInfo()"
  141.                         return diff.signum();
  142.                     }
  143.                 }

  144.             } else
  145.             {
  146.                 Comparable value1 = (Comparable) value1cast;
  147.                 Comparable value2 = (Comparable) value2cast;
  148.                 return value1.compareTo(value2);
  149.             }
  150.         } catch (ClassCastException e)
  151.         {
  152.             throw new TypeCastException(e);
  153.         }
  154.     }

  155.     /**
  156.      * Checks if the given value is zero.
  157.      *
  158.      * @param value
  159.      * @return <code>true</code> if and only if the given value is zero.
  160.      */
  161.     public static final boolean isZero(BigDecimal value)
  162.     {
  163.         return value.signum() == 0;
  164.     }

  165. }