BooleanDataType.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.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Types;

  26. import org.dbunit.dataset.ITable;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;

  29. /**
  30.  * @author Manuel Laflamme
  31.  * @author Last changed by: $Author$
  32.  * @version $Revision$ $Date$
  33.  */
  34. public class BooleanDataType extends AbstractDataType
  35. {
  36.     private static final Logger logger =
  37.             LoggerFactory.getLogger(BooleanDataType.class);

  38.     BooleanDataType()
  39.     {
  40.         this("BOOLEAN", Types.BOOLEAN);
  41.     }

  42.     /**
  43.      * @since 2.3
  44.      */
  45.     BooleanDataType(final String name, final int sqlType)
  46.     {
  47.         super(name, sqlType, Boolean.class, false);
  48.     }

  49.     ////////////////////////////////////////////////////////////////////////////
  50.     // DataType class

  51.     @Override
  52.     public Object typeCast(final Object value) throws TypeCastException
  53.     {
  54.         logger.debug("typeCast(value={}) - start", value);

  55.         if (value == null || value == ITable.NO_VALUE)
  56.         {
  57.             return null;
  58.         }

  59.         if (value instanceof Boolean)
  60.         {
  61.             return value;
  62.         }

  63.         if (value instanceof Number)
  64.         {
  65.             final Number number = (Number) value;
  66.             if (number.intValue() == 0)
  67.             {
  68.                 return Boolean.FALSE;
  69.             } else
  70.             {
  71.                 return Boolean.TRUE;
  72.             }
  73.         }

  74.         if (value instanceof String)
  75.         {
  76.             final String string = (String) value;

  77.             if ("true".equalsIgnoreCase(string)
  78.                     || "false".equalsIgnoreCase(string))
  79.             {
  80.                 return Boolean.valueOf(string);
  81.             } else
  82.             {
  83.                 return typeCast(DataType.INTEGER.typeCast(string));
  84.             }
  85.         }

  86.         throw new TypeCastException(value, this);
  87.     }

  88.     @Override
  89.     protected int compareNonNulls(final Object value1, final Object value2)
  90.             throws TypeCastException
  91.     {
  92.         logger.debug("compareNonNulls(value1={}, value2={}) - start", value1,
  93.                 value2);

  94.         final Boolean value1bool = (Boolean) value1;
  95.         final Boolean value2bool = (Boolean) value2;

  96.         if (value1bool.equals(value2bool))
  97.         {
  98.             return 0;
  99.         }

  100.         if (!value1bool)
  101.         {
  102.             return -1;
  103.         }

  104.         return 1;
  105.     }

  106.     @Override
  107.     public Object getSqlValue(final int column, final ResultSet resultSet)
  108.             throws SQLException, TypeCastException
  109.     {
  110.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
  111.                 resultSet);
  112.         final boolean rawValue = resultSet.getBoolean(column);
  113.         final Boolean value = resultSet.wasNull() ? null : rawValue;
  114.         logger.debug("getSqlValue: column={}, value={}", column, value);
  115.         return value;
  116.     }

  117.     @Override
  118.     public void setSqlValue(final Object value, final int column,
  119.             final PreparedStatement statement)
  120.             throws SQLException, TypeCastException
  121.     {
  122.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  123.                 value, column, statement);

  124.         final Boolean castValue = (Boolean) typeCast(value);
  125.         if (castValue == null)
  126.         {
  127.             statement.setNull(column, Types.BOOLEAN);
  128.         } else
  129.         {
  130.             statement.setBoolean(column, castValue);
  131.         }
  132.     }
  133. }