001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.context.junit4.statements;
018
019import java.lang.reflect.Method;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.junit.runners.model.MultipleFailureException;
024import org.junit.runners.model.Statement;
025
026import org.springframework.test.context.TestContextManager;
027
028/**
029 * {@code RunAfterTestExecutionCallbacks} is a custom JUnit {@link Statement}
030 * which allows the <em>Spring TestContext Framework</em> to be plugged into the
031 * JUnit 4 execution chain by calling {@link TestContextManager#afterTestExecution
032 * afterTestExecution()} on the supplied {@link TestContextManager}.
033 *
034 * <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
035 *
036 * @author Sam Brannen
037 * @since 5.0
038 * @see #evaluate()
039 * @see RunBeforeTestExecutionCallbacks
040 */
041public class RunAfterTestExecutionCallbacks extends Statement {
042
043        private final Statement next;
044
045        private final Object testInstance;
046
047        private final Method testMethod;
048
049        private final TestContextManager testContextManager;
050
051
052        /**
053         * Construct a new {@code RunAfterTestExecutionCallbacks} statement.
054         * @param next the next {@code Statement} in the execution chain
055         * @param testInstance the current test instance (never {@code null})
056         * @param testMethod the test method which has just been executed on the
057         * test instance
058         * @param testContextManager the TestContextManager upon which to call
059         * {@code afterTestExecution()}
060         */
061        public RunAfterTestExecutionCallbacks(Statement next, Object testInstance, Method testMethod,
062                        TestContextManager testContextManager) {
063
064                this.next = next;
065                this.testInstance = testInstance;
066                this.testMethod = testMethod;
067                this.testContextManager = testContextManager;
068        }
069
070        /**
071         * Evaluate the next {@link Statement} in the execution chain (typically an
072         * instance of {@link RunBeforeTestExecutionCallbacks}), catching any exceptions
073         * thrown, and then invoke {@link TestContextManager#afterTestExecution} supplying
074         * the first caught exception (if any).
075         * <p>If the invocation of {@code afterTestExecution()} throws an exception, that
076         * exception will also be tracked. Multiple exceptions will be combined into a
077         * {@link MultipleFailureException}.
078         */
079        @Override
080        public void evaluate() throws Throwable {
081                Throwable testException = null;
082                List<Throwable> errors = new ArrayList<>();
083                try {
084                        this.next.evaluate();
085                }
086                catch (Throwable ex) {
087                        testException = ex;
088                        errors.add(ex);
089                }
090
091                try {
092                        this.testContextManager.afterTestExecution(this.testInstance, this.testMethod, testException);
093                }
094                catch (Throwable ex) {
095                        errors.add(ex);
096                }
097
098                MultipleFailureException.assertEmpty(errors);
099        }
100
101}