Skip to content

Commit b0028a4

Browse files
8272853: improve JavadocTester.runTests
Reviewed-by: hannesw
1 parent e07fd39 commit b0028a4

File tree

2 files changed

+374
-11
lines changed

2 files changed

+374
-11
lines changed

test/langtools/jdk/javadoc/lib/javadoc/tester/JavadocTester.java

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.regex.Matcher;
5959
import java.util.regex.Pattern;
6060
import java.util.stream.Collectors;
61+
import java.util.stream.Stream;
6162
import javax.tools.StandardJavaFileManager;
6263

6364

@@ -266,39 +267,129 @@ void check(Path dir) {
266267

267268
/**
268269
* Run all methods annotated with @Test, followed by printSummary.
269-
* Typically called on a tester object in main()
270+
* The methods are invoked in the order found using getDeclaredMethods.
271+
* The arguments for the invocation are provided {@link #getTestArgs(Method)}.
272+
*
273+
* Typically called on a tester object in main().
270274
*
275+
* @throws IllegalArgumentException if any test method does not have a recognized signature
271276
* @throws Exception if any errors occurred
272277
*/
273278
public void runTests() throws Exception {
274-
runTests(m -> new Object[0]);
279+
runTests(this::getTestArgs);
275280
}
276281

277282
/**
278283
* Runs all methods annotated with @Test, followed by printSummary.
284+
* The methods are invoked in the order found using getDeclaredMethods.
285+
* The arguments for the invocation are provided by a given function.
286+
*
279287
* Typically called on a tester object in main()
280288
*
281289
* @param f a function which will be used to provide arguments to each
282290
* invoked method
283-
* @throws Exception if any errors occurred
291+
* @throws Exception if any errors occurred while executing a test method
284292
*/
285293
public void runTests(Function<Method, Object[]> f) throws Exception {
286-
for (Method m: getClass().getDeclaredMethods()) {
294+
for (Method m : getClass().getDeclaredMethods()) {
287295
Annotation a = m.getAnnotation(Test.class);
288296
if (a != null) {
289-
try {
290-
out.println("Running test " + m.getName());
291-
m.invoke(this, f.apply(m));
292-
} catch (InvocationTargetException e) {
293-
Throwable cause = e.getCause();
294-
throw (cause instanceof Exception) ? ((Exception) cause) : e;
295-
}
297+
runTest(m, f);
296298
out.println();
297299
}
298300
}
299301
printSummary();
300302
}
301303

304+
/**
305+
* Run the specified methods annotated with @Test, or all methods annotated
306+
* with @Test if none are specified, followed by printSummary.
307+
* The methods are invoked in the order given in the methodNames argument,
308+
* or the order returned by getDeclaredMethods if no names are provided.
309+
* The arguments for the invocation are provided {@link #getTestArgs(Method)}.
310+
*
311+
* Typically called on a tester object in main(String[] args), perhaps using
312+
* args as the list of method names.
313+
*
314+
* @throws IllegalStateException if any methods annotated with @Test are overloaded
315+
* @throws IllegalArgumentException if any of the method names does not name a suitable method
316+
* @throws NullPointerException if {@code methodNames} is {@code null}, or if any of the names are {@code null}
317+
* @throws Exception if any errors occurred while executing a test method
318+
*/
319+
public void runTests(String... methodNames) throws Exception {
320+
runTests(this::getTestArgs, methodNames);
321+
}
322+
323+
/**
324+
* Run the specified methods annotated with @Test, or all methods annotated
325+
* with @Test if non are specified, followed by printSummary.
326+
* The methods are invoked in the order given in the methodNames argument,
327+
* or the order returned by getDeclaredMethods if no names are provided.
328+
* The arguments for the invocation are provided {@link #getTestArgs(Method)}.
329+
*
330+
* Typically called on a tester object in main(String[] args), perhaps using
331+
* args as the list of method names.
332+
*
333+
* @throws IllegalStateException if any methods annotated with @Test are overloaded
334+
* @throws IllegalArgumentException if any of the method names does not name a suitable method
335+
* @throws NullPointerException if {@code methodNames} is {@code null}, or if any of the names are {@code null}
336+
* @throws Exception if any errors occurred while executing a test method
337+
*/
338+
public void runTests(Function<Method, Object[]> f, String... methodNames) throws Exception {
339+
if (methodNames.length == 0) {
340+
runTests(f);
341+
} else {
342+
Map<String, Method> testMethods = Stream.of(getClass().getDeclaredMethods())
343+
.filter(this::isTestMethod)
344+
.collect(Collectors.toMap(Method::getName, Function.identity(),
345+
(o, n) -> {
346+
throw new IllegalStateException("test method " + o.getName() + " is overloaded");
347+
}));
348+
349+
List<Method> list = new ArrayList<>();
350+
for (String mn : methodNames) {
351+
Method m = testMethods.get(mn);
352+
if (m == null) {
353+
throw new IllegalArgumentException("test method " + mn + " not found");
354+
}
355+
list.add(m);
356+
}
357+
358+
for (Method m : list) {
359+
runTest(m, f);
360+
}
361+
}
362+
}
363+
364+
protected boolean isTestMethod(Method m) {
365+
return m.getAnnotation(Test.class) != null;
366+
}
367+
368+
protected Object[] getTestArgs(Method m) throws IllegalArgumentException {
369+
Class<?>[] paramTypes = m.getParameterTypes();
370+
if (paramTypes.length == 0) {
371+
return new Object[] {};
372+
} else if (paramTypes.length == 1 && paramTypes[0] == Path.class) {
373+
return new Object[] { Path.of(m.getName())};
374+
} else {
375+
throw new IllegalArgumentException("unknown signature for method "
376+
+ m + Stream.of(paramTypes)
377+
.map(Class::toString)
378+
.collect(Collectors.joining(", ", "(", ")"))) ;
379+
}
380+
}
381+
382+
protected void runTest(Method m, Function<Method, Object[]> f) throws Exception {
383+
try {
384+
out.println("Running test " + m.getName());
385+
m.invoke(this, f.apply(m));
386+
} catch (InvocationTargetException e) {
387+
Throwable cause = e.getCause();
388+
throw (cause instanceof Exception) ? ((Exception) cause) : e;
389+
}
390+
391+
}
392+
302393
/**
303394
* Runs javadoc.
304395
* The output directory used by this call and the final exit code

0 commit comments

Comments
 (0)