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