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  /**
24   * Exception signaling a DbUnit assertion failure while comparing values. 
25   * Is used to avoid the direct dependency to any other testing framework.
26   * 
27   * @author gommma (gommma AT users.sourceforge.net)
28   * @author Last changed by: $Author$
29   * @version $Revision$ $Date$
30   * @since 2.4.0
31   */
32  public class DbComparisonFailure extends AssertionError
33  {
34      private static final long serialVersionUID = 1L;
35  
36      private String reason;
37      private String expected;
38      private String actual;
39      
40      /**
41       * @param reason The reason for the comparison failure
42       * @param expected The expected value
43       * @param actual The actual value
44       */
45      public DbComparisonFailure(String reason, String expected, String actual) 
46      {
47          super(reason);
48          this.reason = reason;
49          this.expected = expected;
50          this.actual = actual;
51      }
52      
53      public String getMessage() 
54      {
55          return buildMessage(this.reason, this.expected, this.actual);
56      }
57  
58      public String getReason() 
59      {
60          return reason;
61      }
62  
63      public String getExpected() 
64      {
65          return expected;
66      }
67  
68      public String getActual() 
69      {
70          return actual;
71      }
72  
73      public String toString()
74      {
75          StringBuffer sb = new StringBuffer();
76          sb.append(getClass().getName()).append("[");
77          sb.append(reason);
78          sb.append("expected:<").append(expected);
79          sb.append(">but was:<").append(actual).append(">");
80          sb.append("]");
81          return sb.toString();
82      }
83      
84      
85      /**
86       * Creates a formatted message string from the given parameters
87       * @param reason The reason for an assertion or comparison failure
88       * @param expected The expected result
89       * @param actual The actual result
90       * @return The formatted message
91       */
92      public static final String buildMessage(String reason, String expected, String actual)
93      {
94          StringBuffer sb = new StringBuffer();
95          sb.append(reason);
96          sb.append(" expected:<").append(expected).append(">");
97          sb.append(" but was:<").append(actual).append(">");
98          return sb.toString();
99  
100     }
101 }