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