View Javadoc
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  
22  package org.dbunit.dataset.datatype;
23  
24  import java.math.BigDecimal;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  
29  import org.dbunit.dataset.ITable;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * @author Manuel Laflamme
35   * @version $Revision$
36   */
37  public class IntegerDataType extends AbstractDataType
38  {
39      private static final Logger logger =
40              LoggerFactory.getLogger(IntegerDataType.class);
41  
42      IntegerDataType(final String name, final int sqlType)
43      {
44          super(name, sqlType, Integer.class, true);
45      }
46  
47      ////////////////////////////////////////////////////////////////////////////
48      // DataType class
49  
50      @Override
51      public Object typeCast(final Object value) throws TypeCastException
52      {
53          logger.debug("typeCast(value={}) - start", value);
54  
55          if (value == null || value == ITable.NO_VALUE)
56          {
57              return null;
58          }
59  
60          if (value instanceof Number)
61          {
62              return ((Number) value).intValue();
63          }
64  
65          // Treat "false" as 0, "true" as 1
66          if (value instanceof String)
67          {
68              final String string = (String) value;
69  
70              if ("false".equalsIgnoreCase(string))
71              {
72                  return 0;
73              }
74  
75              if ("true".equalsIgnoreCase(string))
76              {
77                  return 1;
78              }
79          }
80  
81          // Bugfix in release 2.4.6
82          final String stringValue = value.toString().trim();
83          if (stringValue.length() <= 0)
84          {
85              return null;
86          }
87  
88          try
89          {
90              return typeCast(new BigDecimal(stringValue));
91          } catch (final java.lang.NumberFormatException e)
92          {
93              throw new TypeCastException(value, this, e);
94          }
95      }
96  
97      @Override
98      public Object getSqlValue(final int column, final ResultSet resultSet)
99              throws SQLException, TypeCastException
100     {
101         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
102                 resultSet);
103         final int rawValue = resultSet.getInt(column);
104         final Integer value = resultSet.wasNull() ? null : rawValue;
105         logger.debug("getSqlValue: column={}, value={}", column, value);
106         return value;
107     }
108 
109     @Override
110     public void setSqlValue(final Object value, final int column,
111             final PreparedStatement statement)
112             throws SQLException, TypeCastException
113     {
114         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
115                 value, column, statement);
116 
117         statement.setInt(column, (Integer) typeCast(value));
118     }
119 }