|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.jdbc.support.incrementer; |
| 18 | + |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +import javax.sql.DataSource; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.ValueSource; |
| 26 | + |
| 27 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 28 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 29 | +import org.springframework.jdbc.datasource.SimpleDriverDataSource; |
| 30 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; |
| 31 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 32 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 33 | +import org.springframework.transaction.PlatformTransactionManager; |
| 34 | +import org.springframework.transaction.support.TransactionTemplate; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +/** |
| 39 | + * Integration tests for {@link H2SequenceMaxValueIncrementer}. |
| 40 | + * |
| 41 | + * @author Henning Pöttker |
| 42 | + * @author Sam Brannen |
| 43 | + * @since 5.3.15 |
| 44 | + */ |
| 45 | +class H2SequenceMaxValueIncrementerTests { |
| 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 | + */ |
| 55 | + @Test |
| 56 | + void incrementsSequenceUsingH2EmbeddedDatabaseConfigurer() { |
| 57 | + EmbeddedDatabase database = new EmbeddedDatabaseBuilder() |
| 58 | + .setType(EmbeddedDatabaseType.H2) |
| 59 | + .generateUniqueName(true) |
| 60 | + .addScript("classpath:/org/springframework/jdbc/support/incrementer/schema.sql") |
| 61 | + .build(); |
| 62 | + |
| 63 | + JdbcTemplate jdbcTemplate = new JdbcTemplate(database); |
| 64 | + assertThat(jdbcTemplate.queryForObject("values next value for SEQ", int.class)).isEqualTo(1); |
| 65 | + |
| 66 | + H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(database, "SEQ"); |
| 67 | + assertThat(incrementer.nextIntValue()).isEqualTo(2); |
| 68 | + assertThat(incrementer.nextStringValue()).isEqualTo("3"); |
| 69 | + |
| 70 | + database.shutdown(); |
| 71 | + } |
| 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 | + |
| 99 | +} |
0 commit comments