bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / complexlib / Assurance.java
blob0a43aae6b1c9ef5c4ce2bbf757cd9f730f46f288
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complexlib;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
24 /**
25 * I have removed the assure(...) functions from ComplexTestCase due to the fact now I can
26 * use the functions every where and don't need to be a ComplexTestCase any longer.
28 public class Assurance
30 public static final boolean CONTINUE = true;
32 /** State of the current test method **/
33 protected boolean state = true;
35 /** The message if the test did fail **/
36 protected String message = null;
39 /**
40 * Assure that s is true.
41 * This function generates "Assure failed." as standard message.
42 * @param s The condition that should be true.
44 protected void assure(boolean s) {
45 assure("Assure failed.", s, false);
48 /**
49 * Assure that s is true.
50 * The given message will be only evaluated, if s is false.
51 * @param msg The message that is evaluated.
52 * @param s The condition that should be true.
54 protected void assure(String msg, boolean s) {
55 assure(msg, s, false);
58 /**
59 * Assure that two boolean values are equal
60 * @param expected specifies the expected boolean value
61 * @param actual specifies the actual boolean value
63 protected void assureEquals( boolean expected, boolean actual ) {
64 assureEquals( "Equality test failed", expected, new Boolean( actual ), false );
67 /**
68 * Assure that two boolean values are equal
69 * @param message the message to print when the equality test fails
70 * @param expected specifies the expected boolean value
71 * @param actual specifies the actual boolean value
73 protected void assureEquals( String message, boolean expected, boolean actual ) {
74 assureEquals( message, expected, actual, false );
77 /**
78 * Assure that two byte values are equal
79 * @param expected specifies the expected byte value
80 * @param actual specifies the actual byte value
82 protected void assureEquals( byte expected, byte actual ) {
83 assureEquals( "Equality test failed", new Byte( expected ), new Byte( actual ), false );
86 /**
87 * Assure that two byte values are equal
88 * @param message the message to print when the equality test fails
89 * @param expected specifies the expected byte value
90 * @param actual specifies the actual byte value
92 protected void assureEquals( String message, byte expected, byte actual ) {
93 assureEquals( message, new Byte( expected ), new Byte( actual ), false );
96 /**
97 * Assure that two double values are equal
98 * @param expected specifies the expected double value
99 * @param actual specifies the actual double value
101 protected void assureEquals( double expected, double actual ) {
102 assureEquals( "Equality test failed", new Double( expected ), new Double( actual ), false );
106 * Assure that two double values are equal
107 * @param message the message to print when the equality test fails
108 * @param expected specifies the expected double value
109 * @param actual specifies the actual double value
111 protected void assureEquals( String message, double expected, double actual ) {
112 assureEquals( message, new Double( expected ), new Double( actual ), false );
116 * Assure that two float values are equal
117 * @param expected specifies the expected float value
118 * @param actual specifies the actual float value
120 protected void assureEquals( float expected, float actual ) {
121 assureEquals( "Equality test failed", new Float( expected ), new Float( actual ), false );
125 * Assure that two float values are equal
126 * @param message the message to print when the equality test fails
127 * @param expected specifies the expected float value
128 * @param actual specifies the actual float value
130 protected void assureEquals( String message, float expected, float actual ) {
131 assureEquals( message, new Float( expected ), new Float( actual ), false );
135 * Assure that two short values are equal
136 * @param expected specifies the expected short value
137 * @param actual specifies the actual short value
139 protected void assureEquals( short expected, short actual ) {
140 assureEquals( "Equality test failed", new Short( expected ), new Short( actual ), false );
144 * Assure that two short values are equal
145 * @param message the message to print when the equality test fails
146 * @param expected specifies the expected short value
147 * @param actual specifies the actual short value
149 protected void assureEquals( String message, short expected, short actual ) {
150 assureEquals( message, new Short( expected ), new Short( actual ), false );
154 * Assure that two int values are equal
155 * @param expected specifies the expected int value
156 * @param actual specifies the actual int value
158 protected void assureEquals( int expected, int actual ) {
159 assureEquals( "Equality test failed", new Integer( expected ), new Integer( actual ), false );
163 * Assure that two int values are equal
164 * @param message the message to print when the equality test fails
165 * @param expected specifies the expected int value
166 * @param actual specifies the actual int value
168 protected void assureEquals( String message, int expected, int actual ) {
169 assureEquals( message, new Integer( expected ), new Integer( actual ), false );
173 * Assure that two long values are equal
174 * @param expected specifies the expected long value
175 * @param actual specifies the actual long value
177 protected void assureEquals( long expected, long actual ) {
178 assureEquals( "Equality test failed", new Long( expected ), new Long( actual ), false );
182 * Assure that two long values are equal
183 * @param message the message to print when the equality test fails
184 * @param expected specifies the expected long value
185 * @param actual specifies the actual long value
187 protected void assureEquals( String message, long expected, long actual ) {
188 assureEquals( message, new Long( expected ), new Long( actual ), false );
192 * Assure that two string values are equal
193 * @param expected specifies the expected string value
194 * @param actual specifies the actual string value
196 protected void assureEquals( String expected, String actual ) {
197 assureEquals( "Equality test failed", expected, actual, false );
201 * Assure that two string values are equal
202 * @param message the message to print when the equality test fails
203 * @param expected specifies the expected string value
204 * @param actual specifies the actual string value
206 protected void assureEquals( String message, String expected, String actual ) {
207 assureEquals( message, expected, actual, false );
211 * Assure that two object are equal
212 * @param expected specifies the expected object value
213 * @param actual specifies the actual object value
215 protected void assureEquals( Object expected, Object actual ) {
216 assureEquals( "Equality test failed", expected, actual, false );
220 * Assure that two objects are equal
221 * @param message the message to print when the equality test fails
222 * @param expected specifies the expected object value
223 * @param actual specifies the actual object value
225 protected void assureEquals( String message, Object expected, Object actual ) {
226 assureEquals( message, expected, actual, false );
230 * assures the two given sequences are of equal length, and have equal content
232 public <T> void assureEquals( String i_message, T[] i_expected, T[] i_actual, boolean i_continue )
234 if ( i_expected.length != i_actual.length )
235 failed( i_message + ": expected element count: " + i_expected.length + ", actual element count: " + i_actual.length );
236 for ( int i=0; i<i_expected.length; ++i )
238 assureEquals( i_message + ": mismatch at element pos " + i, i_expected[i], i_actual[i], i_continue );
243 * assures the two given sequences are of equal length, and have equal content
245 public <T> void assureEquals( String i_message, T[] i_expected, T[] i_actual )
247 assureEquals( i_message, i_expected, i_actual, false );
250 /** invokes a given method on a given object, and assures a certain exception is caught
251 * @param _message is the message to print when the check fails
252 * @param _object is the object to invoke the method on
253 * @param _methodName is the name of the method to invoke
254 * @param _methodArgs are the arguments to pass to the method.
255 * @param _argClasses are the classes to assume for the arguments of the methods
256 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
257 * it means that <em>no</em> exception must be throw by invoking the method.
259 protected void assureException( final String _message, final Object _object, final String _methodName,
260 final Class<?>[] _argClasses, final Object[] _methodArgs, final Class<?> _expectedExceptionClass )
262 Class<?> objectClass = _object.getClass();
264 boolean noExceptionAllowed = ( _expectedExceptionClass == null );
266 boolean caughtExpected = noExceptionAllowed ? true : false;
269 Method method = objectClass.getMethod( _methodName, _argClasses );
270 method.invoke(_object, _methodArgs );
272 catch ( InvocationTargetException e )
274 caughtExpected = noExceptionAllowed
275 ? false
276 : ( e.getTargetException().getClass().equals( _expectedExceptionClass ) );
278 catch( Exception e )
280 caughtExpected = false;
283 assure( _message, caughtExpected );
286 /** invokes a given method on a given object, and assures a certain exception is caught
287 * @param _message is the message to print when the check fails
288 * @param _object is the object to invoke the method on
289 * @param _methodName is the name of the method to invoke
290 * @param _methodArgs are the arguments to pass to the method. Those implicitly define
291 * the classes of the arguments of the method which is called.
292 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
293 * it means that <em>no</em> exception must be throw by invoking the method.
295 protected void assureException( final String _message, final Object _object, final String _methodName,
296 final Object[] _methodArgs, final Class<?> _expectedExceptionClass )
298 Class<?>[] argClasses = new Class[ _methodArgs.length ];
299 for ( int i=0; i<_methodArgs.length; ++i )
300 argClasses[i] = _methodArgs[i].getClass();
301 assureException( _message, _object, _methodName, argClasses, _methodArgs, _expectedExceptionClass );
304 /** invokes a given method on a given object, and assures a certain exception is caught
305 * @param _object is the object to invoke the method on
306 * @param _methodName is the name of the method to invoke
307 * @param _methodArgs are the arguments to pass to the method. Those implicitly define
308 * the classes of the arguments of the method which is called.
309 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
310 * it means that <em>no</em> exception must be throw by invoking the method.
312 protected void assureException( final Object _object, final String _methodName, final Object[] _methodArgs,
313 final Class<?> _expectedExceptionClass )
315 assureException(
316 "did not catch the expected exception (" +
317 ( ( _expectedExceptionClass == null ) ? "none" : _expectedExceptionClass.getName() ) +
318 ") while calling " + _object.getClass().getName() + "." + _methodName,
319 _object, _methodName, _methodArgs, _expectedExceptionClass );
322 /** invokes a given method on a given object, and assures a certain exception is caught
323 * @param _object is the object to invoke the method on
324 * @param _methodName is the name of the method to invoke
325 * @param _methodArgs are the arguments to pass to the method
326 * @param _argClasses are the classes to assume for the arguments of the methods
327 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
328 * it means that <em>no</em> exception must be throw by invoking the method.
330 protected void assureException( final Object _object, final String _methodName, final Class<?>[] _argClasses,
331 final Object[] _methodArgs, final Class<?> _expectedExceptionClass )
333 assureException(
334 "did not catch the expected exception (" +
335 ( ( _expectedExceptionClass == null ) ? "none" : _expectedExceptionClass.getName() ) +
336 ") while calling " + _object.getClass().getName() + "." + _methodName,
337 _object, _methodName, _argClasses, _methodArgs, _expectedExceptionClass );
341 * Mark the currently executed method as failed.
342 * This function generates "Test did fail." as standard message.
344 protected void failed() {
345 assure("Test did fail.", false, false);
349 * Mark the currently executed method as failed.
350 * with the given message.
351 * @param msg The message of the failure.
353 protected void failed(String msg) {
354 assure(msg, false, false);
358 * Assure that s is true.
359 * The given message will be only evaluated, if s is false.
360 * Normally, assure() leaves the current test method, and the next one
361 * is executed. With the parameter 'cont' set to true, the current test
362 * method will continue.<br>
363 * The current method will of course marked as failed.
364 * @param msg The message that is evaluated.
365 * @param s The condition that should be true.
366 * @param cont Continue with test method, even if s is false.
368 protected void assure(String msg, boolean s, boolean cont) {
369 state &= s;
370 if (!s) {
371 message += msg + "\r\n";
372 // log.println(msg);
373 if (!cont) {
374 throw new AssureException(msg);
379 protected void assureEquals( String message, Object expected, Object actual, boolean cont ) {
380 assure( message + " (expected: " + expected.toString() + ", actual: " + actual.toString() + ")",
381 expected.equals( actual ), cont );
385 * Mark the currently executed method as failed.
386 * with the given message.
387 * The given message will be only evaluated, if s is false.
388 * With the parameter 'cont' set to true, the current test
389 * method will continue.<br>
390 * The current method will of course marked as failed.
391 * @param msg The message that is evaluated.
392 * @param cont Continue with test method, even if s is false.
394 protected void failed(String msg, boolean cont) {
395 assure(msg, false, cont);
398 public class AssureException extends RuntimeException {
400 public AssureException(String msg) {
401 super(msg);