DbComparisonFailure.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. /**
  23.  * Exception signaling a DbUnit assertion failure while comparing values.
  24.  * Is used to avoid the direct dependency to any other testing framework.
  25.  *
  26.  * @author gommma (gommma AT users.sourceforge.net)
  27.  * @author Last changed by: $Author$
  28.  * @version $Revision$ $Date$
  29.  * @since 2.4.0
  30.  */
  31. public class DbComparisonFailure extends AssertionError
  32. {
  33.     private static final long serialVersionUID = 1L;

  34.     private String reason;
  35.     private String expected;
  36.     private String actual;

  37.     /**
  38.      * @param reason The reason for the comparison failure
  39.      * @param expected The expected value
  40.      * @param actual The actual value
  41.      */
  42.     public DbComparisonFailure(final String reason, final String expected, final String actual)
  43.     {
  44.         super(reason);
  45.         this.reason = reason;
  46.         this.expected = expected;
  47.         this.actual = actual;
  48.     }

  49.     @Override
  50.     public String getMessage()
  51.     {
  52.         return buildMessage(this.reason, this.expected, this.actual);
  53.     }

  54.     public String getReason()
  55.     {
  56.         return reason;
  57.     }

  58.     public String getExpected()
  59.     {
  60.         return expected;
  61.     }

  62.     public String getActual()
  63.     {
  64.         return actual;
  65.     }

  66.     @Override
  67.     public String toString()
  68.     {
  69.         final StringBuilder sb = new StringBuilder();
  70.         sb.append(getClass().getName()).append("[");
  71.         sb.append(reason);
  72.         sb.append("expected:<").append(expected);
  73.         sb.append("> but was:<").append(actual).append(">");
  74.         sb.append("]");
  75.         return sb.toString();
  76.     }


  77.     /**
  78.      * Creates a formatted message string from the given parameters
  79.      * @param reason The reason for an assertion or comparison failure
  80.      * @param expected The expected result
  81.      * @param actual The actual result
  82.      * @return The formatted message
  83.      */
  84.     public static final String buildMessage(final String reason, final String expected, final String actual)
  85.     {
  86.         final StringBuilder sb = new StringBuilder();
  87.         sb.append(reason);
  88.         sb.append(" expected:<").append(expected).append(">");
  89.         sb.append(" but was:<").append(actual).append(">");
  90.         return sb.toString();

  91.     }
  92. }