|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.session;
|
18 | 18 |
|
| 19 | +import javax.sql.DataSource; |
| 20 | + |
| 21 | +import org.apache.commons.dbcp2.BasicDataSource; |
19 | 22 | import org.junit.jupiter.api.Test;
|
20 | 23 |
|
21 | 24 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
29 | 32 | import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
30 | 33 | import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
31 | 34 | import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.context.annotation.Primary; |
32 | 38 | import org.springframework.jdbc.BadSqlGrammarException;
|
33 | 39 | import org.springframework.jdbc.core.JdbcOperations;
|
34 | 40 | import org.springframework.session.FlushMode;
|
|
37 | 43 | import org.springframework.session.data.redis.RedisIndexedSessionRepository;
|
38 | 44 | import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
|
39 | 45 | import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
|
| 46 | +import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource; |
40 | 47 |
|
41 | 48 | import static org.assertj.core.api.Assertions.assertThat;
|
42 | 49 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -170,4 +177,42 @@ void customSaveMode() {
|
170 | 177 | });
|
171 | 178 | }
|
172 | 179 |
|
| 180 | + @Test |
| 181 | + void sessionDataSourceIsUsedWhenAvailable() { |
| 182 | + this.contextRunner.withUserConfiguration(SessionDataSourceConfiguration.class) |
| 183 | + .withPropertyValues("spring.session.store-type=jdbc").run((context) -> { |
| 184 | + JdbcIndexedSessionRepository repository = validateSessionRepository(context, |
| 185 | + JdbcIndexedSessionRepository.class); |
| 186 | + assertThat(repository).extracting("jdbcOperations").extracting("dataSource") |
| 187 | + .isEqualTo(context.getBean("sessionDataSource")); |
| 188 | + assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy( |
| 189 | + () -> context.getBean(JdbcOperations.class).queryForList("select * from SPRING_SESSION")); |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + @Configuration |
| 194 | + static class SessionDataSourceConfiguration { |
| 195 | + |
| 196 | + @Bean |
| 197 | + @SpringSessionDataSource |
| 198 | + DataSource sessionDataSource() { |
| 199 | + BasicDataSource dataSource = new BasicDataSource(); |
| 200 | + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); |
| 201 | + dataSource.setUrl("jdbc:hsqldb:mem:sessiondb"); |
| 202 | + dataSource.setUsername("sa"); |
| 203 | + return dataSource; |
| 204 | + } |
| 205 | + |
| 206 | + @Bean |
| 207 | + @Primary |
| 208 | + DataSource mainDataSource() { |
| 209 | + BasicDataSource dataSource = new BasicDataSource(); |
| 210 | + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); |
| 211 | + dataSource.setUrl("jdbc:hsqldb:mem:maindb"); |
| 212 | + dataSource.setUsername("sa"); |
| 213 | + return dataSource; |
| 214 | + } |
| 215 | + |
| 216 | + } |
| 217 | + |
173 | 218 | }
|
0 commit comments