Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2007 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 package org.mockftpserver.core.util;
     17 
     18 import java.util.Collection;
     19 import java.util.Map;
     20 
     21 /**
     22  * Provides static helper methods to make runtime assertions. Throws an
     23  * <code>AssertFailedException</code> when the assertion fails. All methods are static.
     24  *
     25  * @version $Revision$ - $Date$
     26  *
     27  * @author Chris Mair
     28  */
     29 public final class Assert {
     30 
     31     /**
     32      * Verify that arg is null. Throw an AssertFailedException if it is not null.
     33      * @param arg - the method parameter value
     34      * @param argName - the name of the parameter; used in the exception message
     35      * @throws AssertFailedException - if arg is not null
     36      */
     37     public static void isNull(Object arg, String argName) {
     38         if (arg != null) {
     39             throw new AssertFailedException("The value for \"" + argName + "\" must be null");
     40         }
     41     }
     42 
     43 	/**
     44 	 * Verify that arg is not null. Throw an AssertFailedException if it is null.
     45 	 * @param arg - the method parameter value
     46      * @param argName - the name of the parameter; used in the exception message
     47 	 * @throws AssertFailedException - if arg is null
     48 	 */
     49     public static void notNull(Object arg, String argName) {
     50 		if (arg == null) {
     51             throw new AssertFailedException("The value of \"" + argName + "\" is null");
     52 		}
     53 	}
     54 
     55 	/**
     56 	 * Verify that condition is true. Throw an AssertFailedException if it is false.
     57 	 * @param condition - the condition that should be true
     58 	 * @throws AssertFailedException - if condition is false
     59 	 */
     60 	public static void isTrue(boolean condition, String message) {
     61 		if (!condition) {
     62 			throw new AssertFailedException(message);
     63 		}
     64 	}
     65 
     66 	/**
     67 	 * Verify that condition is false. Throw an AssertFailedException if it is true.
     68 	 * @param condition - the condition that should be false
     69 	 * @throws AssertFailedException - if condition is true
     70 	 */
     71 	public static void isFalse(boolean condition, String message) {
     72 		if (condition) {
     73 			throw new AssertFailedException(message);
     74 		}
     75 	}
     76 
     77 	/**
     78 	 * Verify that the collection is not null or empty. Throw an
     79 	 * AssertFailedException if it is null or empty.
     80 	 * @param collection - the Collection
     81      * @param argName - the name of the parameter; used in the exception message
     82 	 * @throws AssertFailedException - if collection is null or empty
     83 	 */
     84     public static void notNullOrEmpty(Collection collection, String argName) {
     85         notNull(collection, argName);
     86 		if (collection.isEmpty()) {
     87             throw new AssertFailedException("The \"" + argName + "\" Collection is empty");
     88 		}
     89 	}
     90 
     91 	/**
     92 	 * Verify that the Map is not null or empty. Throw an AssertFailedException
     93 	 * if it is null or empty.
     94 	 * @param map - the Map
     95      * @param argName - the name of the parameter; used in the exception message
     96 	 * @throws AssertFailedException - if map is null or empty
     97 	 */
     98     public static void notNullOrEmpty(Map map, String argName) {
     99         notNull(map, argName);
    100 		if (map.isEmpty()) {
    101             throw new AssertFailedException("The \"" + argName + "\" Map is empty");
    102 		}
    103 	}
    104 
    105 	/**
    106 	 * Verify that the array is not null or empty. Throw an
    107 	 * AssertFailedException if it is null or empty.
    108 	 * @param array - the array
    109      * @param argName - the name of the parameter; used in the exception message
    110 	 * @throws AssertFailedException - if array is null or empty
    111 	 */
    112     public static void notNullOrEmpty(Object[] array, String argName) {
    113         notNull(array, argName);
    114 		if (array.length == 0) {
    115             throw new AssertFailedException("The \"" + argName + "\" array is empty");
    116 		}
    117 	}
    118 
    119 	/**
    120 	 * Verify that the String is not null or empty. Throw an
    121 	 * AssertFailedException if it is null or empty.
    122 	 * @param string - the String
    123      * @param argName - the name of the parameter; used in the exception message
    124 	 * @throws AssertFailedException - if string is null or empty
    125 	 */
    126     public static void notNullOrEmpty(String string, String argName) {
    127         notNull(string, argName);
    128 		if (string.trim().length() == 0) {
    129             throw new AssertFailedException("The \"" + argName + "\" String is empty");
    130 		}
    131 	}
    132 
    133 	/**
    134 	 * Private constructor. All methods are static
    135 	 */
    136 	private Assert() {
    137 	}
    138 }
    139