View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2009, 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.ext.db2;
22  
23  import java.sql.DatabaseMetaData;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  
27  import org.dbunit.database.DefaultMetadataHandler;
28  import org.dbunit.util.SQLHelper;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Customized MetadataHandler for DB2 as match Columns of {@link DefaultMetadataHandler}
34   * fails with a RuntimeException.
35   * 
36   * @author gommma (gommma AT users.sourceforge.net)
37   * @author Last changed by: $Author$
38   * @version $Revision$ $Date$
39   * @since 2.4.7
40   */
41  public class Db2MetadataHandler extends DefaultMetadataHandler {
42  
43      private static final Logger logger = LoggerFactory.getLogger(Db2MetadataHandler.class);
44  
45      public Db2MetadataHandler() {
46          super();
47      }
48  
49      /**
50       * This method is overridden since - at least with DB2 driver db2jcc-9.5.jar - there is a
51       * problem that the {@link DatabaseMetaData} does not return the same values for catalog and schema
52       * like the columns {@link ResultSet} does. The debugging constellation is as follows
53       * <pre>
54       * catalog="BLA", catalogName=<null>
55       * schema="BLA", schemaName="BLA"
56       * </pre>
57       * This problem is taken into account by this metadata handler.
58       * 
59       * {@inheritDoc}
60       * @see org.dbunit.database.DefaultMetadataHandler#matches(java.sql.ResultSet, java.lang.String, java.lang.String, java.lang.String, java.lang.String, boolean)
61       */
62      public boolean matches(ResultSet columnsResultSet, String catalog,
63              String schema, String table, String column, boolean caseSensitive)
64              throws SQLException {
65          if (logger.isTraceEnabled())
66              logger.trace("matches(columnsResultSet={}, catalog={}, schema={},"
67                      + " table={}, column={}, caseSensitive={}) - start",
68                      new Object[] { columnsResultSet, catalog, schema, table,
69                              column, Boolean.valueOf(caseSensitive) });
70  
71          String catalogName = columnsResultSet.getString(1);
72          String schemaName = columnsResultSet.getString(2);
73          String tableName = columnsResultSet.getString(3);
74          String columnName = columnsResultSet.getString(4);
75  
76          if (logger.isDebugEnabled()) {
77              logger
78                      .debug(
79                              "Comparing the following values using caseSensitive={} (searched<=>actual): "
80                                      + "catalog: {}<=>{} schema: {}<=>{} table: {}<=>{} column: {}<=>{}",
81                              new Object[] { Boolean.valueOf(caseSensitive),
82                                      catalog, catalogName, schema, schemaName,
83                                      table, tableName, column, columnName });
84          }
85  
86          boolean areEqual = areEqualIgnoreBothNull(catalog, catalogName, caseSensitive)
87                  && areEqualIgnoreNull(schema, schemaName, caseSensitive)
88                  && areEqualIgnoreNull(table, tableName, caseSensitive)
89                  && areEqualIgnoreNull(column, columnName, caseSensitive);
90          return areEqual;
91      }
92  
93      private boolean areEqualIgnoreBothNull(String value1, String value2,
94              boolean caseSensitive) {
95          boolean areEqual = true;
96          if (value1 != null && value2 != null) {
97              if (value1.equals("") && value2.equals("")) {
98                  if (caseSensitive) {
99                      areEqual = value1.equals(value2);
100                 } else {
101                     areEqual = value1.equalsIgnoreCase(value2);
102                 }
103             }
104         }
105         return areEqual;
106     }
107 
108     private boolean areEqualIgnoreNull(String value1, String value2,
109             boolean caseSensitive) {
110         return SQLHelper.areEqualIgnoreNull(value1, value2, caseSensitive);
111     }
112 
113 }