The Mozilla
Organization
Our Mission
Who We Are
Getting Involved
Community
Editorials
What's New
Newsbot
Development
Roadmap
Module Owners
Blue Sky
Projects
Status
Tools
Products
Source Code
Binaries
Documentation
License Terms
Bug Reports
Quality
Search
Feedback
: Class Assert

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(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(myValue != null);

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

Another usage that is more efficient but bulkier:

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

Such usage prevents not only mytest() from being executed if assertions are disabled but also the assert 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(boolean test)
          Throws AssertionFailureException if enabled is true and test is false; otherwise, does nothing.
static boolean assert(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

public static boolean assert(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

public static boolean assert(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 © 1998-1999 The Mozilla Organization.