From: <kp...@us...> - 2023-09-16 19:59:57
|
Revision: 25671 http://sourceforge.net/p/jedit/svn/25671 Author: kpouer Date: 2023-09-16 19:59:56 +0000 (Sat, 16 Sep 2023) Log Message: ----------- The unit test should not depends on the execution environment Modified Paths: -------------- jEdit/trunk/test/org/gjt/sp/jedit/MiscUtilitiesTest.java Modified: jEdit/trunk/test/org/gjt/sp/jedit/MiscUtilitiesTest.java =================================================================== --- jEdit/trunk/test/org/gjt/sp/jedit/MiscUtilitiesTest.java 2023-09-07 20:14:30 UTC (rev 25670) +++ jEdit/trunk/test/org/gjt/sp/jedit/MiscUtilitiesTest.java 2023-09-16 19:59:56 UTC (rev 25671) @@ -30,6 +30,7 @@ import java.io.File; import java.lang.reflect.Field; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -174,16 +175,14 @@ updateOS(WINDOWS_NT); assertEquals(value, MiscUtilities.expandVariables("%" + key + '%')); } - @Test public void expandVariablesEnvWindowsAsUnix() throws Exception { - Map<String, String> env = System.getenv(); - Map.Entry<String, String> firstEntry = env.entrySet().iterator().next(); - String key = firstEntry.getKey(); - String value = firstEntry.getValue(); + var key = "jEdit_TEST"; + var value = "c:\\home\\jEdit"; + setEnv(Map.of(key, value)); updateOS(UNIX); - assertEquals(value, MiscUtilities.expandVariables("%" + key + '%')); + assertEquals(value, MiscUtilities.expandVariables('%' + key + '%')); } @Test @@ -197,6 +196,33 @@ assertEquals(value, MiscUtilities.expandVariables("$" + key)); } + private static void setEnv(Map<String, String> newenv) throws Exception { + try { + var processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); + Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); + theEnvironmentField.setAccessible(true); + var env = (Map<String, String>) theEnvironmentField.get(null); + env.putAll(newenv); + Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment"); + theCaseInsensitiveEnvironmentField.setAccessible(true); + var cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null); + cienv.putAll(newenv); + } catch (NoSuchFieldException e) { + Class[] classes = Collections.class.getDeclaredClasses(); + Map<String, String> env = System.getenv(); + for(var cl : classes) { + if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { + Field field = cl.getDeclaredField("m"); + field.setAccessible(true); + var obj = field.get(env); + var map = (Map<String, String>) obj; + map.clear(); + map.putAll(newenv); + } + } + } + } + @Test public void expandVariablesEnvUnix2() throws Exception { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |