1 /* 2 * ************************************************************************ 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * 8 * OpenOffice.org - a multi-platform office productivity suite 9 * 10 * This file is part of OpenOffice.org. 11 * 12 * OpenOffice.org is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU Lesser General Public License version 3 14 * only, as published by the Free Software Foundation. 15 * 16 * OpenOffice.org is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License version 3 for more details 20 * (a copy is included in the LICENSE file that accompanied this code). 21 * 22 * You should have received a copy of the GNU Lesser General Public License 23 * version 3 along with OpenOffice.org. If not, see 24 * <http://www.openoffice.org/license.html> 25 * for a copy of the LGPLv3 License. 26 * 27 * *********************************************************************** 28 */ 29 30 package complexlib; 31 32 import java.lang.reflect.InvocationTargetException; 33 import java.lang.reflect.Method; 34 35 /** 36 * 37 * @author ll93751 38 * 39 * I have removed the assure(...) functions from ComplexTestCase due to the fact now I can 40 * use the functions every where and don't need to be a ComplexTestCase any longer. 41 */ 42 public class Assurance 43 { 44 public static final boolean CONTINUE = true; 45 46 /** State of the current test method **/ 47 protected boolean state = true; 48 49 /** The message if the test did fail **/ 50 protected String message = null; 51 52 53 /** 54 * Assure that s is true. 55 * This function generates "Assure failed." as standard message. 56 * @param s The condition that should be true. 57 */ 58 protected void assure(boolean s) { 59 assure("Assure failed.", s, false); 60 } 61 62 /** 63 * Assure that s is true. 64 * The given message will be only evaluated, if s is false. 65 * @param msg The message that is evaluated. 66 * @param s The condition that should be true. 67 */ 68 protected void assure(String msg, boolean s) { 69 assure(msg, s, false); 70 } 71 72 /** 73 * Assure that two boolean values are equal 74 * @param expected specifies the expected boolean value 75 * @param actual specifies the actual boolean value 76 */ 77 protected void assureEquals( boolean expected, boolean actual ) { 78 assureEquals( "Equality test failed", expected, new Boolean( actual ), false ); 79 } 80 81 /** 82 * Assure that two boolean values are equal 83 * @param message the message to print when the equality test fails 84 * @param expected specifies the expected boolean value 85 * @param actual specifies the actual boolean value 86 */ 87 protected void assureEquals( String message, boolean expected, boolean actual ) { 88 assureEquals( message, expected, actual, false ); 89 } 90 91 /** 92 * Assure that two byte values are equal 93 * @param expected specifies the expected byte value 94 * @param actual specifies the actual byte value 95 */ 96 protected void assureEquals( byte expected, byte actual ) { 97 assureEquals( "Equality test failed", new Byte( expected ), new Byte( actual ), false ); 98 } 99 100 /** 101 * Assure that two byte values are equal 102 * @param message the message to print when the equality test fails 103 * @param expected specifies the expected byte value 104 * @param actual specifies the actual byte value 105 */ 106 protected void assureEquals( String message, byte expected, byte actual ) { 107 assureEquals( message, new Byte( expected ), new Byte( actual ), false ); 108 } 109 110 /** 111 * Assure that two double values are equal 112 * @param expected specifies the expected double value 113 * @param actual specifies the actual double value 114 */ 115 protected void assureEquals( double expected, double actual ) { 116 assureEquals( "Equality test failed", new Double( expected ), new Double( actual ), false ); 117 } 118 119 /** 120 * Assure that two double values are equal 121 * @param message the message to print when the equality test fails 122 * @param expected specifies the expected double value 123 * @param actual specifies the actual double value 124 */ 125 protected void assureEquals( String message, double expected, double actual ) { 126 assureEquals( message, new Double( expected ), new Double( actual ), false ); 127 } 128 129 /** 130 * Assure that two float values are equal 131 * @param expected specifies the expected float value 132 * @param actual specifies the actual float value 133 */ 134 protected void assureEquals( float expected, float actual ) { 135 assureEquals( "Equality test failed", new Float( expected ), new Float( actual ), false ); 136 } 137 138 /** 139 * Assure that two float values are equal 140 * @param message the message to print when the equality test fails 141 * @param expected specifies the expected float value 142 * @param actual specifies the actual float value 143 */ 144 protected void assureEquals( String message, float expected, float actual ) { 145 assureEquals( message, new Float( expected ), new Float( actual ), false ); 146 } 147 148 /** 149 * Assure that two short values are equal 150 * @param expected specifies the expected short value 151 * @param actual specifies the actual short value 152 */ 153 protected void assureEquals( short expected, short actual ) { 154 assureEquals( "Equality test failed", new Short( expected ), new Short( actual ), false ); 155 } 156 157 /** 158 * Assure that two short values are equal 159 * @param message the message to print when the equality test fails 160 * @param expected specifies the expected short value 161 * @param actual specifies the actual short value 162 */ 163 protected void assureEquals( String message, short expected, short actual ) { 164 assureEquals( message, new Short( expected ), new Short( actual ), false ); 165 } 166 167 /** 168 * Assure that two int values are equal 169 * @param expected specifies the expected int value 170 * @param actual specifies the actual int value 171 */ 172 protected void assureEquals( int expected, int actual ) { 173 assureEquals( "Equality test failed", new Integer( expected ), new Integer( actual ), false ); 174 } 175 176 /** 177 * Assure that two int values are equal 178 * @param message the message to print when the equality test fails 179 * @param expected specifies the expected int value 180 * @param actual specifies the actual int value 181 */ 182 protected void assureEquals( String message, int expected, int actual ) { 183 assureEquals( message, new Integer( expected ), new Integer( actual ), false ); 184 } 185 186 /** 187 * Assure that two long values are equal 188 * @param expected specifies the expected long value 189 * @param actual specifies the actual long value 190 */ 191 protected void assureEquals( long expected, long actual ) { 192 assureEquals( "Equality test failed", new Long( expected ), new Long( actual ), false ); 193 } 194 195 /** 196 * Assure that two long values are equal 197 * @param message the message to print when the equality test fails 198 * @param expected specifies the expected long value 199 * @param actual specifies the actual long value 200 */ 201 protected void assureEquals( String message, long expected, long actual ) { 202 assureEquals( message, new Long( expected ), new Long( actual ), false ); 203 } 204 205 /** 206 * Assure that two string values are equal 207 * @param expected specifies the expected string value 208 * @param actual specifies the actual string value 209 */ 210 protected void assureEquals( String expected, String actual ) { 211 assureEquals( "Equality test failed", expected, actual, false ); 212 } 213 214 /** 215 * Assure that two string values are equal 216 * @param message the message to print when the equality test fails 217 * @param expected specifies the expected string value 218 * @param actual specifies the actual string value 219 */ 220 protected void assureEquals( String message, String expected, String actual ) { 221 assureEquals( message, expected, actual, false ); 222 } 223 224 /** 225 * Assure that two object are equal 226 * @param expected specifies the expected object value 227 * @param actual specifies the actual object value 228 */ 229 protected void assureEquals( Object expected, Object actual ) { 230 assureEquals( "Equality test failed", expected, actual, false ); 231 } 232 233 /** 234 * Assure that two objects are equal 235 * @param message the message to print when the equality test fails 236 * @param expected specifies the expected object value 237 * @param actual specifies the actual object value 238 */ 239 protected void assureEquals( String message, Object expected, Object actual ) { 240 assureEquals( message, expected, actual, false ); 241 } 242 243 /** 244 * assures the two given sequences are of equal length, and have equal content 245 */ 246 public <T> void assureEquals( String i_message, T[] i_expected, T[] i_actual, boolean i_continue ) 247 { 248 if ( i_expected.length != i_actual.length ) 249 failed( i_message + ": expected element count: " + i_expected.length + ", actual element count: " + i_actual.length ); 250 for ( int i=0; i<i_expected.length; ++i ) 251 { 252 assureEquals( i_message + ": mismatch at element pos " + i, i_expected[i], i_actual[i], i_continue ); 253 } 254 } 255 256 /** 257 * assures the two given sequences are of equal length, and have equal content 258 */ 259 public <T> void assureEquals( String i_message, T[] i_expected, T[] i_actual ) 260 { 261 assureEquals( i_message, i_expected, i_actual, false ); 262 } 263 264 /** invokes a given method on a given object, and assures a certain exception is caught 265 * @param _message is the message to print when the check fails 266 * @param _object is the object to invoke the method on 267 * @param _methodName is the name of the method to invoke 268 * @param _methodArgs are the arguments to pass to the method. 269 * @param _argClasses are the classes to assume for the arguments of the methods 270 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null, 271 * it means that <em>no</em> exception must be throw by invoking the method. 272 */ 273 protected void assureException( final String _message, final Object _object, final String _methodName, 274 final Class[] _argClasses, final Object[] _methodArgs, final Class _expectedExceptionClass ) 275 { 276 Class objectClass = _object.getClass(); 277 278 boolean noExceptionAllowed = ( _expectedExceptionClass == null ); 279 280 boolean caughtExpected = noExceptionAllowed ? true : false; 281 try 282 { 283 Method method = objectClass.getMethod( _methodName, _argClasses ); 284 method.invoke(_object, _methodArgs ); 285 } 286 catch ( InvocationTargetException e ) 287 { 288 caughtExpected = noExceptionAllowed 289 ? false 290 : ( e.getTargetException().getClass().equals( _expectedExceptionClass ) ); 291 } 292 catch( Exception e ) 293 { 294 caughtExpected = false; 295 } 296 297 assure( _message, caughtExpected ); 298 } 299 300 /** invokes a given method on a given object, and assures a certain exception is caught 301 * @param _message is the message to print when the check fails 302 * @param _object is the object to invoke the method on 303 * @param _methodName is the name of the method to invoke 304 * @param _methodArgs are the arguments to pass to the method. Those implicitly define 305 * the classes of the arguments of the method which is called. 306 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null, 307 * it means that <em>no</em> exception must be throw by invoking the method. 308 */ 309 protected void assureException( final String _message, final Object _object, final String _methodName, 310 final Object[] _methodArgs, final Class _expectedExceptionClass ) 311 { 312 Class[] argClasses = new Class[ _methodArgs.length ]; 313 for ( int i=0; i<_methodArgs.length; ++i ) 314 argClasses[i] = _methodArgs[i].getClass(); 315 assureException( _message, _object, _methodName, argClasses, _methodArgs, _expectedExceptionClass ); 316 } 317 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. Those implicitly define 322 * the classes of the arguments of the method which is called. 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. 325 */ 326 protected void assureException( final Object _object, final String _methodName, final Object[] _methodArgs, 327 final Class _expectedExceptionClass ) 328 { 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, _methodArgs, _expectedExceptionClass ); 334 } 335 336 /** invokes a given method on a given object, and assures a certain exception is caught 337 * @param _object is the object to invoke the method on 338 * @param _methodName is the name of the method to invoke 339 * @param _methodArgs are the arguments to pass to the method 340 * @param _argClasses are the classes to assume for the arguments of the methods 341 * @param _expectedExceptionClass is the class of the exception to be caught. If this is null, 342 * it means that <em>no</em> exception must be throw by invoking the method. 343 */ 344 protected void assureException( final Object _object, final String _methodName, final Class[] _argClasses, 345 final Object[] _methodArgs, final Class _expectedExceptionClass ) 346 { 347 assureException( 348 "did not catch the expected exception (" + 349 ( ( _expectedExceptionClass == null ) ? "none" : _expectedExceptionClass.getName() ) + 350 ") while calling " + _object.getClass().getName() + "." + _methodName, 351 _object, _methodName, _argClasses, _methodArgs, _expectedExceptionClass ); 352 } 353 354 /** 355 * Mark the currently executed method as failed. 356 * This function generates "Test did fail." as standard message. 357 */ 358 protected void failed() { 359 assure("Test did fail.", false, false); 360 } 361 362 /** 363 * Mark the currently executed method as failed. 364 * with the given message. 365 * @param msg The message of the failure. 366 */ 367 protected void failed(String msg) { 368 assure(msg, false, false); 369 } 370 371 /** 372 * Assure that s is true. 373 * The given message will be only evaluated, if s is false. 374 * Normally, assure() leaves the current test method, and the next one 375 * is executed. With the parameter 'cont' set to true, the current test 376 * method will continue.<br> 377 * The current method will of course marked as failed. 378 * @param msg The message that is evaluated. 379 * @param s The condition that should be true. 380 * @param cont Continue with test method, even if s is false. 381 */ 382 protected void assure(String msg, boolean s, boolean cont) { 383 state &= s; 384 if (!s) { 385 message += msg + "\r\n"; 386 // log.println(msg); 387 if (!cont) { 388 throw new AssureException(msg); 389 } 390 } 391 } 392 393 protected void assureEquals( String message, Object expected, Object actual, boolean cont ) { 394 assure( message + " (expected: " + expected.toString() + ", actual: " + actual.toString() + ")", 395 expected.equals( actual ), cont ); 396 } 397 398 /** 399 * Mark the currently executed method as failed. 400 * with the given message. 401 * The given message will be only evaluated, if s is false. 402 * With the parameter 'cont' set to true, the current test 403 * method will continue.<br> 404 * The current method will of course marked as failed. 405 * @param msg The message that is evaluated. 406 * @param cont Continue with test method, even if s is false. 407 */ 408 protected void failed(String msg, boolean cont) { 409 assure(msg, false, cont); 410 } 411 412 /** 413 * @deprecated 414 */ 415 // protected void addResult(String message, boolean state) { 416 // String msg = message + " - " + state; 417 // this.state &= state; 418 // this.message += msg + "\r\n"; 419 // log.println(msg); 420 // } 421 422 public class AssureException extends RuntimeException { 423 424 public AssureException(String msg) { 425 super(msg); 426 } 427 } 428 } 429