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 junit.framework.TestCase;
41  
42  /***
43   *  Tests for OrderedTestSuite
44   *
45   * @version  $Revision: 1.5 $
46   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
47   */
48  public class OrderedTestSuiteTest extends TestCase {
49      /***
50       *  Create an instance
51       *
52       * @param  name Name of the test
53       */
54      public OrderedTestSuiteTest( final String name ) {
55          super( name );
56      }
57  
58  
59      /***
60       *  Try passing a null class into the constructor
61       */
62      public void testConstructor_NullClass() {
63          try {
64              new OrderedTestSuite( null, new String[0] );
65              fail( "Expected exception" );
66          }
67          catch( final NullPointerException e ) {
68              // Expected path
69          }
70      }
71  
72  
73      /***
74       *  Try passing a null method list into the constructor
75       */
76      public void testConstructor_NullMethodList() {
77          try {
78              new OrderedTestSuite( MyTestCase.class, null );
79              fail( "Expected exception" );
80          }
81          catch( final NullPointerException e ) {
82              // Expected path
83          }
84      }
85  
86  
87      /***
88       *  Pass in a class that is not a subclass of TestCase
89       */
90      public void testConstructor_NonTestCase() {
91          try {
92              new OrderedTestSuite( String.class, new String[0] );
93              fail( "Expected exception" );
94          }
95          catch( final IllegalArgumentException e ) {
96              // Expected path
97          }
98      }
99  
100 
101     /***
102      *  Test constructor - good path
103      */
104     public void testConstructor() {
105         final OrderedTestSuite suite = new OrderedTestSuite( MyTestCase.class, new String[0] );
106         assertEquals( 3, suite.testCount() );
107     }
108 
109 
110     /***
111      *  Test that the ordering is correct
112      */
113     public void testOrdering() {
114 
115         doTestOrdering( new String[]{"testThree", "testTwo", "testOne"} );
116         doTestOrdering( new String[]{"testOne", "testTwo", "testThree"} );
117     }
118 
119 
120     /***
121      *  Test order with one method that doesn't start with "test"
122      */
123     public void testOrdering_NonTest() {
124         doTestOrdering( new String[]{"testThree", "testTwo", "doMyTest", "testOne"} );
125     }
126 
127 
128     /***
129      *  Test ordering with one bad method name
130      */
131     public void testOrdering_BadName() {
132         try {
133             doTestOrdering( new String[]{"testThree", "testTwo", "xx", "testOne"} );
134             fail( "Expected exception" );
135         }
136         catch( final TestInitializationFailedException e ) {
137             // expected path
138         }
139     }
140 
141 
142     private void doTestOrdering( final String methodNames[] ) {
143         final OrderedTestSuite suite = new OrderedTestSuite( MyTestCase.class, methodNames );
144         assertEquals( methodNames.length, suite.testCount() );
145 
146         int i;
147         for( i = 0; i < methodNames.length; i++ ) {
148             assertEquals( methodNames[i], ( (TestCase)suite.testAt( i ) ).getName() );
149         }
150     }
151 
152 
153     /***
154      *  Fake TestCase class for testing
155      *
156      * @version  $Revision: 1.5 $
157      */
158     public static class MyTestCase extends TestCase {
159         /***
160          *  Instantiate one
161          *
162          * @param  name Name of the test
163          */
164         public MyTestCase( final String name ) {
165             super( name );
166         }
167 
168 
169         /***
170          *  Test method
171          */
172         public void doMyTest() {
173         }
174 
175 
176         /***
177          *  Test method
178          */
179         public void testOne() {
180         }
181 
182 
183         /***
184          *  Test method
185          */
186         public void testTwo() {
187         }
188 
189 
190         /***
191          *  Test method
192          */
193         public void testThree() {
194         }
195 
196 
197         /***
198          *  Test method
199          *
200          * @param  arg dummy arg
201          */
202         public void testWithArg( final Object arg ) {
203         }
204 
205 
206         /***
207          *  Test method
208          *
209          * @return  true
210          */
211         public boolean testThatReturns() {
212             return true;
213         }
214 
215 
216         /***
217          *  Test method
218          */
219         private void testPrivate() {
220         }
221     }
222 
223 }
224