19d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/*
29d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Copyright 2007 the original author or authors.
39d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
49d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Licensed under the Apache License, Version 2.0 (the "License");
59d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * you may not use this file except in compliance with the License.
69d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * You may obtain a copy of the License at
79d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
89d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *      http://www.apache.org/licenses/LICENSE-2.0
99d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Unless required by applicable law or agreed to in writing, software
119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * distributed under the License is distributed on an "AS IS" BASIS,
129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * See the License for the specific language governing permissions and
149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * limitations under the License.
159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpackage org.mockftpserver.core.util;
179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport java.util.Collection;
199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport java.util.Map;
209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/**
229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Provides static helper methods to make runtime assertions. Throws an
239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * <code>AssertFailedException</code> when the assertion fails. All methods are static.
249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @version $Revision$ - $Date$
269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @author Chris Mair
289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpublic final class Assert {
309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Verify that arg is null. Throw an AssertFailedException if it is not null.
339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param arg - the method parameter value
349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param argName - the name of the parameter; used in the exception message
359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @throws AssertFailedException - if arg is not null
369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public static void isNull(Object arg, String argName) {
389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        if (arg != null) {
399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            throw new AssertFailedException("The value for \"" + argName + "\" must be null");
409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
439d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
449d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Verify that arg is not null. Throw an AssertFailedException if it is null.
459d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @param arg - the method parameter value
469d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param argName - the name of the parameter; used in the exception message
479d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @throws AssertFailedException - if arg is null
489d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
499d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public static void notNull(Object arg, String argName) {
509d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		if (arg == null) {
519d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            throw new AssertFailedException("The value of \"" + argName + "\" is null");
529d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		}
539d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
549d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
559d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
569d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Verify that condition is true. Throw an AssertFailedException if it is false.
579d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @param condition - the condition that should be true
589d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @throws AssertFailedException - if condition is false
599d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
609d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	public static void isTrue(boolean condition, String message) {
619d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		if (!condition) {
629d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair			throw new AssertFailedException(message);
639d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		}
649d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
659d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
669d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
679d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Verify that condition is false. Throw an AssertFailedException if it is true.
689d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @param condition - the condition that should be false
699d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @throws AssertFailedException - if condition is true
709d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
719d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	public static void isFalse(boolean condition, String message) {
729d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		if (condition) {
739d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair			throw new AssertFailedException(message);
749d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		}
759d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
769d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
779d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
789d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Verify that the collection is not null or empty. Throw an
799d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * AssertFailedException if it is null or empty.
809d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @param collection - the Collection
819d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param argName - the name of the parameter; used in the exception message
829d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @throws AssertFailedException - if collection is null or empty
839d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
849d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public static void notNullOrEmpty(Collection collection, String argName) {
859d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        notNull(collection, argName);
869d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		if (collection.isEmpty()) {
879d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            throw new AssertFailedException("The \"" + argName + "\" Collection is empty");
889d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		}
899d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
909d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
919d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
929d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Verify that the Map is not null or empty. Throw an AssertFailedException
939d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * if it is null or empty.
949d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @param map - the Map
959d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param argName - the name of the parameter; used in the exception message
969d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @throws AssertFailedException - if map is null or empty
979d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
989d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public static void notNullOrEmpty(Map map, String argName) {
999d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        notNull(map, argName);
1009d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		if (map.isEmpty()) {
1019d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            throw new AssertFailedException("The \"" + argName + "\" Map is empty");
1029d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		}
1039d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
1049d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1059d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
1069d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Verify that the array is not null or empty. Throw an
1079d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * AssertFailedException if it is null or empty.
1089d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @param array - the array
1099d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param argName - the name of the parameter; used in the exception message
1109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @throws AssertFailedException - if array is null or empty
1119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
1129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public static void notNullOrEmpty(Object[] array, String argName) {
1139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        notNull(array, argName);
1149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		if (array.length == 0) {
1159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            throw new AssertFailedException("The \"" + argName + "\" array is empty");
1169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		}
1179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
1189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
1209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Verify that the String is not null or empty. Throw an
1219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * AssertFailedException if it is null or empty.
1229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @param string - the String
1239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param argName - the name of the parameter; used in the exception message
1249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * @throws AssertFailedException - if string is null or empty
1259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
1269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public static void notNullOrEmpty(String string, String argName) {
1279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        notNull(string, argName);
1289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		if (string.trim().length() == 0) {
1299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            throw new AssertFailedException("The \"" + argName + "\" String is empty");
1309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair		}
1319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
1329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	/**
1349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 * Private constructor. All methods are static
1359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	 */
1369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	private Assert() {
1379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair	}
1389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair}
139