com.tc.util
Class Assert

java.lang.Object
  extended by com.tc.util.Assert

public class Assert
extends java.lang.Object

A general purpose assertion utility. By default it is on, but you can disable the throwing of exceptions by giving the system property "tcassert" a value of 'false'.


Constructor Summary
Assert()
           
 
Method Summary
static void assertConsistentCollection(java.util.Collection collection, java.lang.Class elementClass, boolean allowNullElements)
          Assert that all items in collection are of type elementClass, also if !allowNullElements, check that all items in the collection are non-null.
static void assertContainsElement(java.lang.Object[] objectArray, java.lang.Object requiredElement)
          Tests for equality using the == operator, not Object.equals(Object).
static void assertDoesNotContainsElement(java.lang.Object[] objectArray, java.lang.Object element)
           
static void assertEquals(boolean expected, boolean actual)
          Assert expected and actual values are equal
static void assertEquals(byte[] expected, byte[] actual)
          Assert expected and actual values are equal or both null
static void assertEquals(double expected, double actual)
          Assert expected and actual values are equal
static void assertEquals(double expected, double actual, double epsilon)
          Assert expected and actual values are equal within epsilon
static void assertEquals(int expected, int actual)
          Assert expected and actual values are equal
static void assertEquals(long expected, long actual)
          Assert expected and actual values are equal
static void assertEquals(java.lang.Object msg, int expected, int actual)
          Assert expected and actual values are equal and return what as a message
static void assertEquals(java.lang.Object expected, java.lang.Object actual)
          Assert expected and actual values are equal or both null
static void assertEquals(java.lang.Object msg, java.lang.Object expected, java.lang.Object actual)
           
static void assertFalse(boolean expr)
          Evaluate the boolean exception and throw an assertion error if true
static void assertFalse(java.lang.Object message, boolean expr)
          Evaluate the boolean exception and throw an assertion error if true
static void assertNoBlankElements(java.lang.String[] array)
          Validate that the given array of strings contains no nulls or empty strings
static void assertNoNullElements(java.lang.Object[] array)
          Validate that the given (1 dimensional) array of references contains no nulls
static void assertNotBlank(java.lang.Object what, java.lang.String s)
          Validate that s is not blank and throw what as a message
static void assertNotBlank(java.lang.String s)
          Validate that s is not blank
static void assertNotEmpty(java.lang.Object what, java.lang.String s)
          Validate that s is not null or empty and throw what as a message
static void assertNotEmpty(java.lang.String s)
          Validate that s is not null or empty
static void assertNotNull(java.lang.Object o)
          If o is null, throw assertion error
static void assertNotNull(java.lang.Object what, java.lang.Object o)
          If o is null, throw assertion error with message what
static void assertNull(java.lang.Object o)
          If o is non-null, throw assertion error
static void assertNull(java.lang.Object what, java.lang.Object o)
          If o is non-null, throw assertion error
static void assertSame(java.lang.Object lhs, java.lang.Object rhs)
          Validate that lhs and rhs are identical object references or both are null
static void assertTrue(boolean expr)
          Evaluate the boolean exception and throw an assertion error if false
static void assertTrue(java.lang.Object message, boolean expr)
          Evaluate the boolean exception and throw an assertion error if false
static void eval(boolean expr)
          Evaluate the boolean exception and throw an assertion error if false
static void eval(java.lang.Object message, boolean expr)
          Evaluate the boolean exception and throw an assertion error if false
static void fail()
          Throw assertion error with generic message
static void fail(java.lang.String message)
          Throw assertion error with specified message
static TCAssertionError failure(java.lang.Object message)
          This returns an exception, instead of throwing one, so that you can do (e.g.): public Object foo() { throw Assert.failure("doesn't work"); } or whatever.
static TCAssertionError failure(java.lang.Object message, java.lang.Throwable t)
          This returns an exception, instead of throwing one, so that you can do (e.g.): public Object foo() { throw Assert.failure("doesn't work"); } or whatever.
static void inv(boolean v)
          Assert invariant
static void post(boolean v)
          Assert postcondition
static void pre(boolean v)
          Assert precondition
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Assert

public Assert()
Method Detail

failure

public static TCAssertionError failure(java.lang.Object message,
                                       java.lang.Throwable t)
This returns an exception, instead of throwing one, so that you can do (e.g.): public Object foo() { throw Assert.failure("doesn't work"); } or whatever. If this just threw the exception itself, the compiler would complain (above) that there's no value being returned.

Parameters:
message - The message to put in the assertion error
t - The exception to wrap
Returns:
New TCAssertionError, ready to throw

failure

public static TCAssertionError failure(java.lang.Object message)
This returns an exception, instead of throwing one, so that you can do (e.g.): public Object foo() { throw Assert.failure("doesn't work"); } or whatever. If this just threw the exception itself, the compiler would complain (above) that there's no value being returned.

Parameters:
message - The message to put in the assertion error
Returns:
New TCAssertionError, ready to throw

eval

public static void eval(boolean expr)
Evaluate the boolean exception and throw an assertion error if false

Parameters:
expr - Expression

eval

public static void eval(java.lang.Object message,
                        boolean expr)
Evaluate the boolean exception and throw an assertion error if false

Parameters:
expr - Expression
message - Message for assertion error if false

assertTrue

public static void assertTrue(boolean expr)
Evaluate the boolean exception and throw an assertion error if false

Parameters:
expr - Expression

assertTrue

public static void assertTrue(java.lang.Object message,
                              boolean expr)
Evaluate the boolean exception and throw an assertion error if false

Parameters:
expr - Expression
message - Message for assertion error if false

assertFalse

public static void assertFalse(boolean expr)
Evaluate the boolean exception and throw an assertion error if true

Parameters:
expr - Expression
message - Message for assertion error if true

assertFalse

public static void assertFalse(java.lang.Object message,
                               boolean expr)
Evaluate the boolean exception and throw an assertion error if true

Parameters:
expr - Expression
message - Message for assertion error if true

assertNull

public static void assertNull(java.lang.Object o)
If o is non-null, throw assertion error

Parameters:
o - Object

assertNull

public static void assertNull(java.lang.Object what,
                              java.lang.Object o)
If o is non-null, throw assertion error

Parameters:
o - Object
what - Message for error

assertNotNull

public static void assertNotNull(java.lang.Object what,
                                 java.lang.Object o)
If o is null, throw assertion error with message what

Parameters:
o - Object
what - Message for error

assertNotNull

public static void assertNotNull(java.lang.Object o)
If o is null, throw assertion error

Parameters:
o - Object

assertNoNullElements

public static void assertNoNullElements(java.lang.Object[] array)
Validate that the given (1 dimensional) array of references contains no nulls

Parameters:
array - Array

assertNoBlankElements

public static void assertNoBlankElements(java.lang.String[] array)
Validate that the given array of strings contains no nulls or empty strings

Parameters:
array - Array of strings

assertNotEmpty

public static void assertNotEmpty(java.lang.Object what,
                                  java.lang.String s)
Validate that s is not null or empty and throw what as a message

Parameters:
s - String
what - Message

assertNotEmpty

public static void assertNotEmpty(java.lang.String s)
Validate that s is not null or empty

Parameters:
s - String

assertNotBlank

public static void assertNotBlank(java.lang.Object what,
                                  java.lang.String s)
Validate that s is not blank and throw what as a message

Parameters:
s - String
what - Message

assertNotBlank

public static void assertNotBlank(java.lang.String s)
Validate that s is not blank

Parameters:
s - String

assertSame

public static void assertSame(java.lang.Object lhs,
                              java.lang.Object rhs)
Validate that lhs and rhs are identical object references or both are null

Parameters:
lhs - Left hand side
rhs - Right hand side

assertEquals

public static void assertEquals(int expected,
                                int actual)
Assert expected and actual values are equal

Parameters:
expected - Expected value
actual - Actual value

assertEquals

public static void assertEquals(long expected,
                                long actual)
Assert expected and actual values are equal

Parameters:
expected - Expected value
actual - Actual value

assertEquals

public static void assertEquals(java.lang.Object msg,
                                int expected,
                                int actual)
Assert expected and actual values are equal and return what as a message

Parameters:
expected - Expected value
actual - Actual value
msg - Message, should be non-null

assertEquals

public static void assertEquals(double expected,
                                double actual)
Assert expected and actual values are equal

Parameters:
expected - Expected value
actual - Actual value

assertEquals

public static void assertEquals(double expected,
                                double actual,
                                double epsilon)
Assert expected and actual values are equal within epsilon

Parameters:
expected - Expected value
actual - Actual value
epsilon - Maximum allowed difference between expected and actual

assertEquals

public static void assertEquals(boolean expected,
                                boolean actual)
Assert expected and actual values are equal

Parameters:
expected - Expected value
actual - Actual value

assertEquals

public static void assertEquals(byte[] expected,
                                byte[] actual)
Assert expected and actual values are equal or both null

Parameters:
expected - Expected value
actual - Actual value

assertEquals

public static void assertEquals(java.lang.Object expected,
                                java.lang.Object actual)
Assert expected and actual values are equal or both null

Parameters:
expected - Expected value
actual - Actual value

assertEquals

public static void assertEquals(java.lang.Object msg,
                                java.lang.Object expected,
                                java.lang.Object actual)

assertConsistentCollection

public static void assertConsistentCollection(java.util.Collection collection,
                                              java.lang.Class elementClass,
                                              boolean allowNullElements)
Assert that all items in collection are of type elementClass, also if !allowNullElements, check that all items in the collection are non-null.

Parameters:
collection - The collection
elementClass - The expected super type of all items in collection
allowNullElements - Flag for whether null elements are allowed or not

assertContainsElement

public static void assertContainsElement(java.lang.Object[] objectArray,
                                         java.lang.Object requiredElement)
Tests for equality using the == operator, not Object.equals(Object). null is a valid element.

Parameters:
objectArray - Array of objects
requiredElement - Must be in objectArray

assertDoesNotContainsElement

public static void assertDoesNotContainsElement(java.lang.Object[] objectArray,
                                                java.lang.Object element)

fail

public static void fail()
Throw assertion error with generic message


fail

public static void fail(java.lang.String message)
Throw assertion error with specified message

Parameters:
message - Message

pre

public static void pre(boolean v)
Assert precondition

Parameters:
v - Precondition

post

public static void post(boolean v)
Assert postcondition

Parameters:
v - Postcondition

inv

public static void inv(boolean v)
Assert invariant

Parameters:
v - Invariant


Copyright © 2010 Terracotta, Inc.. All Rights Reserved.