|
1 | 1 | package org.utplsql.api.testRunner;
|
2 | 2 |
|
3 | 3 | import oracle.jdbc.OracleConnection;
|
| 4 | +import org.hamcrest.Matcher; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
4 | 6 | import org.junit.jupiter.api.Test;
|
| 7 | +import org.mockito.verification.VerificationMode; |
5 | 8 | import org.utplsql.api.CustomTypes;
|
6 | 9 | import org.utplsql.api.FileMapping;
|
7 | 10 | import org.utplsql.api.TestRunnerOptions;
|
|
13 | 16 |
|
14 | 17 | import static org.hamcrest.CoreMatchers.containsString;
|
15 | 18 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 19 | +import static org.hamcrest.Matchers.not; |
16 | 20 | import static org.mockito.Mockito.*;
|
17 | 21 |
|
18 | 22 | public class DynamicTestRunnerStatementTest {
|
19 | 23 |
|
20 |
| - @Test |
21 |
| - void explore() throws SQLException { |
22 |
| - // Expectation objects |
23 |
| - Object[] expectedFileMapping = new Object[]{new FileMapping("someFile", "owner", "object", "PACKAGE")}; |
| 24 | + private DynamicTestRunnerStatement testRunnerStatement; |
| 25 | + private CallableStatement callableStatement; |
| 26 | + private OracleConnection oracleConnection; |
| 27 | + private TestRunnerOptions options; |
| 28 | + private Object[] expectedFileMapping; |
24 | 29 |
|
25 |
| - // Mock some internals. This is not pretty, but a first step |
| 30 | + private OracleConnection getMockedOracleConnection( Object[] expectedFileMapping ) throws SQLException { |
26 | 31 | OracleConnection oracleConnection = mock(OracleConnection.class);
|
27 | 32 | when(oracleConnection.unwrap(OracleConnection.class))
|
28 | 33 | .thenReturn(oracleConnection);
|
29 |
| - CallableStatement callableStatement = mock(CallableStatement.class); |
| 34 | + mockFileMapper(oracleConnection, expectedFileMapping); |
| 35 | + return oracleConnection; |
| 36 | + } |
30 | 37 |
|
31 |
| - // FileMapper mocks |
| 38 | + private void mockFileMapper( OracleConnection mockedOracleConnection, Object[] expectedFileMapping ) throws SQLException { |
| 39 | + Array fileMapperArray = mock(Array.class); |
32 | 40 | CallableStatement fileMapperStatement = mock(CallableStatement.class);
|
| 41 | + |
| 42 | + when(fileMapperArray.getArray()) |
| 43 | + .thenReturn(expectedFileMapping); |
| 44 | + when(fileMapperStatement.getArray(1)) |
| 45 | + .thenReturn(fileMapperArray); |
33 | 46 | when(
|
34 |
| - oracleConnection.prepareCall(argThat( |
| 47 | + mockedOracleConnection.prepareCall(argThat( |
35 | 48 | a -> a.startsWith("BEGIN ? := ut_file_mapper.build_file_mappings("))
|
36 | 49 | ))
|
37 | 50 | .thenReturn(fileMapperStatement);
|
38 |
| - Array fileMapperArray = mock(Array.class); |
39 |
| - when(fileMapperStatement.getArray(1)) |
40 |
| - .thenReturn(fileMapperArray); |
41 |
| - when(fileMapperArray.getArray()) |
42 |
| - .thenReturn(expectedFileMapping); |
| 51 | + } |
43 | 52 |
|
44 |
| - // Act |
45 |
| - TestRunnerOptions options = TestRunnerStatementProviderIT.getCompletelyFilledOptions(); |
| 53 | + private Matcher<String> doesOrDoesNotContainString( String string, boolean shouldBeThere ) { |
| 54 | + return (shouldBeThere) |
| 55 | + ? containsString(string) |
| 56 | + : not(containsString(string)); |
| 57 | + } |
46 | 58 |
|
47 |
| - DynamicTestRunnerStatement testRunnerStatement = DynamicTestRunnerStatement |
48 |
| - .forVersion(Version.V3_1_7, oracleConnection, options, callableStatement); |
| 59 | + private VerificationMode doesOrDoesNotGetCalled( boolean shouldBeThere ) { |
| 60 | + return (shouldBeThere) |
| 61 | + ? times(1) |
| 62 | + : never(); |
| 63 | + } |
49 | 64 |
|
50 |
| - // Assert all parameters are set appropriately |
| 65 | + private void initTestRunnerStatementForVersion( Version version ) throws SQLException { |
| 66 | + testRunnerStatement = DynamicTestRunnerStatement |
| 67 | + .forVersion(version, oracleConnection, options, callableStatement); |
| 68 | + } |
| 69 | + |
| 70 | + private void checkBaseParameters() throws SQLException { |
51 | 71 | assertThat(testRunnerStatement.getSql(), containsString("a_paths => ?"));
|
52 | 72 | verify(callableStatement).setArray(1, null);
|
53 | 73 | verify(oracleConnection).createOracleArray(CustomTypes.UT_VARCHAR2_LIST, options.pathList.toArray());
|
@@ -77,20 +97,160 @@ void explore() throws SQLException {
|
77 | 97 | assertThat(testRunnerStatement.getSql(), containsString("a_exclude_objects => ?"));
|
78 | 98 | verify(callableStatement).setArray(8, null);
|
79 | 99 | verify(oracleConnection).createOracleArray(CustomTypes.UT_VARCHAR2_LIST, options.includeObjects.toArray());
|
| 100 | + } |
| 101 | + |
| 102 | + private void checkFailOnError( boolean shouldBeThere ) throws SQLException { |
| 103 | + assertThat(testRunnerStatement.getSql(), doesOrDoesNotContainString("a_fail_on_errors => (case ? when 1 then true else false)", shouldBeThere)); |
| 104 | + verify(callableStatement, doesOrDoesNotGetCalled(shouldBeThere)).setInt(9, 1); |
| 105 | + } |
| 106 | + |
| 107 | + private void checkClientCharacterSet( boolean shouldBeThere ) throws SQLException { |
| 108 | + assertThat(testRunnerStatement.getSql(), doesOrDoesNotContainString("a_client_character_set => ?", shouldBeThere)); |
| 109 | + verify(callableStatement, doesOrDoesNotGetCalled(shouldBeThere)).setString(10, "UTF8"); |
| 110 | + } |
| 111 | + |
| 112 | + private void checkRandomTestOrder( boolean shouldBeThere ) throws SQLException { |
| 113 | + assertThat(testRunnerStatement.getSql(), doesOrDoesNotContainString("a_random_test_order => (case ? when 1 then true else false)", shouldBeThere)); |
| 114 | + verify(callableStatement, doesOrDoesNotGetCalled(shouldBeThere)).setInt(11, 1); |
| 115 | + assertThat(testRunnerStatement.getSql(), doesOrDoesNotContainString("a_random_test_order_seed => ?", shouldBeThere)); |
| 116 | + verify(callableStatement, doesOrDoesNotGetCalled(shouldBeThere)).setInt(12, 123); |
| 117 | + } |
| 118 | + |
| 119 | + private void checkTags( boolean shouldBeThere ) throws SQLException { |
| 120 | + assertThat(testRunnerStatement.getSql(), doesOrDoesNotContainString("a_tags => ?", shouldBeThere)); |
| 121 | + verify(callableStatement, doesOrDoesNotGetCalled(shouldBeThere)).setString(13, "WIP,long_running"); |
| 122 | + } |
80 | 123 |
|
81 |
| - assertThat(testRunnerStatement.getSql(), containsString("a_fail_on_errors => (case ? when 1 then true else false)")); |
82 |
| - verify(callableStatement).setInt(9, 1); |
| 124 | + @BeforeEach |
| 125 | + void initParameters() throws SQLException { |
| 126 | + expectedFileMapping = new Object[]{new FileMapping("someFile", "owner", "object", "PACKAGE")}; |
83 | 127 |
|
84 |
| - assertThat(testRunnerStatement.getSql(), containsString("a_client_character_set => ?")); |
85 |
| - verify(callableStatement).setString(10, "UTF8"); |
| 128 | + // Mock some internals. This is not pretty, but a first step |
| 129 | + oracleConnection = getMockedOracleConnection(expectedFileMapping); |
| 130 | + callableStatement = mock(CallableStatement.class); |
86 | 131 |
|
87 |
| - assertThat(testRunnerStatement.getSql(), containsString("a_random_test_order => (case ? when 1 then true else false)")); |
88 |
| - verify(callableStatement).setInt(11, 1); |
| 132 | + // Act |
| 133 | + options = TestRunnerStatementProviderIT.getCompletelyFilledOptions(); |
| 134 | + } |
89 | 135 |
|
90 |
| - assertThat(testRunnerStatement.getSql(), containsString("a_random_test_order_seed => ?")); |
91 |
| - verify(callableStatement).setInt(12, 123); |
| 136 | + @Test |
| 137 | + void version_3_0_2_parameters() throws SQLException { |
| 138 | + initTestRunnerStatementForVersion(Version.V3_0_2); |
| 139 | + |
| 140 | + checkBaseParameters(); |
| 141 | + checkFailOnError(false); |
| 142 | + checkClientCharacterSet(false); |
| 143 | + checkRandomTestOrder(false); |
| 144 | + checkTags(false); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void version_3_0_3_parameters() throws SQLException { |
| 149 | + initTestRunnerStatementForVersion(Version.V3_0_3); |
| 150 | + |
| 151 | + checkBaseParameters(); |
| 152 | + checkFailOnError(true); |
| 153 | + checkClientCharacterSet(false); |
| 154 | + checkRandomTestOrder(false); |
| 155 | + checkTags(false); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void version_3_0_4_parameters() throws SQLException { |
| 160 | + initTestRunnerStatementForVersion(Version.V3_0_4); |
| 161 | + |
| 162 | + checkBaseParameters(); |
| 163 | + checkFailOnError(true); |
| 164 | + checkClientCharacterSet(false); |
| 165 | + checkRandomTestOrder(false); |
| 166 | + checkTags(false); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void version_3_1_0_parameters() throws SQLException { |
| 171 | + initTestRunnerStatementForVersion(Version.V3_1_0); |
| 172 | + |
| 173 | + checkBaseParameters(); |
| 174 | + checkFailOnError(true); |
| 175 | + checkClientCharacterSet(false); |
| 176 | + checkRandomTestOrder(false); |
| 177 | + checkTags(false); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + void version_3_1_1_parameters() throws SQLException { |
| 182 | + initTestRunnerStatementForVersion(Version.V3_1_1); |
| 183 | + |
| 184 | + checkBaseParameters(); |
| 185 | + checkFailOnError(true); |
| 186 | + checkClientCharacterSet(false); |
| 187 | + checkRandomTestOrder(false); |
| 188 | + checkTags(false); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void version_3_1_2_parameters() throws SQLException { |
| 193 | + initTestRunnerStatementForVersion(Version.V3_1_2); |
| 194 | + |
| 195 | + checkBaseParameters(); |
| 196 | + checkFailOnError(true); |
| 197 | + checkClientCharacterSet(true); |
| 198 | + checkRandomTestOrder(false); |
| 199 | + checkTags(false); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + void version_3_1_3_parameters() throws SQLException { |
| 204 | + initTestRunnerStatementForVersion(Version.V3_1_3); |
| 205 | + |
| 206 | + checkBaseParameters(); |
| 207 | + checkFailOnError(true); |
| 208 | + checkClientCharacterSet(true); |
| 209 | + checkRandomTestOrder(false); |
| 210 | + checkTags(false); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + void version_3_1_4_parameters() throws SQLException { |
| 215 | + initTestRunnerStatementForVersion(Version.V3_1_4); |
| 216 | + |
| 217 | + checkBaseParameters(); |
| 218 | + checkFailOnError(true); |
| 219 | + checkClientCharacterSet(true); |
| 220 | + checkRandomTestOrder(false); |
| 221 | + checkTags(false); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + void version_3_1_5_parameters() throws SQLException { |
| 226 | + initTestRunnerStatementForVersion(Version.V3_1_5); |
| 227 | + |
| 228 | + checkBaseParameters(); |
| 229 | + checkFailOnError(true); |
| 230 | + checkClientCharacterSet(true); |
| 231 | + checkRandomTestOrder(false); |
| 232 | + checkTags(false); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + void version_3_1_6_parameters() throws SQLException { |
| 237 | + initTestRunnerStatementForVersion(Version.V3_1_6); |
| 238 | + |
| 239 | + checkBaseParameters(); |
| 240 | + checkFailOnError(true); |
| 241 | + checkClientCharacterSet(true); |
| 242 | + checkRandomTestOrder(false); |
| 243 | + checkTags(false); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + void version_3_1_7_parameters() throws SQLException { |
| 248 | + initTestRunnerStatementForVersion(Version.V3_1_7); |
92 | 249 |
|
93 |
| - assertThat(testRunnerStatement.getSql(), containsString("a_tags => ?")); |
94 |
| - verify(callableStatement).setString(13, "WIP,long_running"); |
| 250 | + checkBaseParameters(); |
| 251 | + checkFailOnError(true); |
| 252 | + checkClientCharacterSet(true); |
| 253 | + checkRandomTestOrder(true); |
| 254 | + checkTags(true); |
95 | 255 | }
|
96 | 256 | }
|
0 commit comments