Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008, 2010 VMware Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: VMware Inc. - initial contribution /
 
 
 package org.eclipse.virgo.nano.serviceability;
 
 import java.util.Map;

A set of useful assertions based on those provided by the Spring Framework's Assert class. Concurrent Semantics
This class is thread safe.

See also:
org.springframework.util.Assert
 
 public final class Assert {

    
A FatalAssertionException is thrown when an assertion failure occurs and will result in a dump being generated. Concurrent Semantics
This class is thread-safe.
 
     final public static class FatalAssertionException extends RuntimeException {
 
         private static final long serialVersionUID = -4633344457818398425L;

        
Creates a new FatalAssertionException with the supplied message

Parameters:
message The exception message
 
         public FatalAssertionException(String message) {
             super(message);
         }
     }

    
Assert a boolean expression, throwing a FatalAssertionException if the test result is false.
 Assert.isTrue(i > 0, "The value must be greater than zero");
 

Parameters:
expression a boolean expression
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if expression is false
 
     public static void isTrue(boolean expressionString messageObject... inserts) {
         if (!expression) {
             throw new FatalAssertionException(String.format(messageinserts));
         }
     }

    
Assert a boolean expression, throwing a FatalAssertionException if the test result is true.
 Assert.isFalse(state.isBroken(), "The state is broken");
 

Parameters:
expression a boolean expression
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if expression is false
 
     public static void isFalse(boolean expressionString messageObject... inserts) {
         if (expression) {
             throw new FatalAssertionException(String.format(messageinserts));
         }
     }

    
Assert that an object is null.
 Assert.isNull(value, "The value must be null");
 

Parameters:
object the object to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if the object is not null
    public static void isNull(Object objectString messageObject... inserts) {
        if (object != null) {
            throw new FatalAssertionException(String.format(messageinserts));
        }
    }

    
Assert that an object is not null.
 Assert.notNull(clazz, "The class must not be null");
 

Parameters:
object the object to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if the object is null
    public static void notNull(Object objectString messageObject... inserts) {
        if (object == null) {
            throw new FatalAssertionException(String.format(messageinserts));
        }
    }

    
Assert that the given String is not empty; that is, it must not be null and not the empty String.
 Assert.hasLength(name, "Name must not be empty");
 

Parameters:
text the String to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
See also:
org.eclipse.virgo.util.common.StringUtils.hasLength(java.lang.String)
    public static void hasLength(String textString messageObject... inserts) {
        if (text == null || text.length() == 0) {
            throw new FatalAssertionException(String.format(messageinserts));
        }
    }

    
Assert that an array has elements; that is, it must not be null and must have at least one element.
 Assert.notEmpty(array, "The array must have elements");
 

Parameters:
array the array to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the object array is null or has no elements
    public static void notEmpty(Object[] arrayString messageObject... inserts) {
        if (array == null || array.length == 0) {
            throw new FatalAssertionException(String.format(messageinserts));
        }
    }

    
Assert that a collection has elements; that is, it must not be null and must have at least one element.
 Assert.notEmpty(collection, "Collection must have elements");
 

Parameters:
<T> Element type of collection
collection the collection to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if the collection is null or has no elements
    public static <T> void notEmpty(Collection<T> collectionString messageObject... inserts) {
        if (collection == null || collection.isEmpty()) {
            throw new FatalAssertionException(String.format(messageinserts));
        }
    }

    
Assert that a Map has entries; that is, it must not be null and must have at least one entry.
 Assert.notEmpty(map, "Map must have entries");
 

Parameters:
<K> Key type of map
<V> Value type of map
map the map to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if the map is null or has no entries
    public static <K, V> void notEmpty(Map<K, V> mapString messageObject... inserts) {
        if (map == null || map.isEmpty()) {
            throw new FatalAssertionException(String.format(messageinserts));
        }
    }

    
Assert that the provided object is a non-null instance of the provided class.
 Assert.instanceOf(Foo.class, foo);
 

Parameters:
<T> Type generic
type the type to check against
obj the object to check
message a message which will be prepended to the message produced by the function itself, and which may be used to provide context. It should normally end in a ": " or ". " so that the function generate message looks ok when prepended to it.
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if the object is not an instance of clazz
See also:
java.lang.Class.isInstance(java.lang.Object)
    public static <T> void isInstanceOf(Class<T> typeObject objString messageObject... inserts) {
        notNull(type"The type to check against must not be null");
        if (!type.isInstance(obj)) {
            throw new FatalAssertionException(String.format(messageinserts) + "Object of class [" + (obj != null ? obj.getClass().getName() : "null")
                + "] must be an instance of " + type);
        }
    }

    
Assert that superType.isAssignableFrom(subType) is true.
 Assert.isAssignable(Number.class, myClass);
 

Parameters:
<T> SuperType
<U> SubType
superType the super type to check against
subType the sub type to check
message a message which will be prepended to the message produced by the function itself, and which may be used to provide context. It should normally end in a ": " or ". " so that the function generate message looks ok when prepended to it.
inserts any inserts to include if the message is a format string.
Throws:
Assert.FatalAssertionException if the classes are not assignable
    public static <T, U> void isAssignable(Class<T> superTypeClass<U> subTypeString messageObject... inserts) {
        notNull(superType"Type to check against must not be null");
        if (subType == null || !superType.isAssignableFrom(subType)) {
            throw new FatalAssertionException(String.format(messageinserts) + subType + " is not assignable to " + superType);
        }
    }
    /*
     * Prevent instantiation - Java does not allow final abstract classes.
     */
    private Assert() {
    }
New to GrepCode? Check out our FAQ X