001/*
002 * Copyright 2002-2020 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.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022import org.springframework.test.context.ActiveProfiles;
023import org.springframework.test.context.ActiveProfilesResolver;
024import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
025import org.springframework.util.Assert;
026
027import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor;
028
029/**
030 * Default implementation of the {@link ActiveProfilesResolver} strategy that
031 * resolves <em>active bean definition profiles</em> based solely on profiles
032 * configured declaratively via {@link ActiveProfiles#profiles} or
033 * {@link ActiveProfiles#value}.
034 *
035 * @author Sam Brannen
036 * @since 4.1
037 * @see ActiveProfiles
038 * @see ActiveProfilesResolver
039 */
040public class DefaultActiveProfilesResolver implements ActiveProfilesResolver {
041
042        private static final String[] EMPTY_STRING_ARRAY = new String[0];
043
044        private static final Log logger = LogFactory.getLog(DefaultActiveProfilesResolver.class);
045
046
047        /**
048         * Resolve the <em>bean definition profiles</em> for the given {@linkplain
049         * Class test class} based on profiles configured declaratively via
050         * {@link ActiveProfiles#profiles} or {@link ActiveProfiles#value}.
051         * @param testClass the test class for which the profiles should be resolved;
052         * never {@code null}
053         * @return the list of bean definition profiles to use when loading the
054         * {@code ApplicationContext}; never {@code null}
055         */
056        @Override
057        public String[] resolve(Class<?> testClass) {
058                Assert.notNull(testClass, "Class must not be null");
059                AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, ActiveProfiles.class);
060
061                if (descriptor == null) {
062                        if (logger.isDebugEnabled()) {
063                                logger.debug(String.format(
064                                        "Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
065                                        ActiveProfiles.class.getName(), testClass.getName()));
066                        }
067                        return EMPTY_STRING_ARRAY;
068                }
069                else {
070                        ActiveProfiles annotation = descriptor.synthesizeAnnotation();
071                        if (logger.isTraceEnabled()) {
072                                logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
073                                        descriptor.getDeclaringClass().getName()));
074                        }
075                        return annotation.profiles();
076                }
077        }
078
079}