Fix datasource resource closing tests (#1)
This commit is contained in:
parent
5c850e46c4
commit
9f5b995217
@ -32,6 +32,7 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -192,8 +193,8 @@ public abstract class AbstractResourceClosingTest {
|
|||||||
for (Class<?> paramType : method.getParameterTypes()) {
|
for (Class<?> paramType : method.getParameterTypes()) {
|
||||||
// Checking List.class == paramType instead of Class#isAssignableFrom means we really only accept List,
|
// 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
|
// but that is a sensible assumption and makes our life much easier later on when juggling with Type
|
||||||
Object param = (List.class == paramType)
|
Object param = Collection.class.isAssignableFrom(paramType)
|
||||||
? getTypedList(method.getGenericParameterTypes()[index])
|
? getTypedCollection(method.getGenericParameterTypes()[index])
|
||||||
: PARAM_VALUES.get(paramType);
|
: PARAM_VALUES.get(paramType);
|
||||||
Preconditions.checkNotNull(param, "No param type for " + paramType);
|
Preconditions.checkNotNull(param, "No param type for " + paramType);
|
||||||
params.add(param);
|
params.add(param);
|
||||||
@ -208,15 +209,21 @@ public abstract class AbstractResourceClosingTest {
|
|||||||
* @param type The list type to process and build a test list for
|
* @param type The list type to process and build a test list for
|
||||||
* @return Test list with sample elements of the correct type
|
* @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) {
|
if (type instanceof ParameterizedType) {
|
||||||
ParameterizedType parameterizedType = (ParameterizedType) type;
|
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];
|
Type genericType = parameterizedType.getActualTypeArguments()[0];
|
||||||
|
|
||||||
Object element = PARAM_VALUES.get(genericType);
|
Object element = PARAM_VALUES.get(genericType);
|
||||||
Preconditions.checkNotNull(element, "No sample element for list of generic type " + 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);
|
throw new IllegalStateException("Cannot build list for unexpected Type: " + type);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user