View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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  package org.apache.commons.jelly.tags.junit;
17  
18  import org.apache.commons.jelly.xpath.XPathTagSupport;
19  
20  /***
21   * The abstract base class of any assertion tag which is
22   * useful for implementation inheritence.
23   *
24   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25   * @version $Revision: 345902 $
26   */
27  public abstract class AssertTagSupport extends XPathTagSupport {
28  
29      /*** the default message to display if none is given */
30      private static String DEFAULT_MESSAGE = "assertion failed";
31      
32      public AssertTagSupport() {
33      }
34  
35      // Implementation methods
36      //-------------------------------------------------------------------------
37  
38      /***
39       * Produces a failure assertion with the given message
40       * @throws JellyAssertionFailedError to signify failure
41       */
42      protected void fail(String message) throws JellyAssertionFailedError {
43          throw new JellyAssertionFailedError(message);
44      }
45  
46      /***
47       * Produces a failure assertion with a default message
48       * @throws JellyAssertionFailedError to signify failure
49       */
50      protected void fail() throws JellyAssertionFailedError {
51          throw new JellyAssertionFailedError(DEFAULT_MESSAGE);
52      }
53  
54      /***
55       * Produces a failure assertion with the given message and added detail.
56       * @throws JellyAssertionFailedError to signify failure
57       */
58      protected void fail(String message, String detail) throws JellyAssertionFailedError {
59          if (message == null || message.length() == 0) {
60              fail(detail);
61          }
62          else {
63              fail(message + ". Assertion failed while " + detail);
64          }
65      }
66  
67      /***
68       * Produces a failure if the actual value was not equal to the expected value
69       * @throws JellyAssertionFailedError if expected != actual.
70       */
71      protected void failNotEquals(String message, Object expected, Object actual, String expressions) throws JellyAssertionFailedError {
72          String formatted= "";
73          if (message != null) {
74              formatted = message +" ";
75          }
76          fail(formatted + "expected:[" + expected + "] but was:[" + actual + "]" + expressions);
77      }
78      
79      /***
80       * Fail if actual is not true
81       * @param message failure message
82       * @param actual value to test
83       * @throws JellyAssertionFailedError to signify failure
84       */
85      protected void assertTrue(String message, boolean actual) throws JellyAssertionFailedError
86      {
87          if (!actual) fail(message);
88      }
89      
90      /***
91       * Fail if actual is not true
92       * @param actual value to test
93       * @throws JellyAssertionFailedError to signify failure
94       */
95      protected void assertTrue(boolean actual) throws JellyAssertionFailedError
96      {
97          assertTrue(DEFAULT_MESSAGE, actual);
98      }
99  
100     /***
101      * Fail if actual is true
102      * @param message failure message
103      * @param actual value to test
104      * @throws JellyAssertionFailedError to signify failure
105      */
106     protected void assertFalse(String message, boolean actual) throws JellyAssertionFailedError
107     {
108         if (actual) fail(message);
109     }
110     
111     /***
112      * Fail if actual is true
113      * @param actual value to test
114      * @throws JellyAssertionFailedError to signify failure
115      */
116     protected void assertFalse(boolean actual) throws JellyAssertionFailedError
117     {
118         assertFalse(DEFAULT_MESSAGE, actual);
119     }
120     
121     /***
122      * Fail if !expected.equals(actual). If expected is null, actual must be.
123      * @param message failure message.
124      * @param expected expected value.
125      * @param actual actual value to compare against expected.
126      * @throws JellyAssertionFailedError to signify failure
127      */
128     protected void assertEquals(String message, Object expected, Object actual)
129         throws JellyAssertionFailedError
130     {
131         if (expected == null)
132         {
133             assertTrue(message, actual == null);
134         }
135         else
136         {
137             assertTrue(message, expected.equals(actual));
138         }
139     }
140 
141     /***
142      * @see #assertEquals(String, Object, Object)
143      */
144     protected void assertEquals(Object expected, Object actual)
145     throws JellyAssertionFailedError
146     {
147         assertEquals(DEFAULT_MESSAGE, expected, actual);
148     }
149     
150     /***
151      * @see #assertEquals(String, Object, Object)
152      */
153     protected void assertEquals(String message, boolean expected, boolean actual)
154     throws JellyAssertionFailedError
155     {
156         assertTrue(message, expected == actual);
157     }
158     /***
159      * @see #assertEquals(Object, Object)
160      */
161     protected void assertEquals(boolean expected, boolean actual)
162     throws JellyAssertionFailedError
163     {
164         assertTrue(DEFAULT_MESSAGE, expected == actual);
165     }
166     
167     /***
168      * @see #assertEquals(String, Object, Object)
169      */
170     protected void assertEquals(String message, byte expected, byte actual)
171     throws JellyAssertionFailedError
172     {
173         assertTrue(message, expected == actual);
174     }
175     /***
176      * @see #assertEquals(Object, Object)
177      */
178     protected void assertEquals(byte expected, byte actual)
179     throws JellyAssertionFailedError
180     {
181         assertTrue(DEFAULT_MESSAGE, expected == actual);
182     }
183     /***
184      * @see #assertEquals(String, Object, Object)
185      */
186     protected void assertEquals(String message, char expected, char actual)
187     throws JellyAssertionFailedError
188     {
189         assertTrue(message, expected == actual);
190     }
191     /***
192      * @see #assertEquals(Object, Object)
193      */
194     protected void assertEquals(char expected, char actual)
195     throws JellyAssertionFailedError
196     {
197         assertTrue(DEFAULT_MESSAGE, expected == actual);
198     }
199     /***
200      * @see #assertEquals(String, Object, Object)
201      */
202     protected void assertEquals(String message, double expected, double actual)
203     throws JellyAssertionFailedError
204     {
205         assertTrue(message, expected == actual);
206     }
207     /***
208      * @see #assertEquals(Object, Object)
209      */
210     protected void assertEquals(double expected, double actual)
211     throws JellyAssertionFailedError
212     {
213         assertTrue(DEFAULT_MESSAGE, expected == actual);
214     }
215     /***
216      * @see #assertEquals(String, Object, Object)
217      */
218     protected void assertEquals(String message, float expected, float actual)
219     throws JellyAssertionFailedError
220     {
221         assertTrue(message, expected == actual);
222     }
223     /***
224      * @see #assertEquals(Object, Object)
225      */
226     protected void assertEquals(float expected, float actual)
227     throws JellyAssertionFailedError
228     {
229         assertTrue(DEFAULT_MESSAGE, expected == actual);
230     }
231     /***
232      * @see #assertEquals(String, Object, Object)
233      */
234     protected void assertEquals(String message, int expected, int actual)
235     throws JellyAssertionFailedError
236     {
237         assertTrue(message, expected == actual);
238     }
239     /***
240      * @see #assertEquals(Object, Object)
241      */
242     protected void assertEquals(int expected, int actual)
243     throws JellyAssertionFailedError
244     {
245         assertTrue(DEFAULT_MESSAGE, expected == actual);
246     }
247     /***
248      * @see #assertEquals(String, Object, Object)
249      */
250     protected void assertEquals(String message, long expected, long actual)
251     throws JellyAssertionFailedError
252     {
253         assertTrue(message, expected == actual);
254     }
255     /***
256      * @see #assertEquals(Object, Object)
257      */
258     protected void assertEquals(long expected, long actual)
259     throws JellyAssertionFailedError
260     {
261         assertTrue(DEFAULT_MESSAGE, expected == actual);
262     }
263     /***
264      * @see #assertEquals(String, Object, Object)
265      */
266     protected void assertEquals(String message, short expected, short actual)
267     throws JellyAssertionFailedError
268     {
269         assertTrue(message, expected == actual);
270     }
271     /***
272      * @see #assertEquals(Object, Object)
273      */
274     protected void assertEquals(short expected, short actual)
275     throws JellyAssertionFailedError
276     {
277         assertTrue(DEFAULT_MESSAGE, expected == actual);
278     }
279 
280     /***
281      * Fail if actual is not null
282      * @param message failure message
283      * @param actual value to check
284      * @throws JellyAssertionFailedError to signify failure
285      */
286     protected void assertNull(String message, Object actual)
287     {
288         assertTrue(message, actual == null);
289     }
290     /***
291      * @see assertNull(String, Object)
292      */
293     protected void assertNull(Object actual)
294     {
295         assertNull(DEFAULT_MESSAGE, actual);
296     }
297 
298     /***
299      * Fail if actual is null
300      * @param message failure message
301      * @param actual value to check
302      * @throws JellyAssertionFailedError to signify failure
303      */
304     protected void assertNotNull(String message, Object actual)
305     {
306         assertTrue(message, actual != null);
307     }
308     /***
309      * @see assertNotNull(String, Object)
310      */
311     protected void assertNotNull(Object actual)
312     {
313         assertNotNull(DEFAULT_MESSAGE, actual);
314     }
315 
316     /***
317      * Fail if expected != actual. If expected is null, actual must not be.
318      * @param message failure message.
319      * @param expected expected value.
320      * @param actual actual value to compare against expected.
321      * @throws JellyAssertionFailedError to signify failure
322      */
323     protected void assertSame(String message, Object expected, Object actual)
324         throws JellyAssertionFailedError
325     {
326         assertTrue(message, expected == actual);
327     }
328     /***
329      * @see #assertSame(String, Object, Object)
330      */
331     protected void assertSame(Object expected, Object actual)
332     throws JellyAssertionFailedError
333     
334     {
335         assertSame(DEFAULT_MESSAGE, expected, actual);
336     }
337 
338     /***
339      * Fail if expected == actual. If expected is null, actual must be.
340      * @param message failure message.
341      * @param expected expected value.
342      * @param actual actual value to compare against expected.
343      * @throws JellyAssertionFailedError to signify failure
344      */
345     protected void assertNotSame(String message, Object expected, Object actual)
346         throws JellyAssertionFailedError
347     {
348         assertTrue(message, expected != actual);
349     }
350     /***
351      * @see #assertNotSame(String, Object, Object)
352      */
353     protected void assertNotSame(Object expected, Object actual)
354     throws JellyAssertionFailedError
355     
356     {
357         assertNotSame(DEFAULT_MESSAGE, expected, actual);
358     }
359 }