16
16
17
17
package org .springframework .jdbc .support .incrementer ;
18
18
19
+ import java .util .UUID ;
20
+
21
+ import javax .sql .DataSource ;
22
+
19
23
import org .junit .jupiter .api .Test ;
24
+ import org .junit .jupiter .params .ParameterizedTest ;
25
+ import org .junit .jupiter .params .provider .ValueSource ;
20
26
21
27
import org .springframework .jdbc .core .JdbcTemplate ;
28
+ import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
29
+ import org .springframework .jdbc .datasource .SimpleDriverDataSource ;
22
30
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
23
31
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
24
32
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
33
+ import org .springframework .transaction .PlatformTransactionManager ;
34
+ import org .springframework .transaction .support .TransactionTemplate ;
25
35
26
36
import static org .assertj .core .api .Assertions .assertThat ;
27
37
34
44
*/
35
45
class H2SequenceMaxValueIncrementerTests {
36
46
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
+ */
37
55
@ Test
38
- void incrementsSequence () {
56
+ void incrementsSequenceUsingH2EmbeddedDatabaseConfigurer () {
39
57
EmbeddedDatabase database = new EmbeddedDatabaseBuilder ()
40
58
.setType (EmbeddedDatabaseType .H2 )
41
59
.generateUniqueName (true )
@@ -52,4 +70,30 @@ void incrementsSequence() {
52
70
database .shutdown ();
53
71
}
54
72
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
+
55
99
}
0 commit comments