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  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Dbunit's own small assertion utility, independent from the testing framework
28   * that is used.
29   * 
30   * @author gommma (gommma AT users.sourceforge.net)
31   * @author Last changed by: $Author$
32   * @version $Revision$ $Date$
33   * @since 2.4.0
34   */
35  public class SimpleAssert 
36  {
37      /**
38       * Logger for this class
39       */
40      private static final Logger logger = LoggerFactory.getLogger(SimpleAssert.class);
41  
42      private FailureHandler failureHandler;
43      
44      public SimpleAssert(FailureHandler failureHandler)
45      {
46          if (failureHandler == null) {
47              throw new NullPointerException(
48                      "The parameter 'failureHandler' must not be null");
49          }
50          this.failureHandler = failureHandler;
51      }
52      
53      /**
54       * Asserts that propertyName is not a null String and has a length greater
55       * than zero.
56       */
57      protected void assertNotNullNorEmpty( String propertyName, String property )
58      {
59          logger.debug("assertNotNullNorEmpty(propertyName={}, property={}) - start", propertyName, property);
60  
61          assertTrue( propertyName + " is null", property != null );
62          assertTrue( "Invalid " + propertyName, property.trim()
63                  .length() > 0 );
64      }
65  
66      public void assertTrue(boolean condition) {
67          assertTrue(null, condition);
68      }
69      
70      /**
71       * Evaluate if the given condition is <code>true</code> or not.
72       * @param message message displayed if assertion is false
73       * @param condition condition to be tested
74       */
75      public void assertTrue(String message, boolean condition) {
76          if (!condition) {
77              fail( message );
78          }
79      }
80  
81      public void assertNotNull(Object object) {
82          assertTrue(null, object!=null);
83      }
84  
85      public void assertNotNull(String message, Object object) {
86          assertTrue(message, object!=null);
87      }
88      
89      public void fail(String message) {
90          throw failureHandler.createFailure(message);
91      }
92  
93  }