Browse Source
This commit improves the initial submission by adding more tests and more configuration options. Closes gh-5158pull/5836/merge
19 changed files with 522 additions and 318 deletions
@ -0,0 +1,76 @@ |
|||||||
|
/* |
||||||
|
* 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.session; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
|
||||||
|
import org.junit.After; |
||||||
|
|
||||||
|
import org.springframework.beans.DirectFieldAccessor; |
||||||
|
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
||||||
|
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; |
||||||
|
import org.springframework.boot.test.util.EnvironmentTestUtils; |
||||||
|
import org.springframework.session.SessionRepository; |
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* Share test utilities for {@link SessionAutoConfiguration} tests. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
public abstract class AbstractSessionAutoConfigurationTests { |
||||||
|
|
||||||
|
protected AnnotationConfigWebApplicationContext context; |
||||||
|
|
||||||
|
@After |
||||||
|
public void close() { |
||||||
|
if (this.context != null) { |
||||||
|
this.context.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected <T extends SessionRepository<?>> T validateSessionRepository(Class<T> type) { |
||||||
|
SessionRepository cacheManager = this.context.getBean(SessionRepository.class); |
||||||
|
assertThat(cacheManager).as("Wrong session repository type").isInstanceOf(type); |
||||||
|
return type.cast(cacheManager); |
||||||
|
} |
||||||
|
|
||||||
|
protected Integer getSessionTimeout(SessionRepository<?> sessionRepository) { |
||||||
|
return (Integer) new DirectFieldAccessor(sessionRepository) |
||||||
|
.getPropertyValue("defaultMaxInactiveInterval"); |
||||||
|
} |
||||||
|
|
||||||
|
protected void load(String... environment) { |
||||||
|
load(null, environment); |
||||||
|
} |
||||||
|
|
||||||
|
protected void load(Collection<Class<?>> configs, String... environment) { |
||||||
|
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); |
||||||
|
EnvironmentTestUtils.addEnvironment(ctx, environment); |
||||||
|
if (configs != null) { |
||||||
|
ctx.register(configs.toArray(new Class<?>[configs.size()])); |
||||||
|
} |
||||||
|
ctx.register(ServerPropertiesAutoConfiguration.class, |
||||||
|
SessionAutoConfiguration.class, |
||||||
|
PropertyPlaceholderAutoConfiguration.class); |
||||||
|
ctx.refresh(); |
||||||
|
this.context = ctx; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,69 @@ |
|||||||
|
/* |
||||||
|
* 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.session; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
|
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import org.springframework.beans.DirectFieldAccessor; |
||||||
|
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; |
||||||
|
import org.springframework.boot.redis.RedisTestServer; |
||||||
|
import org.springframework.session.data.redis.RedisFlushMode; |
||||||
|
import org.springframework.session.data.redis.RedisOperationsSessionRepository; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* Redis specific tests for {@link SessionAutoConfiguration}. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
public class SessionAutoConfigurationRedisTests |
||||||
|
extends AbstractSessionAutoConfigurationTests { |
||||||
|
|
||||||
|
@Rule |
||||||
|
public final RedisTestServer redis = new RedisTestServer(); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void redisSessionStore() { |
||||||
|
load(Collections.<Class<?>>singletonList(RedisAutoConfiguration.class), |
||||||
|
"spring.session.store-type=redis"); |
||||||
|
RedisOperationsSessionRepository repository = validateSessionRepository( |
||||||
|
RedisOperationsSessionRepository.class); |
||||||
|
assertThat(repository.getSessionCreatedChannelPrefix()) |
||||||
|
.isEqualTo("spring:session:event:created:"); |
||||||
|
assertThat(new DirectFieldAccessor(repository).getPropertyValue("redisFlushMode")) |
||||||
|
.isEqualTo(RedisFlushMode.ON_SAVE); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void redisSessionStoreWithCustomizations() { |
||||||
|
load(Collections.<Class<?>>singletonList(RedisAutoConfiguration.class), |
||||||
|
"spring.session.store-type=redis", |
||||||
|
"spring.session.redis.namespace=foo", |
||||||
|
"spring.session.redis.flush-mode=immediate"); |
||||||
|
RedisOperationsSessionRepository repository = validateSessionRepository( |
||||||
|
RedisOperationsSessionRepository.class); |
||||||
|
assertThat(repository.getSessionCreatedChannelPrefix()) |
||||||
|
.isEqualTo("spring:session:foo:event:created:"); |
||||||
|
assertThat(new DirectFieldAccessor(repository).getPropertyValue("redisFlushMode")) |
||||||
|
.isEqualTo(RedisFlushMode.IMMEDIATE); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,120 +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.session; |
|
||||||
|
|
||||||
import java.net.UnknownHostException; |
|
||||||
|
|
||||||
import com.hazelcast.core.Hazelcast; |
|
||||||
import com.hazelcast.core.HazelcastInstance; |
|
||||||
|
|
||||||
import com.mongodb.MongoClient; |
|
||||||
import org.junit.After; |
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value; |
|
||||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
|
||||||
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; |
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; |
|
||||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; |
|
||||||
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; |
|
||||||
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; |
|
||||||
import org.springframework.boot.test.util.EnvironmentTestUtils; |
|
||||||
import org.springframework.context.annotation.Bean; |
|
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
import org.springframework.session.MapSessionRepository; |
|
||||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository; |
|
||||||
import org.springframework.session.jdbc.JdbcOperationsSessionRepository; |
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
|
|
||||||
public class StoreTypesConfigurationTests { |
|
||||||
|
|
||||||
private AnnotationConfigWebApplicationContext context; |
|
||||||
|
|
||||||
@After |
|
||||||
public void close() { |
|
||||||
if (this.context != null) { |
|
||||||
this.context.close(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void hashMapSessionStore() { |
|
||||||
load("spring.session.store.type=hash-map"); |
|
||||||
MapSessionRepository sessionRepository = this.context.getBean(MapSessionRepository.class); |
|
||||||
assertThat(sessionRepository).isNotNull(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void jdbcSessionStore() { |
|
||||||
load(new String[] {"spring.session.store.type=jdbc"}, EmbeddedDataSourceConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class); |
|
||||||
assertThat(this.context.getBean(JdbcOperationsSessionRepository.class)).isNotNull(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void hazelcastSessionStore() { |
|
||||||
load(new String[] {"spring.session.store.type=hazelcast"}, MockHazelcastInstanceConfiguration.class); |
|
||||||
assertThat(this.context.getBean(MapSessionRepository.class)).isNotNull(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void mongoSessionStore() { |
|
||||||
load(new String[] {"spring.session.store.type=mongo", "spring.data.mongodb.port=0"}, MockMongoConfiguration.class, MongoDataAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class); |
|
||||||
assertThat(this.context.getBean(MongoOperationsSessionRepository.class)).isNotNull(); |
|
||||||
} |
|
||||||
|
|
||||||
private void load(String storeType) { |
|
||||||
load(new String[] {storeType}, null); |
|
||||||
} |
|
||||||
|
|
||||||
private void load(String[] storeType, Class<?>... config) { |
|
||||||
this.context = new AnnotationConfigWebApplicationContext(); |
|
||||||
for (String property : storeType) { |
|
||||||
EnvironmentTestUtils.addEnvironment(this.context, storeType); |
|
||||||
} |
|
||||||
if (config != null) { |
|
||||||
this.context.register(config); |
|
||||||
} |
|
||||||
this.context.register(ServerPropertiesAutoConfiguration.class, |
|
||||||
SessionAutoConfiguration.class, |
|
||||||
PropertyPlaceholderAutoConfiguration.class); |
|
||||||
this.context.refresh(); |
|
||||||
} |
|
||||||
|
|
||||||
@Configuration |
|
||||||
static class MockHazelcastInstanceConfiguration { |
|
||||||
|
|
||||||
@Bean |
|
||||||
public HazelcastInstance hazelcastInstance() { |
|
||||||
return Hazelcast.newHazelcastInstance(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Configuration |
|
||||||
static class MockMongoConfiguration { |
|
||||||
|
|
||||||
@Bean |
|
||||||
public MongoClient mongoClient(@Value("${local.mongo.port}") int port) |
|
||||||
throws UnknownHostException { |
|
||||||
return new MongoClient("localhost", port); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue