1/*2 * Copyright 2015-2019 the original author or authors.3 *4 * All rights reserved. This program and the accompanying materials are5 * made available under the terms of the Eclipse Public License v2.0 which6 * accompanies this distribution and is available at7 *8 * https://www.eclipse.org/legal/epl-v20.html9 */10package org.junit.jupiter.engine.descriptor;11import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedMethods;12import static org.junit.platform.commons.util.ReflectionUtils.returnsVoid;13import java.lang.annotation.Annotation;14import java.lang.reflect.Method;15import java.util.List;16import org.junit.jupiter.api.AfterAll;17import org.junit.jupiter.api.AfterEach;18import org.junit.jupiter.api.BeforeAll;19import org.junit.jupiter.api.BeforeEach;20import org.junit.platform.commons.JUnitException;21import org.junit.platform.commons.util.ReflectionUtils;22import org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode;23/**24 * Collection of utilities for working with test lifecycle methods.25 *26 * @since 5.027 */28final class LifecycleMethodUtils {29 private LifecycleMethodUtils() {30 /* no-op */31 }32 static List<Method> findBeforeAllMethods(Class<?> testClass, boolean requireStatic) {33 return findMethodsAndAssertStatic(testClass, requireStatic, BeforeAll.class, HierarchyTraversalMode.TOP_DOWN);34 }35 static List<Method> findAfterAllMethods(Class<?> testClass, boolean requireStatic) {36 return findMethodsAndAssertStatic(testClass, requireStatic, AfterAll.class, HierarchyTraversalMode.BOTTOM_UP);37 }38 static List<Method> findBeforeEachMethods(Class<?> testClass) {39 return findMethodsAndAssertNonStatic(testClass, BeforeEach.class, HierarchyTraversalMode.TOP_DOWN);40 }41 static List<Method> findAfterEachMethods(Class<?> testClass) {42 return findMethodsAndAssertNonStatic(testClass, AfterEach.class, HierarchyTraversalMode.BOTTOM_UP);43 }44 private static void assertStatic(Class<? extends Annotation> annotationType, Method method) {45 if (ReflectionUtils.isNotStatic(method)) {46 throw new JUnitException(String.format(47 "@%s method '%s' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).",48 annotationType.getSimpleName(), method.toGenericString()));49 }50 }51 private static void assertNonStatic(Class<? extends Annotation> annotationType, Method method) {52 if (ReflectionUtils.isStatic(method)) {53 throw new JUnitException(String.format("@%s method '%s' must not be static.",54 annotationType.getSimpleName(), method.toGenericString()));55 }56 }57 private static void assertVoid(Class<? extends Annotation> annotationType, Method method) {58 if (!returnsVoid(method)) {59 throw new JUnitException(String.format("@%s method '%s' must not return a value.",60 annotationType.getSimpleName(), method.toGenericString()));61 }62 }63 private static List<Method> findMethodsAndAssertStatic(Class<?> testClass, boolean requireStatic,64 Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {65 List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);66 if (requireStatic) {67 methods.forEach(method -> assertStatic(annotationType, method));68 }69 return methods;70 }71 private static List<Method> findMethodsAndAssertNonStatic(Class<?> testClass,72 Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {73 List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);74 methods.forEach(method -> assertNonStatic(annotationType, method));75 return methods;76 }77 private static List<Method> findMethodsAndCheckVoidReturnType(Class<?> testClass,78 Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {79 List<Method> methods = findAnnotatedMethods(testClass, annotationType, traversalMode);80 methods.forEach(method -> assertVoid(annotationType, method));81 return methods;82 }83}...