001/*
002 * Copyright 2002-2015 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 org.junit.runners.model.Statement;
020
021import org.springframework.test.context.TestContextManager;
022
023/**
024 * {@code RunPrepareTestInstanceCallbacks} is a custom JUnit {@link Statement} which
025 * allows the <em>Spring TestContext Framework</em> to be plugged into the JUnit
026 * execution chain by calling {@link TestContextManager#prepareTestInstance(Object)
027 * prepareTestInstance()} on the supplied {@link TestContextManager}.
028 *
029 * @author Sam Brannen
030 * @since 4.2
031 * @see #evaluate()
032 */
033public class RunPrepareTestInstanceCallbacks extends Statement {
034
035        private final Statement next;
036
037        private final Object testInstance;
038
039        private final TestContextManager testContextManager;
040
041
042        /**
043         * Construct a new {@code RunPrepareTestInstanceCallbacks} statement.
044         * @param next the next {@code Statement} in the execution chain; never {@code null}
045         * @param testInstance the current test instance; never {@code null}
046         * @param testContextManager the {@code TestContextManager} upon which to call
047         * {@code prepareTestInstance()}; never {@code null}
048         */
049        public RunPrepareTestInstanceCallbacks(Statement next, Object testInstance, TestContextManager testContextManager) {
050                this.next = next;
051                this.testInstance = testInstance;
052                this.testContextManager = testContextManager;
053        }
054
055
056        /**
057         * Invoke {@link TestContextManager#prepareTestInstance(Object)} and
058         * then evaluate the next {@link Statement} in the execution chain
059         * (typically an instance of {@link RunAfterTestMethodCallbacks}).
060         */
061        @Override
062        public void evaluate() throws Throwable {
063                this.testContextManager.prepareTestInstance(testInstance);
064                this.next.evaluate();
065        }
066
067}