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(final String reason, final String expected, final String actual)
46 {
47 super(reason);
48 this.reason = reason;
49 this.expected = expected;
50 this.actual = actual;
51 }
52
53 @Override
54 public String getMessage()
55 {
56 return buildMessage(this.reason, this.expected, this.actual);
57 }
58
59 public String getReason()
60 {
61 return reason;
62 }
63
64 public String getExpected()
65 {
66 return expected;
67 }
68
69 public String getActual()
70 {
71 return actual;
72 }
73
74 @Override
75 public String toString()
76 {
77 final StringBuilder sb = new StringBuilder();
78 sb.append(getClass().getName()).append("[");
79 sb.append(reason);
80 sb.append("expected:<").append(expected);
81 sb.append("> but was:<").append(actual).append(">");
82 sb.append("]");
83 return sb.toString();
84 }
85
86
87 /**
88 * Creates a formatted message string from the given parameters
89 * @param reason The reason for an assertion or comparison failure
90 * @param expected The expected result
91 * @param actual The actual result
92 * @return The formatted message
93 */
94 public static final String buildMessage(final String reason, final String expected, final String actual)
95 {
96 final StringBuilder sb = new StringBuilder();
97 sb.append(reason);
98 sb.append(" expected:<").append(expected).append(">");
99 sb.append(" but was:<").append(actual).append(">");
100 return sb.toString();
101
102 }
103 }