Skip to content

Commit 6cc1913

Browse files
committed
Merge pull request #49063 from dj258255
* pr/49063: Polish "Add Kotlin DSL extension functions for TestEntityManager" Add Kotlin DSL extension functions for TestEntityManager Closes gh-49063
2 parents c143c2c + c607594 commit 6cc1913

3 files changed

Lines changed: 146 additions & 0 deletions

File tree

module/spring-boot-jpa-test/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
plugins {
18+
id "org.jetbrains.kotlin.jvm"
1819
id "java-library"
1920
id "org.springframework.boot.deployed"
2021
id "org.springframework.boot.optional-dependencies"
@@ -29,6 +30,7 @@ dependencies {
2930
api(project(":module:spring-boot-jpa"))
3031

3132
optional(project(":core:spring-boot-autoconfigure"))
33+
optional("org.jetbrains.kotlin:kotlin-stdlib")
3234

3335
testImplementation(project(":test-support:spring-boot-test-support"))
3436

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-present 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+
17+
package org.springframework.boot.jpa.test.autoconfigure
18+
19+
/**
20+
* Extension for [TestEntityManager.find] providing a `find<MyEntity>(...)`
21+
* variant leveraging Kotlin reified type parameters.
22+
*
23+
* @param primaryKey the primary key of the entity
24+
* @author Beom Su
25+
* @since 4.1.0
26+
*/
27+
inline fun <reified E : Any> TestEntityManager.find(primaryKey: Any): E? =
28+
find(E::class.java, primaryKey)
29+
30+
/**
31+
* Extension for [TestEntityManager.persistAndGetId] providing a
32+
* `persistAndGetId<Long>(...)` variant leveraging Kotlin reified type parameters.
33+
*
34+
* @param entity the source entity
35+
* @author Beom Su
36+
* @since 4.1.0
37+
*/
38+
inline fun <reified T : Any> TestEntityManager.persistAndGetId(entity: Any): T? =
39+
persistAndGetId(entity, T::class.java)
40+
41+
/**
42+
* Extension for [TestEntityManager.getId] providing a `getId<MyEntity>(...)`
43+
* variant leveraging Kotlin reified type parameters.
44+
*
45+
* @param entity the source entity
46+
* @author Beom Su
47+
* @since 4.1.0
48+
*/
49+
inline fun <reified T : Any> TestEntityManager.getId(entity: Any): T? =
50+
getId(entity, T::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2012-present 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+
17+
package org.springframework.boot.jpa.test.autoconfigure
18+
19+
import jakarta.persistence.EntityManager
20+
import jakarta.persistence.EntityManagerFactory
21+
import jakarta.persistence.PersistenceUnitUtil
22+
import org.assertj.core.api.Assertions.assertThat
23+
import org.junit.jupiter.api.BeforeEach
24+
import org.junit.jupiter.api.Test
25+
import org.junit.jupiter.api.extension.ExtendWith
26+
import org.mockito.BDDMockito.given
27+
import org.mockito.BDDMockito.then
28+
import org.mockito.Mock
29+
import org.mockito.junit.jupiter.MockitoExtension
30+
import org.springframework.orm.jpa.EntityManagerHolder
31+
import org.springframework.transaction.support.TransactionSynchronizationManager
32+
33+
/**
34+
* Tests for [TestEntityManager] Kotlin extensions.
35+
*
36+
* @author Beom Su
37+
* @since 4.1.0
38+
*/
39+
@ExtendWith(MockitoExtension::class)
40+
class TestEntityManagerExtensionsTests {
41+
42+
@Mock
43+
private lateinit var entityManagerFactory: EntityManagerFactory
44+
45+
@Mock
46+
private lateinit var entityManager: EntityManager
47+
48+
@Mock
49+
private lateinit var persistenceUnitUtil: PersistenceUnitUtil
50+
51+
private lateinit var testEntityManager: TestEntityManager
52+
53+
@BeforeEach
54+
fun setup() {
55+
this.testEntityManager = TestEntityManager(this.entityManagerFactory)
56+
}
57+
58+
@Test
59+
fun `find with reified type parameter`() {
60+
bindEntityManager()
61+
val entity = TestEntity()
62+
given(this.entityManager.find(TestEntity::class.java, 123)).willReturn(entity)
63+
val result = this.testEntityManager.find<TestEntity>(123)
64+
assertThat(result).isSameAs(entity)
65+
}
66+
67+
@Test
68+
fun `persistAndGetId with reified type parameter`() {
69+
bindEntityManager()
70+
val entity = TestEntity()
71+
given(this.entityManagerFactory.persistenceUnitUtil).willReturn(this.persistenceUnitUtil)
72+
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(123)
73+
val result = this.testEntityManager.persistAndGetId<Int>(entity)
74+
then(this.entityManager).should().persist(entity)
75+
assertThat(result).isEqualTo(123)
76+
}
77+
78+
@Test
79+
fun `getId with reified type parameter`() {
80+
val entity = TestEntity()
81+
given(this.entityManagerFactory.persistenceUnitUtil).willReturn(this.persistenceUnitUtil)
82+
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(123)
83+
val result = this.testEntityManager.getId<Int>(entity)
84+
assertThat(result).isEqualTo(123)
85+
}
86+
87+
private fun bindEntityManager() {
88+
val holder = EntityManagerHolder(this.entityManager)
89+
TransactionSynchronizationManager.bindResource(this.entityManagerFactory, holder)
90+
}
91+
92+
class TestEntity
93+
94+
}

0 commit comments

Comments
 (0)