Skip to content

Commit 91d8693

Browse files
juan-medinaschauder
authored andcommitted
DATAJDBC-522 - Add Kotlin extensions for Criteria.
Adding Kotlin extensions for easier consumption of Criteria when using Kotlin. Original pull request: #240.
1 parent 1f36fbc commit 91d8693

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

spring-data-relational/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@
7171
<scope>test</scope>
7272
</dependency>
7373

74+
<!-- DATAJDBC-522: Add Kotlin extensions for Criteria -->
75+
<dependency>
76+
<groupId>org.jetbrains.kotlin</groupId>
77+
<artifactId>kotlin-stdlib</artifactId>
78+
<optional>true</optional>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>io.mockk</groupId>
83+
<artifactId>mockk</artifactId>
84+
<version>${mockk}</version>
85+
<scope>test</scope>
86+
</dependency>
87+
7488
</dependencies>
7589

7690
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2019-2020 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+
package org.springframework.data.relational.core.query
17+
18+
/**
19+
* Extension for [Criteria.CriteriaStep. is] providing a
20+
* `isEquals(value)` variant.
21+
*
22+
* @author Juan Medina<jamedina@gmail.com>
23+
*/
24+
infix fun Criteria.CriteriaStep.isEquals(value: Any): Criteria =
25+
`is`(value)
26+
27+
/**
28+
* Extension for [Criteria.CriteriaStep. in] providing a
29+
* `isIn(value)` variant.
30+
*
31+
* @author Juan Medina<jamedina@gmail.com>
32+
*/
33+
fun Criteria.CriteriaStep.isIn(vararg value: Any): Criteria =
34+
`in`(value)
35+
36+
/**
37+
* Extension for [Criteria.CriteriaStep. in] providing a
38+
* `isIn(value)` variant.
39+
*
40+
* @author Juan Medina<jamedina@gmail.com>
41+
*/
42+
fun Criteria.CriteriaStep.isIn(values: Collection<Any>): Criteria =
43+
`in`(values)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2019 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+
package org.springframework.data.relational.core.query
17+
18+
import io.mockk.every
19+
import io.mockk.mockk
20+
import io.mockk.verify
21+
import org.assertj.core.api.Assertions.assertThat
22+
import org.junit.Test
23+
24+
/**
25+
* Unit tests for [Criteria.CriteriaStep] extensions.
26+
*
27+
* @author Juan Medina<jamedina@gmail.com>
28+
*/
29+
class CriteriaStepExtensionsTests {
30+
31+
@Test // DATAJDBC-522
32+
fun eqIsCriteriaStep(){
33+
34+
val spec = mockk<Criteria.CriteriaStep>()
35+
val criteria = mockk<Criteria>()
36+
37+
every { spec.`is`("test") } returns criteria
38+
39+
assertThat(spec isEquals "test").isEqualTo(criteria)
40+
41+
verify {
42+
spec.`is`("test")
43+
}
44+
}
45+
46+
@Test // DATAJDBC-522
47+
fun inVarargCriteriaStep() {
48+
49+
val spec = mockk<Criteria.CriteriaStep>()
50+
val criteria = mockk<Criteria>()
51+
52+
every { spec.`in`(any() as Array<Any>) } returns criteria
53+
54+
assertThat(spec.isIn("test")).isEqualTo(criteria)
55+
56+
verify {
57+
spec.`in`(arrayOf("test"))
58+
}
59+
}
60+
61+
@Test // DATAJDBC-522
62+
fun inListCriteriaStep() {
63+
64+
val spec = mockk<Criteria.CriteriaStep>()
65+
val criteria = mockk<Criteria>()
66+
67+
every { spec.`in`(listOf("test")) } returns criteria
68+
69+
assertThat(spec.isIn(listOf("test"))).isEqualTo(criteria)
70+
71+
verify {
72+
spec.`in`(listOf("test"))
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)