Skip to content

Commit 0e27c24

Browse files
committed
Since the destination is mandatory, pass it as argument to dbSetup. So there's no way one can forget to set it, and it's more consistent with insertInto, which takes its destination table as argument.
Also add a shortcut dbSetup() method taking a DataSource as argument directly.
1 parent f394c23 commit 0e27c24

File tree

4 files changed

+80
-51
lines changed

4 files changed

+80
-51
lines changed

DbSetup-kotlin/src/main/kotlin/com/ninja_squad/dbsetup_kotlin/DbSetupBuilder.kt

+5-12
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,11 @@ import com.ninja_squad.dbsetup.operation.Operation
3434
/**
3535
* A builder allowing to configure a DbSetup from a lambda expression whose receiver type is this builder.
3636
* The intended usage is to use the [dbSetup] top level function.
37+
*
38+
* @param to The destination of the DbSetup
39+
* @property binderConfiguration The binder configuration of the DbSetup. It not set, the default configuration is used
3740
*/
38-
class DbSetupBuilder {
39-
40-
/**
41-
* The destination of the DbSetup. It is mandatory
42-
*/
43-
var destination: Destination? = null
44-
45-
/**
46-
* The binder configuration of the DbSetup. It not set, the default configuration is used
47-
*/
48-
var binderConfiguration: BinderConfiguration = DefaultBinderConfiguration.INSTANCE
41+
class DbSetupBuilder(private val to: Destination, var binderConfiguration: BinderConfiguration = DefaultBinderConfiguration.INSTANCE) {
4942

5043
private val operations = mutableListOf<Operation>()
5144

@@ -159,7 +152,7 @@ class DbSetupBuilder {
159152
* @throws IllegalStateException if the destination has not been set
160153
*/
161154
internal fun build(): DbSetup {
162-
return DbSetup(destination ?: throw IllegalStateException("destination hasn't been set"),
155+
return DbSetup(to,
163156
Operations.sequenceOf(operations),
164157
binderConfiguration)
165158
}

DbSetup-kotlin/src/main/kotlin/com/ninja_squad/dbsetup_kotlin/Functions.kt

+43-7
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@
2424
package com.ninja_squad.dbsetup_kotlin
2525

2626
import com.ninja_squad.dbsetup.DbSetup
27+
import com.ninja_squad.dbsetup.bind.BinderConfiguration
28+
import com.ninja_squad.dbsetup.bind.DefaultBinderConfiguration
29+
import com.ninja_squad.dbsetup.destination.DataSourceDestination
30+
import com.ninja_squad.dbsetup.destination.Destination
2731
import com.ninja_squad.dbsetup.operation.Insert
32+
import javax.sql.DataSource
2833

2934
/**
30-
* Top-level function allowing to create a DbSetup by passing a lambda expression configuring it.
35+
* Top-level function allowing to create a DbSetup by passing a destination and a lambda expression used to configure
36+
* the operations that must be made.
3137
*
3238
* Example usage:
3339
*
3440
* ```
35-
* val setup = dbSetup {
36-
* destination = DataSourceDestination(dataSource)
37-
*
41+
* val setup = dbSetup(to = DriverManagerDestination(url, user, password)) {
3842
* deleteAllFrom("user", "country")
3943
* insertInto("country") {
4044
* ...
@@ -46,18 +50,50 @@ import com.ninja_squad.dbsetup.operation.Insert
4650
* }
4751
* ```
4852
*
53+
* @param to the destination of the DbSetup
54+
* @param binderConfiguration a custom binder configuration. The default one is used if not specified
4955
* @param configure the function used to configure the DbSetup
50-
* @throws IllegalStateException if the destination has not been set by the configure function
5156
* @return the created DbSetup
5257
*
5358
* @author JB Nizet
5459
*/
55-
fun dbSetup(configure: DbSetupBuilder.() -> Unit): DbSetup {
56-
val builder = DbSetupBuilder()
60+
fun dbSetup(to: Destination,
61+
binderConfiguration: BinderConfiguration = DefaultBinderConfiguration.INSTANCE,
62+
configure: DbSetupBuilder.() -> Unit): DbSetup {
63+
val builder = DbSetupBuilder(to, binderConfiguration)
5764
builder.configure()
5865
return builder.build()
5966
}
6067

68+
/**
69+
* Top-level function allowing to create a DbSetup by passing a DataSource and a lambda expression used to configure
70+
* the operations that must be made.
71+
*
72+
* Example usage:
73+
*
74+
* ```
75+
* val setup = dbSetup(to = dataSource) {
76+
* deleteAllFrom("user", "country")
77+
* insertInto("country") {
78+
* ...
79+
* }
80+
* insertInto("user") {
81+
* ...
82+
* }
83+
* sql(...)
84+
* }
85+
* ```
86+
*
87+
* @param to the destination of the DbSetup
88+
* @param binderConfiguration a custom binder configuration. The default one is used if not specified
89+
* @param configure the function used to configure the DbSetup
90+
* @return the created DbSetup
91+
*
92+
* @author JB Nizet
93+
*/
94+
fun dbSetup(to: DataSource,
95+
binderConfiguration: BinderConfiguration = DefaultBinderConfiguration.INSTANCE,
96+
configure: DbSetupBuilder.() -> Unit) = dbSetup(DataSourceDestination(to), binderConfiguration, configure)
6197
/**
6298
* Top-level function allowing to create an Insert operation by passing a lambda expression configuring it.
6399
*

DbSetup-kotlin/src/test/kotlin/com/ninja_squad/dbsetup_kotlin/DbSetupBuilderTest.kt

+32-31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.mockito.Mockito.verify
1010
import java.sql.Connection
1111
import java.sql.PreparedStatement
1212
import java.sql.Statement
13+
import javax.sql.DataSource
1314

1415
class DbSetupBuilderTest {
1516

@@ -31,9 +32,8 @@ class DbSetupBuilderTest {
3132
}
3233

3334
@Test
34-
fun `should execute an operation`() {
35-
dbSetup {
36-
destination = mockDestination
35+
fun `should execute an operation with the binder configuration specified as property`() {
36+
dbSetup(to = mockDestination) {
3737
binderConfiguration = mockConfig
3838
execute(mockOperation)
3939
}.launch()
@@ -42,26 +42,37 @@ class DbSetupBuilderTest {
4242
}
4343

4444
@Test
45-
fun `should use the default binder configuration if not set`() {
46-
dbSetup {
47-
destination = mockDestination
45+
fun `should execute an operation with the binder configuration specified as argument`() {
46+
dbSetup(to = mockDestination, binderConfiguration = mockConfig) {
4847
execute(mockOperation)
4948
}.launch()
5049

51-
verify(mockOperation).execute(mockConnection, DefaultBinderConfiguration.INSTANCE)
50+
verify(mockOperation).execute(mockConnection, mockConfig)
5251
}
5352

54-
@Test(expected = IllegalStateException::class)
55-
fun `should throw if destination not set`() {
56-
dbSetup {
53+
@Test
54+
fun `should execute an operation with a DataSource specified as argument`() {
55+
val mockDataSource = mock<DataSource>()
56+
whenever(mockDataSource.connection).thenReturn(mockConnection)
57+
dbSetup(to = mockDataSource, binderConfiguration = mockConfig) {
5758
execute(mockOperation)
5859
}.launch()
60+
61+
verify(mockOperation).execute(mockConnection, mockConfig)
62+
}
63+
64+
@Test
65+
fun `should use the default binder configuration if not set`() {
66+
dbSetup(to = mockDestination) {
67+
execute(mockOperation)
68+
}.launch()
69+
70+
verify(mockOperation).execute(mockConnection, DefaultBinderConfiguration.INSTANCE)
5971
}
6072

6173
@Test
6274
fun `should delete all from one table`() {
63-
dbSetup {
64-
destination = mockDestination
75+
dbSetup(to = mockDestination) {
6576
deleteAllFrom("user")
6677
}.launch()
6778

@@ -70,8 +81,7 @@ class DbSetupBuilderTest {
7081

7182
@Test
7283
fun `should delete all from multiple tables passed as vararg`() {
73-
dbSetup {
74-
destination = mockDestination
84+
dbSetup(to = mockDestination) {
7585
deleteAllFrom("country", "user")
7686
}.launch()
7787

@@ -81,8 +91,7 @@ class DbSetupBuilderTest {
8191

8292
@Test
8393
fun `should delete all from multiple tables passed as list`() {
84-
dbSetup {
85-
destination = mockDestination
94+
dbSetup(to = mockDestination) {
8695
deleteAllFrom(listOf("country", "user"))
8796
}.launch()
8897

@@ -92,8 +101,7 @@ class DbSetupBuilderTest {
92101

93102
@Test
94103
fun `should truncate one table`() {
95-
dbSetup {
96-
destination = mockDestination
104+
dbSetup(to = mockDestination) {
97105
truncate("user")
98106
}.launch()
99107

@@ -102,8 +110,7 @@ class DbSetupBuilderTest {
102110

103111
@Test
104112
fun `should truncate multiple tables passed as vararg`() {
105-
dbSetup {
106-
destination = mockDestination
113+
dbSetup(to = mockDestination) {
107114
truncate("country", "user")
108115
}.launch()
109116

@@ -113,8 +120,7 @@ class DbSetupBuilderTest {
113120

114121
@Test
115122
fun `should truncate multiple tables passed as list`() {
116-
dbSetup {
117-
destination = mockDestination
123+
dbSetup(to = mockDestination) {
118124
truncate(listOf("country", "user"))
119125
}.launch()
120126

@@ -125,8 +131,7 @@ class DbSetupBuilderTest {
125131
@Test
126132
fun `should execute one sql statement`() {
127133
val query = "update foo where bar = 1"
128-
dbSetup {
129-
destination = mockDestination
134+
dbSetup(to = mockDestination) {
130135
sql(query)
131136
}.launch()
132137

@@ -137,8 +142,7 @@ class DbSetupBuilderTest {
137142
fun `should execute multiple sql statements passed as vararg`() {
138143
val query1 = "update foo where bar = 1"
139144
val query2 = "update baz where qux = 1"
140-
dbSetup {
141-
destination = mockDestination
145+
dbSetup(to = mockDestination) {
142146
sql(query1, query2)
143147
}.launch()
144148

@@ -150,8 +154,7 @@ class DbSetupBuilderTest {
150154
fun `should execute multiple sql statements passed as list`() {
151155
val query1 = "update foo where bar = 1"
152156
val query2 = "update baz where qux = 1"
153-
dbSetup {
154-
destination = mockDestination
157+
dbSetup(to = mockDestination) {
155158
sql(listOf(query1, query2))
156159
}.launch()
157160

@@ -165,9 +168,7 @@ class DbSetupBuilderTest {
165168
whenever(mockConnection.prepareStatement("insert into user (id, name) values (?, ?)"))
166169
.thenReturn(mockPreparedStatement)
167170

168-
dbSetup {
169-
destination = mockDestination
170-
171+
dbSetup(to = mockDestination) {
171172
insertInto("user") {
172173
columns("id", "name")
173174
values(1, "John")

build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ subprojects { subProject ->
142142
publish = true
143143
pkg {
144144
repo = 'maven'
145-
println(subProject.name)
146145
name = subProject.name
147146
desc = subProject.description
148147
websiteUrl = 'http://dbsetup.ninja-squad.com'

0 commit comments

Comments
 (0)