@SuppressWarnings("unchecked")
    static void setEnv(Map<String, String> newenv) throws Exception {
        Map<String, String> env = System.getenv();
        // The map is an unmodifiable view of the environment map, so find a Map field
        for (Field field : env.getClass().getDeclaredFields()) {
            if (Map.class.isAssignableFrom(field.getType())) {
                field.setAccessible(true);
                Object obj = field.get(env);
                Map<String, String> map = (Map<String, String>) obj;
                map.clear();
                map.putAll(newenv);
            }
        }
        // OpenJDK 8-17 on Windows
        if (Os.current() == Os.WINDOWS) {
            Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
            Field theCaseInsensitiveEnvironmentField =
                    processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
            theCaseInsensitiveEnvironmentField.setAccessible(true);
            Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
            cienv.clear();
            cienv.putAll(newenv);
        }
    }
@Test
    void testSetEnv() throws Exception {
        List<String> log = new ArrayList<>();
        String id = "Aa" + UUID.randomUUID();
        assertNull(System.getenv(id));
        assertNull(System.getenv().get(id));
        Map<String, String> env = new HashMap<>(System.getenv());
        env.put(id, id);
        EnvHelper.environment(System.getProperty("user.dir"), env, log::add);
        assertEquals(Collections.emptyList(), log);
        assertEquals(id, System.getenv(id));
        assertEquals(id, System.getenv().get(id));
        assertEquals(Os.current() == Os.WINDOWS ? id : null, System.getenv(id.toLowerCase(Locale.ROOT)));
        assertNull(System.getenv().get(id.toLowerCase(Locale.ROOT)));
        assertEquals(Os.current() == Os.WINDOWS ? id : null, System.getenv(id.toUpperCase(Locale.ROOT)));
        assertNull(System.getenv().get(id.toUpperCase(Locale.ROOT)));
        env.remove(id);
        EnvHelper.environment(System.getProperty("user.dir"), env, log::add);
        assertEquals(Collections.emptyList(), log);
        assertNull(System.getenv(id));
        assertNull(System.getenv().get(id));
    }