/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.test.context.support;

import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ActiveProfilesResolver;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import static org.springframework.test.context.TestContextAnnotationUtils.findAnnotationDescriptor;

/**
 * Default implementation of the {@link ActiveProfilesResolver} strategy that
 * resolves <em>active bean definition profiles</em> based solely on profiles
 * configured declaratively via {@link ActiveProfiles#profiles} or
 * {@link ActiveProfiles#value}.
 *
 * @author Sam Brannen
 * @since 4.1
 * @see ActiveProfiles
 * @see ActiveProfilesResolver
 */
public class DefaultActiveProfilesResolver implements ActiveProfilesResolver {

	private static final Log logger = LogFactory.getLog(DefaultActiveProfilesResolver.class);


	/**
	 * Resolve the <em>bean definition profiles</em> for the given {@linkplain
	 * Class test class} based on profiles configured declaratively via
	 * {@link ActiveProfiles#profiles} or {@link ActiveProfiles#value}.
	 * @param testClass the test class for which the profiles should be resolved;
	 * never {@code null}
	 * @return the list of bean definition profiles to use when loading the
	 * {@code ApplicationContext}; never {@code null}
	 */
	@Override
	public String[] resolve(Class<?> testClass) {
		Assert.notNull(testClass, "Class must not be null");

		Set<String> activeProfiles = new TreeSet<>();

		Class<ActiveProfiles> annotationType = ActiveProfiles.class;
		AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, annotationType);

		if (descriptor == null) {
			if (logger.isDebugEnabled()) {
				logger.debug(String.format(
					"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
					annotationType.getName(), testClass.getName()));
			}
		}
		else {
			Class<?> declaringClass = descriptor.getDeclaringClass();
			ActiveProfiles annotation = descriptor.synthesizeAnnotation();

			if (logger.isTraceEnabled()) {
				logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
					declaringClass.getName()));
			}

			for (String profile : annotation.profiles()) {
				if (StringUtils.hasText(profile)) {
					activeProfiles.add(profile.trim());
				}
			}
		}

		return StringUtils.toStringArray(activeProfiles);
	}

}

@@ -671,57 +671,8 @@
 t;%0A%0A
-import java.util.Set;%0Aimport java.util.TreeSet;%0A%0A
 impo
@@ -1005,53 +1005,8 @@
 ert;
-%0Aimport org.springframework.util.StringUtils;
 %0A%0Aim
@@ -1542,16 +1542,84 @@
 lver %7B%0A%0A
+%09private static final String%5B%5D EMPTY_STRING_ARRAY = new String%5B0%5D;%0A%0A
 %09private
@@ -2269,121 +2269,8 @@
 l%22);
-%0A%0A%09%09Set%3CString%3E activeProfiles = new TreeSet%3C%3E();%0A%0A%09%09Class%3CActiveProfiles%3E annotationType = ActiveProfiles.class;
 %0A%09%09A
@@ -2354,30 +2354,36 @@
 tClass, 
-annotationType
+ActiveProfiles.class
 );%0A%0A%09%09if
@@ -2576,22 +2576,28 @@
 %09%09%09%09
-annotationType
+ActiveProfiles.class
 .get
@@ -2638,79 +2638,48 @@
 %7D%0A%09%09
-%7D%0A%09%09else %7B%0A%09%09%09Class%3C?%3E declaringClass = descriptor.getDeclaringClass();
+%09return EMPTY_STRING_ARRAY;%0A%09%09%7D%0A%09%09else %7B
 %0A%09%09%09
@@ -2741,17 +2741,16 @@
 tion();%0A
-%0A
 %09%09%09if (l
@@ -2885,16 +2885,30 @@
 %0A%09%09%09%09%09de
+scriptor.getDe
 claringC
@@ -2907,24 +2907,26 @@
 claringClass
+()
 .getName()))
@@ -2936,33 +2936,17 @@
 %09%09%7D%0A
-%0A
 %09%09%09
-for (String profile :
+return
  ann
@@ -2967,160 +2967,13 @@
 es()
-) %7B%0A%09%09%09%09if (StringUtils.hasText(profile)) %7B%0A%09%09%09%09%09activeProfiles.add(profile.trim());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return StringUtils.toStringArray(activeProfiles);
+;%0A%09%09%7D
 %0A%09%7D%0A

Simplify implementation of DefaultActiveProfilesResolver