8
8
import org .junit .internal .requests .SortingRequest ;
9
9
import org .junit .internal .runners .ErrorReportingRunner ;
10
10
import org .junit .runner .manipulation .Filter ;
11
+ import org .junit .runner .manipulation .InvalidOrderingException ;
12
+ import org .junit .runner .manipulation .Ordering ;
11
13
import org .junit .runners .model .InitializationError ;
12
14
13
15
/**
@@ -148,15 +150,15 @@ public Request filterWith(final Description desiredDescription) {
148
150
* For example, here is code to run a test suite in alphabetical order:
149
151
* <pre>
150
152
* private static Comparator<Description> forward() {
151
- * return new Comparator<Description>() {
152
- * public int compare(Description o1, Description o2) {
153
- * return o1.getDisplayName().compareTo(o2.getDisplayName());
154
- * }
155
- * };
153
+ * return new Comparator<Description>() {
154
+ * public int compare(Description o1, Description o2) {
155
+ * return o1.getDisplayName().compareTo(o2.getDisplayName());
156
+ * }
157
+ * };
156
158
* }
157
159
*
158
160
* public static main() {
159
- * new JUnitCore().run(Request.aClass(AllTests.class).sortWith(forward()));
161
+ * new JUnitCore().run(Request.aClass(AllTests.class).sortWith(forward()));
160
162
* }
161
163
* </pre>
162
164
*
@@ -166,4 +168,44 @@ public Request filterWith(final Description desiredDescription) {
166
168
public Request sortWith (Comparator <Description > comparator ) {
167
169
return new SortingRequest (this , comparator );
168
170
}
171
+
172
+ /**
173
+ * Returns a Request whose Tests can be run in a certain order, defined by
174
+ * <code>ordering</code>
175
+ * <p>
176
+ * For example, here is code to run a test suite in reverse order:
177
+ * <pre>
178
+ * private static Ordering reverse() {
179
+ * return new Ordering() {
180
+ * public List<Description> order(Collection<Description> siblings) {
181
+ * List<Description> ordered = new ArrayList<>(siblings);
182
+ * Collections.reverse(ordered);
183
+ * return ordered;
184
+ * }
185
+ * }
186
+ * }
187
+ *
188
+ * public static main() {
189
+ * new JUnitCore().run(Request.aClass(AllTests.class).orderWith(reverse()));
190
+ * }
191
+ * </pre>
192
+ *
193
+ * @return a Request with ordered Tests
194
+ * @since 4.13
195
+ */
196
+ public Request orderWith (final Ordering ordering ) {
197
+ final Request delegate = this ;
198
+ return new Request () {
199
+ @ Override
200
+ public Runner getRunner () {
201
+ try {
202
+ Runner runner = delegate .getRunner ();
203
+ ordering .apply (runner );
204
+ return runner ;
205
+ } catch (InvalidOrderingException e ) {
206
+ return new ErrorReportingRunner (ordering .getClass (), e );
207
+ }
208
+ }
209
+ };
210
+ }
169
211
}
0 commit comments