Assertion

Another variation of debugging is the assertion technique. This is done by inserting assertion statements in the application's code. An assertion statement is a logical check in the source code that determines the condition of the application at certain checkpoints in the application. The checking done by an assertion statement is normally a check of certain important conditions at different stages in the application that evaluates to a Boolean value. The assertion statement uses this Boolean value to determine a success or failure. When encountering a failure, the assertion statement can throw an exception that may even result in the application terminating its execution flow.

Note

Assertion statements can be viewed as active debugging because the developer predefines halting or redirecting the execution based on certain conditions. The other debugging techniques are essentially passive debugging because the developer has not predefined any action to be taken.


Assertions are a feature of C/C++ languages and prove to be an extremely useful technique for debugging applications. In C/C++ languages, assertion statements can be easily built and switched on/off by the preprocessor with a compile-time flag. Because Java does not have a preprocessor, such compile-time activities cannot be easily performed. Consequently, preprocessing can be built a couple of ways: through enabling or disabling debug or assertion statements by checking the value of a static variable of an application class that acts as a flag. The caveat is that in Java applications, the debug or assertion statements stay in the .class file, whereas in C/C++, you simply change the compiler directive and recompile the application, which then strips the debug statements out of the executable file.

Take a look at the following example of using assertion statements:

 class MyAssertHelper  {     public static void assert(boolean checkPointFlag, String methodName)     {         if(!checkPointFlag)         {             System.out.println("Assertion failed at " + methodName "!");             System.exit(1);         }     } } class MyApp {     public static void myMethod()     {         ...         // perform application processing         // place assert statement         MyAssertHelper.assert((myInputData == null), "MyApp.myMethod()");         ...     } } 

The expression that checks for the null condition of the myInputData variable evaluates either to a Boolean true or false value. This evaluation result and the method name where the assert statement is called are passed as parameters to the static assert() method. The assert() method checks the evaluation result of the expression used as a checkpoint. If it evaulates to a false value, the assert() method displays the assertion failed message on the standard output and exits the application.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net