private static void validatePatterns(EnumSource enumSource, Set<String> names) { try { names.forEach(Pattern::compile); } catch (PatternSyntaxException e) { throw new PreconditionViolationException( "Pattern compilation failed for a regular expression supplied in " + enumSource, e); } }
@Override public Stream<? extends Arguments> provideArguments(ExtensionContext context) { Method testMethod = context.getRequiredTestMethod(); Class<?>[] parameterTypes = testMethod.getParameterTypes(); Preconditions.condition(parameterTypes.length > 0, () -> String.format( "@EmptySource cannot provide an empty argument to method [%s]: the method does not declare any formal parameters.", testMethod.toGenericString())); Class<?> parameterType = parameterTypes[0]; if (String.class.equals(parameterType)) { return Stream.of(arguments("")); } if (List.class.equals(parameterType)) { return Stream.of(arguments(Collections.emptyList())); } if (Set.class.equals(parameterType)) { return Stream.of(arguments(Collections.emptySet())); } if (Map.class.equals(parameterType)) { return Stream.of(arguments(Collections.emptyMap())); } if (parameterType.isArray()) { Object array = Array.newInstance(parameterType.getComponentType(), 0); return Stream.of(arguments(array)); } // else throw new PreconditionViolationException( String.format("@EmptySource cannot provide an empty argument to method [%s]: [%s] is not a supported type.", testMethod.toGenericString(), parameterType.getName())); }
@Override public void accept(CsvFileSource annotation) { this.annotation = annotation; resources = annotation.resources(); try { this.charset = Charset.forName(annotation.encoding()); } catch (Exception ex) { throw new PreconditionViolationException("The charset supplied in " + this.annotation + " is invalid", ex); } numLinesToSkip = annotation.numLinesToSkip(); settings = new CsvParserSettings(); // Do not use the built-in support for skipping rows/lines since it will // throw an IllegalArgumentException if the file does not contain at least // the number of specified lines to skip. // settings.setNumberOfRowsToSkip(annotation.numLinesToSkip()); settings.getFormat().setDelimiter(annotation.delimiter()); settings.getFormat().setLineSeparator(annotation.lineSeparator()); settings.getFormat().setQuote('"'); settings.getFormat().setQuoteEscape('"'); settings.setEmptyValue(""); settings.setAutoConfigurationEnabled(false); }
public static TestRuleAnnotatedMember from(Object testInstance, Member member) { if (member instanceof Method) { return new TestRuleAnnotatedMethod(testInstance, (Method) member); } else if (member instanceof Field) { return new TestRuleAnnotatedField(testInstance, (Field) member); } throw new PreconditionViolationException( String.format("Unsupported Member type [%s] for TestRule. Member must be of type %s or %s", member, Method.class.getName(), Field.class.getName())); }
private void lazyLoadJavaMethod() { lazyLoadJavaClass(); if (this.javaMethod == null) { if (StringUtils.isNotBlank(this.methodParameterTypes)) { this.javaMethod = ReflectionUtils.findMethod(this.javaClass, this.methodName, this.methodParameterTypes).orElseThrow( () -> new PreconditionViolationException(String.format( "Could not find method with name [%s] and parameter types [%s] in class [%s].", this.methodName, this.methodParameterTypes, this.javaClass.getName()))); } else { this.javaMethod = ReflectionUtils.findMethod(this.javaClass, this.methodName).orElseThrow( () -> new PreconditionViolationException( String.format("Could not find method with name [%s] in class [%s].", this.methodName, this.javaClass.getName()))); } } }
private void lazyLoadJavaClass() { if (this.javaClass == null) { // @formatter:off this.javaClass = ReflectionUtils.tryToLoadClass(this.className).getOrThrow( cause -> new PreconditionViolationException("Could not load class with name: " + this.className, cause)); // @formatter:on } }
/** * Create a {@code UriSelector} for the supplied URI. * * @param uri the URI to select; never {@code null} or blank * @see UriSelector * @see #selectUri(URI) * @see #selectFile(String) * @see #selectFile(File) * @see #selectDirectory(String) * @see #selectDirectory(File) */ public static UriSelector selectUri(String uri) { Preconditions.notBlank(uri, "URI must not be null or blank"); try { return new UriSelector(new URI(uri)); } catch (URISyntaxException ex) { throw new PreconditionViolationException("Failed to create a java.net.URI from: " + uri, ex); } }
/** * Get the selected {@link Class}. * * <p>If the {@link Class} was not provided, but only the name, this method * attempts to lazily load the {@link Class} based on its name and throws a * {@link PreconditionViolationException} if the class cannot be loaded. */ public Class<?> getJavaClass() { if (this.javaClass == null) { this.javaClass = ReflectionUtils.tryToLoadClass(this.className).getOrThrow( cause -> new PreconditionViolationException("Could not load class with name: " + this.className, cause)); } return this.javaClass; }
/** * Get the {@linkplain Class Java class} of this source. * * <p>If the {@link Class} was not provided, but only the name, this method * attempts to lazily load the {@link Class} based on its name and throws a * {@link PreconditionViolationException} if the class cannot be loaded. * * @see #getClassName() * @see #getPosition() */ public final Class<?> getJavaClass() { if (this.javaClass == null) { this.javaClass = ReflectionUtils.tryToLoadClass(this.className).getOrThrow( cause -> new PreconditionViolationException("Could not load class with name: " + this.className, cause)); } return this.javaClass; }
/** * Create a {@code FileSelector} for the supplied {@linkplain File file}. * * <p>This method selects the file in its {@linkplain File#getCanonicalPath() * canonical} form and throws a {@link PreconditionViolationException} if the * file does not exist. * * @param file the file to select; never {@code null} * @see FileSelector * @see #selectFile(String) * @see #selectDirectory(String) * @see #selectDirectory(File) */ public static FileSelector selectFile(File file) { Preconditions.notNull(file, "File must not be null"); Preconditions.condition(file.isFile(), () -> String.format("The supplied java.io.File [%s] must represent an existing file", file)); try { return new FileSelector(file.getCanonicalPath()); } catch (IOException ex) { throw new PreconditionViolationException("Failed to retrieve canonical path for file: " + file, ex); } }
/** * Create a {@code DirectorySelector} for the supplied {@linkplain File directory}. * * <p>This method selects the directory in its {@linkplain File#getCanonicalPath() * canonical} form and throws a {@link PreconditionViolationException} if the * directory does not exist. * * @param directory the directory to select; never {@code null} * @see DirectorySelector * @see #selectDirectory(String) * @see #selectFile(String) * @see #selectFile(File) */ public static DirectorySelector selectDirectory(File directory) { Preconditions.notNull(directory, "Directory must not be null"); Preconditions.condition(directory.isDirectory(), () -> String.format("The supplied java.io.File [%s] must represent an existing directory", directory)); try { return new DirectorySelector(directory.getCanonicalPath()); } catch (IOException ex) { throw new PreconditionViolationException("Failed to retrieve canonical path for directory: " + directory, ex); } }
@Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { if (testIdentifier.isTest()) { getMethodStats(testIdentifier).stopTime = clock.currentTimeMillis(); switch (testExecutionResult.getStatus()) { case SUCCESSFUL: break; case ABORTED: { break; } case FAILED: { eventsCollected.add(createEventFrom(testIdentifier, testExecutionResult)); break; } default: throw new PreconditionViolationException( "Unsupported execution status:" + testExecutionResult.getStatus()); } } }