EnvironmentPropertySource extends BasePropertySource { @Override public Map<String, PropertyValue> getProperties() { if(disabled){ return Collections.emptyMap(); } String prefix = this.prefix; if(prefix==null) { Map<String, PropertyValue> entries = new HashMap<>(System.getenv().size()); for (Map.Entry<String, String> entry : System.getenv().entrySet()) { entries.put(entry.getKey(), PropertyValue.createValue(entry.getKey(), entry.getValue()) .setMeta("source", getName())); } return entries; }else{ Map<String, PropertyValue> entries = new HashMap<>(System.getenv().size()); for (Map.Entry<String, String> entry : System.getenv().entrySet()) { entries.put(prefix + entry.getKey(), PropertyValue.createValue(prefix + entry.getKey(), entry.getValue()) .setMeta("source", getName())); } return entries; } } EnvironmentPropertySource();  EnvironmentPropertySource(int ordinal);  EnvironmentPropertySource(String prefix, int ordinal);  EnvironmentPropertySource(String prefix); @Override int getDefaultOrdinal(); @Override String getName(); @Override PropertyValue get(String key); @Override Map<String, PropertyValue> getProperties(); String getPrefix(); boolean isDisabled(); @Override ChangeSupport getChangeSupport(); static final int DEFAULT_ORDINAL; }
@Test public void testGetProperties() throws Exception { Map<String, PropertyValue> props = envPropertySource.getProperties(); for (Map.Entry<String, PropertyValue> en : props.entrySet()) { if (!en.getKey().startsWith("_")) { assertThat(en.getValue().getValue()).isEqualTo(System.getenv(en.getKey())); } } }
@Test public void testPrefixedGetProperties() throws Exception { EnvironmentPropertySource localEnvironmentPropertySource = new EnvironmentPropertySource("someprefix"); Map<String, PropertyValue> props = localEnvironmentPropertySource.getProperties(); for (Map.Entry<String, PropertyValue> en : props.entrySet()) { assertThat(en.getKey().startsWith("someprefix")).isTrue(); String thisKey = en.getKey().replaceFirst("someprefix", ""); if (!thisKey.startsWith("_")) { assertThat(en.getValue().getValue()).isEqualTo(System.getenv(thisKey)); } } }