static String getCanonicalName(Class<?> clazz) { try { String canonicalName = clazz.getCanonicalName(); return (canonicalName != null ? canonicalName : clazz.getName()); } catch (Throwable t) { BlacklistedExceptions.rethrowIfBlacklisted(t); return clazz.getName(); } }
static void handleCsvException(Throwable throwable, Annotation annotation) { BlacklistedExceptions.rethrowIfBlacklisted(throwable); if (throwable instanceof PreconditionViolationException) { throw (PreconditionViolationException) throwable; } throw new CsvParsingException("Failed to parse CSV input configured via " + annotation, throwable); }
private static String readVersion(Supplier<String> versionSupplier) { try { return versionSupplier.get(); } catch (Throwable t) { BlacklistedExceptions.rethrowIfBlacklisted(t); throw new JUnitException("Failed to read version of junit:junit", t); } }
private static void assertDoesNotThrow(Executable executable, Object messageOrSupplier) { try { executable.execute(); } catch (Throwable t) { BlacklistedExceptions.rethrowIfBlacklisted(t); throw createAssertionFailedError(messageOrSupplier, t); } }
private static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, Object messageOrSupplier) { try { return supplier.get(); } catch (Throwable t) { BlacklistedExceptions.rethrowIfBlacklisted(t); throw createAssertionFailedError(messageOrSupplier, t); } }
static Evaluator forName(String nameOfScriptEngine, String name) { // Assert that precondition name is loadable via basic reflection. try { Class.forName(nameOfScriptEngine); } catch (Throwable cause) { BlacklistedExceptions.rethrowIfBlacklisted(cause); String message = "Class `" + nameOfScriptEngine + "` is not loadable, " // + "script-based test execution is disabled. " // + "If the originating cause is a `NoClassDefFoundError: javax/script/...` and " // + "the underlying runtime environment is executed with an activated module system " // + "(aka Jigsaw or JPMS) you need to add the `java.scripting` module to the " // + "root modules via `--add-modules ...,java.scripting`"; return new ThrowingEvaluator(message, cause); } // Now create the evaluator instance specified by its class name. try { return (Evaluator) Class.forName(name).getDeclaredConstructor().newInstance(); } catch (ReflectiveOperationException cause) { String message = "Creating instance of class `" + name + "` failed," // + "script-based test execution is disabled."; return new ThrowingEvaluator(message, cause); } }
private void invokeTestExecutionExceptionHandlers(Throwable ex, List<TestExecutionExceptionHandler> handlers, ExtensionContext context) { // No handlers left? if (handlers.isEmpty()) { ExceptionUtils.throwAsUncheckedException(ex); } try { // Invoke next available handler handlers.remove(0).handleTestExecutionException(context, ex); } catch (Throwable t) { BlacklistedExceptions.rethrowIfBlacklisted(t); invokeTestExecutionExceptionHandlers(t, handlers, context); } }
static void assertAll(String heading, Stream<Executable> executables) { Preconditions.notNull(executables, "executables stream must not be null"); List<Throwable> failures = executables // .peek(executable -> Preconditions.notNull(executable, "individual executables must not be null"))// .map(executable -> { try { executable.execute(); return null; } catch (Throwable t) { BlacklistedExceptions.rethrowIfBlacklisted(t); return t; } }) // .filter(Objects::nonNull) // .collect(Collectors.toList()); if (!failures.isEmpty()) { MultipleFailuresError multipleFailuresError = new MultipleFailuresError(heading, failures); failures.forEach(multipleFailuresError::addSuppressed); throw multipleFailuresError; } }
private void resolveClass(Class<?> testClass) { try { Set<TestDescriptor> resolvedDescriptors = resolveContainerWithParents(testClass); resolvedDescriptors.forEach(this::resolveChildren); if (resolvedDescriptors.isEmpty()) { logger.debug(() -> format("Class '%s' could not be resolved.", nullSafeToString(testClass))); } } catch (Throwable t) { rethrowIfBlacklisted(t); logger.debug(t, () -> format("Class '%s' could not be resolved.", nullSafeToString(testClass))); } }
void resolveModule(ModuleSelector selector) { try { findAllClassesInModule(selector.getModuleName(), this.classFilter).forEach(this::resolveClass); } catch (Throwable t) { rethrowIfBlacklisted(t); logger.debug(t, () -> format("Failed to resolve classes in module '%s'.", selector.getModuleName())); } }
void resolveClasspathRoot(ClasspathRootSelector selector) { try { findAllClassesInClasspathRoot(selector.getClasspathRoot(), this.classFilter).forEach(this::resolveClass); } catch (Throwable t) { rethrowIfBlacklisted(t); logger.debug(t, () -> format("Failed to resolve classes in classpath root '%s'.", selector.getClasspathRoot())); } }
void resolvePackage(PackageSelector selector) { try { findAllClassesInPackage(selector.getPackageName(), this.classFilter).forEach(this::resolveClass); } catch (Throwable t) { rethrowIfBlacklisted(t); logger.debug(t, () -> format("Failed to resolve classes in package '%s'.", selector.getPackageName())); } }
BlacklistedExceptions.rethrowIfBlacklisted(throwable);
void resolveClass(ClassSelector selector) { // Even though resolveClass(Class<?>) has its own similar try-catch block, the // try-catch block is necessary here as well since ClassSelector#getJavaClass() // may throw an exception. try { resolveClass(selector.getJavaClass()); } catch (Throwable t) { rethrowIfBlacklisted(t); logger.debug(t, () -> format("Class '%s' could not be resolved.", selector.getClassName())); } }
void resolveUniqueId(UniqueIdSelector selector) { UniqueId uniqueId = selector.getUniqueId(); // Ignore Unique IDs from other test engines. if (JupiterEngineDescriptor.ENGINE_ID.equals(uniqueId.getEngineId().orElse(null))) { try { Deque<TestDescriptor> resolvedDescriptors = resolveAllSegments(uniqueId); handleResolvedDescriptorsForUniqueId(uniqueId, resolvedDescriptors); } catch (Throwable t) { rethrowIfBlacklisted(t); logger.warn(t, () -> format("Unique ID '%s' could not be resolved.", selector.getUniqueId())); } } }
void resolveMethod(MethodSelector selector) { try { Class<?> testClass = selector.getJavaClass(); Method testMethod = selector.getJavaMethod(); Set<TestDescriptor> potentialParents = resolveContainerWithParents(testClass); Set<TestDescriptor> resolvedDescriptors = resolveForAllParents(testMethod, potentialParents); if (resolvedDescriptors.isEmpty()) { logger.debug(() -> format("Method '%s' could not be resolved.", testMethod.toGenericString())); } logMultipleTestDescriptorsForSingleElement(testMethod, resolvedDescriptors); } catch (Throwable t) { rethrowIfBlacklisted(t); logger.debug(t, () -> format("Method '%s' in class '%s' could not be resolved.", selector.getMethodName(), selector.getClassName())); } }
/** * @since 5.4 */ private void invokeTestWatchers(JupiterEngineExecutionContext context, boolean reverseOrder, Consumer<TestWatcher> callback) { ExtensionRegistry registry = context.getExtensionRegistry(); List<TestWatcher> watchers = reverseOrder // ? registry.getReversedExtensions(TestWatcher.class) : registry.getExtensions(TestWatcher.class); watchers.forEach(watcher -> { try { callback.accept(watcher); } catch (Throwable throwable) { BlacklistedExceptions.rethrowIfBlacklisted(throwable); ExtensionContext extensionContext = context.getExtensionContext(); logger.warn(throwable, () -> String.format("Failed to invoke TestWatcher [%s] for method [%s] with display name [%s]", watcher.getClass().getName(), ReflectionUtils.getFullyQualifiedMethodName(extensionContext.getRequiredTestClass(), extensionContext.getRequiredTestMethod()), getDisplayName())); } }); }
@SuppressWarnings("unchecked") private static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable, Object messageOrSupplier) { try { executable.execute(); } catch (Throwable actualException) { if (expectedType.isInstance(actualException)) { return (T) actualException; } else { BlacklistedExceptions.rethrowIfBlacklisted(actualException); String message = buildPrefix(nullSafeGet(messageOrSupplier)) + format(expectedType, actualException.getClass(), "Unexpected exception type thrown"); throw new AssertionFailedError(message, actualException); } } String message = buildPrefix(nullSafeGet(messageOrSupplier)) + String.format("Expected %s to be thrown, but nothing was thrown.", getCanonicalName(expectedType)); throw new AssertionFailedError(message); }
public void execute(RunnerTestDescriptor runnerTestDescriptor) { TestRun testRun = new TestRun(runnerTestDescriptor); JUnitCore core = new JUnitCore(); core.addListener(new RunListenerAdapter(testRun, engineExecutionListener)); try { core.run(runnerTestDescriptor.toRequest()); } catch (Throwable t) { BlacklistedExceptions.rethrowIfBlacklisted(t); reportUnexpectedFailure(testRun, runnerTestDescriptor, failed(t)); } }
protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) { ExtensionContext extensionContext = context.getExtensionContext(); ThrowableCollector throwableCollector = context.getThrowableCollector(); throwableCollector.execute(() -> { try { Method testMethod = getTestMethod(); Object instance = extensionContext.getRequiredTestInstance(); executableInvoker.invoke(testMethod, instance, extensionContext, context.getExtensionRegistry()); } catch (Throwable throwable) { BlacklistedExceptions.rethrowIfBlacklisted(throwable); invokeTestExecutionExceptionHandlers(context.getExtensionRegistry(), extensionContext, throwable); } }); }