Skip to content

Commit 114c58c

Browse files
fix mock framework
1 parent 7abdf4f commit 114c58c

1 file changed

Lines changed: 54 additions & 24 deletions

File tree

fe/fe-core/src/test/java/org/apache/doris/nereids/parser/EncryptSQLTest.java

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -446,35 +446,65 @@ public void testEncryption() throws Exception {
446446
@Test
447447
public void testCreateUserPasswordMasking() throws Exception {
448448
ctx.setDatabase("test");
449-
new MockUp<StmtExecutor>() {
450-
@Mock
451-
public boolean isForwardToMaster() {
452-
return false;
453-
}
454-
};
455-
ctx.setEnv(env);
456-
Config.enable_nereids_load = true;
457-
// testing for https://github.com/apache/doris/issues/62140
458-
String sql = "CREATE USER 'test_user62140'@'%' IDENTIFIED BY '123456'";
459-
String res = "CREATE USER 'test_user62140'@'%' IDENTIFIED BY '*XXX'";
460-
parseAndCheck(sql, res);
449+
try (MockedConstruction<StmtExecutor> ignored = Mockito.mockConstruction(StmtExecutor.class,
450+
Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS),
451+
(mock, context) -> {
452+
Mockito.doReturn(false).when(mock).isForwardToMaster();
453+
Profile profile = new Profile(false, 0, 0);
454+
Deencapsulation.setField(mock, "profile", profile);
455+
Mockito.doReturn(profile).when(mock).getProfile();
456+
Mockito.doReturn(ctx).when(mock).getContext();
457+
Mockito.doNothing().when(mock).execute();
458+
Deencapsulation.setField(mock, "context", ctx);
459+
if (context.arguments().size() >= 2
460+
&& context.arguments().get(1) instanceof StatementBase) {
461+
Deencapsulation.setField(mock, "parsedStmt",
462+
context.arguments().get(1));
463+
}
464+
if (ctx.getStatementContext() == null) {
465+
ctx.setStatementContext(new StatementContext());
466+
}
467+
})) {
468+
ctx.setEnv(env);
469+
ctx.setCurrentUserIdentity(UserIdentity.ROOT);
470+
Config.enable_nereids_load = true;
471+
// testing for https://github.com/apache/doris/issues/62140
472+
String sql = "CREATE USER 'test_user62140'@'%' IDENTIFIED BY '123456'";
473+
String res = "CREATE USER 'test_user62140'@'%' IDENTIFIED BY '*XXX'";
474+
parseAndCheck(sql, res);
475+
}
461476
}
462477

463478
@Test
464479
public void testAlterUserPasswordMasking() throws Exception {
465480
ctx.setDatabase("test");
466-
new MockUp<StmtExecutor>() {
467-
@Mock
468-
public boolean isForwardToMaster() {
469-
return false;
470-
}
471-
};
472-
ctx.setEnv(env);
473-
Config.enable_nereids_load = true;
474-
// testing for https://github.com/apache/doris/issues/62140
475-
String sql = "ALTER USER 'test_user62140'@'%' IDENTIFIED BY '123456'";
476-
String res = "ALTER USER 'test_user62140'@'%' IDENTIFIED BY '*XXX'";
477-
parseAndCheck(sql, res);
481+
try (MockedConstruction<StmtExecutor> ignored = Mockito.mockConstruction(StmtExecutor.class,
482+
Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS),
483+
(mock, context) -> {
484+
Mockito.doReturn(false).when(mock).isForwardToMaster();
485+
Profile profile = new Profile(false, 0, 0);
486+
Deencapsulation.setField(mock, "profile", profile);
487+
Mockito.doReturn(profile).when(mock).getProfile();
488+
Mockito.doReturn(ctx).when(mock).getContext();
489+
Mockito.doNothing().when(mock).execute();
490+
Deencapsulation.setField(mock, "context", ctx);
491+
if (context.arguments().size() >= 2
492+
&& context.arguments().get(1) instanceof StatementBase) {
493+
Deencapsulation.setField(mock, "parsedStmt",
494+
context.arguments().get(1));
495+
}
496+
if (ctx.getStatementContext() == null) {
497+
ctx.setStatementContext(new StatementContext());
498+
}
499+
})) {
500+
ctx.setEnv(env);
501+
ctx.setCurrentUserIdentity(UserIdentity.ROOT);
502+
Config.enable_nereids_load = true;
503+
// testing for https://github.com/apache/doris/issues/62140
504+
String sql = "ALTER USER 'test_user62140'@'%' IDENTIFIED BY '123456'";
505+
String res = "ALTER USER 'test_user62140'@'%' IDENTIFIED BY '*XXX'";
506+
parseAndCheck(sql, res);
507+
}
478508
}
479509

480510
private void parseAndCheck(String sql, String expected) throws Exception {

0 commit comments

Comments
 (0)