Skip to content

Commit d80c022

Browse files
committed
refactor: migrate :src:protocol:jdbc Groovy tests to Kotlin
1 parent 2f2cbbd commit d80c022

3 files changed

Lines changed: 140 additions & 92 deletions

File tree

src/protocol/jdbc/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ dependencies {
3131
}
3232

3333
testImplementation(testFixtures(projects.src.core))
34+
testImplementation("io.mockk:mockk")
3435
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.jmeter.protocol.jdbc
19+
20+
import org.apache.jmeter.samplers.SampleResult
21+
import java.sql.Connection
22+
23+
fun AbstractJDBCTestElement.executeForTest(conn: Connection, sampleResult: SampleResult) =
24+
execute(conn, sampleResult)

src/protocol/jdbc/src/test/kotlin/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerTest.kt

Lines changed: 115 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,116 +17,139 @@
1717

1818
package org.apache.jmeter.protocol.jdbc.sampler
1919

20+
import io.mockk.every
21+
import io.mockk.justRun
22+
import io.mockk.mockk
23+
import io.mockk.verifyOrder
24+
import org.apache.jmeter.config.ConfigTestElement
25+
import org.apache.jmeter.config.gui.SimpleConfigGui
26+
import org.apache.jmeter.protocol.jdbc.executeForTest
27+
import org.apache.jmeter.samplers.SampleResult
28+
import org.apache.jmeter.testelement.TestElementSchema
29+
import org.junit.jupiter.api.Assertions.assertArrayEquals
30+
import org.junit.jupiter.api.Assertions.assertEquals
31+
import org.junit.jupiter.api.Assertions.assertFalse
32+
import org.junit.jupiter.api.Assertions.assertTrue
33+
import org.junit.jupiter.api.BeforeEach
34+
import org.junit.jupiter.api.Test
35+
import org.junit.jupiter.api.TestInstance
36+
import org.junit.jupiter.params.ParameterizedTest
37+
import org.junit.jupiter.params.provider.MethodSource
2038
import java.sql.Connection
2139
import java.sql.ResultSet
2240
import java.sql.ResultSetMetaData
2341
import java.sql.SQLException
2442
import java.sql.Statement
2543

26-
import org.apache.jmeter.config.ConfigTestElement
27-
import org.apache.jmeter.samplers.SampleResult
28-
import org.apache.jmeter.testelement.property.JMeterProperty
29-
30-
import spock.lang.Specification
31-
import spock.lang.Unroll
32-
33-
@Unroll
34-
class JDBCSamplerSpec extends Specification {
35-
36-
def sut = new JDBCSampler()
37-
38-
def "applies matches SimpleConfigGui"() {
39-
given:
40-
def mockConfig = Mock(ConfigTestElement)
41-
def mockProperty = Mock(JMeterProperty)
42-
when:
43-
def applies = sut.applies(mockConfig)
44-
then:
45-
1 * mockConfig.getProperty(_ as String) >> mockProperty
46-
1 * mockProperty.getStringValue() >> propertyValue
47-
applies == expectedApplies
48-
// this check will catch any future additions
49-
sut.APPLIABLE_CONFIG_CLASSES.size() == 1
50-
where:
51-
propertyValue | expectedApplies
52-
"org.apache.jmeter.config.gui.SimpleConfigGui" | true
53-
"org.apache.jmeter.config.gui.SomethingElse" | false
44+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
45+
class JDBCSamplerTest {
46+
47+
lateinit var sut: JDBCSampler
48+
49+
@BeforeEach
50+
fun setup() {
51+
sut = JDBCSampler()
52+
}
53+
54+
@Test
55+
fun `applies matches SimpleConfigGui`() {
56+
val configElement = SimpleConfigGui().createTestElement() as ConfigTestElement
57+
assertTrue(
58+
sut.applies(configElement),
59+
"JDBCSampler should apply to SimpleConfigGui"
60+
)
61+
configElement[TestElementSchema.guiClass] = "org.apache.jmeter.config.gui.SomethingElse"
62+
assertFalse(
63+
sut.applies(configElement),
64+
"JDBCSampler should not apply to gui.SomethingElse"
65+
)
5466
}
5567

5668
/* AbstractJDBCTestElement tests */
5769

58-
def "execute with SELECT query"() {
59-
given:
60-
def conn = Mock(Connection)
61-
def sample = Mock(SampleResult)
62-
def stmt = Mock(Statement)
63-
def rs = Mock(ResultSet)
64-
def meta = Mock(ResultSetMetaData)
65-
sut.setQuery("SELECT")
66-
when:
67-
def response = sut.execute(conn, sample)
68-
then:
69-
1 * conn.createStatement() >> stmt
70-
1 * stmt.setQueryTimeout(0)
71-
1 * stmt.executeQuery(_ as String) >> rs
72-
1 * sample.latencyEnd()
70+
@Test
71+
fun `execute with SELECT query`() {
72+
val meta = mockk<ResultSetMetaData> {
73+
every { columnCount } returns 0
74+
}
75+
val rs = mockk<ResultSet> {
76+
every { metaData } returns meta
77+
every { close() } throws SQLException()
78+
every { next() } returns false
79+
}
80+
val stmt = mockk<Statement> {
81+
every { executeQuery(any()) } returns rs
82+
justRun { queryTimeout = any() }
83+
justRun { close() }
84+
}
85+
val conn = mockk<Connection> {
86+
every { createStatement() } returns stmt
87+
}
88+
val sample = mockk<SampleResult> {
89+
justRun { latencyEnd() }
90+
}
91+
92+
sut.query = "SELECT"
93+
val response = sut.executeForTest(conn, sample)
94+
95+
verifyOrder {
96+
conn.createStatement()
97+
stmt.queryTimeout = 0
98+
stmt.executeQuery(any())
99+
sample.latencyEnd()
73100
// getStringFromResultSet
74-
1 * rs.getMetaData() >> meta
75-
1 * rs.next()
76-
1 * rs.close() >> { throw new SQLException() }
77-
1 * stmt.close()
78-
// 1 * conn.close() // closed by JDBCSampler
79-
1 * meta.getColumnCount() >> 0
80-
response == [] as byte[]
101+
rs.metaData
102+
meta.columnCount
103+
rs.next()
104+
rs.close()
105+
stmt.close()
106+
// conn.close() // closed by JDBCSampler
107+
}
108+
assertArrayEquals(byteArrayOf(), response, "response")
81109
}
82110

83-
def "Catches SQLException during Connection closing"() {
84-
given:
85-
def mockConnection = Mock(Connection)
86-
when:
87-
sut.close(mockConnection)
88-
then:
89-
1 * mockConnection.close() >> { throw new SQLException() }
90-
noExceptionThrown()
111+
@Test
112+
fun `Catches SQLException during Connection closing`() {
113+
val mockConnection = mockk<Connection> {
114+
every { close() } throws SQLException()
115+
}
116+
JDBCSampler.close(mockConnection)
91117
}
92118

93-
def "Catches SQLException during Statement closing"() {
94-
given:
95-
def mockStatement = Mock(Statement)
96-
when:
97-
sut.close(mockStatement)
98-
then:
99-
1 * mockStatement.close() >> { throw new SQLException() }
100-
noExceptionThrown()
119+
@Test
120+
fun `Catches SQLException during Statement closing`() {
121+
val mockStatement = mockk<Statement> {
122+
every { close() } throws SQLException()
123+
}
124+
JDBCSampler.close(mockStatement)
101125
}
102126

103-
def "Catches SQLException during ResultSet closing"() {
104-
given:
105-
def mockResultSet = Mock(ResultSet)
106-
when:
107-
sut.close(mockResultSet)
108-
then:
109-
1 * mockResultSet.close() >> { throw new SQLException() }
110-
noExceptionThrown()
127+
@Test
128+
fun `Catches SQLException during ResultSet closing`() {
129+
val mockStatement = mockk<ResultSet> {
130+
every { close() } throws SQLException()
131+
}
132+
JDBCSampler.close(mockStatement)
111133
}
112134

113-
def "getIntegerQueryTimeout returns #expectedTimeout from #initialTimeout"() {
114-
given:
115-
sut.setQueryTimeout(initialTimeout)
116-
when:
117-
def timeout = sut.getIntegerQueryTimeout()
118-
then:
119-
timeout == expectedTimeout
120-
where:
121-
initialTimeout | expectedTimeout
122-
"0" | 0
123-
"1" | 1
124-
"2147483647" | Integer.MAX_VALUE
125-
"-1" | -1
126-
"-2147483648" | Integer.MIN_VALUE
127-
"2147483648" | 0 // max int + 1
128-
"-2147483649" | 0 // min int - 1
129-
"nan" | 0
130-
"" | 0
135+
data class GetIntegerQueryTimeoutCase(val initialTimeout: String, val expectedTimeout: Int, val message: String? = null)
136+
137+
@ParameterizedTest
138+
@MethodSource("getIntegerQueryTimeoutCases")
139+
fun `getIntegerQueryTimeout returns #expectedTimeout from #initialTimeout`(case: GetIntegerQueryTimeoutCase) {
140+
sut.queryTimeout = case.initialTimeout
141+
assertEquals(case.expectedTimeout, sut.getIntegerQueryTimeout(), case.message)
131142
}
143+
144+
fun getIntegerQueryTimeoutCases() = listOf(
145+
GetIntegerQueryTimeoutCase("0", 0),
146+
GetIntegerQueryTimeoutCase("1", 1),
147+
GetIntegerQueryTimeoutCase("2147483647", Integer.MAX_VALUE),
148+
GetIntegerQueryTimeoutCase("-1", -1),
149+
GetIntegerQueryTimeoutCase("-2147483648", Integer.MIN_VALUE),
150+
GetIntegerQueryTimeoutCase("2147483648", 0, "max int + 1"),
151+
GetIntegerQueryTimeoutCase("-2147483649", 0, "min int - 1"),
152+
GetIntegerQueryTimeoutCase("nan", 0),
153+
GetIntegerQueryTimeoutCase("", 0),
154+
)
132155
}

0 commit comments

Comments
 (0)