View Javadoc

1   /*
2    * Copyright 2005-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.test.support;
18  
19  import javax.xml.transform.Source;
20  
21  /**
22   * JUnit independent assertion class.
23   *
24   * @author Lukas Krecan
25   * @author Arjen Poutsma
26   * @since 2.0
27   */
28  public abstract class AssertionErrors {
29  
30      private AssertionErrors() {
31      }
32  
33      /**
34       * Fails a test with the given message.
35       *
36       * @param message the message
37       */
38      public static void fail(String message) {
39          throw new AssertionError(message);
40      }
41  
42      /**
43       * Fails a test with the given message and source.
44       *
45       * @param message the message
46       * @param source  the source
47       */
48      public static void fail(String message, String sourceLabel, Source source) {
49          if (source != null) {
50              throw new SourceAssertionError(message, sourceLabel, source);
51          }
52          else {
53              fail(message);
54          }
55      }
56  
57      /**
58       * Asserts that a condition is {@code true}. If not, throws an {@link AssertionError} with the given message.
59       *
60       * @param message   the message
61       * @param condition the condition to test for
62       */
63      public static void assertTrue(String message, boolean condition) {
64          assertTrue(message, condition, null, null);
65      }
66  
67      /**
68       * Asserts that a condition is {@code true}. If not, throws an {@link AssertionError} with the given message and
69       * source.
70       *
71       * @param message   the message
72       * @param condition the condition to test for
73       */
74      public static void assertTrue(String message, boolean condition, String sourceLabel, Source source) {
75          if (!condition) {
76              fail(message, sourceLabel, source);
77          }
78      }
79  
80      /**
81       * Asserts that two objects are equal. If not, an {@link AssertionError} is thrown with the given message.
82       *
83       * @param message  the message
84       * @param expected the expected value
85       * @param actual   the actual value
86       */
87      public static void assertEquals(String message, Object expected, Object actual) {
88          assertEquals(message, expected, actual, null, null);
89      }
90  
91      /**
92       * Asserts that two objects are equal. If not, an {@link AssertionError} is thrown with the given message.
93       *
94       * @param message  the message
95       * @param expected the expected value
96       * @param actual   the actual value
97       * @param source   the source
98       */
99      public static void assertEquals(String message, Object expected, Object actual, String sourceLabel, Source source) {
100         if (expected == null && actual == null) {
101             return;
102         }
103         if (expected != null && expected.equals(actual)) {
104             return;
105         }
106         fail(message + " expected:<" + expected + "> but was:<" + actual + ">", sourceLabel, source);
107     }
108 
109 }