|
51 | 51 | import com.starrocks.sql.ast.PrepareStmt; |
52 | 52 | import com.starrocks.thrift.TUniqueId; |
53 | 53 | import com.starrocks.utframe.UtFrameUtils; |
54 | | -import mockit.Expectations; |
55 | | -import mockit.Mock; |
56 | | -import mockit.MockUp; |
57 | | -import mockit.Mocked; |
58 | 54 | import org.junit.jupiter.api.Assertions; |
59 | 55 | import org.junit.jupiter.api.BeforeAll; |
60 | 56 | import org.junit.jupiter.api.BeforeEach; |
61 | 57 | import org.junit.jupiter.api.Test; |
| 58 | +import org.mockito.MockedConstruction; |
| 59 | +import org.mockito.Mockito; |
62 | 60 | import org.xnio.StreamConnection; |
63 | 61 |
|
64 | | -import java.io.IOException; |
65 | 62 | import java.nio.ByteBuffer; |
66 | 63 |
|
| 64 | +import static org.mockito.Mockito.anyLong; |
| 65 | +import static org.mockito.Mockito.doNothing; |
| 66 | +import static org.mockito.Mockito.doReturn; |
| 67 | +import static org.mockito.Mockito.mock; |
| 68 | +import static org.mockito.Mockito.spy; |
| 69 | +import static org.mockito.Mockito.when; |
| 70 | + |
67 | 71 | public class ConnectProcessorExecuteTest extends DDLTestBase { |
68 | 72 | private static ByteBuffer initDbPacket; |
69 | 73 | private static ByteBuffer initWarehousePacket; |
@@ -76,9 +80,14 @@ public class ConnectProcessorExecuteTest extends DDLTestBase { |
76 | 80 | private static AuditEventBuilder auditBuilder = new AuditEventBuilder(); |
77 | 81 | private static ConnectContext myContext; |
78 | 82 |
|
79 | | - @Mocked |
80 | 83 | private static StreamConnection connection; |
81 | 84 |
|
| 85 | + static { |
| 86 | + connection = mock(StreamConnection.class); |
| 87 | + java.net.InetSocketAddress mockAddr = new java.net.InetSocketAddress("127.0.0.1", 12345); |
| 88 | + when(connection.getPeerAddress()).thenReturn(mockAddr); |
| 89 | + } |
| 90 | + |
82 | 91 | private static PQueryStatistics statistics = new PQueryStatistics(); |
83 | 92 |
|
84 | 93 |
|
@@ -177,50 +186,26 @@ public void setUp() throws Exception { |
177 | 186 | changeUserPacket.clear(); |
178 | 187 | resetConnectionPacket.clear(); |
179 | 188 | // Mock |
180 | | - MysqlChannel channel = new MysqlChannel(connection); |
181 | | - new Expectations(channel) { |
182 | | - { |
183 | | - channel.getRemoteHostPortString(); |
184 | | - minTimes = 0; |
185 | | - result = "127.0.0.1:12345"; |
186 | | - } |
187 | | - }; |
| 189 | + MysqlChannel channel = mock(MysqlChannel.class); |
| 190 | + when(channel.getRemoteHostPortString()).thenReturn("127.0.0.1:12345"); |
188 | 191 | myContext = new ConnectContext(connection); |
189 | 192 | Deencapsulation.setField(myContext, "mysqlChannel", channel); |
190 | 193 | super.setUp(); |
191 | 194 | } |
192 | 195 |
|
193 | 196 | private static MysqlChannel mockChannel(ByteBuffer packet) { |
194 | 197 | try { |
195 | | - MysqlChannel channel = new MysqlChannel(connection); |
196 | | - new Expectations(channel) { |
197 | | - { |
198 | | - // Mock receive |
199 | | - channel.fetchOnePacket(); |
200 | | - minTimes = 0; |
201 | | - result = packet; |
202 | | - |
203 | | - // Mock reset |
204 | | - channel.setSequenceId(0); |
205 | | - times = 1; |
206 | | - |
207 | | - // Mock send |
208 | | - channel.sendAndFlush((ByteBuffer) any); |
209 | | - minTimes = 0; |
210 | | - |
211 | | - channel.getRemoteHostPortString(); |
212 | | - minTimes = 0; |
213 | | - result = "127.0.0.1:12345"; |
214 | | - } |
215 | | - }; |
| 198 | + MysqlChannel channel = mock(MysqlChannel.class); |
| 199 | + when(channel.fetchOnePacket()).thenReturn(packet); |
| 200 | + when(channel.getRemoteHostPortString()).thenReturn("127.0.0.1:12345"); |
216 | 201 | return channel; |
217 | | - } catch (IOException e) { |
| 202 | + } catch (Exception e) { |
218 | 203 | return null; |
219 | 204 | } |
220 | 205 | } |
221 | 206 |
|
222 | 207 | private static ConnectContext initMockContext(MysqlChannel channel, GlobalStateMgr globalStateMgr) { |
223 | | - ConnectContext context = new ConnectContext(connection) { |
| 208 | + ConnectContext context = spy(new ConnectContext(connection) { |
224 | 209 | private boolean firstTimeToSetCommand = true; |
225 | 210 |
|
226 | 211 | @Override |
@@ -257,67 +242,23 @@ public void setCommand(MysqlCommand command) { |
257 | 242 | super.setCommand(command); |
258 | 243 | } |
259 | 244 | } |
260 | | - }; |
261 | | - |
262 | | - new Expectations(context) { |
263 | | - { |
264 | | - context.getMysqlChannel(); |
265 | | - minTimes = 0; |
266 | | - result = channel; |
267 | | - |
268 | | - context.isKilled(); |
269 | | - minTimes = 0; |
270 | | - maxTimes = 3; |
271 | | - returns(false, true, false); |
272 | | - |
273 | | - context.getGlobalStateMgr(); |
274 | | - minTimes = 0; |
275 | | - result = globalStateMgr; |
276 | | - |
277 | | - context.getAuditEventBuilder(); |
278 | | - minTimes = 0; |
279 | | - result = auditBuilder; |
280 | | - |
281 | | - context.getQualifiedUser(); |
282 | | - minTimes = 0; |
283 | | - result = "testCluster:user"; |
284 | | - |
285 | | - context.getCurrentUserIdentity(); |
286 | | - minTimes = 0; |
287 | | - result = UserIdentity.ROOT; |
288 | | - |
289 | | - context.getStartTime(); |
290 | | - minTimes = 0; |
291 | | - result = 0L; |
292 | | - |
293 | | - context.getReturnRows(); |
294 | | - minTimes = 0; |
295 | | - result = 1L; |
296 | | - |
297 | | - context.setStmtId(anyLong); |
298 | | - minTimes = 0; |
299 | | - |
300 | | - context.getStmtId(); |
301 | | - minTimes = 0; |
302 | | - result = 1L; |
303 | | - |
304 | | - context.getExecutionId(); |
305 | | - minTimes = 0; |
306 | | - result = new TUniqueId(); |
307 | | - |
308 | | - context.getCapability(); |
309 | | - minTimes = 0; |
310 | | - result = MysqlCapability.DEFAULT_CAPABILITY; |
311 | | - |
312 | | - context.getAccessControlContext(); |
313 | | - minTimes = 0; |
314 | | - result = new AccessControlContext(); |
315 | | - |
316 | | - context.getAuthenticationProvider(); |
317 | | - minTimes = 0; |
318 | | - result = new PlainPasswordAuthenticationProvider(MysqlPassword.EMPTY_PASSWORD); |
319 | | - } |
320 | | - }; |
| 245 | + }); |
| 246 | + |
| 247 | + doReturn(channel).when(context).getMysqlChannel(); |
| 248 | + doReturn(false).doReturn(true).doReturn(false).when(context).isKilled(); |
| 249 | + doReturn(globalStateMgr).when(context).getGlobalStateMgr(); |
| 250 | + doReturn(auditBuilder).when(context).getAuditEventBuilder(); |
| 251 | + doReturn("testCluster:user").when(context).getQualifiedUser(); |
| 252 | + doReturn(UserIdentity.ROOT).when(context).getCurrentUserIdentity(); |
| 253 | + doReturn(0L).when(context).getStartTime(); |
| 254 | + doReturn(1L).when(context).getReturnRows(); |
| 255 | + doNothing().when(context).setStmtId(anyLong()); |
| 256 | + doReturn(1L).when(context).getStmtId(); |
| 257 | + doReturn(new TUniqueId()).when(context).getExecutionId(); |
| 258 | + doReturn(MysqlCapability.DEFAULT_CAPABILITY).when(context).getCapability(); |
| 259 | + doReturn(new AccessControlContext()).when(context).getAccessControlContext(); |
| 260 | + doReturn(new PlainPasswordAuthenticationProvider(MysqlPassword.EMPTY_PASSWORD)) |
| 261 | + .when(context).getAuthenticationProvider(); |
321 | 262 |
|
322 | 263 | return context; |
323 | 264 | } |
@@ -340,17 +281,15 @@ public void testStmtExecute() throws Exception { |
340 | 281 |
|
341 | 282 | ConnectProcessor processor = new ConnectProcessor(ctx); |
342 | 283 | // Mock statement executor |
343 | | - // Create mock for StmtExecutor using MockUp instead of @Mocked parameter |
344 | | - new MockUp<StmtExecutor>() { |
345 | | - @Mock |
346 | | - public PQueryStatistics getQueryStatisticsForAuditLog() { |
347 | | - return statistics; |
348 | | - } |
349 | | - }; |
| 284 | + try (MockedConstruction<StmtExecutor> mocked = Mockito.mockConstruction(StmtExecutor.class, |
| 285 | + (mock, context) -> { |
| 286 | + when(mock.getQueryStatisticsForAuditLog()).thenReturn(statistics); |
| 287 | + })) { |
350 | 288 |
|
351 | | - processor.processOnce(); |
| 289 | + processor.processOnce(); |
352 | 290 |
|
353 | | - Assertions.assertEquals(MysqlCommand.COM_STMT_EXECUTE, myContext.getCommand()); |
354 | | - Assertions.assertTrue(ctx.getState().isQuery()); |
| 291 | + Assertions.assertEquals(MysqlCommand.COM_STMT_EXECUTE, myContext.getCommand()); |
| 292 | + Assertions.assertTrue(ctx.getState().isQuery()); |
| 293 | + } |
355 | 294 | } |
356 | 295 | } |