View Javadoc

1   /*
2    * Copyright 2007 united internet (unitedinternet.com) Robert Zimmermann
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 com.unitedinternet.portal.selenium.utils.logging;
18  
19  import org.hamcrest.Matcher;
20  
21  /**
22   * Wrapped junit4 assertions to enable logging them.
23   * 
24   * Only a couple junit4 assertions are wrapped here. And these always expect a message (use null if you do not care)
25   * 
26   * TODO: use reflection proxy or AOP to get this working more smoothly
27   * 
28   * Maybe wrap also these? fail(String message), assertArrayEquals(String message, Object[] expecteds, Object[] actuals)
29   * 
30   * @author Robert Zimmermann
31   * 
32   * $Id: LoggingAssert.java 66 2008-02-11 22:27:05Z bobbyde $
33   */
34  public final class LoggingAssert {
35  
36      private LoggingAssert() {
37          // static class only
38      }
39  
40      /**
41       * As in Junit4 (see there for details).
42       * 
43       * Additionaly an selenium object is needed to enable logging.
44       * 
45       * @param message always needed - please provide a good description what may have been wrong
46       * @param condition condition to be checked for true
47       * @param selenium selenium object to be logged through
48       */
49      public static void assertTrue(java.lang.String message, boolean condition, LoggingSelenium selenium) {
50          try {
51              org.junit.Assert.assertTrue(message, condition);
52          } catch (AssertionError e) {
53              selenium.logAssertion("assertTrue", message, "condition=" + condition);
54              throw e;
55          }
56      }
57  
58      /**
59       * As in Junit4 (see there for details).
60       * 
61       * Additionaly an selenium object is needed to enable logging.
62       * 
63       * @param message always needed - please provide a good description what may have been wrong
64       * @param condition condition to be checked for false
65       * @param selenium selenium object to be logged through
66       */
67      public static void assertFalse(java.lang.String message, boolean condition, LoggingSelenium selenium) {
68          try {
69              org.junit.Assert.assertFalse(message, condition);
70          } catch (AssertionError e) {
71              selenium.logAssertion("assertFalse", message, "condition=" + condition);
72              throw e;
73          }
74      }
75  
76      /**
77       * As in Junit4 (see there for details).
78       * 
79       * Additionaly an selenium object is needed to enable logging.
80       * 
81       * @param message always needed - please provide a good description what may have been wrong
82       * @param expected what is expected
83       * @param actual what is to be checked for equality
84       * @param selenium selenium object to be logged through
85       */
86      public static void assertEquals(java.lang.String message, Object expected, Object actual, LoggingSelenium selenium) {
87          try {
88              org.junit.Assert.assertEquals(message, expected, actual);
89          } catch (AssertionError e) {
90              selenium.logAssertion("assertEquals", message, "expected=" + expected + " actual=" + actual);
91              throw e;
92          }
93      }
94  
95      /**
96       * As in Junit4 (see there for details).
97       * 
98       * Additionaly an selenium object is needed to enable logging.
99       * 
100      * @param <T> see junit4 for details
101      * @param message always needed - please provide a good description what may have been wrong
102      * @param actual what is to be checked for equality
103      * @param matcher see junit4 for details on possible matcher
104      * @param selenium selenium object to be logged through
105      */
106     public static <T> void assertThat(java.lang.String message, T actual, Matcher<T> matcher, LoggingSelenium selenium) {
107         try {
108             org.junit.Assert.assertThat(message, actual, matcher);
109         } catch (AssertionError e) {
110             selenium.logAssertion("assertThat", message, e.getMessage());
111             throw e;
112         }
113     }
114 }