Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2012-2016 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
*
* http://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.boot.test;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.web.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.SpringVersion;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.support.AbstractContextLoader;
import org.springframework.test.context.support.AnnotationConfigContextLoaderUtils;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.web.WebMergedContextConfiguration;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.GenericWebApplicationContext;
/**
* A {@link ContextLoader} that can be used to test Spring Boot applications (those that
* normally startup using {@link SpringApplication}). Can be used to test non-web features
* (like a repository layer) or start an fully-configured embedded servlet container.
*
* Use {@code @WebIntegrationTest} (or {@code @IntegrationTest} with
* {@code @WebAppConfiguration}) to indicate that you want to use a real servlet container
* or {@code @WebAppConfiguration} alone to use a {@link MockServletContext}.
*
* If {@code @ActiveProfiles} are provided in the test class they will be used to create
* the application context.
*
* @author Dave Syer
* @author Phillip Webb
* @author Andy Wilkinson
* @see IntegrationTest
* @see WebIntegrationTest
* @see TestRestTemplate
*/
public class SpringApplicationContextLoader extends AbstractContextLoader {
@Override
public ApplicationContext loadContext(final MergedContextConfiguration config)
throws Exception {
assertValidAnnotations(config.getTestClass());
SpringApplication application = getSpringApplication();
application.setMainApplicationClass(config.getTestClass());
application.setSources(getSources(config));
ConfigurableEnvironment environment = new StandardEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
setActiveProfiles(environment, config.getActiveProfiles());
}
Map properties = getEnvironmentProperties(config);
addProperties(environment, properties);
application.setEnvironment(environment);
List> initializers = getInitializers(config,
application);
if (config instanceof WebMergedContextConfiguration) {
new WebConfigurer().configure(config, application, initializers);
}
else {
application.setWebEnvironment(false);
}
application.setInitializers(initializers);
ConfigurableApplicationContext applicationContext = application.run();
return applicationContext;
}
private void assertValidAnnotations(Class> testClass) {
boolean hasWebAppConfiguration = AnnotationUtils.findAnnotation(testClass,
WebAppConfiguration.class) != null;
boolean hasWebIntegrationTest = AnnotationUtils.findAnnotation(testClass,
WebIntegrationTest.class) != null;
if (hasWebAppConfiguration && hasWebIntegrationTest) {
throw new IllegalStateException("@WebIntegrationTest and "
+ "@WebAppConfiguration cannot be used together");
}
}
/**
* Builds new {@link org.springframework.boot.SpringApplication} instance. You can
* override this method to add custom behavior
* @return {@link org.springframework.boot.SpringApplication} instance
*/
protected SpringApplication getSpringApplication() {
return new SpringApplication();
}
private Set