private static Predicate<? super Throwable> createAbortedExecutionPredicate() { Predicate<Throwable> otaPredicate = TestAbortedException.class::isInstance; // Additionally support JUnit 4's AssumptionViolatedException? Class<?> clazz = ReflectionUtils.tryToLoadClass( "org.junit.internal.AssumptionViolatedException").toOptional().orElse(null); if (clazz != null) { return otaPredicate.or(clazz::isInstance); } // Else just OTA's TestAbortedException return otaPredicate; }
private static Class<?> toClass(String type) { //@formatter:off return ReflectionUtils .tryToLoadClass(type) .getOrThrow(cause -> new ArgumentConversionException( "Failed to convert String \"" + type + "\" to type " + Class.class.getName(), cause)); //@formatter:on }
private Class<?> loadRequiredClass(String className) { return ReflectionUtils.tryToLoadClass(className).getOrThrow( cause -> new JUnitException(format("Could not load class [%s]", className), cause)); }
private Optional<Class<?>> loadTestClass(String className, UniqueId uniqueId) { // @formatter:off return ReflectionUtils.tryToLoadClass(className) .ifFailure(cause -> logger.warn(cause, () -> format("Unresolvable Unique ID (%s): Unknown class %s", uniqueId, className))) .toOptional(); // @formatter:on }
@Override public Optional<TestDescriptor> resolveUniqueId(UniqueId.Segment segment, TestDescriptor parent) { if (!segment.getType().equals(getSegmentType())) { return Optional.empty(); } if (!requiredParentType().isInstance(parent)) { return Optional.empty(); } String className = getClassName(parent, segment.getValue()); Optional<Class<?>> optionalContainerClass = ReflectionUtils.tryToLoadClass(className).toOptional(); if (!optionalContainerClass.isPresent()) { return Optional.empty(); } Class<?> containerClass = optionalContainerClass.get(); if (!isPotentialCandidate(containerClass)) { return Optional.empty(); } UniqueId uniqueId = createUniqueId(containerClass, parent); return Optional.of(resolveClass(containerClass, uniqueId)); }
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 } }
/** * 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; }
@Override public ParallelExecutionConfiguration createConfiguration(ConfigurationParameters configurationParameters) { String className = configurationParameters.get(CONFIG_CUSTOM_CLASS_PROPERTY_NAME).orElseThrow( () -> new JUnitException(CONFIG_CUSTOM_CLASS_PROPERTY_NAME + " must be set")); return ReflectionUtils.tryToLoadClass(className) // .andThenTry(strategyClass -> { Preconditions.condition( ParallelExecutionConfigurationStrategy.class.isAssignableFrom(strategyClass), CONFIG_CUSTOM_CLASS_PROPERTY_NAME + " does not implement " + ParallelExecutionConfigurationStrategy.class); return (ParallelExecutionConfigurationStrategy) ReflectionUtils.newInstance(strategyClass); }) // .andThenTry(strategy -> strategy.createConfiguration(configurationParameters)) // .getOrThrow(cause -> new JUnitException( "Could not create configuration for strategy class: " + className, cause)); } };