diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/incrementer/H2SequenceMaxValueIncrementerTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/incrementer/H2SequenceMaxValueIncrementerTests.java index 18b1cde5103..512e82dea0c 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/incrementer/H2SequenceMaxValueIncrementerTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/incrementer/H2SequenceMaxValueIncrementerTests.java @@ -16,12 +16,22 @@ package org.springframework.jdbc.support.incrementer; +import java.util.UUID; + +import javax.sql.DataSource; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.SimpleDriverDataSource; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; import static org.assertj.core.api.Assertions.assertThat; @@ -34,8 +44,16 @@ import static org.assertj.core.api.Assertions.assertThat; */ class H2SequenceMaxValueIncrementerTests { + /** + * Tests that the incrementer works when using the JDBC connection URL used + * in the {@code H2EmbeddedDatabaseConfigurer} which is used transparently + * when using Spring's {@link EmbeddedDatabaseBuilder}. + * + *
In other words, this tests compatibility with the default H2 + * compatibility mode. + */ @Test - void incrementsSequence() { + void incrementsSequenceUsingH2EmbeddedDatabaseConfigurer() { EmbeddedDatabase database = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) .generateUniqueName(true) @@ -52,4 +70,30 @@ class H2SequenceMaxValueIncrementerTests { database.shutdown(); } + /** + * Tests that the incrementer works when using all supported H2 compatibility modes. + * + *
The following modes are only supported with H2 2.x or higher: STRICT, LEGACY, MariaDB + */ + @ParameterizedTest + @ValueSource(strings = { "DB2", "Derby", "HSQLDB", "MSSQLServer", "MySQL", "Oracle", "PostgreSQL" }) + void incrementsSequenceWithExplicitH2CompatibilityMode(String compatibilityMode) { + String connectionUrl = String.format("jdbc:h2:mem:%s;MODE=%s", UUID.randomUUID().toString(), compatibilityMode); + DataSource dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", ""); + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource); + TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); + + transactionTemplate.executeWithoutResult(status -> { + jdbcTemplate.execute("CREATE SEQUENCE SEQ"); + assertThat(jdbcTemplate.queryForObject("values next value for SEQ", int.class)).isEqualTo(1); + + H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(dataSource, "SEQ"); + assertThat(incrementer.nextIntValue()).isEqualTo(2); + assertThat(incrementer.nextStringValue()).isEqualTo("3"); + }); + + jdbcTemplate.execute("SHUTDOWN"); + } + }