You are currently viewing a snapshot of www.mozilla.org taken on April 21, 2008. Most of this content is highly out of date (some pages haven't been updated since the project began in 1998) and exists for historical purposes only. If there are any pages on this archive site that you think should be added back to www.mozilla.org, please file a bug.




org.mozilla.util
Class Assert

java.lang.Object
  |
  +--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