Download archive here |
Version 1.0
Stand: 03.05.2000
Status: Draft
The package magie.assert and all its sub-packages and extensions is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the
Free Software Foundation, Inc.,
59 Temple Place,
Suite 330, Boston,
MA 02111-1307
USA
Or visit http://www.gnu.org/copyleft/lesser.html.
Usually the functional test of a client application (with GUI elements) has to be done in a manual, ad-hoc way or with a GUI Test tool (such as WinRunner, JavaStar, SQASuite etc.). As these tools require the programming/recording of tests to perform automatic tests with a high effort, the usage of such a tool was not considered in the project. In many cases during development programmers use very simple mechanisms, like
System.out.println("Method doSomething() entered."); ... System.out.println("Print something: "+myObject.toString()); ... System.out.println("Method doSomething() exited.");
This kind of information is appropriate for controls flows, it is in many cases not very helpful for the prove of internal states.
For this reason the Assert Manager was introduced. It simply provides a utility class to perform some assertions, as the JUnit framework does. These assertions can also be implemented in client transactions and is aimed to recognize the cause of a problem rather than its results. For example, a statement
assertNotNull("Empty result at <xy>", result);
indicates the reason for an error, which could result in a NullPointerException on any other location of the system transaction (in the worst case in another process).
To check system states and object attributes you simply have to write assert statements on any location in your code, where you want to ensure a defined state. Therefore one of the following methods can be used:
Assert method | Description |
---|---|
assert(boolean condition) assert(String message, boolean condition) |
Asserts that a condition is true. If it isn't, an Assert is instantiated and passed to the assert handler in order to handle it. |
assertNotNull(Object was) assertNotNull(String message, Object was) |
Asserts that the argument 'was' is not null. If it isn't, an Assert is instantiated and passed to the assert handler in order to handle it. |
assertEquals(Object expected, Object was) assertEquals(String message, Object expected, Object was) assertEquals(String message, String expected, String was) assertEquals(double expected, double was, double delta) assertEquals(String message, double expected, double was, double delta) ... |
Asserts that the arguments 'expected' and 'was' are equal. If they are not, an Assert is instantiated and passed to the assert handler in order to handle it. |
assertNull(Object was) assertNull(String message, Object was) |
Asserts that the argument 'was' is null. If it isn't, an Assert is instantiated and passed to the assert handler in order to handle it. |
assertSame(Object expected, Object was) assertSame(String message, Object expected, Object was) |
Asserts that the arguments 'expected' and 'was' are the same. If they are not, an Assert is instantiated and passed to the assert handler in order to handle it. |
For simple performance measures some simple utility methods are implemented in the class Assert. The example illustrates their use:
// switch to FileAssertHandler Assert.file(); // start timer Assert.startTimer("Some Action"); // now perform actiion, which shall be measured doSomething(); // stop timer Assert.stopTimer("Some Action"); // finally output result to handler Assert.assertTimer("Additional Message");
As result, the following entry is written to the Assert file:
--------------------------------------------------------------------- 03.05.00 - 10:42 Time Profile: Additional Message Some Action=4,837 sec. ---------------------------------------------------------------------
To switch behavior of the Assert Manager, the behavior is defined in an interface IAssertManager, which can be implemented by anonymous inner classes or special Assert Handler classes, e.g. the DialogAssertHandler or the FileAssertHandler. The screenshot shows the Dialog Handler in action:
For the deployment a special "Silent Assert Handler" exists, which is not reporting any of the found assert violations.
The table shows the available handlers, a description of their behavior and the static method in the class Assert, which can be used to activate them:
Assert Handler |
Description |
Assert method |
---|---|---|
Trace to Java Console |
Behaves like a simple System.out.println(): traces assert failures to the Java console |
console() |
Protocol to file |
Writes assert failures (including method stack) into a file Assert-yymmdd.txt |
file() |
Assertion Dialog |
Pop up a dialog to indicate text and stack of the assert failure. |
dialog() |
Silent Assert Handler |
Simply ignores the assertion. |
silent() |
Throwing an Exception |
Throws an RuntimeException, which could be caught by your application. |
exception() |
[R1] GNU Lesser General Public License
[R2] JUnit Documentation http://www.armaties.com/extreme.htm
[R3] Download Sources here.