Skip to content

Commit

Permalink
fix: revert divided methods into one method
Browse files Browse the repository at this point in the history
  • Loading branch information
bagger3025 committed Oct 11, 2024
1 parent e914ced commit 20cf102
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any?>,
): TypedQuery<Long> {
return entityManager.createQuery(query, Long::class.javaObjectType).apply {
setCountQueryParams(this, queryParams)
createQuery(entityManager, queryEnhancer.createCountQueryFor(), rendered.params, Long::class.javaObjectType)
}
}

Expand All @@ -106,21 +96,13 @@ internal object JpqlEntityManagerUtils {
}
}

private fun setCountQueryParams(query: Query, params: Map<String, Any?>) {
val parameterList = query.parameters.map {
it.name
}.toHashSet()
private fun setParams(query: Query, params: Map<String, Any?>) {
val parameterList = query.parameters.map { it.name }.toHashSet()

params.forEach { (name, value) ->
if (parameterList.contains(name)) {
query.setParameter(name, value)
}
}
}

private fun setParams(query: Query, params: Map<String, Any?>) {
params.forEach { (name, value) ->
query.setParameter(name, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
@MockK
private lateinit var stringTypedQuery1: TypedQuery<String>

@MockK
private lateinit var stringTypedQueryParam1: Parameter<String>

@MockK
private lateinit var stringTypedQueryParam2: Parameter<String>

@MockK
private lateinit var longTypedQuery1: TypedQuery<Long>

Expand Down Expand Up @@ -76,6 +82,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
excludeRecords { longTypedQuery1.equals(any()) }
excludeRecords { longTypedQueryParam1.hashCode() }
excludeRecords { longTypedQueryParam2.hashCode() }
excludeRecords { stringTypedQueryParam1.hashCode() }
excludeRecords { stringTypedQueryParam2.hashCode() }
}

@Test
Expand All @@ -85,7 +93,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), 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)
Expand All @@ -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)
}
Expand All @@ -108,7 +122,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -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)
}
Expand All @@ -132,7 +152,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context)
Expand All @@ -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)
}
Expand All @@ -155,7 +181,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -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)
}
Expand All @@ -184,7 +216,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
every {
entityManager.createQuery(any<String>(), any<Class<*>>())
} returns stringTypedQuery1 andThen longTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), 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<String>(), any()) } returns longTypedQuery1
every { longTypedQueryParam1.name } returns renderedParam1.first
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any?>,
): TypedQuery<Long> {
return entityManager.createQuery(query, Long::class.javaObjectType).apply {
setCountQueryParams(this, queryParams)
createQuery(entityManager, queryEnhancer.createCountQueryFor(), rendered.params, Long::class.javaObjectType)
}
}

Expand All @@ -106,7 +96,7 @@ internal object JpqlEntityManagerUtils {
}
}

private fun setCountQueryParams(query: Query, params: Map<String, Any?>) {
private fun setParams(query: Query, params: Map<String, Any?>) {
val parameterList = query.parameters.map { it.name }.toHashSet()

params.forEach { (name, value) ->
Expand All @@ -115,10 +105,4 @@ internal object JpqlEntityManagerUtils {
}
}
}

private fun setParams(query: Query, params: Map<String, Any?>) {
params.forEach { (name, value) ->
query.setParameter(name, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
@MockK
private lateinit var stringTypedQuery1: TypedQuery<String>

@MockK
private lateinit var stringTypedQueryParam1: Parameter<String>

@MockK
private lateinit var stringTypedQueryParam2: Parameter<String>

@MockK
private lateinit var longTypedQuery1: TypedQuery<Long>

Expand Down Expand Up @@ -76,6 +82,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
excludeRecords { longTypedQuery1.equals(any()) }
excludeRecords { longTypedQueryParam1.hashCode() }
excludeRecords { longTypedQueryParam2.hashCode() }
excludeRecords { stringTypedQueryParam1.hashCode() }
excludeRecords { stringTypedQueryParam2.hashCode() }
}

@Test
Expand All @@ -85,7 +93,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), 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)
Expand All @@ -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)
}
Expand All @@ -108,7 +122,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -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)
}
Expand All @@ -132,7 +152,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context)
Expand All @@ -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)
}
Expand All @@ -155,7 +181,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -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)
}
Expand All @@ -184,7 +216,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
every {
entityManager.createQuery(any<String>(), any<Class<*>>())
} returns stringTypedQuery1 andThen longTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), 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<String>(), any()) } returns longTypedQuery1
every { longTypedQueryParam1.name } returns renderedParam1.first
Expand All @@ -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)

Expand Down

0 comments on commit 20cf102

Please sign in to comment.