From 20cf102953a334c8bf54b9c18d350911ad27ec10 Mon Sep 17 00:00:00 2001 From: bagger3025 Date: Fri, 11 Oct 2024 17:42:34 +0900 Subject: [PATCH] fix: revert divided methods into one method --- .../data/jpa/javax/JpqlEntityManagerUtils.kt | 24 ++---------- .../jpa/javax/JpqlEntityManagerUtilsTest.kt | 38 +++++++++++++++++++ .../spring/data/jpa/JpqlEntityManagerUtils.kt | 20 +--------- .../data/jpa/JpqlEntityManagerUtilsTest.kt | 38 +++++++++++++++++++ 4 files changed, 81 insertions(+), 39 deletions(-) diff --git a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt index d6aed6093..2914210cd 100644 --- a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt +++ b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt @@ -71,17 +71,7 @@ internal object JpqlEntityManagerUtils { createQuery(entityManager, queryEnhancer.applySorting(sort), rendered.params, returnType.java), ) { // Lazy - createCountQuery(entityManager, queryEnhancer.createCountQueryFor(), rendered.params) - } - } - - private fun createCountQuery( - entityManager: EntityManager, - query: String, - queryParams: Map, - ): TypedQuery { - return entityManager.createQuery(query, Long::class.javaObjectType).apply { - setCountQueryParams(this, queryParams) + createQuery(entityManager, queryEnhancer.createCountQueryFor(), rendered.params, Long::class.javaObjectType) } } @@ -106,10 +96,8 @@ internal object JpqlEntityManagerUtils { } } - private fun setCountQueryParams(query: Query, params: Map) { - val parameterList = query.parameters.map { - it.name - }.toHashSet() + private fun setParams(query: Query, params: Map) { + val parameterList = query.parameters.map { it.name }.toHashSet() params.forEach { (name, value) -> if (parameterList.contains(name)) { @@ -117,10 +105,4 @@ internal object JpqlEntityManagerUtils { } } } - - private fun setParams(query: Query, params: Map) { - params.forEach { (name, value) -> - query.setParameter(name, value) - } - } } diff --git a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt index fdc7a8259..fa0d2a70e 100644 --- a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt @@ -42,6 +42,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + @MockK private lateinit var longTypedQuery1: TypedQuery @@ -76,6 +82,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { longTypedQuery1.equals(any()) } excludeRecords { longTypedQueryParam1.hashCode() } excludeRecords { longTypedQueryParam2.hashCode() } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -85,7 +93,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -96,6 +107,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -108,7 +122,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -120,6 +137,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -132,7 +152,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -143,6 +166,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -155,7 +181,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -167,6 +196,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -184,7 +216,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 andThen longTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first every { longTypedQuery1.parameters } returns setOf(longTypedQueryParam1, longTypedQueryParam2) every { longTypedQuery1.setParameter(any(), any()) } returns longTypedQuery1 every { longTypedQueryParam1.name } returns renderedParam1.first @@ -205,6 +240,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { queryEnhancer.applySorting(sort1) entityManager.createQuery(sortedQuery1, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) diff --git a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt index 918dfe895..6f3400be4 100644 --- a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt +++ b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt @@ -71,17 +71,7 @@ internal object JpqlEntityManagerUtils { createQuery(entityManager, queryEnhancer.applySorting(sort), rendered.params, returnType.java), ) { // Lazy - createCountQuery(entityManager, queryEnhancer.createCountQueryFor(), rendered.params) - } - } - - private fun createCountQuery( - entityManager: EntityManager, - query: String, - queryParams: Map, - ): TypedQuery { - return entityManager.createQuery(query, Long::class.javaObjectType).apply { - setCountQueryParams(this, queryParams) + createQuery(entityManager, queryEnhancer.createCountQueryFor(), rendered.params, Long::class.javaObjectType) } } @@ -106,7 +96,7 @@ internal object JpqlEntityManagerUtils { } } - private fun setCountQueryParams(query: Query, params: Map) { + private fun setParams(query: Query, params: Map) { val parameterList = query.parameters.map { it.name }.toHashSet() params.forEach { (name, value) -> @@ -115,10 +105,4 @@ internal object JpqlEntityManagerUtils { } } } - - private fun setParams(query: Query, params: Map) { - params.forEach { (name, value) -> - query.setParameter(name, value) - } - } } diff --git a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt index be7497b0c..cce065fa9 100644 --- a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt @@ -42,6 +42,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + @MockK private lateinit var longTypedQuery1: TypedQuery @@ -76,6 +82,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { longTypedQuery1.equals(any()) } excludeRecords { longTypedQueryParam1.hashCode() } excludeRecords { longTypedQueryParam2.hashCode() } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -85,7 +93,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -96,6 +107,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -108,7 +122,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -120,6 +137,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -132,7 +152,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -143,6 +166,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -155,7 +181,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -167,6 +196,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -184,7 +216,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 andThen longTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first every { longTypedQuery1.parameters } returns setOf(longTypedQueryParam1, longTypedQueryParam2) every { longTypedQuery1.setParameter(any(), any()) } returns longTypedQuery1 every { longTypedQueryParam1.name } returns renderedParam1.first @@ -205,6 +240,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { queryEnhancer.applySorting(sort1) entityManager.createQuery(sortedQuery1, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)