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