001/*
002 *   Copyright (C) Christian Schulte, 2005-206
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: Assert.java 4613 2012-09-22 10:07:08Z schulte $
029 *
030 */
031package org.jomc.ant.test.support;
032
033import java.text.MessageFormat;
034import java.util.ResourceBundle;
035import org.apache.tools.ant.BuildEvent;
036import static org.junit.Assert.assertEquals;
037import static org.junit.Assert.assertNotNull;
038import static org.junit.Assert.assertNull;
039import static org.junit.Assert.assertTrue;
040
041/**
042 * Provides static methods for testing various assertions related to Ant executions.
043 *
044 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
045 * @version $JOMC: Assert.java 4613 2012-09-22 10:07:08Z schulte $
046 */
047public abstract class Assert
048{
049
050    /** Creates a new {@code Assert} instance. */
051    public Assert()
052    {
053        super();
054    }
055
056    /**
057     * Tests an Ant execution to have thrown an exception of a given type.
058     *
059     * @param result The result to test.
060     * @param exception The class of the expected exception.
061     */
062    public static void assertException( final AntExecutionResult result, final Class<? extends Throwable> exception )
063    {
064        assertNotNull( result );
065        assertNotNull( exception );
066        assertNotNull( getMessage( "expectedException", exception.getName() ), result.getThrowable() );
067        assertTrue( getMessage( "unexpectedExceptionClass", result.getThrowable().getClass().getName(),
068                                exception.getName() ), result.getThrowable().getClass() == exception );
069
070    }
071
072    /**
073     * Tests an Ant execution to have thrown an exception of a given type taking a reason.
074     *
075     * @param result The result to test.
076     * @param exception The class of the expected exception.
077     * @param reason The reason describing why {@code exception} should have been thrown.
078     */
079    public static void assertException( final AntExecutionResult result, final Class<? extends Throwable> exception,
080                                        final CharSequence reason )
081    {
082        assertNotNull( result );
083        assertNotNull( exception );
084        assertNotNull( reason );
085        assertNotNull( reason.toString(), result.getThrowable() );
086        assertTrue( reason.toString(), result.getThrowable().getClass() == exception );
087    }
088
089    /**
090     * Tests an Ant execution to have thrown an instance of a given exception.
091     *
092     * @param result The result to test.
093     * @param exception The class the expected exception should be an instance of.
094     */
095    public static void assertExceptionInstance( final AntExecutionResult result,
096                                                final Class<? extends Throwable> exception )
097    {
098        assertNotNull( result );
099        assertNotNull( exception );
100        assertNotNull( getMessage( "expectedException", exception.getName() ), result.getThrowable() );
101        assertTrue( getMessage( "unexpectedExceptionInstance", result.getThrowable().getClass().getName(),
102                                exception.getName() ), result.getThrowable().getClass().isAssignableFrom( exception ) );
103
104    }
105
106    /**
107     * Tests an Ant execution to have thrown an instance of a given exception taking a reason.
108     *
109     * @param result The result to test.
110     * @param exception The class the expected exception should be an instance of.
111     * @param reason The reason describing why {@code exception} should have been thrown.
112     */
113    public static void assertExceptionInstance( final AntExecutionResult result,
114                                                final Class<? extends Throwable> exception, final CharSequence reason )
115    {
116        assertNotNull( result );
117        assertNotNull( exception );
118        assertNotNull( reason.toString(), result.getThrowable() );
119        assertTrue( reason.toString(), result.getThrowable().getClass().isAssignableFrom( exception ) );
120    }
121
122    /**
123     * Tests an Ant execution to have thrown an exception with a given message.
124     *
125     * @param result The result to test.
126     * @param message The message of the expected exception.
127     */
128    public static void assertExceptionMessage( final AntExecutionResult result, final CharSequence message )
129    {
130        assertNotNull( result );
131        assertNotNull( message );
132        assertNotNull( getMessage( "unexpectedSuccess" ), result.getThrowable() );
133        assertEquals( getMessage( "unexpectedExceptionMessage", result.getThrowable().getMessage(), message ), message,
134                      result.getThrowable().getMessage() );
135
136    }
137
138    /**
139     * Tests an Ant execution to have thrown an exception with a given message taking a reason.
140     *
141     * @param result The result to test.
142     * @param message The message of the expected exception.
143     * @param reason The reason describing why {@code exception} should have been thrown.
144     */
145    public static void assertExceptionMessage( final AntExecutionResult result, final CharSequence message,
146                                               final CharSequence reason )
147    {
148        assertNotNull( result );
149        assertNotNull( message );
150        assertNotNull( reason );
151        assertNotNull( reason.toString(), result.getThrowable() );
152        assertEquals( reason.toString(), message, result.getThrowable().getMessage() );
153    }
154
155    /**
156     * Tests an Ant execution to have thrown an exception with a message containing a given string.
157     *
158     * @param result The result to test.
159     * @param needle The text the message of the expected exception should contain.
160     */
161    public static void assertExceptionMessageContaining( final AntExecutionResult result, final CharSequence needle )
162    {
163        assertNotNull( result );
164        assertNotNull( needle );
165        assertNotNull( getMessage( "unexpectedSuccess" ), result.getThrowable() );
166        assertTrue( getMessage( "unexpectedExceptionMessageContaining", result.getThrowable().getMessage(), needle ),
167                    result.getThrowable().getMessage().contains( needle ) );
168
169    }
170
171    /**
172     * Tests an Ant execution to have thrown an exception with a message containing a given string taking a reason.
173     *
174     * @param result The result to test.
175     * @param needle The text the message of the expected exception should contain.
176     * @param reason The reason describing why {@code exception} should have been thrown.
177     */
178    public static void assertExceptionMessageContaining( final AntExecutionResult result, final CharSequence needle,
179                                                         final CharSequence reason )
180    {
181        assertNotNull( result );
182        assertNotNull( needle );
183        assertNotNull( reason );
184        assertNotNull( reason.toString(), result.getThrowable() );
185        assertTrue( reason.toString(), result.getThrowable().getMessage().contains( needle ) );
186    }
187
188    /**
189     * Tests an Ant execution to not have thrown an exception.
190     *
191     * @param result The result to test.
192     */
193    public static void assertNoException( final AntExecutionResult result )
194    {
195        assertNotNull( result );
196        assertNull( getMessage( "unexpectedException", result.getThrowable() ), result.getThrowable() );
197    }
198
199    /**
200     * Tests an Ant execution to not have thrown an exception taking a reason.
201     *
202     * @param result The result to test.
203     * @param reason The reason describing why the execution should not have thrown an exception.
204     */
205    public static void assertNoException( final AntExecutionResult result, final CharSequence reason )
206    {
207        assertNotNull( result );
208        assertNotNull( reason );
209        assertNull( reason.toString(), result.getThrowable() );
210    }
211
212    /**
213     * Tests an Ant execution to have written a given text to the system output stream.
214     *
215     * @param result The result to test.
216     * @param needle The text which should have been written to the system output stream.
217     */
218    public static void assertSystemOutputContaining( final AntExecutionResult result, final CharSequence needle )
219    {
220        assertNotNull( result );
221        assertNotNull( needle );
222        assertNotNull( getMessage( "expectedSystemOutputContaining", needle ), result.getSystemOutput() );
223        assertTrue( getMessage( "expectedSystemOutputContaining", needle ),
224                    result.getSystemOutput().contains( needle ) );
225
226    }
227
228    /**
229     * Tests an Ant execution to have written a given text to the system output stream taking a reason.
230     *
231     * @param result The result to test.
232     * @param needle The text which should have been written to the system output stream.
233     * @param reason The reason describing why {@code needle} should have been written to the system output stream.
234     */
235    public static void assertSystemOutputContaining( final AntExecutionResult result, final CharSequence needle,
236                                                     final CharSequence reason )
237    {
238        assertNotNull( result );
239        assertNotNull( needle );
240        assertNotNull( reason );
241        assertNotNull( reason.toString(), result.getSystemOutput() );
242        assertTrue( reason.toString(), result.getSystemOutput().contains( needle ) );
243    }
244
245    /**
246     * Tests an Ant execution to have written a given text to the system error stream.
247     *
248     * @param result The result to test.
249     * @param needle The text which should have been written to the system error stream.
250     */
251    public static void assertSystemErrorContaining( final AntExecutionResult result, final CharSequence needle )
252    {
253        assertNotNull( result );
254        assertNotNull( needle );
255        assertNotNull( getMessage( "expectedSystemErrorContaining", needle ), result.getSystemError() );
256        assertTrue( getMessage( "expectedSystemErrorContaining", needle ),
257                    result.getSystemError().contains( needle ) );
258
259    }
260
261    /**
262     * Tests an Ant execution to have written a given text to the system error stream taking a reason.
263     *
264     * @param result The result to test.
265     * @param needle The text which should have been written to the system error stream.
266     * @param reason The reason describing why {@code needle} should have been written to the system error stream.
267     */
268    public static void assertSystemErrorContaining( final AntExecutionResult result, final CharSequence needle,
269                                                    final CharSequence reason )
270    {
271        assertNotNull( result );
272        assertNotNull( needle );
273        assertNotNull( reason );
274        assertNotNull( reason.toString(), result.getSystemError() );
275        assertTrue( reason.toString(), result.getSystemError().contains( needle ) );
276    }
277
278    /**
279     * Tests an Ant execution to have fired a {@code messageLogged} event holding a given message.
280     *
281     * @param result The result to test.
282     * @param message The message which should have been logged.
283     */
284    public static void assertMessageLogged( final AntExecutionResult result, final CharSequence message )
285    {
286        assertNotNull( result );
287        assertNotNull( message );
288
289        BuildEvent messageLoggedEvent = null;
290
291        for ( BuildEvent e : result.getMessageLoggedEvents() )
292        {
293            if ( message.equals( e.getMessage() ) )
294            {
295                messageLoggedEvent = e;
296                break;
297            }
298        }
299
300        assertNotNull( getMessage( "expectedMessageLogged", message ), messageLoggedEvent );
301    }
302
303    /**
304     * Tests an Ant execution to have fired a {@code messageLogged} event holding a given message taking a reason.
305     *
306     * @param result The result to test.
307     * @param message The message which should have been logged.
308     * @param reason The reason describing why {@code message} should have been logged.
309     */
310    public static void assertMessageLogged( final AntExecutionResult result, final CharSequence message,
311                                            final CharSequence reason )
312    {
313        assertNotNull( result );
314        assertNotNull( message );
315        assertNotNull( reason );
316
317        BuildEvent messageLoggedEvent = null;
318
319        for ( BuildEvent e : result.getMessageLoggedEvents() )
320        {
321            if ( message.equals( e.getMessage() ) )
322            {
323                messageLoggedEvent = e;
324                break;
325            }
326        }
327
328        assertNotNull( reason.toString(), messageLoggedEvent );
329    }
330
331    /**
332     * Tests an Ant execution to have fired a {@code messageLogged} event holding a given message with a given priority.
333     *
334     * @param result The result to test.
335     * @param message The message which should have been logged.
336     * @param priority The priority the message should have been logged with.
337     */
338    public static void assertMessageLogged( final AntExecutionResult result, final CharSequence message,
339                                            final int priority )
340    {
341        assertNotNull( result );
342        assertNotNull( message );
343
344        BuildEvent messageLoggedEvent = null;
345
346        for ( BuildEvent e : result.getMessageLoggedEvents() )
347        {
348            if ( message.equals( e.getMessage() ) )
349            {
350                messageLoggedEvent = e;
351                break;
352            }
353        }
354
355        assertNotNull( getMessage( "expectedMessageLogged", message ), messageLoggedEvent );
356        assertEquals( getMessage( "unexpectedMessagePriority", messageLoggedEvent.getPriority(), priority ), priority,
357                      messageLoggedEvent.getPriority() );
358
359    }
360
361    /**
362     * Tests an Ant execution to have fired a {@code messageLogged} event holding a given message with a given priority
363     * taking a reason.
364     *
365     * @param result The result to test.
366     * @param message The message which should have been logged.
367     * @param priority The priority the message should have been logged with.
368     * @param reason The reason describing why {@code message} should have been logged.
369     */
370    public static void assertMessageLogged( final AntExecutionResult result, final CharSequence message,
371                                            final int priority, final CharSequence reason )
372    {
373        assertNotNull( result );
374        assertNotNull( message );
375        assertNotNull( reason );
376
377        BuildEvent messageLoggedEvent = null;
378
379        for ( BuildEvent e : result.getMessageLoggedEvents() )
380        {
381            if ( message.equals( e.getMessage() ) )
382            {
383                messageLoggedEvent = e;
384                break;
385            }
386        }
387
388        assertNotNull( reason.toString(), messageLoggedEvent );
389        assertEquals( reason.toString(), priority, messageLoggedEvent.getPriority() );
390    }
391
392    /**
393     * Tests an Ant execution to have fired a {@code messageLogged} event holding a message containing a given text.
394     *
395     * @param result The result to test.
396     * @param needle The text contained in a message which should have been logged.
397     */
398    public static void assertMessageLoggedContaining( final AntExecutionResult result, final CharSequence needle )
399    {
400        assertNotNull( result );
401        assertNotNull( needle );
402
403        BuildEvent messageLoggedEvent = null;
404
405        for ( BuildEvent e : result.getMessageLoggedEvents() )
406        {
407            if ( e.getMessage() != null && e.getMessage().contains( needle ) )
408            {
409                messageLoggedEvent = e;
410                break;
411            }
412        }
413
414        assertNotNull( getMessage( "expectedMessageLoggedContaining", needle ), messageLoggedEvent );
415    }
416
417    /**
418     * Tests an Ant execution to have fired a {@code messageLogged} event holding a message containing a given text
419     * taking a reason.
420     *
421     * @param result The result to test.
422     * @param needle The text contained in a message which should have been logged.
423     * @param reason The reason describing why {@code needle} should have been logged.
424     */
425    public static void assertMessageLoggedContaining( final AntExecutionResult result, final CharSequence needle,
426                                                      final CharSequence reason )
427    {
428        assertNotNull( result );
429        assertNotNull( needle );
430        assertNotNull( reason );
431
432        BuildEvent messageLoggedEvent = null;
433
434        for ( BuildEvent e : result.getMessageLoggedEvents() )
435        {
436            if ( e.getMessage() != null && e.getMessage().contains( needle ) )
437            {
438                messageLoggedEvent = e;
439                break;
440            }
441        }
442
443        assertNotNull( reason.toString(), messageLoggedEvent );
444    }
445
446    /**
447     * Tests an Ant execution to have fired a {@code messageLogged} event holding a message containing a given text with
448     * a given priority.
449     *
450     * @param result The result to test.
451     * @param needle The text contained in a message which should have been logged.
452     * @param priority The priority the message should have been logged with.
453     */
454    public static void assertMessageLoggedContaining( final AntExecutionResult result, final CharSequence needle,
455                                                      final int priority )
456    {
457        assertNotNull( result );
458        assertNotNull( needle );
459
460        BuildEvent messageLoggedEvent = null;
461
462        for ( BuildEvent e : result.getMessageLoggedEvents() )
463        {
464            if ( e.getMessage() != null && e.getMessage().contains( needle ) )
465            {
466                messageLoggedEvent = e;
467                break;
468            }
469        }
470
471        assertNotNull( getMessage( "expectedMessageLoggedContaining", needle ), messageLoggedEvent );
472        assertEquals( getMessage( "unexpectedMessagePriority", messageLoggedEvent.getPriority(), priority ), priority,
473                      messageLoggedEvent.getPriority() );
474
475    }
476
477    /**
478     * Tests an Ant execution to have fired a {@code messageLogged} event holding a message containing a given text with
479     * a given priority taking a reason.
480     *
481     * @param result The result to test.
482     * @param needle The text contained in a message which should have been logged.
483     * @param priority The priority the message should have been logged with.
484     * @param reason The reason describing why {@code message} should have been logged.
485     */
486    public static void assertMessageLoggedContaining( final AntExecutionResult result, final CharSequence needle,
487                                                      final int priority, final CharSequence reason )
488    {
489        assertNotNull( result );
490        assertNotNull( needle );
491        assertNotNull( reason );
492
493        BuildEvent messageLoggedEvent = null;
494
495        for ( BuildEvent e : result.getMessageLoggedEvents() )
496        {
497            if ( e.getMessage() != null && e.getMessage().contains( needle ) )
498            {
499                messageLoggedEvent = e;
500                break;
501            }
502        }
503
504        assertNotNull( reason.toString(), messageLoggedEvent );
505        assertEquals( reason.toString(), priority, messageLoggedEvent.getPriority() );
506    }
507
508    /**
509     * Tests an Ant execution to not have fired a {@code messageLogged} event holding a given message.
510     *
511     * @param result The result to test.
512     * @param message The message which should not have been logged.
513     */
514    public static void assertMessageNotLogged( final AntExecutionResult result, final CharSequence message )
515    {
516        assertNotNull( result );
517        assertNotNull( message );
518
519        BuildEvent messageLoggedEvent = null;
520
521        for ( BuildEvent e : result.getMessageLoggedEvents() )
522        {
523            if ( message.equals( e.getMessage() ) )
524            {
525                messageLoggedEvent = e;
526                break;
527            }
528        }
529
530        assertNull( getMessage( "unexpectedMessageLogged", messageLoggedEvent != null
531                                                           ? messageLoggedEvent.getMessage() : null ),
532                    messageLoggedEvent );
533
534    }
535
536    /**
537     * Tests an Ant execution to not have fired a {@code messageLogged} event holding a given message taking a reason.
538     *
539     * @param result The result to test.
540     * @param message The message which should not have been logged.
541     * @param reason The reason describing why {@code message} should not have been logged.
542     */
543    public static void assertMessageNotLogged( final AntExecutionResult result, final CharSequence message,
544                                               final CharSequence reason )
545    {
546        assertNotNull( result );
547        assertNotNull( message );
548        assertNotNull( reason );
549
550        BuildEvent messageLoggedEvent = null;
551
552        for ( BuildEvent e : result.getMessageLoggedEvents() )
553        {
554            if ( message.equals( e.getMessage() ) )
555            {
556                messageLoggedEvent = e;
557                break;
558            }
559        }
560
561        assertNull( reason.toString(), messageLoggedEvent );
562    }
563
564    /**
565     * Tests an Ant execution to not have fired a {@code messageLogged} event holding a message containing a given text.
566     *
567     * @param result The result to test.
568     * @param needle The text contained in a message which should not have been logged.
569     */
570    public static void assertMessageNotLoggedContaining( final AntExecutionResult result, final CharSequence needle )
571    {
572        assertNotNull( result );
573        assertNotNull( needle );
574
575        BuildEvent messageLoggedEvent = null;
576
577        for ( BuildEvent e : result.getMessageLoggedEvents() )
578        {
579            if ( e.getMessage() != null && e.getMessage().contains( needle ) )
580            {
581                messageLoggedEvent = e;
582                break;
583            }
584        }
585
586        assertNull( getMessage( "unexpectedMessageLogged", messageLoggedEvent != null
587                                                           ? messageLoggedEvent.getMessage() : null ),
588                    messageLoggedEvent );
589
590    }
591
592    /**
593     * Tests an Ant execution to not have fired a {@code messageLogged} event holding a message containing a given text
594     * taking a reason.
595     *
596     * @param result The result to test.
597     * @param needle The text contained in a message which should not have been logged.
598     * @param reason The reason describing why {@code message} should not have been logged.
599     */
600    public static void assertMessageNotLoggedContaining( final AntExecutionResult result, final CharSequence needle,
601                                                         final CharSequence reason )
602    {
603        assertNotNull( result );
604        assertNotNull( needle );
605        assertNotNull( reason );
606
607        BuildEvent messageLoggedEvent = null;
608
609        for ( BuildEvent e : result.getMessageLoggedEvents() )
610        {
611            if ( e.getMessage() != null && e.getMessage().contains( needle ) )
612            {
613                messageLoggedEvent = e;
614                break;
615            }
616        }
617
618        assertNull( reason.toString(), messageLoggedEvent );
619    }
620
621    private static String getMessage( final String key, final Object... arguments )
622    {
623        return MessageFormat.format( ResourceBundle.getBundle( Assert.class.getName().replace( '.', '/' ) ).
624            getString( key ), arguments );
625
626    }
627
628}