View Javadoc
1   package org.dbunit.assertion.comparer.value.verifier;
2   
3   import java.util.Map;
4   
5   import org.dbunit.VerifyTableDefinition;
6   import org.dbunit.assertion.comparer.value.ValueComparer;
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  /**
11   * Default implementation for
12   * {@link ValueComparerAndVerifyTableDefinitionVerifier} which throws
13   * {@link IllegalStateException} on configuration conflicts.
14   *
15   * @author jjensen
16   * @since 2.6.0
17   */
18  public class DefaultValueComparerAndVerifyTableDefinitionVerifier
19          implements ValueComparerAndVerifyTableDefinitionVerifier
20  {
21      private final Logger log = LoggerFactory.getLogger(getClass());
22  
23      public void verify(
24              final Map<String, Map<String, ValueComparer>> tableColumnValueComparers,
25              final VerifyTableDefinition[] verifyTableDefinitions)
26      {
27          if (isNoTableColumnValueComparers(tableColumnValueComparers))
28          {
29              log.info("verify: no tableColumnValueComparers specified");
30          } else if (isNoVerifyTableDefinitions(verifyTableDefinitions))
31          {
32              log.info("verify: no verifyTableDefs specified");
33          } else
34          {
35              doVerify(tableColumnValueComparers, verifyTableDefinitions);
36          }
37      }
38  
39      protected void doVerify(
40              final Map<String, Map<String, ValueComparer>> tableColumnValueComparers,
41              final VerifyTableDefinition[] verifyTableDefinitions)
42      {
43          for (final VerifyTableDefinition verifyTableDefinition : verifyTableDefinitions)
44          {
45              processVerifyTableDefinition(tableColumnValueComparers,
46                      verifyTableDefinition);
47          }
48      }
49  
50      protected void processVerifyTableDefinition(
51              final Map<String, Map<String, ValueComparer>> tableColumnValueComparers,
52              final VerifyTableDefinition verifyTableDefinition)
53      {
54          log.trace(
55                  "processVerifyTableDefinition:"
56                          + " processing verifyTableDefinition={}",
57                  verifyTableDefinition);
58          final String tableName = verifyTableDefinition.getTableName();
59  
60          final Map<String, ValueComparer> columnValueComparerMap =
61                  tableColumnValueComparers.get(tableName);
62          if (columnValueComparerMap == null)
63          {
64              log.trace(
65                      "processVerifyTableDefinition:"
66                              + " no columnValueComparerMap found for table={}",
67                      tableName);
68          } else
69          {
70              final String[] columnExclusionFilters =
71                      verifyTableDefinition.getColumnExclusionFilters();
72              processColumnExclusionFilters(tableName, columnValueComparerMap,
73                      columnExclusionFilters);
74          }
75      }
76  
77      protected void processColumnExclusionFilters(final String tableName,
78              final Map<String, ValueComparer> columnValueComparerMap,
79              final String[] columnExclusionFilters)
80      {
81          for (final String columnName : columnExclusionFilters)
82          {
83              log.trace("processColumnExclusionFilters: columnName={}",
84                      columnName);
85              failIfColumnValueComparerMapHasExcludedColumn(tableName, columnName,
86                      columnValueComparerMap);
87          }
88      }
89  
90      protected void failIfColumnValueComparerMapHasExcludedColumn(
91              final String tableName, final String columnName,
92              final Map<String, ValueComparer> columnValueComparerMap)
93      {
94          final ValueComparer valueComparer =
95                  columnValueComparerMap.get(columnName);
96          if (valueComparer == null)
97          {
98              log.trace("failIfColumnValueComparerMapHasExcludedColumn:"
99                      + "config ok as no valueComparer found"
100                     + " for excluded columnName={}", columnName);
101         } else
102         {
103             final String msg = "Test setup conflict: table=" + tableName
104                     + ", columnName=" + columnName
105                     + ", has a VerifyTableDefinition column exclusion"
106                     + " and a specific column ValueComparer=" + valueComparer
107                     + "; to test the column, remove the exclusion;"
108                     + " to ignore the column, remove the ValueComparer";
109             log.error("failIfColumnValueComparerMapHasExcludedColumn: {}", msg);
110             throw new IllegalStateException(msg);
111         }
112     }
113 
114     protected boolean isNoTableColumnValueComparers(
115             final Map<String, Map<String, ValueComparer>> tableColumnValueComparers)
116     {
117         return tableColumnValueComparers == null
118                 || tableColumnValueComparers.isEmpty();
119     }
120 
121     protected boolean isNoVerifyTableDefinitions(
122             final VerifyTableDefinition[] verifyTableDefs)
123     {
124         return verifyTableDefs == null || verifyTableDefs.length == 0;
125     }
126 }