|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.core.simple; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.core.io.ClassRelativeResourceLoader; |
| 24 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; |
| 25 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 26 | +import org.springframework.jdbc.datasource.init.DatabasePopulator; |
| 27 | +import org.springframework.jdbc.support.GeneratedKeyHolder; |
| 28 | +import org.springframework.jdbc.support.KeyHolder; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.H2; |
| 32 | + |
| 33 | +/** |
| 34 | + * Integration tests for {@link JdbcClient} using an embedded H2 database. |
| 35 | + * |
| 36 | + * @author Sam Brannen |
| 37 | + * @since 6.1 |
| 38 | + * @see JdbcClientIndexedParameterTests |
| 39 | + * @see JdbcClientNamedParameterTests |
| 40 | + */ |
| 41 | +class JdbcClientIntegrationTests { |
| 42 | + |
| 43 | + private static final String INSERT_WITH_NAMED_PARAMS = |
| 44 | + "INSERT INTO users (first_name, last_name) VALUES(:firstName, :lastName)"; |
| 45 | + private static final String INSERT_WITH_POSITIONAL_PARAMS = |
| 46 | + "INSERT INTO users (first_name, last_name) VALUES(?, ?)"; |
| 47 | + |
| 48 | + |
| 49 | + private final EmbeddedDatabase embeddedDatabase = |
| 50 | + new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(DatabasePopulator.class)) |
| 51 | + .generateUniqueName(true) |
| 52 | + .setType(H2) |
| 53 | + .addScripts("users-schema.sql", "users-data.sql") |
| 54 | + .build(); |
| 55 | + |
| 56 | + private final JdbcClient jdbcClient = JdbcClient.create(this.embeddedDatabase); |
| 57 | + |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void checkDatabase() { |
| 61 | + assertNumUsers(1); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterEach |
| 65 | + void shutdownDatabase() { |
| 66 | + this.embeddedDatabase.shutdown(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void updateWithGeneratedKeysAndPositionalParameters() { |
| 71 | + int expectedId = 2; |
| 72 | + String firstName = "Jane"; |
| 73 | + String lastName = "Smith"; |
| 74 | + |
| 75 | + KeyHolder generatedKeyHolder = new GeneratedKeyHolder(); |
| 76 | + |
| 77 | + int rowsAffected = this.jdbcClient.sql(INSERT_WITH_POSITIONAL_PARAMS) |
| 78 | + .params(firstName, lastName) |
| 79 | + .update(generatedKeyHolder); |
| 80 | + |
| 81 | + assertThat(rowsAffected).isEqualTo(1); |
| 82 | + assertThat(generatedKeyHolder.getKey()).isEqualTo(expectedId); |
| 83 | + assertNumUsers(2); |
| 84 | + assertUser(expectedId, firstName, lastName); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void updateWithGeneratedKeysAndNamedParameters() { |
| 89 | + int expectedId = 2; |
| 90 | + String firstName = "Jane"; |
| 91 | + String lastName = "Smith"; |
| 92 | + |
| 93 | + KeyHolder generatedKeyHolder = new GeneratedKeyHolder(); |
| 94 | + |
| 95 | + int rowsAffected = this.jdbcClient.sql(INSERT_WITH_NAMED_PARAMS) |
| 96 | + .param("firstName", firstName) |
| 97 | + .param("lastName", lastName) |
| 98 | + .update(generatedKeyHolder); |
| 99 | + |
| 100 | + assertThat(rowsAffected).isEqualTo(1); |
| 101 | + assertThat(generatedKeyHolder.getKey()).isEqualTo(expectedId); |
| 102 | + assertNumUsers(2); |
| 103 | + assertUser(expectedId, firstName, lastName); |
| 104 | + } |
| 105 | + |
| 106 | + private void assertNumUsers(long count) { |
| 107 | + long numUsers = this.jdbcClient.sql("select count(id) from users").query(Long.class).single(); |
| 108 | + assertThat(numUsers).isEqualTo(count); |
| 109 | + } |
| 110 | + |
| 111 | + private void assertUser(long id, String firstName, String lastName) { |
| 112 | + User user = this.jdbcClient.sql("select * from users where id = ?").param(id).query(User.class).single(); |
| 113 | + assertThat(user).isEqualTo(new User(id, firstName, lastName)); |
| 114 | + } |
| 115 | + |
| 116 | + record User(long id, String firstName, String lastName) {}; |
| 117 | + |
| 118 | +} |
0 commit comments