Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit edad8ca

Browse files
committedNov 22, 2018
#20 - Add Microsoft SQL Server tests.
1 parent d4aece5 commit edad8ca

13 files changed

+442
-63
lines changed
 

‎src/test/java/org/springframework/data/r2dbc/function/AbstractDatabaseClientIntegrationTests.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.junit.Before;
2929
import org.junit.Test;
30+
import org.springframework.dao.DataAccessException;
3031
import org.springframework.dao.DuplicateKeyException;
3132
import org.springframework.data.domain.PageRequest;
3233
import org.springframework.data.domain.Sort;
@@ -53,8 +54,11 @@ public void before() {
5354
connectionFactory = createConnectionFactory();
5455

5556
jdbc = createJdbcTemplate(createDataSource());
57+
58+
try {
59+
jdbc.execute("DROP TABLE legoset");
60+
} catch (DataAccessException e) {}
5661
jdbc.execute(getCreateTableStatement());
57-
jdbc.execute("DELETE FROM legoset");
5862
}
5963

6064
/**
@@ -74,7 +78,7 @@ public void before() {
7478
/**
7579
* Returns the the CREATE TABLE statement for table {@code legoset} with the following three columns:
7680
* <ul>
77-
* <li>id integer (primary key), not null, auto-increment</li>
81+
* <li>id integer (primary key), not null</li>
7882
* <li>name varchar(255), nullable</li>
7983
* <li>manual integer, nullable</li>
8084
* </ul>
@@ -98,7 +102,7 @@ public void executeInsert() {
98102
databaseClient.execute().sql(getInsertIntoLegosetStatement()) //
99103
.bind(0, 42055) //
100104
.bind(1, "SCHAUFELRADBAGGER") //
101-
.bindNull("$3", Integer.class) //
105+
.bindNull(2, Integer.class) //
102106
.fetch().rowsUpdated() //
103107
.as(StepVerifier::create) //
104108
.expectNext(1) //
@@ -117,7 +121,7 @@ public void shouldTranslateDuplicateKeyException() {
117121
databaseClient.execute().sql(getInsertIntoLegosetStatement()) //
118122
.bind(0, 42055) //
119123
.bind(1, "SCHAUFELRADBAGGER") //
120-
.bindNull("$3", Integer.class) //
124+
.bindNull(2, Integer.class) //
121125
.fetch().rowsUpdated() //
122126
.as(StepVerifier::create) //
123127
.expectErrorSatisfies(exception -> {
@@ -157,9 +161,9 @@ public void insert() {
157161
.value("name", "SCHAUFELRADBAGGER") //
158162
.nullValue("manual", Integer.class) //
159163
.exchange() //
160-
.flatMapMany(it -> it.extract((r, m) -> r.get("id", Integer.class)).all()) //
164+
.flatMapMany(FetchSpec::rowsUpdated) //
161165
.as(StepVerifier::create) //
162-
.expectNext(42055).verifyComplete();
166+
.expectNext(1).verifyComplete();
163167

164168
assertThat(jdbc.queryForMap("SELECT id, name, manual FROM legoset")).containsEntry("id", 42055);
165169
}
@@ -192,8 +196,9 @@ public void insertTypedObject() {
192196

193197
databaseClient.insert().into(LegoSet.class)//
194198
.using(legoSet).exchange() //
195-
.flatMapMany(it -> it.extract((r, m) -> r.get("id", Integer.class)).all()).as(StepVerifier::create) //
196-
.expectNext(42055).verifyComplete();
199+
.flatMapMany(FetchSpec::rowsUpdated) //
200+
.as(StepVerifier::create) //
201+
.expectNext(1).verifyComplete();
197202

198203
assertThat(jdbc.queryForMap("SELECT id, name, manual FROM legoset")).containsEntry("id", 42055);
199204
}

‎src/test/java/org/springframework/data/r2dbc/function/AbstractTransactionalDatabaseClientIntegrationTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import org.junit.Before;
3434
import org.junit.Test;
35+
import org.springframework.dao.DataAccessException;
3536
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
3637
import org.springframework.jdbc.core.JdbcTemplate;
3738
import org.springframework.transaction.NoTransactionException;
@@ -55,6 +56,9 @@ public void before() {
5556
connectionFactory = createConnectionFactory();
5657

5758
jdbc = createJdbcTemplate(createDataSource());
59+
try {
60+
jdbc.execute("DROP TABLE legoset");
61+
} catch (DataAccessException e) {}
5862
jdbc.execute(getCreateTableStatement());
5963
jdbc.execute("DELETE FROM legoset");
6064
}
@@ -76,7 +80,7 @@ public void before() {
7680
/**
7781
* Returns the the CREATE TABLE statement for table {@code legoset} with the following three columns:
7882
* <ul>
79-
* <li>id integer (primary key), not null, auto-increment</li>
83+
* <li>id integer (primary key), not null</li>
8084
* <li>name varchar(255), nullable</li>
8185
* <li>manual integer, nullable</li>
8286
* </ul>
@@ -109,7 +113,7 @@ public void executeInsertInManagedTransaction() {
109113
return db.execute().sql(getInsertIntoLegosetStatement()) //
110114
.bind(0, 42055) //
111115
.bind(1, "SCHAUFELRADBAGGER") //
112-
.bindNull("$3", Integer.class) //
116+
.bindNull(2, Integer.class) //
113117
.fetch().rowsUpdated();
114118
});
115119

@@ -128,7 +132,7 @@ public void executeInsertInAutoCommitTransaction() {
128132
Mono<Integer> integerFlux = databaseClient.execute().sql(getInsertIntoLegosetStatement()) //
129133
.bind(0, 42055) //
130134
.bind(1, "SCHAUFELRADBAGGER") //
131-
.bindNull("$3", Integer.class) //
135+
.bindNull(2, Integer.class) //
132136
.fetch().rowsUpdated();
133137

134138
integerFlux.as(StepVerifier::create) //
@@ -184,15 +188,16 @@ public void shouldRollbackTransaction() {
184188
return db.execute().sql(getInsertIntoLegosetStatement()) //
185189
.bind(0, 42055) //
186190
.bind(1, "SCHAUFELRADBAGGER") //
187-
.bindNull("$3", Integer.class) //
191+
.bindNull(2, Integer.class) //
188192
.fetch().rowsUpdated().then(Mono.error(new IllegalStateException("failed")));
189193
});
190194

191195
integerFlux.as(StepVerifier::create) //
192196
.expectError(IllegalStateException.class) //
193197
.verify();
194198

195-
assertThat(jdbc.queryForMap("SELECT count(*) FROM legoset")).containsEntry("count", 0L);
199+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
200+
assertThat(count).isEqualTo(0);
196201
}
197202

198203
@Test

‎src/test/java/org/springframework/data/r2dbc/function/PostgresDatabaseClientIntegrationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.sql.DataSource;
2121

2222
import org.junit.ClassRule;
23+
import org.junit.Ignore;
2324
import org.springframework.data.r2dbc.testing.ExternalDatabase;
2425
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
2526

@@ -51,4 +52,12 @@ protected String getCreateTableStatement() {
5152
protected String getInsertIntoLegosetStatement() {
5253
return PostgresTestSupport.INSERT_INTO_LEGOSET;
5354
}
55+
56+
@Ignore("Adding RETURNING * lets Postgres report 0 affected rows.")
57+
@Override
58+
public void insert() {}
59+
60+
@Ignore("Adding RETURNING * lets Postgres report 0 affected rows.")
61+
@Override
62+
public void insertTypedObject() {}
5463
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2018 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+
* http://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+
package org.springframework.data.r2dbc.function;
17+
18+
import io.r2dbc.spi.ConnectionFactory;
19+
20+
import javax.sql.DataSource;
21+
22+
import org.junit.ClassRule;
23+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
24+
import org.springframework.data.r2dbc.testing.SqlServerTestSupport;
25+
26+
/**
27+
* Integration tests for {@link DatabaseClient} against Microsoft SQL Server.
28+
*
29+
* @author Mark Paluch
30+
*/
31+
public class SqlServerDatabaseClientIntegrationTests extends AbstractDatabaseClientIntegrationTests {
32+
33+
@ClassRule public static final ExternalDatabase database = SqlServerTestSupport.database();
34+
35+
@Override
36+
protected DataSource createDataSource() {
37+
return SqlServerTestSupport.createDataSource(database);
38+
}
39+
40+
@Override
41+
protected ConnectionFactory createConnectionFactory() {
42+
return SqlServerTestSupport.createConnectionFactory(database);
43+
}
44+
45+
@Override
46+
protected String getCreateTableStatement() {
47+
return SqlServerTestSupport.CREATE_TABLE_LEGOSET;
48+
}
49+
50+
@Override
51+
protected String getInsertIntoLegosetStatement() {
52+
return SqlServerTestSupport.INSERT_INTO_LEGOSET;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.springframework.data.r2dbc.function;
2+
3+
import io.r2dbc.spi.ConnectionFactory;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.junit.ClassRule;
8+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
9+
import org.springframework.data.r2dbc.testing.SqlServerTestSupport;
10+
11+
/**
12+
* Integration tests for {@link TransactionalDatabaseClient} against Microsoft SQL Server.
13+
*
14+
* @author Mark Paluch
15+
*/
16+
public class SqlServerTransactionalDatabaseClientIntegrationTests
17+
extends AbstractTransactionalDatabaseClientIntegrationTests {
18+
19+
@ClassRule public static final ExternalDatabase database = SqlServerTestSupport.database();
20+
21+
@Override
22+
protected DataSource createDataSource() {
23+
return SqlServerTestSupport.createDataSource(database);
24+
}
25+
26+
@Override
27+
protected ConnectionFactory createConnectionFactory() {
28+
return SqlServerTestSupport.createConnectionFactory(database);
29+
}
30+
31+
@Override
32+
protected String getCreateTableStatement() {
33+
return SqlServerTestSupport.CREATE_TABLE_LEGOSET;
34+
}
35+
36+
@Override
37+
protected String getInsertIntoLegosetStatement() {
38+
return SqlServerTestSupport.INSERT_INTO_LEGOSET;
39+
}
40+
41+
@Override
42+
protected String getCurrentTransactionIdStatement() {
43+
return "SELECT CURRENT_TRANSACTION_ID();";
44+
}
45+
}

‎src/test/java/org/springframework/data/r2dbc/repository/AbstractR2dbcRepositoryIntegrationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.springframework.beans.factory.annotation.Autowired;
3838
import org.springframework.dao.DataAccessException;
3939
import org.springframework.data.annotation.Id;
40-
import org.springframework.data.r2dbc.dialect.PostgresDialect;
40+
import org.springframework.data.r2dbc.dialect.Database;
4141
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
4242
import org.springframework.data.r2dbc.function.TransactionalDatabaseClient;
4343
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
@@ -159,8 +159,9 @@ public void shouldFindApplyingProjection() {
159159
@Test
160160
public void shouldInsertItemsTransactional() {
161161

162+
Database database = Database.findDatabase(createConnectionFactory()).get();
162163
DefaultReactiveDataAccessStrategy dataAccessStrategy = new DefaultReactiveDataAccessStrategy(
163-
PostgresDialect.INSTANCE, new BasicRelationalConverter(mappingContext));
164+
database.latestDialect(), new BasicRelationalConverter(mappingContext));
164165
TransactionalDatabaseClient client = TransactionalDatabaseClient.builder()
165166
.connectionFactory(createConnectionFactory()).dataAccessStrategy(dataAccessStrategy).build();
166167

‎src/test/java/org/springframework/data/r2dbc/repository/PostgresR2dbcRepositoryIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.springframework.test.context.junit4.SpringRunner;
3737

3838
/**
39-
* Integration tests for {@link LegoSetRepository} using {@link R2dbcRepositoryFactory}.
39+
* Integration tests for {@link LegoSetRepository} using {@link R2dbcRepositoryFactory} against Postgres.
4040
*
4141
* @author Mark Paluch
4242
*/
@@ -69,7 +69,7 @@ protected ConnectionFactory createConnectionFactory() {
6969

7070
@Override
7171
protected String getCreateTableStatement() {
72-
return PostgresTestSupport.CREATE_TABLE_LEGOSET;
72+
return PostgresTestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION;
7373
}
7474

7575
@Override
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2018 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+
* http://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+
package org.springframework.data.r2dbc.repository;
17+
18+
import io.r2dbc.spi.ConnectionFactory;
19+
import reactor.core.publisher.Flux;
20+
import reactor.core.publisher.Mono;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.junit.ClassRule;
25+
import org.junit.Ignore;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.context.annotation.ComponentScan.Filter;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.annotation.FilterType;
30+
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
31+
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
32+
import org.springframework.data.r2dbc.repository.query.Query;
33+
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
34+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
35+
import org.springframework.data.r2dbc.testing.SqlServerTestSupport;
36+
import org.springframework.test.context.ContextConfiguration;
37+
import org.springframework.test.context.junit4.SpringRunner;
38+
39+
/**
40+
* Integration tests for {@link LegoSetRepository} using {@link R2dbcRepositoryFactory} against Microsoft SQL Server.
41+
*
42+
* @author Mark Paluch
43+
*/
44+
@RunWith(SpringRunner.class)
45+
@ContextConfiguration
46+
public class SqlServerR2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIntegrationTests {
47+
48+
@ClassRule public static final ExternalDatabase database = SqlServerTestSupport.database();
49+
50+
@Configuration
51+
@EnableR2dbcRepositories(considerNestedRepositories = true,
52+
includeFilters = @Filter(classes = SqlServerLegoSetRepository.class, type = FilterType.ASSIGNABLE_TYPE))
53+
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
54+
55+
@Override
56+
public ConnectionFactory connectionFactory() {
57+
return SqlServerTestSupport.createConnectionFactory(database);
58+
}
59+
}
60+
61+
@Override
62+
protected DataSource createDataSource() {
63+
return SqlServerTestSupport.createDataSource(database);
64+
}
65+
66+
@Override
67+
protected ConnectionFactory createConnectionFactory() {
68+
return SqlServerTestSupport.createConnectionFactory(database);
69+
}
70+
71+
@Override
72+
protected String getCreateTableStatement() {
73+
return SqlServerTestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION;
74+
}
75+
76+
@Override
77+
protected Class<? extends LegoSetRepository> getRepositoryInterfaceType() {
78+
return SqlServerLegoSetRepository.class;
79+
}
80+
81+
@Ignore("SQL server locks a SELECT COUNT so we cannot proceed.")
82+
@Override
83+
public void shouldInsertItemsTransactional() {}
84+
85+
interface SqlServerLegoSetRepository extends LegoSetRepository {
86+
87+
@Override
88+
@Query("SELECT * FROM legoset WHERE name like @name")
89+
Flux<LegoSet> findByNameContains(String name);
90+
91+
@Override
92+
@Query("SELECT * FROM legoset")
93+
Flux<Named> findAsProjection();
94+
95+
@Override
96+
@Query("SELECT * FROM legoset WHERE manual = @P0")
97+
Mono<LegoSet> findByManual(int manual);
98+
}
99+
}

‎src/test/java/org/springframework/data/r2dbc/repository/support/AbstractSimpleR2dbcRepositoryIntegrationTests.java

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.Arrays;
2929
import java.util.Collections;
30+
import java.util.List;
3031
import java.util.Map;
3132

3233
import javax.sql.DataSource;
@@ -121,9 +122,10 @@ public void shouldSaveNewObject() {
121122
@Test
122123
public void shouldUpdateObject() {
123124

124-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
125+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
126+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
125127

126-
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
128+
LegoSet legoSet = new LegoSet(id, "SCHAUFELRADBAGGER", 12);
127129
legoSet.setManual(14);
128130

129131
repository.save(legoSet) //
@@ -152,8 +154,8 @@ public void shouldSaveObjectsUsingIterable() {
152154
.expectNext(15) //
153155
.verifyComplete();
154156

155-
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM legoset");
156-
assertThat(map).containsEntry("count", 4L);
157+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
158+
assertThat(count).isEqualTo(4);
157159
}
158160

159161
@Test
@@ -167,20 +169,21 @@ public void shouldSaveObjectsUsingPublisher() {
167169
.expectNextCount(2) //
168170
.verifyComplete();
169171

170-
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM legoset");
171-
assertThat(map).containsEntry("count", 2L);
172+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
173+
assertThat(count).isEqualTo(2);
172174
}
173175

174176
@Test
175177
public void shouldFindById() {
176178

177-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
179+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
180+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
178181

179-
repository.findById(42055) //
182+
repository.findById(id) //
180183
.as(StepVerifier::create) //
181184
.assertNext(actual -> {
182185

183-
assertThat(actual.getId()).isEqualTo(42055);
186+
assertThat(actual.getId()).isEqualTo(id);
184187
assertThat(actual.getName()).isEqualTo("SCHAUFELRADBAGGER");
185188
assertThat(actual.getManual()).isEqualTo(12);
186189
}).verifyComplete();
@@ -189,9 +192,10 @@ public void shouldFindById() {
189192
@Test
190193
public void shouldExistsById() {
191194

192-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
195+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
196+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
193197

194-
repository.existsById(42055) //
198+
repository.existsById(id) //
195199
.as(StepVerifier::create) //
196200
.expectNext(true)//
197201
.verifyComplete();
@@ -205,9 +209,10 @@ public void shouldExistsById() {
205209
@Test
206210
public void shouldExistsByIdPublisher() {
207211

208-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
212+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
213+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
209214

210-
repository.existsById(Mono.just(42055)) //
215+
repository.existsById(Mono.just(id)) //
211216
.as(StepVerifier::create) //
212217
.expectNext(true)//
213218
.verifyComplete();
@@ -221,8 +226,8 @@ public void shouldExistsByIdPublisher() {
221226
@Test
222227
public void shouldFindByAll() {
223228

224-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
225-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
229+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
230+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('FORSCHUNGSSCHIFF', 13)");
226231

227232
repository.findAll() //
228233
.map(LegoSet::getName) //
@@ -237,10 +242,12 @@ public void shouldFindByAll() {
237242
@Test
238243
public void shouldFindAllByIdUsingIterable() {
239244

240-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
241-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
245+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
246+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('FORSCHUNGSSCHIFF', 13)");
242247

243-
repository.findAllById(Arrays.asList(42055, 42064)) //
248+
List<Integer> ids = jdbc.queryForList("SELECT id FROM legoset", Integer.class);
249+
250+
repository.findAllById(ids) //
244251
.map(LegoSet::getName) //
245252
.collectList() //
246253
.as(StepVerifier::create) //
@@ -253,10 +260,12 @@ public void shouldFindAllByIdUsingIterable() {
253260
@Test
254261
public void shouldFindAllByIdUsingPublisher() {
255262

256-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
257-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
263+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
264+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('FORSCHUNGSSCHIFF', 13)");
265+
266+
List<Integer> ids = jdbc.queryForList("SELECT id FROM legoset", Integer.class);
258267

259-
repository.findAllById(Flux.just(42055, 42064)) //
268+
repository.findAllById(Flux.fromIterable(ids)) //
260269
.map(LegoSet::getName) //
261270
.collectList() //
262271
.as(StepVerifier::create) //
@@ -274,8 +283,8 @@ public void shouldCount() {
274283
.expectNext(0L) //
275284
.verifyComplete();
276285

277-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
278-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
286+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
287+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('FORSCHUNGSSCHIFF', 13)");
279288

280289
repository.count() //
281290
.as(StepVerifier::create) //
@@ -286,72 +295,77 @@ public void shouldCount() {
286295
@Test
287296
public void shouldDeleteById() {
288297

289-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
298+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
299+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
290300

291-
repository.deleteById(42055) //
301+
repository.deleteById(id) //
292302
.as(StepVerifier::create) //
293303
.verifyComplete();
294304

295-
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM legoset");
296-
assertThat(map).containsEntry("count", 0L);
305+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
306+
assertThat(count).isEqualTo(0);
297307
}
298308

299309
@Test
300310
public void shouldDeleteByIdPublisher() {
301311

302-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
312+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
313+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
303314

304-
repository.deleteById(Mono.just(42055)) //
315+
repository.deleteById(Mono.just(id)) //
305316
.as(StepVerifier::create) //
306317
.verifyComplete();
307318

308-
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM legoset");
309-
assertThat(map).containsEntry("count", 0L);
319+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
320+
assertThat(count).isEqualTo(0);
310321
}
311322

312323
@Test
313324
public void shouldDelete() {
314325

315-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
326+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
327+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
316328

317-
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
329+
LegoSet legoSet = new LegoSet(id, "SCHAUFELRADBAGGER", 12);
318330

319331
repository.delete(legoSet) //
320332
.as(StepVerifier::create) //
321333
.verifyComplete();
322334

323-
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM legoset");
324-
assertThat(map).containsEntry("count", 0L);
335+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
336+
assertThat(count).isEqualTo(0);
325337
}
326338

327339
@Test
328340
public void shouldDeleteAllUsingIterable() {
329341

330-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
342+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
343+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
331344

332-
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
345+
LegoSet legoSet = new LegoSet(id, "SCHAUFELRADBAGGER", 12);
333346

334347
repository.deleteAll(Collections.singletonList(legoSet)) //
335348
.as(StepVerifier::create) //
336349
.verifyComplete();
337350

338-
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM legoset");
339-
assertThat(map).containsEntry("count", 0L);
351+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
352+
assertThat(count).isEqualTo(0);
340353
}
341354

342355
@Test
343356
public void shouldDeleteAllUsingPublisher() {
344357

345-
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
358+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
359+
Integer id = jdbc.queryForObject("SELECT id FROM legoset", Integer.class);
346360

347-
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
361+
LegoSet legoSet = new LegoSet(id, "SCHAUFELRADBAGGER", 12);
348362

349363
repository.deleteAll(Mono.just(legoSet)) //
350364
.as(StepVerifier::create) //
351365
.verifyComplete();
352366

353-
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM legoset");
354-
assertThat(map).containsEntry("count", 0L);
367+
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM legoset", Integer.class);
368+
assertThat(count).isEqualTo(0);
355369
}
356370

357371
@Data

‎src/test/java/org/springframework/data/r2dbc/repository/support/PostgresSimpleR2dbcRepositoryIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.test.context.junit4.SpringRunner;
3030

3131
/**
32-
* Integration tests for {@link SimpleR2dbcRepository}.
32+
* Integration tests for {@link SimpleR2dbcRepository} against Postgres.
3333
*
3434
* @author Mark Paluch
3535
*/
@@ -55,6 +55,6 @@ protected DataSource createDataSource() {
5555

5656
@Override
5757
protected String getCreateTableStatement() {
58-
return PostgresTestSupport.CREATE_TABLE_LEGOSET;
58+
return PostgresTestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION;
5959
}
6060
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2018 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+
* http://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+
package org.springframework.data.r2dbc.repository.support;
17+
18+
import io.r2dbc.spi.ConnectionFactory;
19+
20+
import javax.sql.DataSource;
21+
22+
import org.junit.ClassRule;
23+
import org.junit.runner.RunWith;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
26+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
27+
import org.springframework.data.r2dbc.testing.SqlServerTestSupport;
28+
import org.springframework.test.context.ContextConfiguration;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
31+
/**
32+
* Integration tests for {@link SimpleR2dbcRepository} against Microsoft SQL Server.
33+
*
34+
* @author Mark Paluch
35+
*/
36+
@RunWith(SpringRunner.class)
37+
@ContextConfiguration
38+
public class SqlServerSimpleR2dbcRepositoryIntegrationTests extends AbstractSimpleR2dbcRepositoryIntegrationTests {
39+
40+
@ClassRule public static final ExternalDatabase database = SqlServerTestSupport.database();
41+
42+
@Configuration
43+
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
44+
45+
@Override
46+
public ConnectionFactory connectionFactory() {
47+
return SqlServerTestSupport.createConnectionFactory(database);
48+
}
49+
}
50+
51+
@Override
52+
protected DataSource createDataSource() {
53+
return SqlServerTestSupport.createDataSource(database);
54+
}
55+
56+
@Override
57+
protected String getCreateTableStatement() {
58+
return SqlServerTestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION;
59+
}
60+
}

‎src/test/java/org/springframework/data/r2dbc/testing/PostgresTestSupport.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
*/
1717
public class PostgresTestSupport {
1818

19-
public static String CREATE_TABLE_LEGOSET = "CREATE TABLE IF NOT EXISTS legoset (\n" //
19+
public static String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n" //
20+
+ " id integer CONSTRAINT id PRIMARY KEY,\n" //
21+
+ " name varchar(255) NOT NULL,\n" //
22+
+ " manual integer NULL\n" //
23+
+ ");";
24+
25+
public static String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" //
2026
+ " id serial CONSTRAINT id PRIMARY KEY,\n" //
2127
+ " name varchar(255) NOT NULL,\n" //
2228
+ " manual integer NULL\n" //
@@ -57,11 +63,13 @@ public static ConnectionFactory createConnectionFactory(ExternalDatabase databas
5763
public static DataSource createDataSource(ExternalDatabase database) {
5864

5965
PGSimpleDataSource dataSource = new PGSimpleDataSource();
66+
6067
dataSource.setUser(database.getUsername());
6168
dataSource.setPassword(database.getPassword());
6269
dataSource.setDatabaseName(database.getDatabase());
6370
dataSource.setServerName(database.getHostname());
6471
dataSource.setPortNumber(database.getPort());
72+
6573
return dataSource;
6674
}
6775
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.springframework.data.r2dbc.testing;
2+
3+
import io.r2dbc.mssql.MssqlConnectionConfiguration;
4+
import io.r2dbc.mssql.MssqlConnectionFactory;
5+
import io.r2dbc.spi.ConnectionFactory;
6+
7+
import javax.sql.DataSource;
8+
9+
import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;
10+
11+
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
12+
13+
/**
14+
* Utility class for testing against Microsoft SQL Server.
15+
*
16+
* @author Mark Paluch
17+
*/
18+
public class SqlServerTestSupport {
19+
20+
public static String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n" //
21+
+ " id integer PRIMARY KEY,\n" //
22+
+ " name varchar(255) NOT NULL,\n" //
23+
+ " manual integer NULL\n" //
24+
+ ");";
25+
26+
public static String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" //
27+
+ " id integer IDENTITY(1,1) PRIMARY KEY,\n" //
28+
+ " name varchar(255) NOT NULL,\n" //
29+
+ " manual integer NULL\n" //
30+
+ ");";
31+
32+
public static String INSERT_INTO_LEGOSET = "INSERT INTO legoset (id, name, manual) VALUES(@P0, @P1, @P3)";
33+
34+
/**
35+
* Returns a locally provided database at {@code sqlserver:@localhost:1433/master}.
36+
*
37+
* @return
38+
*/
39+
public static ExternalDatabase database() {
40+
return local();
41+
}
42+
43+
/**
44+
* Returns a locally provided database at {@code postgres:@localhost:5432/postgres}.
45+
*
46+
* @return
47+
*/
48+
private static ExternalDatabase local() {
49+
return ProvidedDatabase.builder().hostname("localhost").port(1433).database("master").username("sa")
50+
.password("my1.password").build();
51+
}
52+
53+
/**
54+
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
55+
*/
56+
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {
57+
return new MssqlConnectionFactory(MssqlConnectionConfiguration.builder().host(database.getHostname()) //
58+
.database(database.getDatabase()) //
59+
.username(database.getUsername()) //
60+
.password(database.getPassword()) //
61+
.build());
62+
}
63+
64+
/**
65+
* Creates a new {@link DataSource} configured from the {@link ExternalDatabase}.
66+
*/
67+
public static DataSource createDataSource(ExternalDatabase database) {
68+
69+
SQLServerDataSource dataSource = new SQLServerDataSource();
70+
71+
dataSource.setUser(database.getUsername());
72+
dataSource.setPassword(database.getPassword());
73+
dataSource.setDatabaseName(database.getDatabase());
74+
dataSource.setServerName(database.getHostname());
75+
dataSource.setPortNumber(database.getPort());
76+
77+
return dataSource;
78+
}
79+
}

0 commit comments

Comments
 (0)
Please sign in to comment.