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.support;
018
019import org.springframework.context.ApplicationContext;
020import org.springframework.test.annotation.DirtiesContext;
021import org.springframework.test.annotation.DirtiesContext.ClassMode;
022import org.springframework.test.annotation.DirtiesContext.MethodMode;
023import org.springframework.test.context.TestContext;
024import org.springframework.test.context.TestExecutionListeners;
025
026import static org.springframework.test.annotation.DirtiesContext.ClassMode.*;
027import static org.springframework.test.annotation.DirtiesContext.MethodMode.*;
028
029/**
030 * {@code TestExecutionListener} which provides support for marking the
031 * {@code ApplicationContext} associated with a test as <em>dirty</em> for
032 * both test classes and test methods annotated with the
033 * {@link DirtiesContext @DirtiesContext} annotation.
034 *
035 * <p>This listener supports test methods with the
036 * {@linkplain DirtiesContext#methodMode method mode} set to
037 * {@link MethodMode#BEFORE_METHOD BEFORE_METHOD} and test classes with the
038 * {@linkplain DirtiesContext#classMode() class mode} set to
039 * {@link ClassMode#BEFORE_EACH_TEST_METHOD BEFORE_EACH_TEST_METHOD} or
040 * {@link ClassMode#BEFORE_CLASS BEFORE_CLASS}. For support for <em>AFTER</em>
041 * modes, see {@link DirtiesContextTestExecutionListener}.
042 *
043 * <p>When {@linkplain TestExecutionListeners#mergeMode merging}
044 * {@code TestExecutionListeners} with the defaults, this listener will
045 * automatically be ordered before the {@link DependencyInjectionTestExecutionListener};
046 * otherwise, this listener must be manually configured to execute before the
047 * {@code DependencyInjectionTestExecutionListener}.
048 *
049 * @author Sam Brannen
050 * @since 4.2
051 * @see DirtiesContext
052 * @see DirtiesContextTestExecutionListener
053 */
054public class DirtiesContextBeforeModesTestExecutionListener extends AbstractDirtiesContextTestExecutionListener {
055
056        /**
057         * Returns {@code 1500}.
058         */
059        @Override
060        public final int getOrder() {
061                return 1500;
062        }
063
064        /**
065         * If the test class of the supplied {@linkplain TestContext test context}
066         * is annotated with {@code @DirtiesContext} and the {@linkplain
067         * DirtiesContext#classMode() class mode} is set to {@link
068         * ClassMode#BEFORE_CLASS BEFORE_CLASS}, the {@linkplain ApplicationContext
069         * application context} of the test context will be
070         * {@linkplain TestContext#markApplicationContextDirty marked as dirty}, and the
071         * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
072         * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to
073         * {@code true}.
074         */
075        @Override
076        public void beforeTestClass(TestContext testContext) throws Exception {
077                beforeOrAfterTestClass(testContext, BEFORE_CLASS);
078        }
079
080        /**
081         * If the current test method of the supplied {@linkplain TestContext test
082         * context} is annotated with {@code @DirtiesContext} and the {@linkplain
083         * DirtiesContext#methodMode() method mode} is set to {@link
084         * MethodMode#BEFORE_METHOD BEFORE_METHOD}, or if the test class is
085         * annotated with {@code @DirtiesContext} and the {@linkplain
086         * DirtiesContext#classMode() class mode} is set to {@link
087         * ClassMode#BEFORE_EACH_TEST_METHOD BEFORE_EACH_TEST_METHOD}, the
088         * {@linkplain ApplicationContext application context} of the test context
089         * will be {@linkplain TestContext#markApplicationContextDirty marked as dirty} and the
090         * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
091         * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to {@code true}.
092         */
093        @Override
094        public void beforeTestMethod(TestContext testContext) throws Exception {
095                beforeOrAfterTestMethod(testContext, BEFORE_METHOD, BEFORE_EACH_TEST_METHOD);
096        }
097
098}