Skip to content

Commit 75e1811

Browse files
committed
Ensure H2SequenceMaxValueIncrementer works with all H2 compatibility modes
See gh-27870
1 parent 18781e5 commit 75e1811

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

spring-jdbc/src/test/java/org/springframework/jdbc/support/incrementer/H2SequenceMaxValueIncrementerTests.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616

1717
package org.springframework.jdbc.support.incrementer;
1818

19+
import java.util.UUID;
20+
21+
import javax.sql.DataSource;
22+
1923
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
2026

2127
import org.springframework.jdbc.core.JdbcTemplate;
28+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
29+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
2230
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
2331
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2432
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
33+
import org.springframework.transaction.PlatformTransactionManager;
34+
import org.springframework.transaction.support.TransactionTemplate;
2535

2636
import static org.assertj.core.api.Assertions.assertThat;
2737

@@ -34,8 +44,16 @@
3444
*/
3545
class H2SequenceMaxValueIncrementerTests {
3646

47+
/**
48+
* Tests that the incrementer works when using the JDBC connection URL used
49+
* in the {@code H2EmbeddedDatabaseConfigurer} which is used transparently
50+
* when using Spring's {@link EmbeddedDatabaseBuilder}.
51+
*
52+
* <p>In other words, this tests compatibility with the default H2
53+
* <em>compatibility mode</em>.
54+
*/
3755
@Test
38-
void incrementsSequence() {
56+
void incrementsSequenceUsingH2EmbeddedDatabaseConfigurer() {
3957
EmbeddedDatabase database = new EmbeddedDatabaseBuilder()
4058
.setType(EmbeddedDatabaseType.H2)
4159
.generateUniqueName(true)
@@ -52,4 +70,30 @@ void incrementsSequence() {
5270
database.shutdown();
5371
}
5472

73+
/**
74+
* Tests that the incrementer works when using all supported H2 <em>compatibility modes</em>.
75+
*
76+
* <p>The following modes are only supported with H2 2.x or higher: STRICT, LEGACY, MariaDB
77+
*/
78+
@ParameterizedTest
79+
@ValueSource(strings = { "DB2", "Derby", "HSQLDB", "MSSQLServer", "MySQL", "Oracle", "PostgreSQL" })
80+
void incrementsSequenceWithExplicitH2CompatibilityMode(String compatibilityMode) {
81+
String connectionUrl = String.format("jdbc:h2:mem:%s;MODE=%s", UUID.randomUUID().toString(), compatibilityMode);
82+
DataSource dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", "");
83+
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
84+
PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
85+
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
86+
87+
transactionTemplate.executeWithoutResult(status -> {
88+
jdbcTemplate.execute("CREATE SEQUENCE SEQ");
89+
assertThat(jdbcTemplate.queryForObject("values next value for SEQ", int.class)).isEqualTo(1);
90+
91+
H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(dataSource, "SEQ");
92+
assertThat(incrementer.nextIntValue()).isEqualTo(2);
93+
assertThat(incrementer.nextStringValue()).isEqualTo("3");
94+
});
95+
96+
jdbcTemplate.execute("SHUTDOWN");
97+
}
98+
5599
}

0 commit comments

Comments
 (0)