View Javadoc
1   /*
2    *
3    *  The DbUnit Database Testing Framework
4    *  Copyright (C)2002-2008, 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.assertion;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * A {@link FailureHandler} that collects the {@link Difference}s that
28   * were found without throwing an exception.
29   * <p>
30   * You can use it as follows:
31   * <code><pre>
32   * IDataSet dataSet = getDataSet();
33   * DiffCollectingFailureHandler myHandler = new DiffCollectingFailureHandler();
34   * //invoke the assertion with the custom handler
35   * assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
36   *                        dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE"),
37   *                        myHandler);
38   * // Evaluate the results
39   * List diffList = myHandler.getDiffList();
40   * Difference diff = (Difference)diffList.get(0);
41   * ...
42   * </pre></code>
43   * 
44   * @author gommma (gommma AT users.sourceforge.net)
45   * @author Last changed by: $Author$
46   * @version $Revision$ $Date$
47   * @since 2.4.0
48   */
49  public class DiffCollectingFailureHandler extends DefaultFailureHandler 
50  {
51      private final List diffList = new ArrayList();
52      
53      public void handle(Difference diff) 
54      {
55          // Simply collect the difference without throwing an exception
56          this.diffList.add(diff);
57      }
58  
59      /**
60       * @return The list of collected {@link Difference}s
61       */
62      public List getDiffList() 
63      {
64          return diffList;
65      }
66  
67      public String toString()
68      {
69          StringBuffer sb = new StringBuffer();
70          sb.append(super.toString());
71          sb.append(DiffCollectingFailureHandler.class.getName()).append("[");
72          sb.append("diffList=").append(diffList);
73          sb.append("]");
74          return sb.toString();
75      }
76  }