Fix datasource resource closing tests (#1)

This commit is contained in:
ljacqu 2016-05-12 20:15:44 +02:00 committed by games647
parent 5c850e46c4
commit 9f5b995217

View File

@ -32,6 +32,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -192,8 +193,8 @@ public abstract class AbstractResourceClosingTest {
for (Class<?> paramType : method.getParameterTypes()) {
// Checking List.class == paramType instead of Class#isAssignableFrom means we really only accept List,
// but that is a sensible assumption and makes our life much easier later on when juggling with Type
Object param = (List.class == paramType)
? getTypedList(method.getGenericParameterTypes()[index])
Object param = Collection.class.isAssignableFrom(paramType)
? getTypedCollection(method.getGenericParameterTypes()[index])
: PARAM_VALUES.get(paramType);
Preconditions.checkNotNull(param, "No param type for " + paramType);
params.add(param);
@ -208,15 +209,21 @@ public abstract class AbstractResourceClosingTest {
* @param type The list type to process and build a test list for
* @return Test list with sample elements of the correct type
*/
private static List<?> getTypedList(Type type) {
private static Collection<?> getTypedCollection(Type type) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Preconditions.checkArgument(List.class == parameterizedType.getRawType(), type + " should be a List");
Preconditions.checkArgument(Collection.class.isAssignableFrom((Class<?>) parameterizedType.getRawType()),
type + " should extend from Collection");
Type genericType = parameterizedType.getActualTypeArguments()[0];
Object element = PARAM_VALUES.get(genericType);
Preconditions.checkNotNull(element, "No sample element for list of generic type " + genericType);
return Arrays.asList(element, element, element);
if (List.class == parameterizedType.getRawType()) {
return Arrays.asList(element, element, element);
} else if (Set.class == parameterizedType.getRawType()) {
return new HashSet<>(Arrays.asList(element, element, element));
}
throw new IllegalStateException("Unknown collection type " + parameterizedType.getRawType());
}
throw new IllegalStateException("Cannot build list for unexpected Type: " + type);
}