Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit c80af36

Browse files
artembilantrisberg
authored andcommitted
Fix docs for from Spring Boot properties
- Use `messages` as a default table name - Fix JDBC test according Spring Boot 1.4 upgrade * Apply `@SpringBootTest` instead of deprecated functionality * Since `@SpringBootTest`s aren't mergeable in case of test classes hierarchy use `@TestPropertySource` for particular impl to accept its own properties * Resolve deprecations in the main classes according SCSt upgrade * Remove redundant `poller` usage in the Source definition since a default one is use implicitly any way Also see spring-attic/app-starters-release#28 - Use DB URL to force embedded usage and close it gracefully in case of `@DirtiesContext` See spring-projects/spring-boot#7897 Fixes: GH-1 (#1) Fixes: GH-3 (#3)
1 parent 1503d21 commit c80af36

File tree

8 files changed

+45
-58
lines changed

8 files changed

+45
-58
lines changed

spring-cloud-starter-stream-sink-jdbc/README.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ The **$$jdbc$$** $$sink$$ has the following options:
1111
$$jdbc.columns$$:: $$The names of the columns that shall receive data, as a set of column[:SpEL] mappings.
1212
Also used at initialization time to issue the DDL.$$ *($$java.util.Map<java.lang.String,java.lang.String>$$, default: `$$<none>$$`)*
1313
$$jdbc.initialize$$:: $$'true', 'false' or the location of a custom initialization script for the table.$$ *($$String$$, default: `$$false$$`)*
14-
$$jdbc.table-name$$:: $$The name of the table to write into.$$ *($$String$$, default: `$$<none>$$`)*
14+
$$jdbc.table-name$$:: $$The name of the table to write into.$$ *($$String$$, default: `$$messages$$`)*
15+
$$spring.datasource.data$$:: $$Data (DML) script resource reference.$$ *($$String$$, default: `$$<none>$$`)*
1516
$$spring.datasource.driver-class-name$$:: $$Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.$$ *($$String$$, default: `$$<none>$$`)*
1617
$$spring.datasource.initialize$$:: $$Populate the database using 'data.sql'.$$ *($$Boolean$$, default: `$$true$$`)*
1718
$$spring.datasource.password$$:: $$Login password of the database.$$ *($$String$$, default: `$$<none>$$`)*
19+
$$spring.datasource.schema$$:: $$Schema (DDL) script resource reference.$$ *($$String$$, default: `$$<none>$$`)*
1820
$$spring.datasource.url$$:: $$JDBC url of the database.$$ *($$String$$, default: `$$<none>$$`)*
1921
$$spring.datasource.username$$:: $$Login user of the database.$$ *($$String$$, default: `$$<none>$$`)*
2022
//end::configuration-properties[]

spring-cloud-starter-stream-sink-jdbc/src/main/java/org/springframework/cloud/stream/app/jdbc/sink/JdbcSinkProperties.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22-
import org.springframework.beans.factory.annotation.Value;
2322
import org.springframework.boot.context.properties.ConfigurationProperties;
2423
import org.springframework.cloud.stream.app.jdbc.SupportsShorthands;
2524

@@ -35,8 +34,7 @@ public class JdbcSinkProperties {
3534
/**
3635
* The name of the table to write into.
3736
*/
38-
@Value("${spring.application.name:messages}")
39-
private String tableName;
37+
private String tableName = "messages";
4038

4139
/**
4240
* The names of the columns that shall receive data, as a set of column[:SpEL] mappings.

spring-cloud-starter-stream-sink-jdbc/src/main/resources/META-INF/spring-configuration-metadata-whitelist.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ spring.datasource.url,\
44
spring.datasource.driver-class-name,\
55
spring.datasource.username,\
66
spring.datasource.password,\
7-
spring.datasource.init-sql,\
7+
spring.datasource.schema,\
8+
spring.datasource.data,\
89
spring.datasource.initialize

spring-cloud-starter-stream-sink-jdbc/src/test/java/org/springframework/cloud/stream/app/jdbc/sink/JdbcSinkIntegrationTests.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@
3030

3131
import org.springframework.beans.factory.annotation.Autowired;
3232
import org.springframework.boot.autoconfigure.SpringBootApplication;
33-
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
34-
import org.springframework.boot.test.IntegrationTest;
35-
import org.springframework.boot.test.SpringApplicationConfiguration;
36-
import org.springframework.cloud.stream.annotation.Bindings;
33+
import org.springframework.boot.test.context.SpringBootTest;
3734
import org.springframework.cloud.stream.messaging.Sink;
3835
import org.springframework.jdbc.core.BeanPropertyRowMapper;
3936
import org.springframework.jdbc.core.JdbcOperations;
4037
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
4138
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
4239
import org.springframework.messaging.support.MessageBuilder;
4340
import org.springframework.test.annotation.DirtiesContext;
44-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41+
import org.springframework.test.context.TestPropertySource;
42+
import org.springframework.test.context.junit4.SpringRunner;
4543
import org.springframework.tuple.Tuple;
4644
import org.springframework.tuple.TupleBuilder;
4745

@@ -50,21 +48,21 @@
5048
*
5149
* @author Eric Bottard
5250
* @author Thomas Risberg
51+
* @author Artem Bilan
5352
*/
54-
@RunWith(SpringJUnit4ClassRunner.class)
55-
@SpringApplicationConfiguration(classes = {JdbcSinkIntegrationTests.JdbcSinkApplication.class, EmbeddedDataSourceConfiguration.class})
56-
@IntegrationTest({"server.port=-1"})
53+
@RunWith(SpringRunner.class)
54+
@SpringBootTest(
55+
properties = "spring.datasource.url=jdbc:h2:mem:test",
56+
webEnvironment = SpringBootTest.WebEnvironment.NONE)
5757
@DirtiesContext
5858
public abstract class JdbcSinkIntegrationTests {
5959

6060
@Autowired
61-
@Bindings(JdbcSinkConfiguration.class)
6261
protected Sink channels;
6362

6463
@Autowired
6564
protected JdbcOperations jdbcOperations;
6665

67-
@IntegrationTest
6866
public static class DefaultBehavior extends JdbcSinkIntegrationTests {
6967

7068
@Test
@@ -76,7 +74,7 @@ public void testInsertion() {
7674
}
7775
}
7876

79-
@IntegrationTest(value = "jdbc.columns=a,b")
77+
@TestPropertySource(properties = "jdbc.columns=a,b")
8078
public static class SimpleMappingTests extends JdbcSinkIntegrationTests {
8179

8280
@Test
@@ -89,7 +87,7 @@ public void testInsertion() {
8987
}
9088

9189
// annotation below relies on java.util.Properties so backslash needs to be doubled
92-
@IntegrationTest(value = "jdbc.columns=a: a.substring(0\\\\, 4), b: b + 624")
90+
@TestPropertySource(properties = "jdbc.columns=a: a.substring(0\\\\, 4), b: b + 624")
9391
public static class SpELTests extends JdbcSinkIntegrationTests {
9492

9593
@Test
@@ -102,7 +100,7 @@ public void testInsertion() {
102100
}
103101
}
104102

105-
@IntegrationTest(value = {"jdbc.columns=a,b"})
103+
@TestPropertySource(properties = "jdbc.columns=a,b")
106104
public static class VaryingInsertTests extends JdbcSinkIntegrationTests {
107105

108106
@Test
@@ -125,7 +123,7 @@ public void testInsertion() {
125123
}
126124
}
127125

128-
@IntegrationTest(value = {"jdbc.tableName=no_script", "jdbc.initialize=true", "jdbc.columns=a,b"})
126+
@TestPropertySource(properties = { "jdbc.tableName=no_script", "jdbc.initialize=true", "jdbc.columns=a,b" })
129127
public static class ImplicitTableCreationTests extends JdbcSinkIntegrationTests {
130128

131129
@Test
@@ -137,8 +135,8 @@ public void testInsertion() {
137135
}
138136
}
139137

140-
@IntegrationTest(value = {"jdbc.tableName=foobar", "jdbc.initialize=classpath:explicit-script.sql", "jdbc.columns=a,b"})
141-
public static class ExlicitTableCreationTests extends JdbcSinkIntegrationTests {
138+
@TestPropertySource(properties = { "jdbc.tableName=foobar", "jdbc.initialize=classpath:explicit-script.sql", "jdbc.columns=a,b" })
139+
public static class ExplicitTableCreationTests extends JdbcSinkIntegrationTests {
142140

143141
@Test
144142
public void testInsertion() {
@@ -149,7 +147,7 @@ public void testInsertion() {
149147
}
150148
}
151149

152-
@IntegrationTest(value = {"jdbc.columns=a,b"})
150+
@TestPropertySource(properties = "jdbc.columns=a,b")
153151
public static class MapPayloadInsertTests extends JdbcSinkIntegrationTests {
154152

155153
@Test
@@ -175,7 +173,7 @@ public void testInsertion() {
175173
}
176174
}
177175

178-
@IntegrationTest(value = {"jdbc.columns=a,b"})
176+
@TestPropertySource(properties = "jdbc.columns=a,b")
179177
public static class TuplePayloadInsertTests extends JdbcSinkIntegrationTests {
180178

181179
@Test
@@ -198,7 +196,7 @@ public void testInsertion() {
198196
}
199197
}
200198

201-
@IntegrationTest(value = {"jdbc.columns=a,b"})
199+
@TestPropertySource(properties = "jdbc.columns=a,b")
202200
public static class JsonStringPayloadInsertTests extends JdbcSinkIntegrationTests {
203201

204202
@Test
@@ -222,6 +220,7 @@ public void testInsertion() {
222220
}
223221

224222
public static class Payload {
223+
225224
private String a;
226225

227226
private Integer b;
@@ -254,6 +253,7 @@ public void setB(Integer b) {
254253
public String toString() {
255254
return a + b;
256255
}
256+
257257
}
258258

259259
@SpringBootApplication

spring-cloud-starter-stream-source-jdbc/README.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ $$jdbc.max-rows-per-poll$$:: $$Max numbers of rows to process for each poll.$$ *
1515
$$jdbc.query$$:: $$The query to use to select data.$$ *($$String$$, default: `$$<none>$$`)*
1616
$$jdbc.split$$:: $$Whether to split the SQL result as individual messages.$$ *($$Boolean$$, default: `$$true$$`)*
1717
$$jdbc.update$$:: $$An SQL update statement to execute for marking polled messages as 'seen'.$$ *($$String$$, default: `$$<none>$$`)*
18+
$$spring.datasource.data$$:: $$Data (DML) script resource reference.$$ *($$String$$, default: `$$<none>$$`)*
1819
$$spring.datasource.driver-class-name$$:: $$Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.$$ *($$String$$, default: `$$<none>$$`)*
1920
$$spring.datasource.initialize$$:: $$Populate the database using 'data.sql'.$$ *($$Boolean$$, default: `$$true$$`)*
2021
$$spring.datasource.password$$:: $$Login password of the database.$$ *($$String$$, default: `$$<none>$$`)*
22+
$$spring.datasource.schema$$:: $$Schema (DDL) script resource reference.$$ *($$String$$, default: `$$<none>$$`)*
2123
$$spring.datasource.url$$:: $$JDBC url of the database.$$ *($$String$$, default: `$$<none>$$`)*
2224
$$spring.datasource.username$$:: $$Login user of the database.$$ *($$String$$, default: `$$<none>$$`)*
2325
$$trigger.cron$$:: $$Cron expression value for the Cron Trigger.$$ *($$String$$, default: `$$<none>$$`)*

spring-cloud-starter-stream-source-jdbc/src/main/java/org/springframework/cloud/stream/app/jdbc/source/JdbcSourceConfiguration.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
import javax.sql.DataSource;
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
22-
import org.springframework.beans.factory.annotation.Qualifier;
2322
import org.springframework.boot.context.properties.EnableConfigurationProperties;
24-
import org.springframework.cloud.stream.annotation.Bindings;
2523
import org.springframework.cloud.stream.annotation.EnableBinding;
2624
import org.springframework.cloud.stream.app.trigger.TriggerConfiguration;
27-
import org.springframework.cloud.stream.app.trigger.TriggerProperties;
2825
import org.springframework.cloud.stream.app.trigger.TriggerPropertiesMaxMessagesDefaultOne;
2926
import org.springframework.cloud.stream.messaging.Source;
3027
import org.springframework.context.annotation.Bean;
@@ -33,10 +30,7 @@
3330
import org.springframework.integration.dsl.IntegrationFlow;
3431
import org.springframework.integration.dsl.IntegrationFlowBuilder;
3532
import org.springframework.integration.dsl.IntegrationFlows;
36-
import org.springframework.integration.dsl.SourcePollingChannelAdapterSpec;
37-
import org.springframework.integration.dsl.support.Consumer;
3833
import org.springframework.integration.jdbc.JdbcPollingChannelAdapter;
39-
import org.springframework.integration.scheduling.PollerMetadata;
4034

4135
/**
4236
* A module that reads data from an RDBMS using JDBC and creates a payload with the data.
@@ -48,18 +42,13 @@
4842
@EnableConfigurationProperties({JdbcSourceProperties.class, TriggerPropertiesMaxMessagesDefaultOne.class})
4943
public class JdbcSourceConfiguration {
5044

51-
@Autowired
52-
@Qualifier("defaultPoller")
53-
private PollerMetadata poller;
54-
5545
@Autowired
5646
private JdbcSourceProperties properties;
5747

5848
@Autowired
5949
private DataSource dataSource;
6050

6151
@Autowired
62-
@Bindings(JdbcSourceConfiguration.class)
6352
private Source source;
6453

6554
@Bean
@@ -73,15 +62,7 @@ public MessageSource<Object> jdbcMessageSource() {
7362

7463
@Bean
7564
public IntegrationFlow pollingFlow() {
76-
IntegrationFlowBuilder flowBuilder = IntegrationFlows.from(jdbcMessageSource(),
77-
new Consumer<SourcePollingChannelAdapterSpec>() {
78-
79-
@Override
80-
public void accept(SourcePollingChannelAdapterSpec sourcePollingChannelAdapterSpec) {
81-
sourcePollingChannelAdapterSpec.poller(poller);
82-
}
83-
84-
});
65+
IntegrationFlowBuilder flowBuilder = IntegrationFlows.from(jdbcMessageSource());
8566
if (this.properties.isSplit()) {
8667
flowBuilder.split();
8768
}

spring-cloud-starter-stream-source-jdbc/src/main/resources/META-INF/spring-configuration-metadata-whitelist.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ spring.datasource.url,\
55
spring.datasource.driver-class-name,\
66
spring.datasource.username,\
77
spring.datasource.password,\
8-
spring.datasource.init-sql,\
8+
spring.datasource.schema,\
9+
spring.datasource.data,\
910
spring.datasource.initialize

spring-cloud-starter-stream-source-jdbc/src/test/java/org/springframework/cloud/stream/app/jdbc/source/JdbcSourceIntegrationTests.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,29 @@
3131

3232
import org.springframework.beans.factory.annotation.Autowired;
3333
import org.springframework.boot.autoconfigure.SpringBootApplication;
34-
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
35-
import org.springframework.boot.test.IntegrationTest;
36-
import org.springframework.boot.test.SpringApplicationConfiguration;
37-
import org.springframework.cloud.stream.annotation.Bindings;
34+
import org.springframework.boot.test.context.SpringBootTest;
3835
import org.springframework.cloud.stream.messaging.Source;
3936
import org.springframework.cloud.stream.test.binder.MessageCollector;
4037
import org.springframework.jdbc.core.JdbcOperations;
4138
import org.springframework.messaging.Message;
4239
import org.springframework.test.annotation.DirtiesContext;
40+
import org.springframework.test.context.TestPropertySource;
4341
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4442

4543
/**
4644
* Integration Tests for JdbcSource. Uses hsqldb as a (real) embedded DB.
4745
*
4846
* @author Thomas Risberg
47+
* @author Artem Bilan
4948
*/
5049
@RunWith(SpringJUnit4ClassRunner.class)
51-
@SpringApplicationConfiguration(classes = {JdbcSourceIntegrationTests.JdbcSourceApplication.class,
52-
EmbeddedDataSourceConfiguration.class})
50+
@SpringBootTest(
51+
properties = "spring.datasource.url=jdbc:h2:mem:test",
52+
webEnvironment = SpringBootTest.WebEnvironment.NONE)
5353
@DirtiesContext
5454
public abstract class JdbcSourceIntegrationTests {
5555

5656
@Autowired
57-
@Bindings(JdbcSourceConfiguration.class)
5857
protected Source source;
5958

6059
@Autowired
@@ -63,7 +62,7 @@ public abstract class JdbcSourceIntegrationTests {
6362
@Autowired
6463
protected MessageCollector messageCollector;
6564

66-
@IntegrationTest("jdbc.query=select id, name from test order by id")
65+
@TestPropertySource(properties = "jdbc.query=select id, name from test order by id")
6766
public static class DefaultBehaviorTests extends JdbcSourceIntegrationTests {
6867

6968
@Test
@@ -84,7 +83,7 @@ public void testExtraction() throws InterruptedException {
8483

8584
}
8685

87-
@IntegrationTest(value = {"jdbc.query=select id, name, tag from test where tag is NULL order by id", "jdbc.split=false"})
86+
@TestPropertySource(properties = { "jdbc.query=select id, name, tag from test where tag is NULL order by id", "jdbc.split=false" })
8887
public static class SelectAllNoSplitTests extends JdbcSourceIntegrationTests {
8988

9089
@Test
@@ -99,7 +98,7 @@ public void testExtraction() throws InterruptedException {
9998

10099
}
101100

102-
@IntegrationTest(value = {"jdbc.query=select id, name from test order by id", "trigger.fixedDelay=600"})
101+
@TestPropertySource(properties = { "jdbc.query=select id, name from test order by id", "trigger.fixedDelay=600" })
103102
public static class SelectAllWithDelayTests extends JdbcSourceIntegrationTests {
104103

105104
@Test
@@ -126,7 +125,7 @@ public void testExtraction() throws InterruptedException {
126125

127126
}
128127

129-
@IntegrationTest(value = {"jdbc.query=select id, name from test order by id", "trigger.fixedDelay=1"})
128+
@TestPropertySource(properties = { "jdbc.query=select id, name from test order by id", "trigger.fixedDelay=1" })
130129
public static class SelectAllWithMinDelayTests extends JdbcSourceIntegrationTests {
131130

132131
@Test
@@ -152,8 +151,11 @@ public void testExtraction() throws InterruptedException {
152151

153152
}
154153

155-
@IntegrationTest(value = {"jdbc.query=select id, name, tag from test where tag is NULL order by id", "jdbc.split=false",
156-
"jdbc.maxRowsPerPoll=2", "jdbc.update=update test set tag='1' where id in (:id)"})
154+
@TestPropertySource(properties = {
155+
"jdbc.query=select id, name, tag from test where tag is NULL order by id",
156+
"jdbc.split=false",
157+
"jdbc.maxRowsPerPoll=2",
158+
"jdbc.update=update test set tag='1' where id in (:id)" })
157159
public static class Select2PerPollNoSplitWithUpdateTests extends JdbcSourceIntegrationTests {
158160

159161
@Test

0 commit comments

Comments
 (0)