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