下面列出了怎么用org.junit.platform.commons.util.Preconditions的API类实例代码及写法,或者点击链接到github查看源代码。
private ConditionEvaluationResult map(EnabledIfProperty annotation) {
final String name = annotation.named().trim();
final String regex = annotation.matches();
Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation);
Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation);
return ConfigProviderResolver.instance().getConfig().getOptionalValue(name, String.class)
.map(actual -> {
return actual.matches(regex)
? enabled(
format("Config property [%s] with value [%s] matches regular expression [%s]",
name, actual, regex))
: disabled(
format("Config property [%s] with value [%s] does not match regular expression [%s]",
name, actual, regex));
})
.orElseGet(() -> {
return disabled(
format("Config property [%s] does not exist", name));
});
}
private static List<Field> findAllFieldsInHierarchy(Class<?> clazz) {
Preconditions.notNull(clazz, "Class must not be null");
List<Field> localFields = getDeclaredFields(clazz).stream().
filter((field) -> !field.isSynthetic())
.collect(Collectors.toList());
List<Field> superclassFields = getSuperclassFields(clazz).stream()
.filter((field) -> !isMethodShadowedByLocalFields(field, localFields))
.collect(Collectors.toList());
List<Field> methods = new ArrayList<>();
methods.addAll(superclassFields);
methods.addAll(localFields);
return methods;
}
private static StoreAdapter getContainerInstance(final Object testInstance, final Field field) {
try {
field.setAccessible(true);
Startable containerInstance = Preconditions.notNull((Startable) field.get(testInstance), "Container " + field.getName() + " needs to be initialized");
return new StoreAdapter(field.getDeclaringClass(), field.getName(), containerInstance);
} catch (IllegalAccessException e) {
throw new ExtensionConfigurationException("Can not access container defined in field " + field.getName());
}
}
private String readFile(String file, ExtensionContext context) {
Preconditions.notBlank(file, "Classpath resource [" + file + "] must not be null or blank");
try {
Class<?> testClass = context.getRequiredTestClass();
return FileUtils.readFileToString(new File(testClass.getResource(file).getFile()), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private InputStream openInputStream(ExtensionContext context, String resource) {
Class<?> testClass = context.getRequiredTestClass();
InputStream inputStream = inputStreamProvider.apply(testClass, resource);
return Preconditions.notNull(inputStream,
() -> "Classpath resource does not exist: " + resource);
}