1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/* 2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2007 the original author or authors. 3bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License. 6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at 7bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 8bda3441225e0607b5ced8b538123fd7c7a417910chrismair * http://www.apache.org/licenses/LICENSE-2.0 9bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software 11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS, 12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and 14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License. 15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.core.util; 17bda3441225e0607b5ced8b538123fd7c7a417910chrismair 18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport java.util.Collection; 19bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport java.util.Map; 20bda3441225e0607b5ced8b538123fd7c7a417910chrismair 21bda3441225e0607b5ced8b538123fd7c7a417910chrismair/** 22bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Provides static helper methods to make runtime assertions. Throws an 23bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <code>AssertFailedException</code> when the assertion fails. All methods are static. 24bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 25bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$ 26bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 27bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair 28bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 29bda3441225e0607b5ced8b538123fd7c7a417910chrismairpublic final class Assert { 30bda3441225e0607b5ced8b538123fd7c7a417910chrismair 31bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 32bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that arg is null. Throw an AssertFailedException if it is not null. 33bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param arg - the method parameter value 34bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param argName - the name of the parameter; used in the exception message 35bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if arg is not null 36bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 37bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void isNull(Object arg, String argName) { 38bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (arg != null) { 39bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException("The value for \"" + argName + "\" must be null"); 40bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 41bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 42bda3441225e0607b5ced8b538123fd7c7a417910chrismair 43bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 44bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that arg is not null. Throw an AssertFailedException if it is null. 45bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param arg - the method parameter value 46bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param argName - the name of the parameter; used in the exception message 47bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if arg is null 48bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 49bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void notNull(Object arg, String argName) { 50bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (arg == null) { 51bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException("The value of \"" + argName + "\" is null"); 52bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 53bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 54bda3441225e0607b5ced8b538123fd7c7a417910chrismair 55bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 56bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that condition is true. Throw an AssertFailedException if it is false. 57bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param condition - the condition that should be true 58bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if condition is false 59bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 60bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void isTrue(boolean condition, String message) { 61bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (!condition) { 62bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException(message); 63bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 64bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 65bda3441225e0607b5ced8b538123fd7c7a417910chrismair 66bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 67bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that condition is false. Throw an AssertFailedException if it is true. 68bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param condition - the condition that should be false 69bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if condition is true 70bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 71bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void isFalse(boolean condition, String message) { 72bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (condition) { 73bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException(message); 74bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 75bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 76bda3441225e0607b5ced8b538123fd7c7a417910chrismair 77bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 78bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that the collection is not null or empty. Throw an 79bda3441225e0607b5ced8b538123fd7c7a417910chrismair * AssertFailedException if it is null or empty. 80bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param collection - the Collection 81bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param argName - the name of the parameter; used in the exception message 82bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if collection is null or empty 83bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 84bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void notNullOrEmpty(Collection collection, String argName) { 85bda3441225e0607b5ced8b538123fd7c7a417910chrismair notNull(collection, argName); 86bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (collection.isEmpty()) { 87bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException("The \"" + argName + "\" Collection is empty"); 88bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 89bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 90bda3441225e0607b5ced8b538123fd7c7a417910chrismair 91bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 92bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that the Map is not null or empty. Throw an AssertFailedException 93bda3441225e0607b5ced8b538123fd7c7a417910chrismair * if it is null or empty. 94bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param map - the Map 95bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param argName - the name of the parameter; used in the exception message 96bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if map is null or empty 97bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 98bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void notNullOrEmpty(Map map, String argName) { 99bda3441225e0607b5ced8b538123fd7c7a417910chrismair notNull(map, argName); 100bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (map.isEmpty()) { 101bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException("The \"" + argName + "\" Map is empty"); 102bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 103bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 104bda3441225e0607b5ced8b538123fd7c7a417910chrismair 105bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 106bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that the array is not null or empty. Throw an 107bda3441225e0607b5ced8b538123fd7c7a417910chrismair * AssertFailedException if it is null or empty. 108bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param array - the array 109bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param argName - the name of the parameter; used in the exception message 110bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if array is null or empty 111bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 112bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void notNullOrEmpty(Object[] array, String argName) { 113bda3441225e0607b5ced8b538123fd7c7a417910chrismair notNull(array, argName); 114bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (array.length == 0) { 115bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException("The \"" + argName + "\" array is empty"); 116bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 117bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 118bda3441225e0607b5ced8b538123fd7c7a417910chrismair 119bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 120bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that the String is not null or empty. Throw an 121bda3441225e0607b5ced8b538123fd7c7a417910chrismair * AssertFailedException if it is null or empty. 122bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param string - the String 123bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param argName - the name of the parameter; used in the exception message 124bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws AssertFailedException - if string is null or empty 125bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 126bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static void notNullOrEmpty(String string, String argName) { 127bda3441225e0607b5ced8b538123fd7c7a417910chrismair notNull(string, argName); 128bda3441225e0607b5ced8b538123fd7c7a417910chrismair if (string.trim().length() == 0) { 129bda3441225e0607b5ced8b538123fd7c7a417910chrismair throw new AssertFailedException("The \"" + argName + "\" String is empty"); 130bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 131bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 132bda3441225e0607b5ced8b538123fd7c7a417910chrismair 133bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 134bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Private constructor. All methods are static 135bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 136bda3441225e0607b5ced8b538123fd7c7a417910chrismair private Assert() { 137bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 138bda3441225e0607b5ced8b538123fd7c7a417910chrismair} 139