View Javadoc

1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   */
38  package com.gargoylesoftware.base.testing;
39  
40  import java.util.Collection;
41  import java.util.Iterator;
42  import java.util.LinkedList;
43  import junit.framework.TestCase;
44  
45  /***
46   * An extension of junit.framework.TestCase that adds those methods that we really
47   * wish were part of JUnit.
48   *
49   * @version  $Revision: 1.5 $
50   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51   */
52  public class BaseTestCase extends TestCase {
53      /***
54       * Create an instance
55       * @param name The name of the test
56       */
57      public BaseTestCase( final String name ) {
58          super(name);
59      }
60  
61  
62      /***
63       * Convenience method to signal that this test hasn't been finished yet.  This
64       * will print the name of the test to System.out.
65       */
66      public void notImplemented() {
67          System.out.println( "Test not implemented yet [" + getClass().getName() + " : " + getName() + "]" );
68      }
69  
70  
71      /***
72       * Assert that the two collections are the same irrespective of order.
73       *
74       * @param  a The first collection
75       * @param  b The second collection
76       */
77      public void assertCollectionsEqual( final Collection a, final Collection b ) {
78          final Collection copyOfB = new LinkedList( b );
79          final Iterator iterator = a.iterator();
80          while( iterator.hasNext() ) {
81              final Object object = iterator.next();
82              if( copyOfB.contains( object ) == false ) {
83                  fail( "Expected: " + a + " but got: " + b );
84              }
85  
86              copyOfB.remove( object );
87          }
88          
89          if( copyOfB.isEmpty() == false ) {
90          		fail("Second collection has elements that aren't in the first collection: "+copyOfB);
91          }
92      }
93  
94  
95      /***
96       * Assert that the two objects are the same.  Junit has a method like this
97       * however it does not display what the two objects are.  This method will
98       * display the toString() representations of the two objects in the case that
99       * the assertion fails.
100      *
101      * @param description The failure message to use if the two objects are not the same.
102      * @param a The first object to compare.
103      * @param b The second object to compare.
104      */
105     public static void assertSame( final String description, final Object a, final Object b ) {
106         if( a != b ) {
107             fail( description + ": Objects not the same <" + a + "> and <" + b + ">" );
108         }
109     }
110 
111 
112     /***
113      * Assert that the two objects are the same.  Junit has a method like this
114      * however it does not display what the two objects are.  This method will
115      * display the toString() representations of the two objects in the case that
116      * the assertion fails.
117      *
118      * @param a The first object to compare.
119      * @param b The second object to compare.
120      */
121     public static void assertSame( final Object a, final Object b ) {
122         if( a != b ) {
123             fail( "Objects not the same <" + a + "> and <" + b + ">" );
124         }
125     }
126 
127 
128     /***
129      * Assert that the specified condition is false.  Older versions of junit have assertTrue()
130      * but not assertFalse so we add it here to be sure that it is present.
131      *
132      * @param description The failure message to be used if the condition is not false.
133      * @param condition The value to check.
134      */
135     public static void assertFalse( final String description, final boolean condition ) {
136         if( condition == true ) {
137             fail( description + ": Expected false" );
138         }
139     }
140 
141 
142     /***
143      * Assert that the specified condition is false.  Older versions of junit have assertTrue()
144      * but not assertFalse so we add it here to be sure that it is present.
145      *
146      * @param condition The value to check.
147      */
148     public static void assertFalse( final boolean condition ) {
149         if( condition == true ) {
150             fail( "Expected false" );
151         }
152     }
153 
154 
155     /***
156      * Assert that the specified object is an instance of this class
157      *
158      * @param label A description of the test
159      * @param object The object to test
160      * @param clazz The class
161      */
162     public void assertInstanceOf( final String label, final Object object, final Class clazz ) {
163         if( clazz.isAssignableFrom( object.getClass() ) == false ) {
164             fail( label + ": object [" + object + "] is not an instance of class ["
165                 + clazz.getName() + "]" );
166         }
167     }
168 
169 
170     /***
171      * Assert that the specified object is an instance of this class
172      *
173      * @param object The object to test
174      * @param clazz The class
175      */
176     public void assertInstanceOf( final Object object, final Class clazz ) {
177         if( clazz.isAssignableFrom( object.getClass() ) == false ) {
178             fail("object ["+object+"] is not an instance of class ["+clazz.getName()+"]" );
179         }
180     }
181 }