Update ooo320-m1
[ooovba.git] / qadevOOo / runner / complexlib / Assurance.java
blobaf2dc802793ea37528f3364939b83e2b3dc5fa52
1 /*
2 * ************************************************************************
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * Copyright 2008 by Sun Microsystems, Inc.
7 *
8 * OpenOffice.org - a multi-platform office productivity suite
9 *
10 * $RCSfile: Assurance.java,v $
11 * $Revision: 1.1.2.1 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 * ***********************************************************************
33 package complexlib;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
38 /**
40 * @author ll93751
42 * I have removed the assure(...) functions from ComplexTestCase due to the fact now I can
43 * use the functions every where and don't need to be a ComplexTestCase any longer.
45 public class Assurance
47 public static final boolean CONTINUE = true;
49 /** State of the current test method **/
50 protected boolean state = true;
52 /** The message if the test did fail **/
53 protected String message = null;
56 /**
57 * Assure that s is true.
58 * This function generates "Assure failed." as standard message.
59 * @param s The condition that should be true.
61 protected void assure(boolean s) {
62 assure("Assure failed.", s, false);
65 /**
66 * Assure that s is true.
67 * The given message will be only evaluated, if s is false.
68 * @param msg The message that is evaluated.
69 * @param s The condition that should be true.
71 protected void assure(String msg, boolean s) {
72 assure(msg, s, false);
75 /**
76 * Assure that two boolean values are equal
77 * @param expected specifies the expected boolean value
78 * @param actual specifies the actual boolean value
80 protected void assureEquals( boolean expected, boolean actual ) {
81 assureEquals( "Equality test failed", new Boolean( expected ), new Boolean( actual ), false );
84 /**
85 * Assure that two boolean values are equal
86 * @param message the message to print when the equality test fails
87 * @param expected specifies the expected boolean value
88 * @param actual specifies the actual boolean value
90 protected void assureEquals( String message, boolean expected, boolean actual ) {
91 assureEquals( message, new Boolean( expected ), new Boolean( actual ), false );
94 /**
95 * Assure that two byte values are equal
96 * @param expected specifies the expected byte value
97 * @param actual specifies the actual byte value
99 protected void assureEquals( byte expected, byte actual ) {
100 assureEquals( "Equality test failed", new Byte( expected ), new Byte( actual ), false );
104 * Assure that two byte values are equal
105 * @param message the message to print when the equality test fails
106 * @param expected specifies the expected byte value
107 * @param actual specifies the actual byte value
109 protected void assureEquals( String message, byte expected, byte actual ) {
110 assureEquals( message, new Byte( expected ), new Byte( actual ), false );
114 * Assure that two double values are equal
115 * @param expected specifies the expected double value
116 * @param actual specifies the actual double value
118 protected void assureEquals( double expected, double actual ) {
119 assureEquals( "Equality test failed", new Double( expected ), new Double( actual ), false );
123 * Assure that two double values are equal
124 * @param message the message to print when the equality test fails
125 * @param expected specifies the expected double value
126 * @param actual specifies the actual double value
128 protected void assureEquals( String message, double expected, double actual ) {
129 assureEquals( message, new Double( expected ), new Double( actual ), false );
133 * Assure that two float values are equal
134 * @param expected specifies the expected float value
135 * @param actual specifies the actual float value
137 protected void assureEquals( float expected, float actual ) {
138 assureEquals( "Equality test failed", new Float( expected ), new Float( actual ), false );
142 * Assure that two float values are equal
143 * @param message the message to print when the equality test fails
144 * @param expected specifies the expected float value
145 * @param actual specifies the actual float value
147 protected void assureEquals( String message, float expected, float actual ) {
148 assureEquals( message, new Float( expected ), new Float( actual ), false );
152 * Assure that two short values are equal
153 * @param expected specifies the expected short value
154 * @param actual specifies the actual short value
156 protected void assureEquals( short expected, short actual ) {
157 assureEquals( "Equality test failed", new Short( expected ), new Short( actual ), false );
161 * Assure that two short values are equal
162 * @param message the message to print when the equality test fails
163 * @param expected specifies the expected short value
164 * @param actual specifies the actual short value
166 protected void assureEquals( String message, short expected, short actual ) {
167 assureEquals( message, new Short( expected ), new Short( actual ), false );
171 * Assure that two int values are equal
172 * @param expected specifies the expected int value
173 * @param actual specifies the actual int value
175 protected void assureEquals( int expected, int actual ) {
176 assureEquals( "Equality test failed", new Integer( expected ), new Integer( actual ), false );
180 * Assure that two int values are equal
181 * @param message the message to print when the equality test fails
182 * @param expected specifies the expected int value
183 * @param actual specifies the actual int value
185 protected void assureEquals( String message, int expected, int actual ) {
186 assureEquals( message, new Integer( expected ), new Integer( actual ), false );
190 * Assure that two long values are equal
191 * @param expected specifies the expected long value
192 * @param actual specifies the actual long value
194 protected void assureEquals( long expected, long actual ) {
195 assureEquals( "Equality test failed", new Long( expected ), new Long( actual ), false );
199 * Assure that two long values are equal
200 * @param message the message to print when the equality test fails
201 * @param expected specifies the expected long value
202 * @param actual specifies the actual long value
204 protected void assureEquals( String message, long expected, long actual ) {
205 assureEquals( message, new Long( expected ), new Long( actual ), false );
209 * Assure that two string values are equal
210 * @param expected specifies the expected string value
211 * @param actual specifies the actual string value
213 protected void assureEquals( String expected, String actual ) {
214 assureEquals( "Equality test failed", expected, actual, false );
218 * Assure that two string values are equal
219 * @param message the message to print when the equality test fails
220 * @param expected specifies the expected string value
221 * @param actual specifies the actual string value
223 protected void assureEquals( String message, String expected, String actual ) {
224 assureEquals( message, expected, actual, false );
228 * Assure that two object are equal
229 * @param expected specifies the expected object value
230 * @param actual specifies the actual object value
232 protected void assureEquals( Object expected, Object actual ) {
233 assureEquals( "Equality test failed", expected, actual, false );
237 * Assure that two objects are equal
238 * @param message the message to print when the equality test fails
239 * @param expected specifies the expected object value
240 * @param actual specifies the actual object value
242 protected void assureEquals( String message, Object expected, Object actual ) {
243 assureEquals( message, expected, actual, false );
246 /** invokes a given method on a given object, and assures a certain exception is caught
247 * @param _message is the message to print when the check fails
248 * @param _object is the object to invoke the method on
249 * @param _methodName is the name of the method to invoke
250 * @param _methodArgs are the arguments to pass to the method.
251 * @param _argClasses are the classes to assume for the arguments of the methods
252 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
253 * it means that <em>no</em> exception must be throw by invoking the method.
255 protected void assureException( final String _message, final Object _object, final String _methodName,
256 final Class[] _argClasses, final Object[] _methodArgs, final Class _expectedExceptionClass )
258 Class objectClass = _object.getClass();
260 boolean noExceptionAllowed = ( _expectedExceptionClass == null );
262 boolean caughtExpected = noExceptionAllowed ? true : false;
265 Method method = objectClass.getMethod( _methodName, _argClasses );
266 method.invoke(_object, _methodArgs );
268 catch ( InvocationTargetException e )
270 caughtExpected = noExceptionAllowed
271 ? false
272 : ( e.getTargetException().getClass().equals( _expectedExceptionClass ) );
274 catch( Exception e )
276 caughtExpected = false;
279 assure( _message, caughtExpected );
282 /** invokes a given method on a given object, and assures a certain exception is caught
283 * @param _message is the message to print when the check fails
284 * @param _object is the object to invoke the method on
285 * @param _methodName is the name of the method to invoke
286 * @param _methodArgs are the arguments to pass to the method. Those implicitly define
287 * the classes of the arguments of the method which is called.
288 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
289 * it means that <em>no</em> exception must be throw by invoking the method.
291 protected void assureException( final String _message, final Object _object, final String _methodName,
292 final Object[] _methodArgs, final Class _expectedExceptionClass )
294 Class[] argClasses = new Class[ _methodArgs.length ];
295 for ( int i=0; i<_methodArgs.length; ++i )
296 argClasses[i] = _methodArgs[i].getClass();
297 assureException( _message, _object, _methodName, argClasses, _methodArgs, _expectedExceptionClass );
300 /** invokes a given method on a given object, and assures a certain exception is caught
301 * @param _object is the object to invoke the method on
302 * @param _methodName is the name of the method to invoke
303 * @param _methodArgs are the arguments to pass to the method. Those implicitly define
304 * the classes of the arguments of the method which is called.
305 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
306 * it means that <em>no</em> exception must be throw by invoking the method.
308 protected void assureException( final Object _object, final String _methodName, final Object[] _methodArgs,
309 final Class _expectedExceptionClass )
311 assureException(
312 "did not catch the expected exception (" +
313 ( ( _expectedExceptionClass == null ) ? "none" : _expectedExceptionClass.getName() ) +
314 ") while calling " + _object.getClass().getName() + "." + _methodName,
315 _object, _methodName, _methodArgs, _expectedExceptionClass );
318 /** invokes a given method on a given object, and assures a certain exception is caught
319 * @param _object is the object to invoke the method on
320 * @param _methodName is the name of the method to invoke
321 * @param _methodArgs are the arguments to pass to the method
322 * @param _argClasses are the classes to assume for the arguments of the methods
323 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
324 * it means that <em>no</em> exception must be throw by invoking the method.
326 protected void assureException( final Object _object, final String _methodName, final Class[] _argClasses,
327 final Object[] _methodArgs, final Class _expectedExceptionClass )
329 assureException(
330 "did not catch the expected exception (" +
331 ( ( _expectedExceptionClass == null ) ? "none" : _expectedExceptionClass.getName() ) +
332 ") while calling " + _object.getClass().getName() + "." + _methodName,
333 _object, _methodName, _argClasses, _methodArgs, _expectedExceptionClass );
337 * Mark the currently executed method as failed.
338 * This function generates "Test did fail." as standard message.
340 protected void failed() {
341 assure("Test did fail.", false, false);
345 * Mark the currently executed method as failed.
346 * with the given message.
347 * @param msg The message of the failure.
349 protected void failed(String msg) {
350 assure(msg, false, false);
354 * Assure that s is true.
355 * The given message will be only evaluated, if s is false.
356 * Normally, assure() leaves the current test method, and the next one
357 * is executed. With the parameter 'cont' set to true, the current test
358 * method will continue.<br>
359 * The current method will of course marked as failed.
360 * @param msg The message that is evaluated.
361 * @param s The condition that should be true.
362 * @param cont Continue with test method, even if s is false.
364 protected void assure(String msg, boolean s, boolean cont) {
365 state &= s;
366 if (!s) {
367 message += msg + "\r\n";
368 // log.println(msg);
369 if (!cont) {
370 throw new AssureException(msg);
375 protected void assureEquals( String message, Object expected, Object actual, boolean cont ) {
376 assure( message + " (expected: " + expected.toString() + ", actual: " + actual.toString() + ")",
377 expected.equals( actual ), cont );
381 * Mark the currently executed method as failed.
382 * with the given message.
383 * The given message will be only evaluated, if s is false.
384 * With the parameter 'cont' set to true, the current test
385 * method will continue.<br>
386 * The current method will of course marked as failed.
387 * @param msg The message that is evaluated.
388 * @param cont Continue with test method, even if s is false.
390 protected void failed(String msg, boolean cont) {
391 assure(msg, false, cont);
395 * @deprecated
397 // protected void addResult(String message, boolean state) {
398 // String msg = message + " - " + state;
399 // this.state &= state;
400 // this.message += msg + "\r\n";
401 // log.println(msg);
402 // }
404 public class AssureException extends RuntimeException {
406 public AssureException(String msg) {
407 super(msg);