public static ExecutionMode toExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode mode) { switch (mode) { case CONCURRENT: return ExecutionMode.CONCURRENT; case SAME_THREAD: return ExecutionMode.SAME_THREAD; } throw new JUnitException("Unknown ExecutionMode: " + mode); }
ConsoleLauncherExecutionResult execute(String... args) { CommandLineOptions options = null; try { options = commandLineOptionsParser.parse(args); } catch (JUnitException ex) { errStream.println(ex.getMessage()); StringWriter sw = new StringWriter(); commandLineOptionsParser.printHelp(new PrintWriter(sw)); errStream.println(sw); return ConsoleLauncherExecutionResult.failed(); } try (PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outStream, charset)))) { if (!options.isBannerDisabled()) { displayBanner(out); } if (options.isDisplayHelp()) { commandLineOptionsParser.printHelp(out); return ConsoleLauncherExecutionResult.success(); } return executeTests(options, out); } finally { outStream.flush(); errStream.flush(); } }
private static LockMode toLockMode(ResourceAccessMode mode) { switch (mode) { case READ: return LockMode.READ; case READ_WRITE: return LockMode.READ_WRITE; } throw new JUnitException("Unknown ResourceAccessMode: " + mode); }
@Override public State clone() { try { return (State) super.clone(); } catch (CloneNotSupportedException e) { throw new JUnitException("State could not be cloned", e); } }
private static BigDecimal parseVersion(String versionString) { try { Matcher matcher = versionPattern.matcher(versionString); if (matcher.matches()) { return new BigDecimal(matcher.group(1)); } } catch (Exception e) { throw new JUnitException("Failed to parse version of junit:junit: " + versionString, e); } throw new JUnitException("Failed to parse version of junit:junit: " + versionString); }
private static void assertNonStatic(Class<? extends Annotation> annotationType, Method method) { if (ReflectionUtils.isStatic(method)) { throw new JUnitException(String.format("@%s method '%s' must not be static.", annotationType.getSimpleName(), method.toGenericString())); } }
private static void assertVoid(Class<? extends Annotation> annotationType, Method method) { if (!returnsVoid(method)) { throw new JUnitException(String.format("@%s method '%s' must not return a value.", annotationType.getSimpleName(), method.toGenericString())); } }
private JUnitException invalidReturnTypeException(Throwable cause) { String message = String.format( "@TestFactory method [%s] must return a single %2$s or a Stream, Collection, Iterable, Iterator, or array of %2$s.", getTestMethod().toGenericString(), DynamicNode.class.getName()); return new JUnitException(message, cause); }
private static void assertStatic(Class<? extends Annotation> annotationType, Method method) { if (ReflectionUtils.isNotStatic(method)) { throw new JUnitException(String.format( "@%s method '%s' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).", annotationType.getSimpleName(), method.toGenericString())); } }
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); } }
String format(int invocationIndex, Object... arguments) { try { return formatSafely(invocationIndex, arguments); } catch (Exception ex) { String message = "The display name pattern defined for the parameterized test is invalid. " + "See nested exception for further details."; throw new JUnitException(message, ex); } }
private static <A extends Annotation> void initializeAnnotationConsumer(AnnotationConsumer<A> instance, A annotation) { try { instance.accept(annotation); } catch (Exception ex) { throw new JUnitException("Failed to initialize AnnotationConsumer: " + instance, ex); } }
private Method getMethod(Class<?> clazz, String methodName) { return ReflectionUtils.findMethod(clazz, methodName).orElseThrow(() -> new JUnitException( format("Could not find factory method [%s] in class [%s]", methodName, clazz.getName()))); }
@SuppressWarnings("ConstantConditions") private ArgumentsProvider instantiateArgumentsProvider(Class<? extends ArgumentsProvider> clazz) { try { return ReflectionUtils.newInstance(clazz); } catch (Exception ex) { if (ex instanceof NoSuchMethodException) { String message = String.format("Failed to find a no-argument constructor for ArgumentsProvider [%s]. " + "Please ensure that a no-argument constructor exists and " + "that the class is either a top-level class or a static nested class", clazz.getName()); throw new JUnitException(message, ex); } throw ex; } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T initialize(AnnotatedElement annotatedElement, T instance) { if (instance instanceof AnnotationConsumer) { Method method = findMethods(instance.getClass(), isAnnotationConsumerAcceptMethod, BOTTOM_UP).get(0); Class<? extends Annotation> annotationType = (Class<? extends Annotation>) method.getParameterTypes()[0]; Annotation annotation = AnnotationUtils.findAnnotation(annotatedElement, annotationType) // .orElseThrow(() -> new JUnitException(instance.getClass().getName() + " must be used with an annotation of type " + annotationType.getName())); initializeAnnotationConsumer((AnnotationConsumer) instance, annotation); } return instance; }
private void invokeMethodInExtensionContext(Method method, ExtensionContext context, ExtensionRegistry registry) { TestInstances testInstances = context.getRequiredTestInstances(); Object target = testInstances.findInstance(method.getDeclaringClass()).orElseThrow( () -> new JUnitException("Failed to find instance for method: " + method.toGenericString())); executableInvoker.invoke(method, target, context, registry); }
static void checkSupported(Supplier<String> versionSupplier) { String versionString = readVersion(versionSupplier); BigDecimal version = parseVersion(versionString); if (version.compareTo(minVersion) < 0) { throw new JUnitException("Unsupported version of junit:junit: " + versionString + ". Please upgrade to version " + minVersion + " or later."); } }
private Class<?> loadRequiredClass(String className) { return ReflectionUtils.tryToLoadClass(className).getOrThrow( cause -> new JUnitException(format("Could not load class [%s]", className), cause)); }
@Override public void afterEach( ExtensionContext context ) { TestDirectory testDirectory = getStoredValue( context ); try { testDirectory.complete( !context.getExecutionException().isPresent() ); } catch ( Exception e ) { throw new JUnitException( format( "Fail to cleanup test directory for %s test.", context.getDisplayName() ), e ); } }
@Override protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) { ExtensionContext extensionContext = context.getExtensionContext(); context.getThrowableCollector().execute(() -> { Object instance = extensionContext.getRequiredTestInstance(); Object testFactoryMethodResult = executableInvoker.invoke(getTestMethod(), instance, extensionContext, context.getExtensionRegistry()); TestSource defaultTestSource = getSource().orElseThrow( () -> new JUnitException("Illegal state: TestSource must be present")); try (Stream<DynamicNode> dynamicNodeStream = toDynamicNodeStream(testFactoryMethodResult)) { int index = 1; Iterator<DynamicNode> iterator = dynamicNodeStream.iterator(); while (iterator.hasNext()) { DynamicNode dynamicNode = iterator.next(); Optional<JupiterTestDescriptor> descriptor = createDynamicDescriptor(this, dynamicNode, index++, defaultTestSource, getDynamicDescendantFilter(), configuration); descriptor.ifPresent(dynamicTestExecutor::execute); } } catch (ClassCastException ex) { throw invalidReturnTypeException(ex); } dynamicTestExecutor.awaitFinished(); }); }