use of java8.util.stream.Collectors.toSet in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testMinimalCompletionStage_minimality.
/**
* Minimal completion stages throw UOE for most non-CompletionStage methods
*/
public void testMinimalCompletionStage_minimality() {
if (!testImplementationDetails)
return;
Function<Method, String> toSignature = method -> method.getName() + Arrays.toString(method.getParameterTypes());
Predicate<Method> isNotStatic = method -> (method.getModifiers() & Modifier.STATIC) == 0;
List<Method> minimalMethods = RefStreams.of(Object.class, CompletionStage.class).flatMap(klazz -> RefStreams.of(klazz.getMethods())).filter(isNotStatic).collect(Collectors.toList());
// Methods from CompletableFuture permitted NOT to throw UOE
String[] signatureWhitelist = { "newIncompleteFuture[]", "defaultExecutor[]", "minimalCompletionStage[]", "copy[]" };
Set<String> permittedMethodSignatures = RefStreams.concat(StreamSupport.stream(minimalMethods).map(toSignature), RefStreams.of(signatureWhitelist)).collect(Collectors.toSet());
List<Method> allMethods = RefStreams.of(CompletableFuture.class.getMethods()).filter(isNotStatic).filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))).collect(Collectors.toList());
List<CompletionStage<Integer>> stages = new ArrayList<CompletionStage<Integer>>();
CompletionStage<Integer> min = new CompletableFuture<Integer>().minimalCompletionStage();
stages.add(min);
stages.add(min.thenApply(x -> x));
stages.add(CompletableFuture.completedStage(1));
stages.add(CompletableFuture.failedStage(new CFException()));
List<Method> bugs = new ArrayList<>();
for (Method method : allMethods) {
Class<?>[] parameterTypes = method.getParameterTypes();
Object[] args = new Object[parameterTypes.length];
// Manufacture boxed primitives for primitive params
for (int i = 0; i < args.length; i++) {
Class<?> type = parameterTypes[i];
if (type == boolean.class)
args[i] = false;
else if (type == int.class)
args[i] = 0;
else if (type == long.class)
args[i] = 0L;
}
for (CompletionStage<Integer> stage : stages) {
try {
method.invoke(stage, args);
bugs.add(method);
} catch (java.lang.reflect.InvocationTargetException expected) {
if (!(expected.getCause() instanceof UnsupportedOperationException)) {
bugs.add(method);
// expected.getCause().printStackTrace();
}
} catch (Exception bad) {
throw new Error(bad);
}
}
}
if (!bugs.isEmpty())
throw new Error("Methods did not throw UOE: " + bugs);
}
Aggregations