Start building against Spring Framework 5 snapshot

This commit enables compatibility build against Spring Framework 5.

The Velocity and Guava support that are deprecated in the 1.x line have
been removed and few other classes contain minor change to comply to non
backward compatible changes in Spring Framework 5.

This commit also switches the required java version to 8.

Closes gh-6977
This commit is contained in:
Stephane Nicoll
2016-07-25 20:08:21 +02:00
parent 6643ec3713
commit 9bc77254a7
60 changed files with 35 additions and 2046 deletions

View File

@@ -42,7 +42,6 @@ final class CacheConfigurations {
mappings.put(CacheType.COUCHBASE, CouchbaseCacheConfiguration.class);
mappings.put(CacheType.REDIS, RedisCacheConfiguration.class);
mappings.put(CacheType.CAFFEINE, CaffeineCacheConfiguration.class);
mappings.put(CacheType.GUAVA, GuavaCacheConfiguration.class);
mappings.put(CacheType.SIMPLE, SimpleCacheConfiguration.class);
mappings.put(CacheType.NONE, NoOpCacheConfiguration.class);
MAPPINGS = Collections.unmodifiableMap(mappings);

View File

@@ -20,7 +20,6 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
@@ -57,8 +56,6 @@ public class CacheProperties {
private final JCache jcache = new JCache();
private final Guava guava = new Guava();
public CacheType getType() {
return this.type;
}
@@ -99,10 +96,6 @@ public class CacheProperties {
return this.jcache;
}
public Guava getGuava() {
return this.guava;
}
/**
* Resolve the config location if set.
* @param config the config resource
@@ -256,30 +249,4 @@ public class CacheProperties {
}
/**
* Guava specific cache properties.
*/
public static class Guava {
/**
* The spec to use to create caches. Check CacheBuilderSpec for more details on
* the spec format.
*/
private String spec;
@Deprecated
@DeprecatedConfigurationProperty(
reason = "Caffeine will supersede the Guava support in Spring Boot 2.0",
replacement = "spring.cache.caffeine.spec")
public String getSpec() {
return this.spec;
}
@Deprecated
public void setSpec(String spec) {
this.spec = spec;
}
}
}

View File

@@ -66,12 +66,6 @@ public enum CacheType {
*/
CAFFEINE,
/**
* Guava backed caching.
*/
@Deprecated
GUAVA,
/**
* Simple in-memory caching.
*/

View File

@@ -1,102 +0,0 @@
/*
* 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.autoconfigure.cache;
import java.util.List;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
import com.google.common.cache.CacheLoader;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* Guava cache configuration.
*
* @author Stephane Nicoll
* @since 1.3.0
*/
@Configuration
@ConditionalOnClass({ CacheBuilder.class, GuavaCacheManager.class })
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
class GuavaCacheConfiguration {
private final CacheProperties cacheProperties;
private final CacheManagerCustomizers customizers;
private final CacheBuilder<Object, Object> cacheBuilder;
private final CacheBuilderSpec cacheBuilderSpec;
private final CacheLoader<Object, Object> cacheLoader;
GuavaCacheConfiguration(CacheProperties cacheProperties,
CacheManagerCustomizers customizers,
ObjectProvider<CacheBuilder<Object, Object>> cacheBuilderProvider,
ObjectProvider<CacheBuilderSpec> cacheBuilderSpecProvider,
ObjectProvider<CacheLoader<Object, Object>> cacheLoaderProvider) {
this.cacheProperties = cacheProperties;
this.customizers = customizers;
this.cacheBuilder = cacheBuilderProvider.getIfAvailable();
this.cacheBuilderSpec = cacheBuilderSpecProvider.getIfAvailable();
this.cacheLoader = cacheLoaderProvider.getIfAvailable();
}
@Bean
public GuavaCacheManager cacheManager() {
GuavaCacheManager cacheManager = createCacheManager();
List<String> cacheNames = this.cacheProperties.getCacheNames();
if (!CollectionUtils.isEmpty(cacheNames)) {
cacheManager.setCacheNames(cacheNames);
}
return this.customizers.customize(cacheManager);
}
private GuavaCacheManager createCacheManager() {
GuavaCacheManager cacheManager = new GuavaCacheManager();
setCacheBuilder(cacheManager);
if (this.cacheLoader != null) {
cacheManager.setCacheLoader(this.cacheLoader);
}
return cacheManager;
}
private void setCacheBuilder(GuavaCacheManager cacheManager) {
String specification = this.cacheProperties.getGuava().getSpec();
if (StringUtils.hasText(specification)) {
cacheManager.setCacheSpecification(specification);
}
else if (this.cacheBuilderSpec != null) {
cacheManager.setCacheBuilderSpec(this.cacheBuilderSpec);
}
else if (this.cacheBuilder != null) {
cacheManager.setCacheBuilder(this.cacheBuilder);
}
}
}

View File

@@ -1,165 +0,0 @@
/*
* 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.autoconfigure.velocity;
import java.io.IOException;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.servlet.Servlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.template.TemplateLocation;
import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.velocity.VelocityEngineFactory;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
import org.springframework.web.servlet.view.velocity.VelocityConfig;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Velocity.
*
* @author Andy Wilkinson
* @author Brian Clozel
* @since 1.1.0
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
* 4.3
*/
@Configuration
@ConditionalOnClass({ VelocityEngine.class, VelocityEngineFactory.class })
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(VelocityProperties.class)
@Deprecated
public class VelocityAutoConfiguration {
private static final Log logger = LogFactory.getLog(VelocityAutoConfiguration.class);
private final ApplicationContext applicationContext;
private final VelocityProperties properties;
public VelocityAutoConfiguration(ApplicationContext applicationContext,
VelocityProperties properties) {
this.applicationContext = applicationContext;
this.properties = properties;
}
@PostConstruct
public void checkTemplateLocationExists() {
if (this.properties.isCheckTemplateLocation()) {
TemplateLocation location = new TemplateLocation(
this.properties.getResourceLoaderPath());
if (!location.exists(this.applicationContext)) {
logger.warn("Cannot find template location: " + location
+ " (please add some templates, check your Velocity "
+ "configuration, or set spring.velocity."
+ "checkTemplateLocation=false)");
}
}
}
@Deprecated
protected static class VelocityConfiguration {
@Autowired
protected VelocityProperties properties;
protected void applyProperties(VelocityEngineFactory factory) {
factory.setResourceLoaderPath(this.properties.getResourceLoaderPath());
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
Properties velocityProperties = new Properties();
velocityProperties.setProperty("input.encoding",
this.properties.getCharsetName());
velocityProperties.putAll(this.properties.getProperties());
factory.setVelocityProperties(velocityProperties);
}
}
@Configuration
@ConditionalOnNotWebApplication
@Deprecated
public static class VelocityNonWebConfiguration extends VelocityConfiguration {
@Bean
@ConditionalOnMissingBean
public VelocityEngineFactoryBean velocityConfiguration() {
VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
applyProperties(velocityEngineFactoryBean);
return velocityEngineFactoryBean;
}
}
@Configuration
@ConditionalOnClass(Servlet.class)
@ConditionalOnWebApplication
@Deprecated
public static class VelocityWebConfiguration extends VelocityConfiguration {
@Bean
@ConditionalOnMissingBean(VelocityConfig.class)
public VelocityConfigurer velocityConfigurer() {
VelocityConfigurer configurer = new VelocityConfigurer();
applyProperties(configurer);
return configurer;
}
@Bean
public VelocityEngine velocityEngine(VelocityConfigurer configurer)
throws VelocityException, IOException {
return configurer.getVelocityEngine();
}
@Bean
@ConditionalOnMissingBean(name = "velocityViewResolver")
@ConditionalOnProperty(name = "spring.velocity.enabled", matchIfMissing = true)
public EmbeddedVelocityViewResolver velocityViewResolver() {
EmbeddedVelocityViewResolver resolver = new EmbeddedVelocityViewResolver();
this.properties.applyToViewResolver(resolver);
return resolver;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledResourceChain
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
}
}

View File

@@ -1,138 +0,0 @@
/*
* 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.autoconfigure.velocity;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
/**
* {@link ConfigurationProperties} for configuring Velocity.
*
* @author Andy Wilkinson
* @since 1.1.0
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
* 4.3
*/
@Deprecated
@ConfigurationProperties(prefix = "spring.velocity")
public class VelocityProperties extends AbstractTemplateViewResolverProperties {
public static final String DEFAULT_RESOURCE_LOADER_PATH = "classpath:/templates/";
public static final String DEFAULT_PREFIX = "";
public static final String DEFAULT_SUFFIX = ".vm";
/**
* Name of the DateTool helper object to expose in the Velocity context of the view.
*/
private String dateToolAttribute;
/**
* Name of the NumberTool helper object to expose in the Velocity context of the view.
*/
private String numberToolAttribute;
/**
* Additional velocity properties.
*/
private Map<String, String> properties = new HashMap<String, String>();
/**
* Template path.
*/
private String resourceLoaderPath = DEFAULT_RESOURCE_LOADER_PATH;
/**
* Velocity Toolbox config location, for example "/WEB-INF/toolbox.xml". Automatically
* loads a Velocity Tools toolbox definition file and expose all defined tools in the
* specified scopes.
*/
private String toolboxConfigLocation;
/**
* Prefer file system access for template loading. File system access enables hot
* detection of template changes.
*/
private boolean preferFileSystemAccess = true;
public VelocityProperties() {
super(DEFAULT_PREFIX, DEFAULT_SUFFIX);
}
public String getDateToolAttribute() {
return this.dateToolAttribute;
}
public void setDateToolAttribute(String dateToolAttribute) {
this.dateToolAttribute = dateToolAttribute;
}
public String getNumberToolAttribute() {
return this.numberToolAttribute;
}
public void setNumberToolAttribute(String numberToolAttribute) {
this.numberToolAttribute = numberToolAttribute;
}
public Map<String, String> getProperties() {
return this.properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public String getResourceLoaderPath() {
return this.resourceLoaderPath;
}
public void setResourceLoaderPath(String resourceLoaderPath) {
this.resourceLoaderPath = resourceLoaderPath;
}
public String getToolboxConfigLocation() {
return this.toolboxConfigLocation;
}
public void setToolboxConfigLocation(String toolboxConfigLocation) {
this.toolboxConfigLocation = toolboxConfigLocation;
}
public boolean isPreferFileSystemAccess() {
return this.preferFileSystemAccess;
}
public void setPreferFileSystemAccess(boolean preferFileSystemAccess) {
this.preferFileSystemAccess = preferFileSystemAccess;
}
@Override
public void applyToViewResolver(Object viewResolver) {
super.applyToViewResolver(viewResolver);
VelocityViewResolver resolver = (VelocityViewResolver) viewResolver;
resolver.setToolboxConfigLocation(getToolboxConfigLocation());
resolver.setDateToolAttribute(getDateToolAttribute());
resolver.setNumberToolAttribute(getNumberToolAttribute());
}
}

View File

@@ -1,57 +0,0 @@
/*
* 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.autoconfigure.velocity;
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;
/**
* {@link TemplateAvailabilityProvider} that provides availability information for
* Velocity view templates.
*
* @author Andy Wilkinson
* @since 1.1.0
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
* 4.3
*/
@Deprecated
public class VelocityTemplateAvailabilityProvider
implements TemplateAvailabilityProvider {
@Override
public boolean isTemplateAvailable(String view, Environment environment,
ClassLoader classLoader, ResourceLoader resourceLoader) {
if (ClassUtils.isPresent("org.apache.velocity.app.VelocityEngine", classLoader)) {
PropertyResolver resolver = new RelaxedPropertyResolver(environment,
"spring.velocity.");
String loaderPath = resolver.getProperty("resource-loader-path",
VelocityProperties.DEFAULT_RESOURCE_LOADER_PATH);
String prefix = resolver.getProperty("prefix",
VelocityProperties.DEFAULT_PREFIX);
String suffix = resolver.getProperty("suffix",
VelocityProperties.DEFAULT_SUFFIX);
return resourceLoader.getResource(loaderPath + prefix + view + suffix)
.exists();
}
return false;
}
}

View File

@@ -1,22 +0,0 @@
/*
* 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.
*/
/**
* Auto-configuration for Velocity.
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
* 4.3
*/
package org.springframework.boot.autoconfigure.velocity;

View File

@@ -85,7 +85,6 @@ org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
@@ -114,5 +113,4 @@ org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailability
org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.velocity.VelocityTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.web.JspTemplateAvailabilityProvider

View File

@@ -27,8 +27,8 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader;
@@ -92,12 +92,12 @@ public class EnableAutoConfigurationImportSelectorTests {
@Test
public void classNamesExclusionsAreApplied() {
configureExclusions(new String[0],
new String[] { VelocityAutoConfiguration.class.getName() },
new String[] { ThymeleafAutoConfiguration.class.getName() },
new String[0]);
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 1);
assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions())
.contains(VelocityAutoConfiguration.class.getName());
.contains(ThymeleafAutoConfiguration.class.getName());
}
@Test
@@ -114,12 +114,12 @@ public class EnableAutoConfigurationImportSelectorTests {
public void severalPropertyExclusionsAreApplied() {
configureExclusions(new String[0], new String[0],
new String[] { FreeMarkerAutoConfiguration.class.getName(),
VelocityAutoConfiguration.class.getName() });
ThymeleafAutoConfiguration.class.getName() });
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 2);
assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions())
.contains(FreeMarkerAutoConfiguration.class.getName(),
VelocityAutoConfiguration.class.getName());
ThymeleafAutoConfiguration.class.getName());
}
@Test
@@ -128,24 +128,24 @@ public class EnableAutoConfigurationImportSelectorTests {
this.environment.setProperty("spring.autoconfigure.exclude[0]",
FreeMarkerAutoConfiguration.class.getName());
this.environment.setProperty("spring.autoconfigure.exclude[1]",
VelocityAutoConfiguration.class.getName());
ThymeleafAutoConfiguration.class.getName());
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 2);
assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions())
.contains(FreeMarkerAutoConfiguration.class.getName(),
VelocityAutoConfiguration.class.getName());
ThymeleafAutoConfiguration.class.getName());
}
@Test
public void combinedExclusionsAreApplied() {
configureExclusions(new String[] { VelocityAutoConfiguration.class.getName() },
configureExclusions(new String[] { GroovyTemplateAutoConfiguration.class.getName() },
new String[] { FreeMarkerAutoConfiguration.class.getName() },
new String[] { ThymeleafAutoConfiguration.class.getName() });
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 3);
assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions())
.contains(FreeMarkerAutoConfiguration.class.getName(),
VelocityAutoConfiguration.class.getName(),
GroovyTemplateAutoConfiguration.class.getName(),
ThymeleafAutoConfiguration.class.getName());
}

View File

@@ -36,7 +36,6 @@ import com.couchbase.client.spring.cache.CouchbaseCache;
import com.couchbase.client.spring.cache.CouchbaseCacheManager;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import com.google.common.cache.CacheBuilder;
import com.hazelcast.cache.HazelcastCachingProvider;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
@@ -66,8 +65,6 @@ import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.jcache.JCacheCacheManager;
@@ -632,46 +629,6 @@ public class CacheAutoConfigurationTests {
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "custom1");
}
@Test
public void guavaCacheExplicitWithCaches() {
load(DefaultCacheConfiguration.class, "spring.cache.type=guava",
"spring.cache.cacheNames=foo");
GuavaCacheManager cacheManager = validateCacheManager(GuavaCacheManager.class);
Cache foo = cacheManager.getCache("foo");
foo.get("1");
// See next tests: no spec given so stats should be disabled
assertThat(((GuavaCache) foo).getNativeCache().stats().missCount()).isEqualTo(0L);
}
@Test
public void guavaCacheWithCustomizers() {
testCustomizers(DefaultCacheAndCustomizersConfiguration.class, "guava",
"allCacheManagerCustomizer", "guavaCacheManagerCustomizer");
}
@Test
public void guavaCacheExplicitWithSpec() {
load(DefaultCacheConfiguration.class, "spring.cache.type=guava",
"spring.cache.guava.spec=recordStats", "spring.cache.cacheNames[0]=foo",
"spring.cache.cacheNames[1]=bar");
validateGuavaCacheWithStats();
}
@Test
public void guavaCacheExplicitWithCacheBuilder() {
load(GuavaCacheBuilderConfiguration.class, "spring.cache.type=guava",
"spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar");
validateGuavaCacheWithStats();
}
private void validateGuavaCacheWithStats() {
GuavaCacheManager cacheManager = validateCacheManager(GuavaCacheManager.class);
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
Cache foo = cacheManager.getCache("foo");
foo.get("1");
assertThat(((GuavaCache) foo).getNativeCache().stats().missCount()).isEqualTo(1L);
}
@Test
public void caffeineCacheWithExplicitCaches() {
load(DefaultCacheConfiguration.class, "spring.cache.type=caffeine",
@@ -995,17 +952,6 @@ public class CacheAutoConfigurationTests {
}
@Configuration
@EnableCaching
static class GuavaCacheBuilderConfiguration {
@Bean
CacheBuilder<Object, Object> cacheBuilder() {
return CacheBuilder.newBuilder().recordStats();
}
}
@Configuration
@EnableCaching
static class CaffeineCacheBuilderConfiguration {
@@ -1079,12 +1025,6 @@ public class CacheAutoConfigurationTests {
};
}
@Bean
public CacheManagerCustomizer<GuavaCacheManager> guavaCacheManagerCustomizer() {
return new CacheManagerTestCustomizer<GuavaCacheManager>() {
};
}
@Bean
public CacheManagerCustomizer<CaffeineCacheManager> caffeineCacheManagerCustomizer() {
return new CacheManagerTestCustomizer<CaffeineCacheManager>() {

View File

@@ -1,246 +0,0 @@
/*
* 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.autoconfigure.velocity;
import java.io.File;
import java.io.StringWriter;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
/**
* Tests for {@link VelocityAutoConfiguration}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
@SuppressWarnings("deprecation")
public class VelocityAutoConfigurationTests {
@Rule
public OutputCapture output = new OutputCapture();
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
@Before
public void setupContext() {
this.context.setServletContext(new MockServletContext());
}
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void defaultConfiguration() {
registerAndRefreshContext();
assertThat(this.context.getBean(VelocityViewResolver.class)).isNotNull();
assertThat(this.context.getBean(VelocityConfigurer.class)).isNotNull();
}
@Test
public void nonExistentTemplateLocation() {
registerAndRefreshContext(
"spring.velocity.resourceLoaderPath:" + "classpath:/does-not-exist/");
this.output.expect(containsString("Cannot find template location"));
}
@Test
public void emptyTemplateLocation() {
new File("target/test-classes/templates/empty-directory").mkdir();
registerAndRefreshContext("spring.velocity.resourceLoaderPath:"
+ "classpath:/templates/empty-directory/");
}
@Test
public void defaultViewResolution() throws Exception {
registerAndRefreshContext();
MockHttpServletResponse response = render("home");
String result = response.getContentAsString();
assertThat(result).contains("home");
assertThat(response.getContentType()).isEqualTo("text/html;charset=UTF-8");
}
@Test
public void customContentType() throws Exception {
registerAndRefreshContext("spring.velocity.contentType:application/json");
MockHttpServletResponse response = render("home");
String result = response.getContentAsString();
assertThat(result).contains("home");
assertThat(response.getContentType()).isEqualTo("application/json;charset=UTF-8");
}
@Test
public void customCharset() throws Exception {
registerAndRefreshContext("spring.velocity.charset:ISO-8859-1");
assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine()
.getProperty("input.encoding")).isEqualTo("ISO-8859-1");
}
@Test
public void customPrefix() throws Exception {
registerAndRefreshContext("spring.velocity.prefix:prefix/");
MockHttpServletResponse response = render("prefixed");
String result = response.getContentAsString();
assertThat(result).contains("prefixed");
}
@Test
public void customSuffix() throws Exception {
registerAndRefreshContext("spring.velocity.suffix:.freemarker");
MockHttpServletResponse response = render("suffixed");
String result = response.getContentAsString();
assertThat(result).contains("suffixed");
}
@Test
public void customTemplateLoaderPath() throws Exception {
registerAndRefreshContext(
"spring.velocity.resourceLoaderPath:classpath:/custom-templates/");
MockHttpServletResponse response = render("custom");
String result = response.getContentAsString();
assertThat(result).contains("custom");
}
@Test
public void disableCache() {
registerAndRefreshContext("spring.velocity.cache:false");
assertThat(this.context.getBean(VelocityViewResolver.class).getCacheLimit())
.isEqualTo(0);
}
@Test
public void customVelocitySettings() {
registerAndRefreshContext(
"spring.velocity.properties.directive.parse.max.depth:10");
assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine()
.getProperty("directive.parse.max.depth")).isEqualTo("10");
}
@Test
public void renderTemplate() throws Exception {
registerAndRefreshContext();
VelocityConfigurer velocity = this.context.getBean(VelocityConfigurer.class);
StringWriter writer = new StringWriter();
Template template = velocity.getVelocityEngine().getTemplate("message.vm");
template.process();
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("greeting", "Hello World");
template.merge(velocityContext, writer);
assertThat(writer.toString()).contains("Hello World");
}
@Test
public void renderNonWebAppTemplate() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
VelocityAutoConfiguration.class);
try {
VelocityEngine velocity = context.getBean(VelocityEngine.class);
StringWriter writer = new StringWriter();
Template template = velocity.getTemplate("message.vm");
template.process();
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("greeting", "Hello World");
template.merge(velocityContext, writer);
assertThat(writer.toString()).contains("Hello World");
}
finally {
context.close();
}
}
@Test
public void usesEmbeddedVelocityViewResolver() {
registerAndRefreshContext("spring.velocity.toolbox:/toolbox.xml");
VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class);
assertThat(resolver).isInstanceOf(EmbeddedVelocityViewResolver.class);
}
@Test
public void registerResourceHandlingFilterDisabledByDefault() throws Exception {
registerAndRefreshContext();
assertThat(this.context.getBeansOfType(ResourceUrlEncodingFilter.class))
.isEmpty();
}
@Test
public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled()
throws Exception {
registerAndRefreshContext("spring.resources.chain.enabled:true");
assertThat(this.context.getBean(ResourceUrlEncodingFilter.class)).isNotNull();
}
@Test
public void allowSessionOverride() {
registerAndRefreshContext("spring.velocity.allow-session-override:true");
AbstractTemplateViewResolver viewResolver = this.context
.getBean(VelocityViewResolver.class);
assertThat(viewResolver).extracting("allowSessionOverride").containsExactly(true);
}
private void registerAndRefreshContext(String... env) {
EnvironmentTestUtils.addEnvironment(this.context, env);
this.context.register(VelocityAutoConfiguration.class);
this.context.refresh();
}
public String getGreeting() {
return "Hello World";
}
private MockHttpServletResponse render(String viewName) throws Exception {
VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class);
View view = resolver.resolveViewName(viewName, Locale.UK);
assertThat(view).isNotNull();
HttpServletRequest request = new MockHttpServletRequest();
request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
this.context);
MockHttpServletResponse response = new MockHttpServletResponse();
view.render(null, request, response);
return response;
}
}

View File

@@ -1,75 +0,0 @@
/*
* 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.autoconfigure.velocity;
import org.junit.Test;
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link VelocityTemplateAvailabilityProvider}.
*
* @author Andy Wilkinson
*/
@SuppressWarnings("deprecation")
public class VelocityTemplateAvailabilityProviderTests {
private final TemplateAvailabilityProvider provider = new VelocityTemplateAvailabilityProvider();
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
private final MockEnvironment environment = new MockEnvironment();
@Test
public void availabilityOfTemplateInDefaultLocation() {
assertThat(this.provider.isTemplateAvailable("home", this.environment,
getClass().getClassLoader(), this.resourceLoader)).isTrue();
}
@Test
public void availabilityOfTemplateThatDoesNotExist() {
assertThat(this.provider.isTemplateAvailable("whatever", this.environment,
getClass().getClassLoader(), this.resourceLoader)).isFalse();
}
@Test
public void availabilityOfTemplateWithCustomLoaderPath() {
this.environment.setProperty("spring.velocity.resourceLoaderPath",
"classpath:/custom-templates/");
assertThat(this.provider.isTemplateAvailable("custom", this.environment,
getClass().getClassLoader(), this.resourceLoader)).isTrue();
}
@Test
public void availabilityOfTemplateWithCustomPrefix() {
this.environment.setProperty("spring.velocity.prefix", "prefix/");
assertThat(this.provider.isTemplateAvailable("prefixed", this.environment,
getClass().getClassLoader(), this.resourceLoader)).isTrue();
}
@Test
public void availabilityOfTemplateWithCustomSuffix() {
this.environment.setProperty("spring.velocity.suffix", ".freemarker");
assertThat(this.provider.isTemplateAvailable("suffixed", this.environment,
getClass().getClassLoader(), this.resourceLoader)).isTrue();
}
}

View File

@@ -29,7 +29,9 @@ import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
@@ -61,6 +63,8 @@ public class HttpMessageConvertersTests {
SourceHttpMessageConverter.class,
AllEncompassingFormHttpMessageConverter.class,
MappingJackson2HttpMessageConverter.class,
MappingJackson2SmileHttpMessageConverter.class,
MappingJackson2CborHttpMessageConverter.class,
MappingJackson2XmlHttpMessageConverter.class);
}
@@ -131,7 +135,9 @@ public class HttpMessageConvertersTests {
StringHttpMessageConverter.class, ResourceHttpMessageConverter.class,
SourceHttpMessageConverter.class,
AllEncompassingFormHttpMessageConverter.class,
MappingJackson2HttpMessageConverter.class);
MappingJackson2HttpMessageConverter.class,
MappingJackson2SmileHttpMessageConverter.class,
MappingJackson2CborHttpMessageConverter.class);
}
@Test
@@ -158,7 +164,8 @@ public class HttpMessageConvertersTests {
assertThat(converterClasses).containsExactly(ByteArrayHttpMessageConverter.class,
StringHttpMessageConverter.class, ResourceHttpMessageConverter.class,
SourceHttpMessageConverter.class,
MappingJackson2HttpMessageConverter.class);
MappingJackson2HttpMessageConverter.class,
MappingJackson2SmileHttpMessageConverter.class);
}
@SuppressWarnings("unchecked")