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