193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair/*
293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Copyright 2007 the original author or authors.
393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Licensed under the Apache License, Version 2.0 (the "License");
593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * you may not use this file except in compliance with the License.
693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * You may obtain a copy of the License at
793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *      http://www.apache.org/licenses/LICENSE-2.0
993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
1093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Unless required by applicable law or agreed to in writing, software
1193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * See the License for the specific language governing permissions and
1493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * limitations under the License.
1593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair */
1693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairpackage org.mockftpserver.core.util;
1793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
1893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairimport java.util.Collection;
1993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairimport java.util.Map;
2093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
2193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair/**
2293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Provides static helper methods to make runtime assertions. Throws an
2393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * <code>AssertFailedException</code> when the assertion fails. All methods are static.
2493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
254ca3386623ce60063f27955ad1b2b1b6cbba8b09chrismair * @version $Revision$ - $Date$
2693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
2793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * @author Chris Mair
2893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair */
2993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairpublic final class Assert {
3093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
3193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
3293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * Verify that arg is null. Throw an AssertFailedException if it is not null.
3393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param arg - the method parameter value
3493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param argName - the name of the parameter; used in the exception message
3593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @throws AssertFailedException - if arg is not null
3693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
3793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public static void isNull(Object arg, String argName) {
3893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        if (arg != null) {
3993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair            throw new AssertFailedException("The value for \"" + argName + "\" must be null");
4093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        }
4193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
4293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
4393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
4493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Verify that arg is not null. Throw an AssertFailedException if it is null.
4593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @param arg - the method parameter value
4693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param argName - the name of the parameter; used in the exception message
4793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @throws AssertFailedException - if arg is null
4893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
4993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public static void notNull(Object arg, String argName) {
5093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		if (arg == null) {
5193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair            throw new AssertFailedException("The value of \"" + argName + "\" is null");
5293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		}
5393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
5493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
5593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
5693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Verify that condition is true. Throw an AssertFailedException if it is false.
5793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @param condition - the condition that should be true
5893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @throws AssertFailedException - if condition is false
5993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
6093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	public static void isTrue(boolean condition, String message) {
6193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		if (!condition) {
6293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair			throw new AssertFailedException(message);
6393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		}
6493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
6593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
6693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
6793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Verify that condition is false. Throw an AssertFailedException if it is true.
6893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @param condition - the condition that should be false
6993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @throws AssertFailedException - if condition is true
7093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
7193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	public static void isFalse(boolean condition, String message) {
7293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		if (condition) {
7393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair			throw new AssertFailedException(message);
7493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		}
7593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
7693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
7793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
7893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Verify that the collection is not null or empty. Throw an
7993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * AssertFailedException if it is null or empty.
8093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @param collection - the Collection
8193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param argName - the name of the parameter; used in the exception message
8293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @throws AssertFailedException - if collection is null or empty
8393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
8493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public static void notNullOrEmpty(Collection collection, String argName) {
8593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        notNull(collection, argName);
8693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		if (collection.isEmpty()) {
8793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair            throw new AssertFailedException("The \"" + argName + "\" Collection is empty");
8893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		}
8993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
9093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
9193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
9293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Verify that the Map is not null or empty. Throw an AssertFailedException
9393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * if it is null or empty.
9493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @param map - the Map
9593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param argName - the name of the parameter; used in the exception message
9693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @throws AssertFailedException - if map is null or empty
9793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
9893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public static void notNullOrEmpty(Map map, String argName) {
9993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        notNull(map, argName);
10093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		if (map.isEmpty()) {
10193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair            throw new AssertFailedException("The \"" + argName + "\" Map is empty");
10293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		}
10393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
10493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
10593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
10693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Verify that the array is not null or empty. Throw an
10793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * AssertFailedException if it is null or empty.
10893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @param array - the array
10993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param argName - the name of the parameter; used in the exception message
11093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @throws AssertFailedException - if array is null or empty
11193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
11293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public static void notNullOrEmpty(Object[] array, String argName) {
11393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        notNull(array, argName);
11493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		if (array.length == 0) {
11593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair            throw new AssertFailedException("The \"" + argName + "\" array is empty");
11693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		}
11793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
11893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
11993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
12093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Verify that the String is not null or empty. Throw an
12193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * AssertFailedException if it is null or empty.
12293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @param string - the String
12393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param argName - the name of the parameter; used in the exception message
12493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * @throws AssertFailedException - if string is null or empty
12593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
12693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public static void notNullOrEmpty(String string, String argName) {
12793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        notNull(string, argName);
12893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		if (string.trim().length() == 0) {
12993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair            throw new AssertFailedException("The \"" + argName + "\" String is empty");
13093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair		}
13193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
13293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
13393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	/**
13493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 * Private constructor. All methods are static
13593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	 */
13693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	private Assert() {
13793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair	}
13893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair}
139