|
17 | 17 |
|
18 | 18 | package org.apache.jmeter.protocol.jdbc.sampler |
19 | 19 |
|
| 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 |
20 | 38 | import java.sql.Connection |
21 | 39 | import java.sql.ResultSet |
22 | 40 | import java.sql.ResultSetMetaData |
23 | 41 | import java.sql.SQLException |
24 | 42 | import java.sql.Statement |
25 | 43 |
|
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 | + ) |
54 | 66 | } |
55 | 67 |
|
56 | 68 | /* AbstractJDBCTestElement tests */ |
57 | 69 |
|
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() |
73 | 100 | // 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") |
81 | 109 | } |
82 | 110 |
|
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) |
91 | 117 | } |
92 | 118 |
|
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) |
101 | 125 | } |
102 | 126 |
|
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) |
111 | 133 | } |
112 | 134 |
|
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) |
131 | 142 | } |
| 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 | + ) |
132 | 155 | } |
0 commit comments