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  package org.dbunit.dataset.datatype;
22  
23  import java.sql.Types;
24  import java.util.Arrays;
25  import java.util.Collection;
26  
27  import org.dbunit.dataset.datatype.ToleratedDeltaMap.ToleratedDelta;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Generic factory that handle standard JDBC types.
33   *
34   * @author Manuel Laflamme
35   * @since May 17, 2003
36   * @version $Revision$
37   */
38  public class DefaultDataTypeFactory implements IDataTypeFactory, IDbProductRelatable
39  {
40  
41  	private ToleratedDeltaMap _toleratedDeltaMap = new ToleratedDeltaMap();
42  	
43      /**
44       * Logger for this class
45       */
46      private static final Logger logger = LoggerFactory.getLogger(DefaultDataTypeFactory.class);
47      /**
48       * Database product names supported.
49       */
50      private static final Collection DATABASE_PRODUCTS = Arrays.asList(new String[]{"derby"});
51  
52      /**
53       * @see IDbProductRelatable#getValidDbProducts()
54       */
55      public Collection getValidDbProducts()
56      {
57        return DATABASE_PRODUCTS;
58      }
59  
60      /**
61       * @see org.dbunit.dataset.datatype.IDataTypeFactory#createDataType(int, java.lang.String)
62       */
63      public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException
64      {
65      	if(logger.isDebugEnabled())
66      		logger.debug("createDataType(sqlType={}, sqlTypeName={}) - start", new Integer(sqlType), sqlTypeName);
67  
68          DataType dataType = DataType.UNKNOWN;
69          if (sqlType != Types.OTHER)
70          {
71              dataType = DataType.forSqlType(sqlType);
72          }
73          else
74          {
75              // Necessary for compatibility with DbUnit 1.5 and older
76              // BLOB
77              if ("BLOB".equals(sqlTypeName))
78              {
79                  return DataType.BLOB;
80              }
81  
82              // CLOB
83              if ("CLOB".equals(sqlTypeName))
84              {
85                  return DataType.CLOB;
86              }
87          }
88          return dataType;
89      }
90      
91      /**
92       * @see org.dbunit.dataset.datatype.IDataTypeFactory#createDataType(int, java.lang.String, java.lang.String, java.lang.String)
93       */
94      public DataType createDataType(int sqlType, String sqlTypeName, String tableName, String columnName) throws DataTypeException
95      {
96      	if(logger.isDebugEnabled())
97      		logger.debug("createDataType(sqlType={} , sqlTypeName={}, tableName={}, columnName={}) - start", 
98          		new Object[] {new Integer(sqlType), sqlTypeName, tableName, columnName} );
99  
100         if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL)
101         {
102         	// Check if the user has set a tolerance delta for this floating point field
103         	ToleratedDelta delta = _toleratedDeltaMap.findToleratedDelta(tableName, columnName);
104             // Found a toleratedDelta object
105             if(delta!=null) {
106                 if(logger.isDebugEnabled())
107                     logger.debug("Creating NumberTolerantDataType for table={}, column={}, toleratedDelta={}", 
108             			new Object[]{tableName, columnName, delta.getToleratedDelta() });
109                 
110                 // Use a special data type to implement the tolerance for numbers (floating point things)
111                 NumberTolerantDataType type = new NumberTolerantDataType("NUMERIC_WITH_TOLERATED_DELTA", 
112                 		sqlType, delta.getToleratedDelta());
113                 return type;
114             }
115         }
116         
117         // In all other cases (default) use the default data type creation
118         return this.createDataType(sqlType, sqlTypeName);
119     }
120 
121     
122 	/**
123 	 * @return The whole map of tolerated delta objects that have been set until now
124 	 * @since 2.3.0
125 	 */
126 	public ToleratedDeltaMap getToleratedDeltaMap() 
127 	{
128 		return _toleratedDeltaMap;
129 	}
130 
131     /**
132      * Adds a tolerated delta to this data type factory to be used for numeric comparisons
133      * @param delta 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     /**
142      * Returns a string representation of this {@link DefaultDataTypeFactory} instance
143      * @since 2.4.6
144      */
145     public String toString()
146     {
147         StringBuffer sb = new StringBuffer();
148         sb.append(getClass().getName()).append("[");
149         sb.append("_toleratedDeltaMap=").append(_toleratedDeltaMap);
150         sb.append("]");
151         return sb.toString();
152     }
153 }