Skip to content

Commit 385d8e9

Browse files
committed
Fix system environment tests on all platforms
Issue: SPR-8245
1 parent a1b7af5 commit 385d8e9

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@
3131

3232
import org.junit.After;
3333
import org.junit.Before;
34-
import org.junit.Ignore;
3534
import org.junit.Test;
3635
import org.springframework.beans.factory.support.AbstractBeanDefinition;
3736
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -229,7 +228,6 @@ public void customPlaceholderPrefixAndSuffix() {
229228
assertThat(bf.getBean(TestBean.class).getSex(), is("${key2}"));
230229
}
231230

232-
@Ignore // fails on windows
233231
@Test
234232
public void nullValueIsPreserved() {
235233
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
@@ -247,6 +245,7 @@ public void nullValueIsPreserved() {
247245

248246
@SuppressWarnings("unchecked")
249247
private static Map<String, String> getModifiableSystemEnvironment() {
248+
// for os x / linux
250249
Class<?>[] classes = Collections.class.getDeclaredClasses();
251250
Map<String, String> env = System.getenv();
252251
for (Class<?> cl : classes) {
@@ -255,12 +254,45 @@ private static Map<String, String> getModifiableSystemEnvironment() {
255254
Field field = cl.getDeclaredField("m");
256255
field.setAccessible(true);
257256
Object obj = field.get(env);
258-
return (Map<String, String>) obj;
257+
if (obj != null && obj.getClass().getName().equals("java.lang.ProcessEnvironment$StringEnvironment")) {
258+
return (Map<String, String>) obj;
259+
}
259260
} catch (Exception ex) {
260261
throw new RuntimeException(ex);
261262
}
262263
}
263264
}
265+
266+
// for windows
267+
Class<?> processEnvironmentClass;
268+
try {
269+
processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
270+
} catch (Exception e) {
271+
throw new RuntimeException(e);
272+
}
273+
274+
try {
275+
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
276+
theCaseInsensitiveEnvironmentField.setAccessible(true);
277+
Object obj = theCaseInsensitiveEnvironmentField.get(null);
278+
return (Map<String, String>) obj;
279+
} catch (NoSuchFieldException e) {
280+
// do nothing
281+
} catch (Exception e) {
282+
throw new RuntimeException(e);
283+
}
284+
285+
try {
286+
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
287+
theEnvironmentField.setAccessible(true);
288+
Object obj = theEnvironmentField.get(null);
289+
return (Map<String, String>) obj;
290+
} catch (NoSuchFieldException e) {
291+
// do nothing
292+
} catch (Exception e) {
293+
throw new RuntimeException(e);
294+
}
295+
264296
throw new IllegalStateException();
265297
}
266298
}

org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.Collections;
4040
import java.util.Map;
4141

42-
import org.junit.Ignore;
4342
import org.junit.Test;
4443
import org.springframework.mock.env.MockPropertySource;
4544

@@ -175,7 +174,6 @@ public void acceptsProfiles_defaultProfile() {
175174
assertThat(environment.acceptsProfiles("p1"), is(true));
176175
}
177176

178-
@Ignore // fails on windows
179177
@Test
180178
public void getSystemProperties_withAndWithoutSecurityManager() {
181179
System.setProperty(ALLOWED_PROPERTY_NAME, ALLOWED_PROPERTY_VALUE);
@@ -293,6 +291,7 @@ public void checkPermission(Permission perm) {
293291

294292
@SuppressWarnings("unchecked")
295293
private static Map<String, String> getModifiableSystemEnvironment() {
294+
// for os x / linux
296295
Class<?>[] classes = Collections.class.getDeclaredClasses();
297296
Map<String, String> env = System.getenv();
298297
for (Class<?> cl : classes) {
@@ -301,12 +300,45 @@ private static Map<String, String> getModifiableSystemEnvironment() {
301300
Field field = cl.getDeclaredField("m");
302301
field.setAccessible(true);
303302
Object obj = field.get(env);
304-
return (Map<String, String>) obj;
303+
if (obj != null && obj.getClass().getName().equals("java.lang.ProcessEnvironment$StringEnvironment")) {
304+
return (Map<String, String>) obj;
305+
}
305306
} catch (Exception ex) {
306307
throw new RuntimeException(ex);
307308
}
308309
}
309310
}
311+
312+
// for windows
313+
Class<?> processEnvironmentClass;
314+
try {
315+
processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
316+
} catch (Exception e) {
317+
throw new RuntimeException(e);
318+
}
319+
320+
try {
321+
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
322+
theCaseInsensitiveEnvironmentField.setAccessible(true);
323+
Object obj = theCaseInsensitiveEnvironmentField.get(null);
324+
return (Map<String, String>) obj;
325+
} catch (NoSuchFieldException e) {
326+
// do nothing
327+
} catch (Exception e) {
328+
throw new RuntimeException(e);
329+
}
330+
331+
try {
332+
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
333+
theEnvironmentField.setAccessible(true);
334+
Object obj = theEnvironmentField.get(null);
335+
return (Map<String, String>) obj;
336+
} catch (NoSuchFieldException e) {
337+
// do nothing
338+
} catch (Exception e) {
339+
throw new RuntimeException(e);
340+
}
341+
310342
throw new IllegalStateException();
311343
}
312344
}

0 commit comments

Comments
 (0)