DiffCollectingFailureHandler.java

  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. import java.util.ArrayList;
  23. import java.util.List;

  24. /**
  25.  * A {@link FailureHandler} that collects the {@link Difference}s that
  26.  * were found without throwing an exception.
  27.  * <p>
  28.  * You can use it as follows:
  29.  * <code><pre>
  30.  * IDataSet dataSet = getDataSet();
  31.  * DiffCollectingFailureHandler myHandler = new DiffCollectingFailureHandler();
  32.  * //invoke the assertion with the custom handler
  33.  * assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
  34.  *                        dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE"),
  35.  *                        myHandler);
  36.  * // Evaluate the results
  37.  * List diffList = myHandler.getDiffList();
  38.  * Difference diff = (Difference)diffList.get(0);
  39.  * ...
  40.  * </pre></code>
  41.  *
  42.  * @author gommma (gommma AT users.sourceforge.net)
  43.  * @author Last changed by: $Author$
  44.  * @version $Revision$ $Date$
  45.  * @since 2.4.0
  46.  */
  47. public class DiffCollectingFailureHandler extends DefaultFailureHandler
  48. {
  49.     private final List diffList = new ArrayList();
  50.    
  51.     public void handle(Difference diff)
  52.     {
  53.         // Simply collect the difference without throwing an exception
  54.         this.diffList.add(diff);
  55.     }

  56.     /**
  57.      * @return The list of collected {@link Difference}s
  58.      */
  59.     public List getDiffList()
  60.     {
  61.         return diffList;
  62.     }

  63.     public String toString()
  64.     {
  65.         final StringBuilder sb = new StringBuilder();
  66.         sb.append(super.toString());
  67.         sb.append(DiffCollectingFailureHandler.class.getName()).append("[");
  68.         sb.append("diffList=").append(diffList);
  69.         sb.append("]");
  70.         return sb.toString();
  71.     }
  72. }