diff --git a/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java b/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java index 720c97b..2a9d7e0 100644 --- a/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java +++ b/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java @@ -75,7 +75,8 @@ public class SimpleR2dbcRepository implements ReactiveCrudRepository repository; - private JdbcTemplate jdbc; + SimpleR2dbcRepository repository; + JdbcTemplate jdbc; @Before public void before() { @@ -372,9 +373,26 @@ public abstract class AbstractSimpleR2dbcRepositoryIntegrationTests extends R2db @Table("legoset") @AllArgsConstructor @NoArgsConstructor - static class LegoSet { + static class LegoSet implements Persistable { @Id Integer id; String name; Integer manual; + + @Override + public boolean isNew() { + return id == null; + } + } + + static class AlwaysNewLegoSet extends LegoSet { + + AlwaysNewLegoSet(Integer id, String name, Integer manual) { + super(id, name, manual); + } + + @Override + public boolean isNew() { + return true; + } } } diff --git a/src/test/java/org/springframework/data/r2dbc/repository/support/H2SimpleR2dbcRepositoryIntegrationTests.java b/src/test/java/org/springframework/data/r2dbc/repository/support/H2SimpleR2dbcRepositoryIntegrationTests.java new file mode 100644 index 0000000..0c7f621 --- /dev/null +++ b/src/test/java/org/springframework/data/r2dbc/repository/support/H2SimpleR2dbcRepositoryIntegrationTests.java @@ -0,0 +1,86 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.r2dbc.repository.support; + +import static org.assertj.core.api.Assertions.*; + +import io.r2dbc.spi.ConnectionFactory; +import reactor.test.StepVerifier; + +import java.util.Map; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.context.annotation.Configuration; +import org.springframework.dao.DataAccessException; +import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration; +import org.springframework.data.r2dbc.testing.H2TestSupport; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration tests for {@link SimpleR2dbcRepository} against H2. + * + * @author Mark Paluch + */ +@RunWith(SpringRunner.class) +@ContextConfiguration +public class H2SimpleR2dbcRepositoryIntegrationTests extends AbstractSimpleR2dbcRepositoryIntegrationTests { + + @Configuration + static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration { + + @Override + public ConnectionFactory connectionFactory() { + return H2TestSupport.createConnectionFactory(); + } + } + + @Override + protected DataSource createDataSource() { + return H2TestSupport.createDataSource(); + } + + @Override + protected String getCreateTableStatement() { + return H2TestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION; + } + + @Test // gh-90 + public void shouldInsertNewObjectWithGivenId() { + + try { + this.jdbc.execute("DROP TABLE legoset"); + } catch (DataAccessException e) {} + + this.jdbc.execute(H2TestSupport.CREATE_TABLE_LEGOSET); + + AlwaysNewLegoSet legoSet = new AlwaysNewLegoSet(9999, "SCHAUFELRADBAGGER", 12); + + repository.save(legoSet) // + .as(StepVerifier::create) // + .consumeNextWith(actual -> { + + assertThat(actual.getId()).isEqualTo(9999); + }).verifyComplete(); + + Map map = jdbc.queryForMap("SELECT * FROM legoset"); + assertThat(map).containsEntry("name", "SCHAUFELRADBAGGER").containsEntry("manual", 12).containsKey("id"); + } +} diff --git a/src/test/java/org/springframework/data/r2dbc/testing/H2TestSupport.java b/src/test/java/org/springframework/data/r2dbc/testing/H2TestSupport.java new file mode 100644 index 0000000..7e986e3 --- /dev/null +++ b/src/test/java/org/springframework/data/r2dbc/testing/H2TestSupport.java @@ -0,0 +1,71 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.r2dbc.testing; + +import io.r2dbc.h2.H2ConnectionConfiguration; +import io.r2dbc.h2.H2ConnectionFactory; +import io.r2dbc.spi.ConnectionFactory; + +import javax.sql.DataSource; + +import org.springframework.jdbc.datasource.DriverManagerDataSource; + +/** + * Utility class for testing against H2. + * + * @author Mark Paluch + */ +public class H2TestSupport { + + public static String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n" // + + " id integer CONSTRAINT id PRIMARY KEY,\n" // + + " name varchar(255) NOT NULL,\n" // + + " manual integer NULL\n" // + + ");"; + + public static String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" // + + " id serial CONSTRAINT id PRIMARY KEY,\n" // + + " name varchar(255) NOT NULL,\n" // + + " manual integer NULL\n" // + + ");"; + + /** + * Creates a new {@link ConnectionFactory}. + */ + public static ConnectionFactory createConnectionFactory() { + + return new H2ConnectionFactory(H2ConnectionConfiguration.builder() // + .inMemory("r2dbc") // + .username("sa") // + .password("") // + .option("DB_CLOSE_DELAY=-1").build()); + } + + /** + * Creates a new {@link DataSource}. + */ + public static DataSource createDataSource() { + + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + + dataSource.setUsername("sa"); + dataSource.setPassword(""); + dataSource.setUrl("jdbc:h2:mem:r2dbc;DB_CLOSE_DELAY=-1"); + + return dataSource; + } + +}