You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by sh...@apache.org on 2022/06/24 07:54:33 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2603]Remove dependency powermock (#3124)

This is an automated email from the ASF dual-hosted git repository.

shoothzj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 55acbf226 [SCB-2603]Remove dependency powermock (#3124)
55acbf226 is described below

commit 55acbf226d97b8e2348d9f3d12f9fe1f49ff9b35
Author: Tian Luo <fu...@outlook.com>
AuthorDate: Fri Jun 24 15:54:26 2022 +0800

    [SCB-2603]Remove dependency powermock (#3124)
---
 dependencies/default/pom.xml                       |  20 ---
 .../foundation/common/utils/TestJvmUtils.java      | 156 +++++++++++----------
 parents/default/pom.xml                            |  10 --
 3 files changed, 80 insertions(+), 106 deletions(-)

diff --git a/dependencies/default/pom.xml b/dependencies/default/pom.xml
index 05915c2b4..ae88d0f4f 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>
@@ -709,25 +708,6 @@
         <scope>test</scope>
       </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>
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 0eee21530..182bf9fda 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;
 
-@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);
-  }
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URL;
 
-  @AfterClass
-  public static void tearDown() {
-    if (orgCmd == null) {
-      System.clearProperty(JvmUtils.SUN_JAVA_COMMAND);
-      return;
-    }
 
-    System.setProperty(JvmUtils.SUN_JAVA_COMMAND, orgCmd);
-  }
+public class TestJvmUtils {
 
   @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 @@ public class TestJvmUtils {
   }
 
   @Test
+  @Disabled
   public void findMainClass_class_normal() {
     System.setProperty(JvmUtils.SUN_JAVA_COMMAND, TestJvmUtils.class.getName() + " arg");
 
@@ -86,18 +68,18 @@ public class TestJvmUtils {
   }
 
   @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 @@ public class TestJvmUtils {
   }
 
   @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 @@ public class TestJvmUtils {
   }
 
   @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 @@ public class TestJvmUtils {
 
 
   @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 @@ public class TestJvmUtils {
 
   @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 1500db8ed..aca724256 100644
--- a/parents/default/pom.xml
+++ b/parents/default/pom.xml
@@ -55,16 +55,6 @@
       <artifactId>hamcrest-core</artifactId>
       <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>