1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
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
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
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