Add protected YamlProcessor.getFlattenedMap method

Add a protected getFlattenedMap method to the YamlProcessor that
subclasses can use to flatten a source Map so that it has the same
entries as the Properties, but retains order.

Issue: SPR-12499
This commit is contained in:
Phillip Webb
2014-12-02 17:14:32 -08:00
parent 83ecf5ca1d
commit 87f1512e88
2 changed files with 43 additions and 8 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.beans.factory.config;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
@@ -23,7 +24,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.yaml.snakeyaml.parser.ParserException;
import org.yaml.snakeyaml.scanner.ScannerException;
import org.springframework.core.io.ByteArrayResource;
import static org.junit.Assert.*;
@@ -135,4 +135,24 @@ public class YamlProcessorTests {
}
});
}
@Test
@SuppressWarnings("unchecked")
public void flattenedMapIsSameAsPropertiesButOrdered() {
this.processor.setResources(new ByteArrayResource(
"foo: bar\nbar:\n spam: bucket".getBytes()));
this.processor.process(new MatchCallback() {
@Override
public void process(Properties properties, Map<String, Object> map) {
assertEquals("bucket", properties.get("bar.spam"));
assertEquals(2, properties.size());
Map<String, Object> flattenedMap = processor.getFlattenedMap(map);
assertEquals("bucket", flattenedMap.get("bar.spam"));
assertEquals(2, flattenedMap.size());
assertTrue(flattenedMap instanceof LinkedHashMap);
Map<String, Object> bar = (Map<String, Object>) map.get("bar");
assertEquals("bucket", bar.get("spam"));
}
});
}
}