StringDataType.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.Blob;
  23. import java.sql.Clob;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;

  27. import org.dbunit.dataset.ITable;
  28. import org.dbunit.util.Base64;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

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

  41.     public StringDataType(final String name, final int sqlType)
  42.     {
  43.         super(name, sqlType, String.class, false);
  44.     }

  45.     ////////////////////////////////////////////////////////////////////////////
  46.     // DataType class

  47.     @Override
  48.     public Object typeCast(final Object value) throws TypeCastException
  49.     {
  50.         logger.debug("typeCast(value={}) - start", value);

  51.         if (value == null || value == ITable.NO_VALUE)
  52.         {
  53.             return null;
  54.         }

  55.         if (value instanceof String)
  56.         {
  57.             return value;
  58.         }

  59.         if (value instanceof java.sql.Date || value instanceof java.sql.Time
  60.                 || value instanceof java.sql.Timestamp
  61.                 || value instanceof java.time.temporal.TemporalAccessor)
  62.         {
  63.             return value.toString();
  64.         }

  65.         if (value instanceof Boolean)
  66.         {
  67.             return value.toString();
  68.         }

  69.         if (value instanceof Number)
  70.         {
  71.             try
  72.             {
  73.                 return value.toString();
  74.             } catch (final java.lang.NumberFormatException e)
  75.             {
  76.                 throw new TypeCastException(value, this, e);
  77.             }
  78.         }

  79.         if (value instanceof byte[])
  80.         {
  81.             return Base64.encodeBytes((byte[]) value);
  82.         }

  83.         if (value instanceof Blob)
  84.         {
  85.             try
  86.             {
  87.                 final Blob blob = (Blob) value;
  88.                 final byte[] blobValue = blob.getBytes(1, (int) blob.length());
  89.                 return typeCast(blobValue);
  90.             } catch (final SQLException e)
  91.             {
  92.                 throw new TypeCastException(value, this, e);
  93.             }
  94.         }

  95.         if (value instanceof Clob)
  96.         {
  97.             try
  98.             {
  99.                 final Clob clobValue = (Clob) value;
  100.                 final int length = (int) clobValue.length();
  101.                 if (length > 0)
  102.                 {
  103.                     return clobValue.getSubString(1, length);
  104.                 }
  105.                 return "";
  106.             } catch (final SQLException e)
  107.             {
  108.                 throw new TypeCastException(value, this, e);
  109.             }
  110.         }

  111.         logger.warn(
  112.                 "Unknown/unsupported object type '{}' - "
  113.                         + "will invoke toString() as last fallback which "
  114.                         + "might produce undesired results",
  115.                 value.getClass().getName());
  116.         return value.toString();
  117.     }

  118.     @Override
  119.     public Object getSqlValue(final int column, final ResultSet resultSet)
  120.             throws SQLException, TypeCastException
  121.     {
  122.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
  123.                 resultSet);
  124.         final String rawValue = resultSet.getString(column);
  125.         final String value = resultSet.wasNull() ? null : rawValue;
  126.         logger.debug("getSqlValue: column={}, value={}", column, value);
  127.         return value;
  128.     }

  129.     @Override
  130.     public void setSqlValue(final Object value, final int column,
  131.             final PreparedStatement statement)
  132.             throws SQLException, TypeCastException
  133.     {
  134.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  135.                 value, column, statement);

  136.         statement.setString(column, asString(value));
  137.     }
  138. }