You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2022/02/06 06:54:25 UTC

[pulsar] branch master updated: [test] remove com.github.stefanbirkner:system-rules from the test classpath (#14127)

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

mmerli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 42e5dea  [test] remove com.github.stefanbirkner:system-rules from the test classpath (#14127)
42e5dea is described below

commit 42e5deade47ef717204f16d212f6cded6cc0f762
Author: Nicolò Boschi <bo...@gmail.com>
AuthorDate: Sun Feb 6 07:53:12 2022 +0100

    [test] remove com.github.stefanbirkner:system-rules from the test classpath (#14127)
    
    * [test] remove com.github.stefanbirkner:system-rules from test classpath
    
    * also fix EnvironmentBasedSecretsProviderTest
---
 .../EnvironmentBasedSecretsProviderTest.java       | 37 +++++++---------------
 pulsar-io/flume/pom.xml                            |  6 ----
 .../flume/node/TestEnvVarResolverProperties.java   | 32 ++++++++++++++-----
 3 files changed, 35 insertions(+), 40 deletions(-)

diff --git a/pulsar-functions/secrets/src/test/java/org/apache/pulsar/functions/secretsprovider/EnvironmentBasedSecretsProviderTest.java b/pulsar-functions/secrets/src/test/java/org/apache/pulsar/functions/secretsprovider/EnvironmentBasedSecretsProviderTest.java
index 1fc76b3..8dbc880 100644
--- a/pulsar-functions/secrets/src/test/java/org/apache/pulsar/functions/secretsprovider/EnvironmentBasedSecretsProviderTest.java
+++ b/pulsar-functions/secrets/src/test/java/org/apache/pulsar/functions/secretsprovider/EnvironmentBasedSecretsProviderTest.java
@@ -22,9 +22,10 @@ package org.apache.pulsar.functions.secretsprovider;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
 
-import java.lang.reflect.Field;
+import java.util.HashMap;
 import java.util.Map;
 
+import org.powermock.reflect.Whitebox;
 import org.testng.annotations.Test;
 
 public class EnvironmentBasedSecretsProviderTest {
@@ -40,30 +41,14 @@ public class EnvironmentBasedSecretsProviderTest {
             throws Exception {
 
         Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment");
-
-        Field unmodifiableMapField = getAccessibleField(processEnvironment, "theUnmodifiableEnvironment");
-        Object unmodifiableMap = unmodifiableMapField.get(null);
-        injectIntoUnmodifiableMap(key, value, unmodifiableMap);
-
-        Field mapField = getAccessibleField(processEnvironment, "theEnvironment");
-        Map<String, String> map = (Map<String, String>) mapField.get(null);
-        map.put(key, value);
-    }
-
-    private static Field getAccessibleField(Class<?> clazz, String fieldName)
-            throws NoSuchFieldException {
-
-        Field field = clazz.getDeclaredField(fieldName);
-        field.setAccessible(true);
-        return field;
-    }
-
-    private static void injectIntoUnmodifiableMap(String key, String value, Object map)
-            throws ReflectiveOperationException {
-
-        Class unmodifiableMap = Class.forName("java.util.Collections$UnmodifiableMap");
-        Field field = getAccessibleField(unmodifiableMap, "m");
-        Object obj = field.get(map);
-        ((Map<String, String>) obj).put(key, value);
+        Map<String,String> unmodifiableMap = new HashMap<>(Whitebox
+                .getInternalState(processEnvironment, "theUnmodifiableEnvironment"));
+        unmodifiableMap.put(key, value);
+        Whitebox.setInternalState(processEnvironment, "theUnmodifiableEnvironment", unmodifiableMap);
+
+        Map<String,String> envMap = new HashMap<>(Whitebox
+                .getInternalState(processEnvironment, "theEnvironment"));
+        envMap.put(key, value);
+        Whitebox.setInternalState(processEnvironment, "theEnvironment", envMap);
     }
 }
diff --git a/pulsar-io/flume/pom.xml b/pulsar-io/flume/pom.xml
index 881648f..0c3467c 100644
--- a/pulsar-io/flume/pom.xml
+++ b/pulsar-io/flume/pom.xml
@@ -117,12 +117,6 @@
         </dependency>
 
         <dependency>
-            <groupId>com.github.stefanbirkner</groupId>
-            <artifactId>system-rules</artifactId>
-            <scope>test</scope>
-            <version>1.17.0</version>
-        </dependency>
-        <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
             <version>${guava.version}</version>
diff --git a/pulsar-io/flume/src/test/java/org/apache/pulsar/io/flume/node/TestEnvVarResolverProperties.java b/pulsar-io/flume/src/test/java/org/apache/pulsar/io/flume/node/TestEnvVarResolverProperties.java
index 4440acf..42a055c 100644
--- a/pulsar-io/flume/src/test/java/org/apache/pulsar/io/flume/node/TestEnvVarResolverProperties.java
+++ b/pulsar-io/flume/src/test/java/org/apache/pulsar/io/flume/node/TestEnvVarResolverProperties.java
@@ -19,19 +19,20 @@
 package org.apache.pulsar.io.flume.node;
 
 import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+import lombok.SneakyThrows;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.contrib.java.lang.system.EnvironmentVariables;
+import org.powermock.reflect.Whitebox;
 
 public final class TestEnvVarResolverProperties {
     private static final File TESTFILE = new File(
             TestEnvVarResolverProperties.class.getClassLoader()
                     .getResource("flume-conf-with-envvars.properties").getFile());
 
-    @Rule
-    public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
     private PropertiesFileConfigurationProvider provider;
 
     @Before
@@ -41,15 +42,15 @@ public final class TestEnvVarResolverProperties {
 
     @Test
     public void resolveEnvVar() {
-        environmentVariables.set("VARNAME", "varvalue");
+        injectEnvironmentVariable("VARNAME", "varvalue");
         String resolved = EnvVarResolverProperties.resolveEnvVars("padding ${VARNAME} padding");
         Assert.assertEquals("padding varvalue padding", resolved);
     }
 
     @Test
     public void resolveEnvVars() {
-        environmentVariables.set("VARNAME1", "varvalue1");
-        environmentVariables.set("VARNAME2", "varvalue2");
+        injectEnvironmentVariable("VARNAME1", "varvalue1");
+        injectEnvironmentVariable("VARNAME2", "varvalue2");
         String resolved = EnvVarResolverProperties
                 .resolveEnvVars("padding ${VARNAME1} ${VARNAME2} padding");
         Assert.assertEquals("padding varvalue1 varvalue2 padding", resolved);
@@ -58,7 +59,7 @@ public final class TestEnvVarResolverProperties {
     @Test
     public void getProperty() {
         String NC_PORT = "6667";
-        environmentVariables.set("NC_PORT", NC_PORT);
+        injectEnvironmentVariable("NC_PORT", NC_PORT);
         System.setProperty("propertiesImplementation",
                 "org.apache.pulsar.io.flume.node.EnvVarResolverProperties");
 
@@ -66,4 +67,19 @@ public final class TestEnvVarResolverProperties {
                 .getConfigurationFor("a1")
                 .getSourceContext().get("r1").getParameters().get("port"));
     }
+
+    @SneakyThrows
+    private static void injectEnvironmentVariable(String key, String value) {
+
+        Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment");
+        Map<String,String> unmodifiableMap = new HashMap<>(Whitebox
+                .getInternalState(processEnvironment, "theUnmodifiableEnvironment"));
+        unmodifiableMap.put(key, value);
+        Whitebox.setInternalState(processEnvironment, "theUnmodifiableEnvironment", unmodifiableMap);
+
+        Map<String,String> envMap = new HashMap<>(Whitebox
+                .getInternalState(processEnvironment, "theEnvironment"));
+        envMap.put(key, value);
+        Whitebox.setInternalState(processEnvironment, "theEnvironment", envMap);
+    }
 }