Search in sources :

Example 1 with JdbcIndexedSessionRepository

use of org.springframework.session.jdbc.JdbcIndexedSessionRepository in project spring-boot by spring-projects.

the class SessionAutoConfigurationJdbcTests method sessionDataSourceIsUsedWhenAvailable.

@Test
void sessionDataSourceIsUsedWhenAvailable() {
    this.contextRunner.withUserConfiguration(SessionDataSourceConfiguration.class).withPropertyValues("spring.session.store-type=jdbc").run((context) -> {
        JdbcIndexedSessionRepository repository = validateSessionRepository(context, JdbcIndexedSessionRepository.class);
        DataSource sessionDataSource = context.getBean("sessionDataSource", DataSource.class);
        assertThat(repository).extracting("jdbcOperations.dataSource").isEqualTo(sessionDataSource);
        assertThat(context.getBean(JdbcSessionDataSourceScriptDatabaseInitializer.class)).hasFieldOrPropertyWithValue("dataSource", sessionDataSource);
        assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() -> context.getBean(JdbcOperations.class).queryForList("select * from SPRING_SESSION"));
    });
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) JdbcIndexedSessionRepository(org.springframework.session.jdbc.JdbcIndexedSessionRepository) SpringSessionDataSource(org.springframework.session.jdbc.config.annotation.SpringSessionDataSource) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Test(org.junit.jupiter.api.Test)

Example 2 with JdbcIndexedSessionRepository

use of org.springframework.session.jdbc.JdbcIndexedSessionRepository in project spring-boot by spring-projects.

the class SessionAutoConfigurationJdbcTests method customTimeout.

@Test
void customTimeout() {
    this.contextRunner.withPropertyValues("spring.session.store-type=jdbc", "spring.session.timeout=1m").run((context) -> {
        JdbcIndexedSessionRepository repository = validateSessionRepository(context, JdbcIndexedSessionRepository.class);
        assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", 60);
    });
}
Also used : JdbcIndexedSessionRepository(org.springframework.session.jdbc.JdbcIndexedSessionRepository) Test(org.junit.jupiter.api.Test)

Example 3 with JdbcIndexedSessionRepository

use of org.springframework.session.jdbc.JdbcIndexedSessionRepository in project spring-boot by spring-projects.

the class SessionAutoConfigurationJdbcTests method validateDefaultConfig.

private void validateDefaultConfig(AssertableWebApplicationContext context) {
    JdbcIndexedSessionRepository repository = validateSessionRepository(context, JdbcIndexedSessionRepository.class);
    assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", (int) new ServerProperties().getServlet().getSession().getTimeout().getSeconds());
    assertThat(repository).hasFieldOrPropertyWithValue("tableName", "SPRING_SESSION");
    assertThat(context.getBean(JdbcSessionProperties.class).getInitializeSchema()).isEqualTo(DatabaseInitializationMode.EMBEDDED);
    assertThat(context.getBean(JdbcOperations.class).queryForList("select * from SPRING_SESSION")).isEmpty();
    SpringBootJdbcHttpSessionConfiguration configuration = context.getBean(SpringBootJdbcHttpSessionConfiguration.class);
    assertThat(configuration).hasFieldOrPropertyWithValue("cleanupCron", "0 * * * * *");
}
Also used : ServerProperties(org.springframework.boot.autoconfigure.web.ServerProperties) JdbcIndexedSessionRepository(org.springframework.session.jdbc.JdbcIndexedSessionRepository) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) SpringBootJdbcHttpSessionConfiguration(org.springframework.boot.autoconfigure.session.JdbcSessionConfiguration.SpringBootJdbcHttpSessionConfiguration)

Example 4 with JdbcIndexedSessionRepository

use of org.springframework.session.jdbc.JdbcIndexedSessionRepository in project spring-boot by spring-projects.

the class SessionAutoConfigurationJdbcTests method disableDataSourceInitializer.

@Test
void disableDataSourceInitializer() {
    this.contextRunner.withPropertyValues("spring.session.store-type=jdbc", "spring.session.jdbc.initialize-schema=never").run((context) -> {
        assertThat(context).doesNotHaveBean(JdbcSessionDataSourceScriptDatabaseInitializer.class);
        JdbcIndexedSessionRepository repository = validateSessionRepository(context, JdbcIndexedSessionRepository.class);
        assertThat(repository).hasFieldOrPropertyWithValue("tableName", "SPRING_SESSION");
        assertThat(context.getBean(JdbcSessionProperties.class).getInitializeSchema()).isEqualTo(DatabaseInitializationMode.NEVER);
        assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() -> context.getBean(JdbcOperations.class).queryForList("select * from SPRING_SESSION"));
    });
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) JdbcIndexedSessionRepository(org.springframework.session.jdbc.JdbcIndexedSessionRepository) Test(org.junit.jupiter.api.Test)

Example 5 with JdbcIndexedSessionRepository

use of org.springframework.session.jdbc.JdbcIndexedSessionRepository in project spring-boot by spring-projects.

the class SessionAutoConfigurationJdbcTests method customTableName.

@Test
void customTableName() {
    this.contextRunner.withPropertyValues("spring.session.store-type=jdbc", "spring.session.jdbc.table-name=FOO_BAR", "spring.session.jdbc.schema=classpath:session/custom-schema-h2.sql").run((context) -> {
        JdbcIndexedSessionRepository repository = validateSessionRepository(context, JdbcIndexedSessionRepository.class);
        assertThat(repository).hasFieldOrPropertyWithValue("tableName", "FOO_BAR");
        assertThat(context.getBean(JdbcSessionProperties.class).getInitializeSchema()).isEqualTo(DatabaseInitializationMode.EMBEDDED);
        assertThat(context.getBean(JdbcOperations.class).queryForList("select * from FOO_BAR")).isEmpty();
    });
}
Also used : JdbcIndexedSessionRepository(org.springframework.session.jdbc.JdbcIndexedSessionRepository) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcIndexedSessionRepository (org.springframework.session.jdbc.JdbcIndexedSessionRepository)5 Test (org.junit.jupiter.api.Test)4 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)2 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)2 DataSource (javax.sql.DataSource)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 SpringBootJdbcHttpSessionConfiguration (org.springframework.boot.autoconfigure.session.JdbcSessionConfiguration.SpringBootJdbcHttpSessionConfiguration)1 ServerProperties (org.springframework.boot.autoconfigure.web.ServerProperties)1 SpringSessionDataSource (org.springframework.session.jdbc.config.annotation.SpringSessionDataSource)1