Show JAssert.java syntax highlighted
// JAssert.java
// <http://redshed.sf.net/JAssert>
//
// Copyright (c) 2001-2001 Red Shed Software. All rights reserved.
// by Jonathan 'Wolf' Rentzsch (jon at redshed dot net)
//
// Wed Mar 28 2001 wolf: Created as Assert.
// Mon Dec 24 2001 wolf: Refactored. Explicit assertion testing method names,
// assertion messages now explain what they were looking
// for, added pluggable failure handlers.
// Wed Dec 26 2001 wolf: Added [is|not]EmptyString().
// Wed Dec 26 2001 wolf: Renamed to JAssert because one day it will go public...
// Thu Apr 25 2002 wolf: notEmptyString() now reports whether a null string or an
// empty string caused it to fire.
public class JAssert {
//----------------------------------
// Public client assertion interface
public static void isTrue( boolean value ) {
if( value == false ) fail( "expecting true" );
}
public static void isFalse( boolean value ) {
if( value == true ) fail( "expecting false" );
}
public static void isNull( Object value ) {
if( value != null ) fail( "expecting null object, got " + value );
}
public static void notNull( Object value ) {
if( value == null ) fail( "expecting non-null object" );
}
public static void isEmpty( String value ) {
if( value != null && !value.equals( "" ) ) fail( "expecting empty String, got " + value );
}
public static void notEmpty( String value ) {
if( value == null )
fail( "expecting non-empty String, got null" );
else if( value.equals( "" ) )
fail( "expecting non-empty String, got empty string" );
}
public static void isZero( int value ) {
if( value != 0 ) fail( "expecting zero int, got " + value );
}
public static void isZero( long value ) {
if( value != 0L ) fail( "expecting zero long, got " + value );
}
public static void isZero( double value ) {
if( value != 0.0 ) fail( "expecting zero double, got " + value );
}
public static void notZero( int value ) {
if( value == 0 ) fail( "expecting non-zero int, got " + value );
}
public static void notZero( long value ) {
if( value == 0L ) fail( "expecting non-zero long, got " + value );
}
public static void notZero( double value ) {
if( value == 0.0 ) fail( "expecting non-zero double, got " + value );
}
public static void isNegative( int value ) {
if( value >= 0 ) fail( "expecting negative int, got " + value );
}
public static void isNegative( long value ) {
if( value >= 0L ) fail( "expecting negative long, got " + value );
}
public static void isNegative( double value ) {
if( value >= 0.0 ) fail( "expecting negative double, got " + value );
}
public static void notNegative( int value ) {
if( value < 0 ) fail( "expecting non-negative int, got " + value );
}
public static void notNegative( long value ) {
if( value < 0L ) fail( "expecting non-negative long, got " + value );
}
public static void notNegative( double value ) {
if( value < 0.0 ) fail( "expecting non-negative double, got " + value );
}
public static void isPositive( int value ) {
if( value < 0 ) fail( "expecting positive int, got " + value );
}
public static void isPositive( long value ) {
if( value < 0L ) fail( "expecting positive long, got " + value );
}
public static void isPositive( double value ) {
if( value < 0.0 ) fail( "expecting positive double, got " + value );
}
public static void notPositive( int value ) {
if( value >= 0 ) fail( "expecting non-positive int, got " + value );
}
public static void notPositive( long value ) {
if( value >= 0L ) fail( "expecting non-positive long, got " + value );
}
public static void notPositive( double value ) {
if( value >= 0.0 ) fail( "expecting non-positive double, got " + value );
}
public static void isEqual( int value1, int value2 ) {
if( value1 != value2 ) fail( "expecting equal integers, got " + value1 + " & " + value2 );
}
public static void isEqual( long value1, long value2 ) {
if( value1 != value2 ) fail( "expecting equal longs, got " + value1 + " & " + value2 );
}
public static void isEqual( double value1, double value2 ) {
if( value1 != value2 ) fail( "expecting equal doubles, got " + value1 + " & " + value2 );
}
public static void isEqual( Object value1, Object value2 ) {
if( !value1.equals( value2 ) ) fail( "expecting equal objects, got " + value1 + " & " + value2 );
}
public static void notEqual( int value1, int value2 ) {
if( value1 == value2 ) fail( "expecting unequal integers, got " + value1 + " & " + value2 );
}
public static void notEqual( long value1, long value2 ) {
if( value1 == value2 ) fail( "expecting unequal longs, got " + value1 + " & " + value2 );
}
public static void notEqual( double value1, double value2 ) {
if( value1 == value2 ) fail( "expecting unequal doubles, got " + value1 + " & " + value2 );
}
public static void notEqual( Object value1, Object value2 ) {
if( value1.equals( value2 ) ) fail( "expecting unequal objects, got " + value1 + " & " + value2 );
}
public static void lessThan( int value1, int value2 ) {
if( value1 >= value2 ) fail( "expecting " + value1 + " to be less than " + value2 );
}
public static void lessThan( long value1, long value2 ) {
if( value1 >= value2 ) fail( "expecting " + value1 + " to be less than " + value2 );
}
public static void lessThan( double value1, double value2 ) {
if( value1 >= value2 ) fail( "expecting " + value1 + " to be less than " + value2 );
}
public static void lessThanOrEqual( int value1, int value2 ) {
if( value1 > value2 ) fail( "expecting " + value1 + " to be less than or equal to " + value2 );
}
public static void lessThanOrEqual( long value1, long value2 ) {
if( value1 > value2 ) fail( "expecting " + value1 + " to be less than or equal to " + value2 );
}
public static void lessThanOrEqual( double value1, double value2 ) {
if( value1 > value2 ) fail( "expecting " + value1 + " to be less than or equal to " + value2 );
}
public static void greaterThan( int value1, int value2 ) {
if( value1 <= value2 ) fail( "expecting " + value1 + " to be greater than " + value2 );
}
public static void greaterThan( long value1, long value2 ) {
if( value1 <= value2 ) fail( "expecting " + value1 + " to be greater than " + value2 );
}
public static void greaterThan( double value1, double value2 ) {
if( value1 <= value2 ) fail( "expecting " + value1 + " to be greater than " + value2 );
}
public static void greaterThanOrEqual( int value1, int value2 ) {
if( value1 < value2 ) fail( "expecting " + value1 + " to be greater than or equal to " + value2 );
}
public static void greaterThanOrEqual( long value1, long value2 ) {
if( value1 < value2 ) fail( "expecting " + value1 + " to be greater than or equal to " + value2 );
}
public static void greaterThanOrEqual( double value1, double value2 ) {
if( value1 < value2 ) fail( "expecting " + value1 + " to be greater than or equal to " + value2 );
}
public static void unknownSwitchCase( int value ) {
fail( "unknown switch case: " + value );
}
//-------------------------------------------
// Public assertion failure handler interface
public interface FailureHandler {
public void handleFailure( String message );
}
public static FailureHandler getFailureHandler() {
return _failureHandler;
}
public static void setFailureHandler( FailureHandler handler ) {
_failureHandler = handler;
}
//-----------------------
// Private implementation
private static FailureHandler _failureHandler;
private static void fail( String message ) {
if( _failureHandler == null ) {
String output = "assertion failed: ";
if( message != null )
output += message;
System.err.println( output );
Throwable e = new Throwable();
e.printStackTrace();
} else {
_failureHandler.handleFailure( message );
}
}
}
See more files for this project here