Skip to content

Commit 9ab79e0

Browse files
authored
[Improvement][Test] Fully remove the usage of powermock from the whole project (#12244)
* Fully remove the usage of powermock from the whole project * Upgrade org.reflections to 0.10.12
1 parent dec28b5 commit 9ab79e0

File tree

22 files changed

+208
-139
lines changed

22 files changed

+208
-139
lines changed

dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.apache.dolphinscheduler.dao.PluginDao;
2424
import org.apache.dolphinscheduler.remote.NettyRemotingServer;
2525
import org.apache.dolphinscheduler.remote.command.CommandType;
26-
import org.apache.dolphinscheduler.remote.config.NettyServerConfig;
26+
import org.apache.dolphinscheduler.remote.factory.NettyRemotingServerFactory;
2727

2828
import java.io.Closeable;
2929

@@ -108,20 +108,16 @@ public void destroy(String cause) {
108108
}
109109
}
110110

111-
private void checkTable() {
111+
protected void checkTable() {
112112
if (!pluginDao.checkPluginDefineTableExist()) {
113113
logger.error("Plugin Define Table t_ds_plugin_define Not Exist . Please Create it First !");
114114
System.exit(1);
115115
}
116116
}
117117

118-
private void startServer() {
119-
NettyServerConfig serverConfig = new NettyServerConfig();
120-
serverConfig.setListenPort(alertConfig.getPort());
121-
122-
nettyRemotingServer = new NettyRemotingServer(serverConfig);
118+
protected void startServer() {
119+
nettyRemotingServer = NettyRemotingServerFactory.buildNettyRemotingServer(alertConfig.getPort());
123120
nettyRemotingServer.registerProcessor(CommandType.ALERT_SEND_REQUEST, alertRequestProcessor);
124121
nettyRemotingServer.start();
125122
}
126-
127123
}

dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,26 @@
1717

1818
package org.apache.dolphinscheduler.alert;
1919

20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyInt;
22+
import static org.mockito.Mockito.doNothing;
23+
import static org.mockito.Mockito.mockStatic;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
26+
2027
import org.apache.dolphinscheduler.dao.PluginDao;
2128
import org.apache.dolphinscheduler.remote.NettyRemotingServer;
22-
import org.apache.dolphinscheduler.remote.config.NettyServerConfig;
23-
import org.junit.jupiter.api.Assertions;
29+
import org.apache.dolphinscheduler.remote.factory.NettyRemotingServerFactory;
30+
2431
import org.junit.jupiter.api.BeforeEach;
2532
import org.junit.jupiter.api.Test;
2633
import org.junit.jupiter.api.extension.ExtendWith;
2734
import org.mockito.InjectMocks;
2835
import org.mockito.Mock;
36+
import org.mockito.MockedStatic;
2937
import org.mockito.Mockito;
38+
import org.mockito.Spy;
3039
import org.mockito.junit.jupiter.MockitoExtension;
31-
import org.powermock.reflect.Whitebox;
3240

3341
@ExtendWith(MockitoExtension.class)
3442
public class AlertServerTest {
@@ -42,28 +50,43 @@ public class AlertServerTest {
4250
@Mock
4351
private AlertSenderService alertSenderService;
4452

53+
@Mock
54+
private NettyRemotingServer nettyRemotingServer;
55+
4556
@InjectMocks
57+
@Spy
4658
private AlertServer alertServer;
4759

4860
@BeforeEach
4961
void init() {
5062
Mockito.lenient().when(pluginDao.checkPluginDefineTableExist()).thenReturn(true);
5163

5264
Mockito.lenient().when(alertConfig.getPort()).thenReturn(50052);
53-
54-
Mockito.doNothing().when(alertSenderService).start();
55-
5665
}
66+
5767
@Test
58-
public void alertServerStartSuccessfully() {
68+
public void alertServerRunSuccessfully() {
69+
doNothing().when(alertServer).checkTable();
70+
doNothing().when(alertServer).startServer();
5971

6072
alertServer.run(null);
6173

62-
NettyRemotingServer nettyRemotingServer = Whitebox.getInternalState(alertServer, "nettyRemotingServer");
63-
64-
NettyServerConfig nettyServerConfig = Whitebox.getInternalState(nettyRemotingServer, "serverConfig");
65-
66-
Assertions.assertEquals(50052, nettyServerConfig.getListenPort());
74+
Mockito.verify(alertServer, times(1)).checkTable();
75+
Mockito.verify(alertServer, times(1)).startServer();
76+
Mockito.verify(alertSenderService, times(1)).start();
77+
}
6778

79+
@Test
80+
public void alertServerServerStartWithExpectedListeningPort() {
81+
try (
82+
MockedStatic<NettyRemotingServerFactory> mockedNettyRemotingServerFactory =
83+
mockStatic(NettyRemotingServerFactory.class)) {
84+
mockedNettyRemotingServerFactory.when(() -> NettyRemotingServerFactory.buildNettyRemotingServer(anyInt()))
85+
.thenReturn(nettyRemotingServer);
86+
alertServer.startServer();
87+
mockedNettyRemotingServerFactory.verify(() -> NettyRemotingServerFactory.buildNettyRemotingServer(50052));
88+
verify(nettyRemotingServer, times(1)).registerProcessor(any(), any());
89+
verify(nettyRemotingServer, times(1)).start();
90+
}
6891
}
6992
}

dolphinscheduler-bom/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<servlet-api.version>2.5</servlet-api.version>
7878
<springfox.version>3.0.0</springfox.version>
7979
<guava-retry.version>2.0.0</guava-retry.version>
80-
<reflections.version>0.9.12</reflections.version>
80+
<reflections.version>0.10.2</reflections.version>
8181
<py4j.version>0.10.9</py4j.version>
8282
<jsr305.version>3.0.0</jsr305.version>
8383
<commons-compress.version>1.21</commons-compress.version>
@@ -162,6 +162,12 @@
162162
<groupId>com.cronutils</groupId>
163163
<artifactId>cron-utils</artifactId>
164164
<version>${cron-utils.version}</version>
165+
<exclusions>
166+
<exclusion>
167+
<groupId>org.javassist</groupId>
168+
<artifactId>javassist</artifactId>
169+
</exclusion>
170+
</exclusions>
165171
</dependency>
166172

167173
<dependency>

dolphinscheduler-common/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
<artifactId>mockito-core</artifactId>
6666
<scope>test</scope>
6767
</dependency>
68+
<dependency>
69+
<groupId>org.mockito</groupId>
70+
<artifactId>mockito-inline</artifactId>
71+
<version>3.12.4</version>
72+
<!-- TODO: move this dependency to root pom after removing powermock in the whole project -->
73+
<scope>test</scope>
74+
</dependency>
6875

6976
<dependency>
7077
<groupId>org.springframework</groupId>

dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus;
2121
import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus;
2222

23-
import org.apache.commons.lang3.StringUtils;
2423
import org.apache.commons.lang3.SystemUtils;
2524

2625
import java.time.Duration;
@@ -797,9 +796,6 @@ private Constants() {
797796
*/
798797
public static final String PSTREE = "pstree";
799798

800-
public static final boolean KUBERNETES_MODE = !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_HOST"))
801-
&& !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_PORT"));
802-
803799
/**
804800
* dry run flag
805801
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.dolphinscheduler.common.utils;
19+
20+
import org.apache.commons.lang3.StringUtils;
21+
22+
import lombok.experimental.UtilityClass;
23+
24+
@UtilityClass
25+
public class KubernetesUtils {
26+
27+
public boolean isKubernetesMode() {
28+
return !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_HOST"))
29+
&& !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_PORT"));
30+
}
31+
32+
}

dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.apache.dolphinscheduler.common.Constants;
2323

24+
import org.apache.http.conn.util.InetAddressUtils;
25+
2426
import java.io.IOException;
2527
import java.net.Inet6Address;
2628
import java.net.InetAddress;
@@ -33,7 +35,6 @@
3335
import java.util.Objects;
3436
import java.util.Optional;
3537

36-
import org.apache.http.conn.util.InetAddressUtils;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

@@ -75,7 +76,7 @@ public static String getAddr(int port) {
7576
*/
7677
public static String getHost(InetAddress inetAddress) {
7778
if (inetAddress != null) {
78-
if (Constants.KUBERNETES_MODE) {
79+
if (KubernetesUtils.isKubernetesMode()) {
7980
String canonicalHost = inetAddress.getCanonicalHostName();
8081
String[] items = canonicalHost.split("\\.");
8182
if (items.length == 6 && "svc".equals(items[3])) {
@@ -98,7 +99,7 @@ public static String getHost() {
9899
HOST_ADDRESS = getHost(address);
99100
return HOST_ADDRESS;
100101
}
101-
return Constants.KUBERNETES_MODE ? "localhost" : "127.0.0.1";
102+
return KubernetesUtils.isKubernetesMode() ? "localhost" : "127.0.0.1";
102103
}
103104

104105
private static InetAddress getLocalAddress() {
@@ -258,16 +259,18 @@ public static boolean ignoreNetworkInterface(NetworkInterface networkInterface)
258259
}
259260

260261
private static boolean isSpecifyNetworkInterface(NetworkInterface networkInterface) {
261-
String preferredNetworkInterface = PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED,
262-
System.getProperty(Constants.DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED));
262+
String preferredNetworkInterface =
263+
PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED,
264+
System.getProperty(Constants.DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED));
263265
return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface);
264266
}
265267

266268
private static NetworkInterface findAddress(List<NetworkInterface> validNetworkInterfaces) {
267269
if (validNetworkInterfaces.isEmpty()) {
268270
return null;
269271
}
270-
String networkPriority = PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_PRIORITY_STRATEGY, NETWORK_PRIORITY_DEFAULT);
272+
String networkPriority = PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_PRIORITY_STRATEGY,
273+
NETWORK_PRIORITY_DEFAULT);
271274
if (NETWORK_PRIORITY_DEFAULT.equalsIgnoreCase(networkPriority)) {
272275
return findAddressByDefaultPolicy(validNetworkInterfaces);
273276
} else if (NETWORK_PRIORITY_INNER.equalsIgnoreCase(networkPriority)) {

dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/CommonUtilsTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@
1717

1818
package org.apache.dolphinscheduler.common.utils;
1919

20-
import org.apache.dolphinscheduler.spi.utils.PropertyUtils;
21-
22-
import org.apache.hadoop.security.UserGroupInformation;
23-
2420
import java.net.InetAddress;
2521
import java.net.UnknownHostException;
2622

2723
import org.junit.Assert;
2824
import org.junit.Test;
2925
import org.junit.runner.RunWith;
30-
import org.powermock.core.classloader.annotations.PrepareForTest;
31-
import org.powermock.modules.junit4.PowerMockRunner;
26+
import org.mockito.junit.MockitoJUnitRunner;
3227
import org.slf4j.Logger;
3328
import org.slf4j.LoggerFactory;
3429

3530
/**
3631
* configuration test
3732
*/
38-
@RunWith(PowerMockRunner.class)
39-
@PrepareForTest(value = {PropertyUtils.class, UserGroupInformation.class})
33+
@RunWith(MockitoJUnitRunner.class)
4034
public class CommonUtilsTest {
35+
4136
private static final Logger logger = LoggerFactory.getLogger(CommonUtilsTest.class);
4237

4338
@Test
@@ -89,4 +84,4 @@ public void test() {
8984
Assert.assertTrue(true);
9085
}
9186

92-
}
87+
}

dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/FileUtilsTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@
2727
import org.junit.Assert;
2828
import org.junit.Test;
2929
import org.junit.runner.RunWith;
30-
import org.powermock.api.mockito.PowerMockito;
31-
import org.powermock.core.classloader.annotations.PrepareForTest;
32-
import org.powermock.modules.junit4.PowerMockRunner;
30+
import org.mockito.MockedStatic;
31+
import org.mockito.Mockito;
32+
import org.mockito.junit.MockitoJUnitRunner;
3333

34-
@RunWith(PowerMockRunner.class)
35-
@PrepareForTest(DateUtils.class)
34+
@RunWith(MockitoJUnitRunner.class)
3635
public class FileUtilsTest {
3736

3837
@Test
3938
public void testGetDownloadFilename() {
40-
PowerMockito.mockStatic(DateUtils.class);
41-
PowerMockito.when(DateUtils.getCurrentTime(YYYYMMDDHHMMSS)).thenReturn("20190101101059");
42-
Assert.assertEquals("/tmp/dolphinscheduler/download/20190101101059/test",
43-
FileUtils.getDownloadFilename("test"));
39+
try (MockedStatic<DateUtils> mockedDateUtils = Mockito.mockStatic(DateUtils.class)) {
40+
mockedDateUtils.when(() -> DateUtils.getCurrentTime(YYYYMMDDHHMMSS)).thenReturn("20190101101059");
41+
Assert.assertEquals("/tmp/dolphinscheduler/download/20190101101059/test",
42+
FileUtils.getDownloadFilename("test"));
43+
}
4444
}
4545

4646
@Test
4747
public void testGetUploadFilename() {
4848
Assert.assertEquals("/tmp/dolphinscheduler/aaa/resources/bbb",
49-
FileUtils.getUploadFilename("aaa","bbb"));
49+
FileUtils.getUploadFilename("aaa", "bbb"));
5050
}
5151

5252
@Test
@@ -68,9 +68,9 @@ public void testCreateWorkDirIfAbsent() {
6868
@Test
6969
public void testSetValue() {
7070
try {
71-
PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE,"true");
71+
PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE, "true");
7272
Assert.assertTrue(PropertyUtils.getBoolean(Constants.DATASOURCE_ENCRYPTION_ENABLE));
73-
PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE,"false");
73+
PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE, "false");
7474
Assert.assertFalse(PropertyUtils.getBoolean(Constants.DATASOURCE_ENCRYPTION_ENABLE));
7575
} catch (Exception e) {
7676
Assert.assertTrue(false);

dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HadoopUtilsTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@
2222
import org.junit.Assert;
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
25-
import org.powermock.api.mockito.PowerMockito;
26-
import org.powermock.core.classloader.annotations.PrepareForTest;
27-
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
28-
import org.powermock.modules.junit4.PowerMockRunner;
25+
import org.mockito.MockedStatic;
26+
import org.mockito.Mockito;
27+
import org.mockito.junit.MockitoJUnitRunner;
2928
import org.slf4j.Logger;
3029
import org.slf4j.LoggerFactory;
3130

3231
/**
3332
* hadoop utils test
3433
*/
35-
@RunWith(PowerMockRunner.class)
36-
@PrepareForTest(value = {HadoopUtils.class})
37-
@SuppressStaticInitializationFor("org.apache.dolphinscheduler.common.utils.HttpUtils")
34+
@RunWith(MockitoJUnitRunner.class)
3835
public class HadoopUtilsTest {
36+
3937
private static final Logger logger = LoggerFactory.getLogger(HadoopUtilsTest.class);
4038

4139
@Test
@@ -64,10 +62,12 @@ public void getHdfsFileName() {
6462

6563
@Test
6664
public void getAppAddress() {
67-
PowerMockito.mockStatic(HttpUtils.class);
68-
PowerMockito.when(HttpUtils.get("http://ds1:8088/ws/v1/cluster/info")).thenReturn("{\"clusterInfo\":{\"state\":\"STARTED\",\"haState\":\"ACTIVE\"}}");
69-
logger.info(HadoopUtils.getAppAddress("http://ds1:8088/ws/v1/cluster/apps/%s", "ds1,ds2"));
70-
Assert.assertTrue(true);
65+
try (MockedStatic<HttpUtils> mockedHttpUtils = Mockito.mockStatic(HttpUtils.class)) {
66+
mockedHttpUtils.when(() -> HttpUtils.get("http://ds1:8088/ws/v1/cluster/info"))
67+
.thenReturn("{\"clusterInfo\":{\"state\":\"STARTED\",\"haState\":\"ACTIVE\"}}");
68+
logger.info(HadoopUtils.getAppAddress("http://ds1:8088/ws/v1/cluster/apps/%s", "ds1,ds2"));
69+
Assert.assertTrue(true);
70+
}
7171
}
7272

73-
}
73+
}

0 commit comments

Comments
 (0)