|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 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;
|
|
28 | 31 | import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
29 | 32 | import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
30 | 33 | import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.context.annotation.Primary; |
31 | 37 | import org.springframework.jdbc.BadSqlGrammarException;
|
32 | 38 | import org.springframework.jdbc.core.JdbcOperations;
|
33 | 39 | import org.springframework.session.FlushMode;
|
|
36 | 42 | import org.springframework.session.data.redis.RedisIndexedSessionRepository;
|
37 | 43 | import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
|
38 | 44 | import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
|
| 45 | +import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource; |
39 | 46 |
|
40 | 47 | import static org.assertj.core.api.Assertions.assertThat;
|
41 | 48 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -104,6 +111,19 @@ void disableDataSourceInitializer() {
|
104 | 111 | });
|
105 | 112 | }
|
106 | 113 |
|
| 114 | + @Test |
| 115 | + void sessionDataSourceIsUsedWhenAvailable() { |
| 116 | + this.contextRunner.withUserConfiguration(SessionDataSourceConfiguration.class) |
| 117 | + .withPropertyValues("spring.session.store-type=jdbc").run((context) -> { |
| 118 | + JdbcIndexedSessionRepository repository = validateSessionRepository(context, |
| 119 | + JdbcIndexedSessionRepository.class); |
| 120 | + assertThat(repository).extracting("jdbcOperations").extracting("dataSource") |
| 121 | + .isEqualTo(context.getBean("sessionDataSource")); |
| 122 | + assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy( |
| 123 | + () -> context.getBean(JdbcOperations.class).queryForList("select * from SPRING_SESSION")); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
107 | 127 | @Test
|
108 | 128 | void customTableName() {
|
109 | 129 | this.contextRunner
|
@@ -157,4 +177,29 @@ void customSaveMode() {
|
157 | 177 | });
|
158 | 178 | }
|
159 | 179 |
|
| 180 | + @Configuration |
| 181 | + static class SessionDataSourceConfiguration { |
| 182 | + |
| 183 | + @Bean |
| 184 | + @SpringSessionDataSource |
| 185 | + DataSource sessionDataSource() { |
| 186 | + BasicDataSource dataSource = new BasicDataSource(); |
| 187 | + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); |
| 188 | + dataSource.setUrl("jdbc:hsqldb:mem:sessiondb"); |
| 189 | + dataSource.setUsername("sa"); |
| 190 | + return dataSource; |
| 191 | + } |
| 192 | + |
| 193 | + @Bean |
| 194 | + @Primary |
| 195 | + DataSource mainDataSource() { |
| 196 | + BasicDataSource dataSource = new BasicDataSource(); |
| 197 | + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); |
| 198 | + dataSource.setUrl("jdbc:hsqldb:mem:maindb"); |
| 199 | + dataSource.setUsername("sa"); |
| 200 | + return dataSource; |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + |
160 | 205 | }
|
0 commit comments