Package: Assert

Assert

nameinstructionbranchcomplexitylinemethod
hasLength(String, String, Object[])
M: 0 C: 13
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isAssignable(Class, Class, String, Object[])
M: 0 C: 30
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isFalse(boolean, String, Object[])
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isInstanceOf(Class, Object, String, Object[])
M: 0 C: 36
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
isNull(Object, String, Object[])
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isTrue(boolean, String, Object[])
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
notEmpty(Collection, String, Object[])
M: 0 C: 13
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
notEmpty(Map, String, Object[])
M: 0 C: 13
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
notEmpty(Object[], String, Object[])
M: 0 C: 13
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
notNull(Object, String, Object[])
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.util.common;
13:
14: import java.util.Collection;
15: import java.util.Map;
16:
17: /**
18: * A set of useful assertions based on those provided by the Spring Framework's Assert class.
19: *
20: * <strong>Concurrent Semantics</strong><br />
21: *
22: * This class is thread safe.
23: *
24: * @see org.junit.Assert
25: */
26: public final class Assert {
27:
28: /**
29: * Assert a boolean expression, throwing a <code>IllegalArgumentException</code> if the test result is
30: * <code>false</code>.
31: *
32: * <pre class="code">
33: * Assert.isTrue(i > 0, "The value must be greater than zero");
34: * </pre>
35: *
36: * @param expression a boolean expression
37: * @param message the exception message to use if the assertion fails
38: * @param inserts any inserts to include if the message is a format string.
39: * @throws IllegalArgumentException if expression is <code>false</code>
40: */
41: public static void isTrue(boolean expression, String message, Object... inserts) {
42:•         if (!expression) {
43:                 throw new IllegalArgumentException(String.format(message, inserts));
44:         }
45: }
46:
47: /**
48: * Assert a boolean expression, throwing a <code>IllegalArgumentException</code> if the test result is
49: * <code>true</code>.
50: *
51: * <pre class="code">
52: * Assert.isFalse(state.isBroken(), "The state is broken");
53: * </pre>
54: *
55: * @param expression a boolean expression
56: * @param message the exception message to use if the assertion fails
57: * @param inserts any inserts to include if the message is a format string.
58: * @throws IllegalArgumentException if expression is <code>false</code>
59: */
60: public static void isFalse(boolean expression, String message, Object... inserts) {
61:• if (expression) {
62:         throw new IllegalArgumentException(String.format(message, inserts));
63: }
64: }
65:
66: /**
67: * Assert that an object is <code>null</code>.
68: *
69: * <pre class="code">
70: * Assert.isNull(value, "The value must be null");
71: * </pre>
72: *
73: * @param object the object to check
74: * @param message the exception message to use if the assertion fails
75: * @param inserts any inserts to include if the message is a format string.
76: * @throws IllegalArgumentException if the object is not <code>null</code>
77: */
78: public static void isNull(Object object, String message, Object... inserts) {
79:•         if (object != null) {
80:                 throw new IllegalArgumentException(String.format(message, inserts));
81:         }
82: }
83:
84: /**
85: * Assert that an object is not <code>null</code>.
86: *
87: * <pre class="code">
88: * Assert.notNull(clazz, "The class must not be null");
89: * </pre>
90: *
91: * @param object the object to check
92: * @param message the exception message to use if the assertion fails
93: * @param inserts any inserts to include if the message is a format string.
94: * @throws IllegalArgumentException if the object is <code>null</code>
95: */
96: public static void notNull(Object object, String message, Object... inserts) {
97:• if (object == null) {
98:         throw new IllegalArgumentException(String.format(message, inserts));
99: }
100: }
101:
102: /**
103: * Assert that the given String is not empty; that is, it must not be <code>null</code> and not the empty String.
104: *
105: * <pre class="code">
106: * Assert.hasLength(name, "Name must not be empty");
107: * </pre>
108: *
109: * @param text the String to check
110: * @param message the exception message to use if the assertion fails
111: * @param inserts any inserts to include if the message is a format string.
112: * @see StringUtils#hasLength
113: */
114: public static void hasLength(String text, String message, Object... inserts) {
115:• if (text == null || text.length() == 0) {
116:         throw new IllegalArgumentException(String.format(message, inserts));
117: }
118: }
119:
120: /**
121: * Assert that an array has elements; that is, it must not be <code>null</code> and must have at least one
122: * element.
123: *
124: * <pre class="code">
125: * Assert.notEmpty(array, "The array must have elements");
126: * </pre>
127: *
128: * @param array the array to check
129: * @param message the exception message to use if the assertion fails
130: * @param inserts any inserts to include if the message is a format string.
131: * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
132: */
133: public static void notEmpty(Object[] array, String message, Object... inserts) {
134:• if (array == null || array.length == 0) {
135: throw new IllegalArgumentException(String.format(message, inserts));
136: }
137: }
138:
139: /**
140: * Assert that a collection has elements; that is, it must not be <code>null</code> and must have at least one
141: * element.
142: *
143: * <pre class="code">
144: * Assert.notEmpty(collection, "Collection must have elements");
145: * </pre>
146: * @param <T> the type of the elements of the collection
147: *
148: * @param collection the collection to check
149: * @param message the exception message to use if the assertion fails
150: * @param inserts any inserts to include if the message is a format string.
151: * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
152: */
153: public static <T> void notEmpty(Collection<T> collection, String message, Object... inserts) {
154:•         if (collection == null || collection.isEmpty()) {
155: throw new IllegalArgumentException(String.format(message, inserts));
156: }
157: }
158:
159: /**
160: * Assert that a Map has entries; that is, it must not be <code>null</code> and must have at least one entry.
161: *
162: * <pre class="code">
163: * Assert.notEmpty(map, "Map must have entries");
164: * </pre>
165: * @param <K> type of Key (domain)
166: * @param <V> type of Value (range)
167: *
168: * @param map the map to check
169: * @param message the exception message to use if the assertion fails
170: * @param inserts any inserts to include if the message is a format string.
171: * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
172: */
173: public static <K, V> void notEmpty(Map<K, V> map, String message, Object... inserts) {
174:• if (map == null || map.isEmpty()) {
175: throw new IllegalArgumentException(String.format(message, inserts));
176: }
177: }
178:
179: /**
180: * Assert that the provided object is a non-null instance of the provided class.
181: *
182: * <pre class="code">
183: * Assert.instanceOf(Foo.class, foo);
184: * </pre>
185: * @param <T> the type to check this for
186: *
187: * @param type the type to check against
188: * @param obj the object to check
189: * @param message a message which will be prepended to the message produced by the function itself, and which may be
190: * used to provide context. It should normally end in a ": " or ". " so that the function generate message
191: * looks ok when appended to it.
192: * @param inserts any inserts to include if the message is a format string.
193: * @throws IllegalArgumentException if the object is not an instance of clazz
194: * @see Class#isInstance
195: */
196: public static <T> void isInstanceOf(Class<T> type, Object obj, String message, Object... inserts) {
197:         notNull(type, "The type to check against must not be null");
198:•         if (!type.isInstance(obj)) {
199:• throw new IllegalArgumentException(String.format(message, inserts) + "Object of class [" + (obj != null ? obj.getClass().getName() : "null")
200: + "] must be an instance of " + type);
201: }
202: }
203:
204: /**
205: * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>.
206: *
207: * <pre class="code">
208: * Assert.isAssignable(Number.class, myClass);
209: * </pre>
210: * @param <T> the supertype
211: * @param <U> the subtype
212: *
213: * @param superType the super type to check against
214: * @param subType the sub type to check
215: * @param message a message which will be prepended to the message produced by the function itself, and which may be
216: * used to provide context. It should normally end in a ": " or ". " so that the function generate message
217: * looks ok when appended to it.
218: * @param inserts any inserts to include if the message is a format string.
219: * @throws IllegalArgumentException if the classes are not assignable
220: */
221: public static <T, U> void isAssignable(Class<T> superType, Class<U> subType, String message, Object... inserts) {
222:         notNull(superType, "Type to check against must not be null");
223:•         if (subType == null || !superType.isAssignableFrom(subType)) {
224:                 throw new IllegalArgumentException(String.format(message, inserts) + subType + " is not assignable to " + superType);
225: }
226: }
227:
228: /*
229: * Prevent instantiation - Java does not allow final abstract classes.
230: */
231: private Assert() {
232: }
233: }