100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair/*
200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Copyright 2007 the original author or authors.
300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Licensed under the Apache License, Version 2.0 (the "License");
500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * you may not use this file except in compliance with the License.
600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * You may obtain a copy of the License at
700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *      http://www.apache.org/licenses/LICENSE-2.0
900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
1000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Unless required by applicable law or agreed to in writing, software
1100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * See the License for the specific language governing permissions and
1400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * limitations under the License.
1500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */
1600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairpackage org.mockftpserver.core.util;
1700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
1800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport java.util.Collection;
1900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport java.util.Map;
2000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
2100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair/**
2200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Provides static helper methods to make runtime assertions. Throws an
2300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * <code>AssertFailedException</code> when the assertion fails. All methods are static.
2400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
2500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @version $Revision$ - $Date$
2600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
2700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @author Chris Mair
2800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */
2900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairpublic final class Assert {
3000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
3100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    /**
3200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * Verify that arg is null. Throw an AssertFailedException if it is not null.
3300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @param arg - the method parameter value
3400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @param argName - the name of the parameter; used in the exception message
3500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @throws AssertFailedException - if arg is not null
3600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     */
3700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    public static void isNull(Object arg, String argName) {
3800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        if (arg != null) {
3900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            throw new AssertFailedException("The value for \"" + argName + "\" must be null");
4000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        }
4100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    }
4200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
4300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
4400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Verify that arg is not null. Throw an AssertFailedException if it is null.
4500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @param arg - the method parameter value
4600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @param argName - the name of the parameter; used in the exception message
4700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @throws AssertFailedException - if arg is null
4800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
4900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    public static void notNull(Object arg, String argName) {
5000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		if (arg == null) {
5100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            throw new AssertFailedException("The value of \"" + argName + "\" is null");
5200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		}
5300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
5400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
5500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
5600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Verify that condition is true. Throw an AssertFailedException if it is false.
5700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @param condition - the condition that should be true
5800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @throws AssertFailedException - if condition is false
5900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
6000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	public static void isTrue(boolean condition, String message) {
6100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		if (!condition) {
6200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair			throw new AssertFailedException(message);
6300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		}
6400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
6500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
6600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
6700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Verify that condition is false. Throw an AssertFailedException if it is true.
6800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @param condition - the condition that should be false
6900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @throws AssertFailedException - if condition is true
7000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
7100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	public static void isFalse(boolean condition, String message) {
7200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		if (condition) {
7300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair			throw new AssertFailedException(message);
7400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		}
7500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
7600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
7700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
7800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Verify that the collection is not null or empty. Throw an
7900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * AssertFailedException if it is null or empty.
8000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @param collection - the Collection
8100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @param argName - the name of the parameter; used in the exception message
8200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @throws AssertFailedException - if collection is null or empty
8300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
8400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    public static void notNullOrEmpty(Collection collection, String argName) {
8500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        notNull(collection, argName);
8600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		if (collection.isEmpty()) {
8700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            throw new AssertFailedException("The \"" + argName + "\" Collection is empty");
8800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		}
8900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
9000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
9100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
9200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Verify that the Map is not null or empty. Throw an AssertFailedException
9300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * if it is null or empty.
9400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @param map - the Map
9500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @param argName - the name of the parameter; used in the exception message
9600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @throws AssertFailedException - if map is null or empty
9700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
9800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    public static void notNullOrEmpty(Map map, String argName) {
9900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        notNull(map, argName);
10000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		if (map.isEmpty()) {
10100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            throw new AssertFailedException("The \"" + argName + "\" Map is empty");
10200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		}
10300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
10400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
10500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
10600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Verify that the array is not null or empty. Throw an
10700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * AssertFailedException if it is null or empty.
10800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @param array - the array
10900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @param argName - the name of the parameter; used in the exception message
11000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @throws AssertFailedException - if array is null or empty
11100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
11200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    public static void notNullOrEmpty(Object[] array, String argName) {
11300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        notNull(array, argName);
11400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		if (array.length == 0) {
11500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            throw new AssertFailedException("The \"" + argName + "\" array is empty");
11600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		}
11700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
11800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
11900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
12000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Verify that the String is not null or empty. Throw an
12100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * AssertFailedException if it is null or empty.
12200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @param string - the String
12300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair     * @param argName - the name of the parameter; used in the exception message
12400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * @throws AssertFailedException - if string is null or empty
12500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
12600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    public static void notNullOrEmpty(String string, String argName) {
12700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        notNull(string, argName);
12800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		if (string.trim().length() == 0) {
12900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            throw new AssertFailedException("The \"" + argName + "\" String is empty");
13000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair		}
13100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
13200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
13300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	/**
13400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 * Private constructor. All methods are static
13500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	 */
13600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	private Assert() {
13700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair	}
13800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair}
139