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