You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by mx...@apache.org on 2016/12/05 23:29:12 UTC
[3/3] flink git commit: [test-utils] cleanup and improve method to
set the environment
[test-utils] cleanup and improve method to set the environment
- introduced parameter to update or overwrite the environment
- make Windows-specific code explicit
- avoid duplicate update of environment map
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/8d7c3ff0
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/8d7c3ff0
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/8d7c3ff0
Branch: refs/heads/master
Commit: 8d7c3ff087ac1390e6da0054ec55974258b6b78c
Parents: 230bf17
Author: Maximilian Michels <mx...@apache.org>
Authored: Mon Dec 5 19:23:32 2016 +0100
Committer: Maximilian Michels <mx...@apache.org>
Committed: Tue Dec 6 00:29:57 2016 +0100
----------------------------------------------------------------------
.../flink/core/testutils/CommonTestUtils.java | 52 ++++++++++----------
1 file changed, 26 insertions(+), 26 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/8d7c3ff0/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/core/testutils/CommonTestUtils.java
----------------------------------------------------------------------
diff --git a/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/core/testutils/CommonTestUtils.java b/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/core/testutils/CommonTestUtils.java
index 45c5a77..2eb18c1 100644
--- a/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/core/testutils/CommonTestUtils.java
+++ b/flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/core/testutils/CommonTestUtils.java
@@ -30,7 +30,6 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
-import java.util.Collections;
import java.util.Map;
import static org.junit.Assert.fail;
@@ -118,37 +117,38 @@ public class CommonTestUtils {
}
}
- // This code is taken from: http://stackoverflow.com/a/7201825/568695
+ public static void setEnv(Map<String, String> newenv) {
+ setEnv(newenv, true);
+ }
+
+ // This code is taken slightly modified from: http://stackoverflow.com/a/7201825/568695
// it changes the environment variables of this JVM. Use only for testing purposes!
@SuppressWarnings("unchecked")
- public static void setEnv(Map<String, String> newenv) {
+ public static void setEnv(Map<String, String> newenv, boolean clearExisting) {
try {
+ Map<String, String> env = System.getenv();
+ Class<?> clazz = env.getClass();
+ Field field = clazz.getDeclaredField("m");
+ field.setAccessible(true);
+ Map<String, String> map = (Map<String, String>) field.get(env);
+ if (clearExisting) {
+ map.clear();
+ }
+ map.putAll(newenv);
+
+ // only for Windows
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
- Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
- theEnvironmentField.setAccessible(true);
- Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
- env.putAll(newenv);
- Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
- theCaseInsensitiveEnvironmentField.setAccessible(true);
- Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
- cienv.putAll(newenv);
- } catch (NoSuchFieldException e) {
try {
- Class<?>[] classes = Collections.class.getDeclaredClasses();
- Map<String, String> env = System.getenv();
- for (Class<?> cl : classes) {
- if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
- Field field = cl.getDeclaredField("m");
- field.setAccessible(true);
- Object obj = field.get(env);
- Map<String, String> map = (Map<String, String>) obj;
- map.clear();
- map.putAll(newenv);
- }
+ Field theCaseInsensitiveEnvironmentField =
+ processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
+ theCaseInsensitiveEnvironmentField.setAccessible(true);
+ Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
+ if (clearExisting) {
+ cienv.clear();
}
- } catch (Exception e2) {
- throw new RuntimeException(e2);
- }
+ cienv.putAll(newenv);
+ } catch (NoSuchFieldException ignored) {}
+
} catch (Exception e1) {
throw new RuntimeException(e1);
}