org.mozilla.util
Class Assert

java.lang.Object
  extended by org.mozilla.util.Assert

public final class Assert
extends java.lang.Object

The Assert class provides convenient means for condition testing. Class methods in the Assert class can be used to verify certain conditions and to raise exceptions if the conditions are not met. Assertions are particularly useful during the development of a project and may be turned off in production code. Turning off exceptions causes Assert not to raise exceptions when conditions are not met.

Typical usage:

Assert.assert_it(Assert.enabled && mytest(), "my test failed");

Such usage prevents mytest() from being executed if assertions are disabled. This techinique allows assertions to do time-consuming checks (such as myArray.containsObject(myObject)) without worrying about them impacting the performance of the final release.

If you know that the condition being tested is fast, you may omit the enabled flag:

Assert.assert_it(myValue != null);

Note that even in this second usage, if assertions are disabled, the assert_it() method will do nothing.

Another usage that is more efficient but bulkier:

if (Assert.enabled && Assert.assert_it(mytest(), "my test failed"));

Such usage prevents not only mytest() from being executed if assertions are disabled but also the assert_it method itself, and some compilers can remove the entire statement if assertions are disabled.

Assert is intended for general condition and invariant testing; for parameter testing, use ParameterCheck.

See Also:
ParameterCheck

Field Summary
static boolean enabled
          True if failed assertions should raise exceptions, or false if they should do nothing.
 
Method Summary
static boolean assert_it(boolean test)
          Throws AssertionFailureException if enabled is true and test is false; otherwise, does nothing.
static boolean assert_it(boolean test, java.lang.String message)
          Throws AssertionFailureException with a message of message if enabled is true and test is false; otherwise, does nothing.
static void setEnabled(boolean newEnabled)
          Sets enabled to newEnabled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

enabled

public static boolean enabled
True if failed assertions should raise exceptions, or false if they should do nothing.

Method Detail

setEnabled

public static void setEnabled(boolean newEnabled)
Sets enabled to newEnabled.

Parameters:
newEnabled - true if and only if assertions should be enabled

assert_it

public static boolean assert_it(boolean test,
                                java.lang.String message)
Throws AssertionFailureException with a message of message if enabled is true and test is false; otherwise, does nothing.

Parameters:
test - value to verify
message - message of exception thrown if test is false
Returns:
true
Throws:
AssertionFailureException - if test is false

assert_it

public static boolean assert_it(boolean test)
Throws AssertionFailureException if enabled is true and test is false; otherwise, does nothing.

Parameters:
test - value to verify
Returns:
true
Throws:
AssertionFailureException - if test is false


Copyright © 2002-2005 Mozilla.org All Rights Reserved.