SimpleAssert.java

  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. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. /**
  25.  * Dbunit's own small assertion utility, independent from the testing framework
  26.  * that is used.
  27.  *
  28.  * @author gommma (gommma AT users.sourceforge.net)
  29.  * @author Last changed by: $Author$
  30.  * @version $Revision$ $Date$
  31.  * @since 2.4.0
  32.  */
  33. public class SimpleAssert
  34. {
  35.     /**
  36.      * Logger for this class
  37.      */
  38.     private static final Logger logger = LoggerFactory.getLogger(SimpleAssert.class);

  39.     private FailureHandler failureHandler;
  40.    
  41.     public SimpleAssert(FailureHandler failureHandler)
  42.     {
  43.         if (failureHandler == null) {
  44.             throw new NullPointerException(
  45.                     "The parameter 'failureHandler' must not be null");
  46.         }
  47.         this.failureHandler = failureHandler;
  48.     }
  49.    
  50.     /**
  51.      * Asserts that propertyName is not a null String and has a length greater
  52.      * than zero.
  53.      */
  54.     protected void assertNotNullNorEmpty( String propertyName, String property )
  55.     {
  56.         logger.debug("assertNotNullNorEmpty(propertyName={}, property={}) - start", propertyName, property);

  57.         assertTrue( propertyName + " is null", property != null );
  58.         assertTrue( "Invalid " + propertyName, property.trim()
  59.                 .length() > 0 );
  60.     }

  61.     public void assertTrue(boolean condition) {
  62.         assertTrue(null, condition);
  63.     }
  64.    
  65.     /**
  66.      * Evaluate if the given condition is <code>true</code> or not.
  67.      * @param message message displayed if assertion is false
  68.      * @param condition condition to be tested
  69.      */
  70.     public void assertTrue(String message, boolean condition) {
  71.         if (!condition) {
  72.             fail( message );
  73.         }
  74.     }

  75.     public void assertNotNull(Object object) {
  76.         assertTrue(null, object!=null);
  77.     }

  78.     public void assertNotNull(String message, Object object) {
  79.         assertTrue(message, object!=null);
  80.     }
  81.    
  82.     public void fail(String message) {
  83.         throw failureHandler.createFailure(message);
  84.     }

  85. }