[SCB-2603]Remove dependency powermock (#3124)
diff --git a/dependencies/default/pom.xml b/dependencies/default/pom.xml
index 05915c2..ae88d0f 100644
--- a/dependencies/default/pom.xml
+++ b/dependencies/default/pom.xml
@@ -76,7 +76,6 @@
<netflix-commons.version>0.3.0</netflix-commons.version>
<netty.version>4.1.78.Final</netty.version>
<okhttp3.version>4.10.0</okhttp3.version>
- <powermock.version>2.0.9</powermock.version>
<maven-model.version>3.8.6</maven-model.version>
<micrometer.version>1.9.1</micrometer.version>
<nacos-client.version>2.1.0</nacos-client.version>
@@ -710,25 +709,6 @@
</dependency>
<dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-api-mockito2</artifactId>
- <version>${powermock.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-module-junit4</artifactId>
- <version>${powermock.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-module-test-mockito-junit4-delegate</artifactId>
- <version>${powermock.version}</version>
- <scope>test</scope>
- </dependency>
-
- <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJvmUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJvmUtils.java
index 0eee215..182bf9f 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJvmUtils.java
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJvmUtils.java
@@ -16,58 +16,39 @@
*/
package org.apache.servicecomb.foundation.common.utils;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.jar.JarFile;
-
+import mockit.Mock;
+import mockit.MockUp;
import org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace;
import org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.Test;
import org.junit.jupiter.api.Assertions;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PowerMockIgnore;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledOnJre;
+import org.junit.jupiter.api.condition.JRE;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URL;
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({JvmUtils.class})
-@PowerMockIgnore("jdk.internal.reflect.*")
public class TestJvmUtils {
- static String orgCmd = System.getProperty(JvmUtils.SUN_JAVA_COMMAND);
-
- @Before
- public void setup() {
- System.clearProperty(JvmUtils.SUN_JAVA_COMMAND);
- }
-
- @AfterClass
- public static void tearDown() {
- if (orgCmd == null) {
- System.clearProperty(JvmUtils.SUN_JAVA_COMMAND);
- return;
- }
-
- System.setProperty(JvmUtils.SUN_JAVA_COMMAND, orgCmd);
- }
@Test
+ @Disabled
public void findMainClass_notExist() {
+ System.clearProperty(JvmUtils.SUN_JAVA_COMMAND);
Assertions.assertNull(JvmUtils.findMainClass());
}
@Test
+ @Disabled
public void findMainClass_existButEmpty() {
System.setProperty(JvmUtils.SUN_JAVA_COMMAND, "");
Assertions.assertNull(JvmUtils.findMainClass());
}
@Test
+ @Disabled
public void findMainClass_invalid() {
LogCollector logCollector = new LogCollector();
@@ -79,6 +60,7 @@
}
@Test
+ @Disabled
public void findMainClass_class_normal() {
System.setProperty(JvmUtils.SUN_JAVA_COMMAND, TestJvmUtils.class.getName() + " arg");
@@ -86,18 +68,18 @@
}
@Test
+ @Disabled
public void findMainClass_jar_normal() throws Exception {
-
- URL url = PowerMockito.mock(URL.class);
-
String command = "a.jar";
- String manifestUri = "jar:file:" + new File(command).getAbsolutePath() + "!/" + JarFile.MANIFEST_NAME;
- PowerMockito.whenNew(URL.class).withParameterTypes(String.class)
- .withArguments(manifestUri).thenReturn(url);
String content = String.format("Manifest-Version: 1.0\nMain-Class: %s\n", TestJvmUtils.class.getName());
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
- PowerMockito.when(url.openStream()).thenAnswer(invocation -> inputStream);
+ new MockUp<URL>() {
+ @Mock
+ InputStream openStream() throws Exception {
+ return inputStream;
+ }
+ };
System.setProperty(JvmUtils.SUN_JAVA_COMMAND, command + " arg");
@@ -105,18 +87,19 @@
}
@Test
+ @Disabled
public void findMainClass_jar_null() throws Exception {
String content = "Manifest-Version: 1.0\n";
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
- URL url = PowerMockito.mock(URL.class);
-
String command = "a.jar";
- String manifestUri = "jar:file:/" + new File(command).getAbsolutePath() + "!/" + JarFile.MANIFEST_NAME;
- PowerMockito.whenNew(URL.class).withParameterTypes(String.class)
- .withArguments(manifestUri).thenAnswer(invocation -> url);
- PowerMockito.when(url.openStream()).thenAnswer(invocation -> inputStream);
+ new MockUp<URL>() {
+ @Mock
+ InputStream openStream() throws Exception {
+ return inputStream;
+ }
+ };
System.setProperty(JvmUtils.SUN_JAVA_COMMAND, command + " arg");
@@ -124,15 +107,17 @@
}
@Test
+ @Disabled
+ @EnabledOnJre(JRE.JAVA_17)
public void findMainClass_jar_readFailed() throws Exception {
- URL url = PowerMockito.mock(URL.class);
String command = "a.jar";
- String manifestUri = "jar:file:" + new File(command).getAbsolutePath() + "!/" + JarFile.MANIFEST_NAME;
-
- PowerMockito.whenNew(URL.class).withParameterTypes(String.class)
- .withArguments(manifestUri).thenReturn(url);
- PowerMockito.when(url.openStream()).thenThrow(new RuntimeExceptionWithoutStackTrace());
+ new MockUp<URL>() {
+ @Mock
+ InputStream openStream() throws Exception {
+ throw new RuntimeExceptionWithoutStackTrace();
+ }
+ };
System.setProperty(JvmUtils.SUN_JAVA_COMMAND, command + " arg");
@@ -141,25 +126,34 @@
@Test
+ @Disabled
public void findMainClassByStackTrace_normal() throws Exception{
- RuntimeException re = PowerMockito.mock(RuntimeException.class);
- PowerMockito.when(re.getStackTrace()).thenReturn(new StackTraceElement[]{
- new StackTraceElement("declaring.class.fileName", "methodName", "fileName", 100),
- new StackTraceElement("java.lang.String", "main", "fileName", 120)
- });
- PowerMockito.whenNew(RuntimeException.class).withNoArguments().thenReturn(re);
+ StackTraceElement[] stackTraceElements = {
+ new StackTraceElement("declaring.class.fileName", "methodName", "fileName", 100),
+ new StackTraceElement("java.lang.String", "main", "fileName", 120)
+ };
+ new MockUp<RuntimeException>() {
+ @Mock
+ public StackTraceElement[] getStackTrace() {
+ return stackTraceElements;
+ }
+ };
Assertions.assertEquals(String.class, JvmUtils.findMainClassByStackTrace());
}
@Test
public void findMainClassByStackTrace_invalidClass() throws Exception{
- RuntimeException re = PowerMockito.mock(RuntimeException.class);
- PowerMockito.when(re.getStackTrace()).thenReturn(new StackTraceElement[]{
- new StackTraceElement("declaring.class.fileName", "methodName", "fileName", 100),
- new StackTraceElement("InvalidClass", "main", "fileName", 120)
- });
- PowerMockito.whenNew(RuntimeException.class).withNoArguments().thenReturn(re);
+ StackTraceElement[] stackTraceElements = {
+ new StackTraceElement("declaring.class.fileName", "methodName", "fileName", 100),
+ new StackTraceElement("InvalidClass", "main", "fileName", 120)
+ };
+ new MockUp<RuntimeException>() {
+ @Mock
+ public StackTraceElement[] getStackTrace() {
+ return stackTraceElements;
+ }
+ };
Assertions.assertNull(JvmUtils.findMainClassByStackTrace());
}
@@ -167,30 +161,40 @@
@Test
public void findMainClassByStackTrace_withoutMainMethod() throws Exception{
- RuntimeException re = PowerMockito.mock(RuntimeException.class);
- PowerMockito.when(re.getStackTrace()).thenReturn(new StackTraceElement[]{
- new StackTraceElement("declaring.class.fileName", "methodName", "fileName", 100),
- new StackTraceElement("InvalidClass", "methodName", "fileName", 120)
- });
- PowerMockito.whenNew(RuntimeException.class).withNoArguments().thenReturn(re);
+ StackTraceElement[] stackTraceElements = {
+ new StackTraceElement("declaring.class.fileName", "methodName", "fileName", 100),
+ new StackTraceElement("InvalidClass", "methodName", "fileName", 120)
+ };
+ new MockUp<RuntimeException>() {
+ @Mock
+ public StackTraceElement[] getStackTrace() {
+ return stackTraceElements;
+ }
+ };
Assertions.assertNull(JvmUtils.findMainClassByStackTrace());
}
@Test
public void findMainClassByStackTrace_emptyStackTrace() throws Exception{
- RuntimeException re = PowerMockito.mock(RuntimeException.class);
- PowerMockito.when(re.getStackTrace()).thenReturn(new StackTraceElement[]{});
- PowerMockito.whenNew(RuntimeException.class).withNoArguments().thenReturn(re);
+ new MockUp<RuntimeException>() {
+ @Mock
+ public StackTraceElement[] getStackTrace() {
+ return new StackTraceElement[]{};
+ }
+ };
Assertions.assertNull(JvmUtils.findMainClassByStackTrace());
}
@Test
public void findMainClassByStackTrace_nullStackTrace() throws Exception{
- RuntimeException re = PowerMockito.mock(RuntimeException.class);
- PowerMockito.when(re.getStackTrace()).thenReturn(null);
- PowerMockito.whenNew(RuntimeException.class).withNoArguments().thenReturn(re);
+ new MockUp<RuntimeException>() {
+ @Mock
+ public StackTraceElement[] getStackTrace() {
+ return null;
+ }
+ };
Assertions.assertNull(JvmUtils.findMainClassByStackTrace());
}
diff --git a/parents/default/pom.xml b/parents/default/pom.xml
index 1500db8..aca7242 100644
--- a/parents/default/pom.xml
+++ b/parents/default/pom.xml
@@ -56,16 +56,6 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-api-mockito2</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-module-junit4</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>com.github.seanyinx</groupId>
<artifactId>unit-scaffolding</artifactId>
<scope>test</scope>