Add multi-document properties file support

Update `OriginTrackedPropertiesLoader` so that it can support
multi-document properties files. These are similar to multi-document
YAML files but use `#---` as the separator.

Closes gh-22495

Co-authored-by: Phillip Webb <pwebb@vmware.com>
pull/22524/head
Madhura Bhave 4 years ago committed by Phillip Webb
parent 945e5b9222
commit 9e9eb90d09

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,8 +21,11 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BooleanSupplier;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginTrackedValue;
@ -59,7 +62,7 @@ class OriginTrackedPropertiesLoader {
* @return the loaded properties
* @throws IOException on read error
*/
Map<String, OriginTrackedValue> load() throws IOException {
List<Document> load() throws IOException {
return load(true);
}
@ -70,18 +73,30 @@ class OriginTrackedPropertiesLoader {
* @return the loaded properties
* @throws IOException on read error
*/
Map<String, OriginTrackedValue> load(boolean expandLists) throws IOException {
List<Document> load(boolean expandLists) throws IOException {
List<Document> result = new ArrayList<>();
Document document = new Document();
try (CharacterReader reader = new CharacterReader(this.resource)) {
Map<String, OriginTrackedValue> result = new LinkedHashMap<>();
StringBuilder buffer = new StringBuilder();
while (reader.read()) {
if (reader.getCharacter() == '#') {
if (isNewDocument(reader)) {
if (!document.isEmpty()) {
result.add(document);
}
document = new Document();
}
else {
reader.skipComment();
}
}
String key = loadKey(buffer, reader).trim();
if (expandLists && key.endsWith("[]")) {
key = key.substring(0, key.length() - 2);
int index = 0;
do {
OriginTrackedValue value = loadValue(buffer, reader, true);
put(result, key + "[" + (index++) + "]", value);
document.put(key + "[" + (index++) + "]", value);
if (!reader.isEndOfLine()) {
reader.read();
}
@ -90,17 +105,15 @@ class OriginTrackedPropertiesLoader {
}
else {
OriginTrackedValue value = loadValue(buffer, reader, false);
put(result, key, value);
document.put(key, value);
}
}
return result;
}
}
private void put(Map<String, OriginTrackedValue> result, String key, OriginTrackedValue value) {
if (!key.isEmpty()) {
result.put(key, value);
}
if (!document.isEmpty() && !result.contains(document)) {
result.add(document);
}
return result;
}
private String loadKey(StringBuilder buffer, CharacterReader reader) throws IOException {
@ -136,6 +149,20 @@ class OriginTrackedPropertiesLoader {
return OriginTrackedValue.of(buffer.toString(), origin);
}
boolean isNewDocument(CharacterReader reader) throws IOException {
boolean result = reader.isPoundCharacter();
result = result && readAndExpect(reader, reader::isHyphenCharacter);
result = result && readAndExpect(reader, reader::isHyphenCharacter);
result = result && readAndExpect(reader, reader::isHyphenCharacter);
result = result && readAndExpect(reader, reader::isEndOfLine);
return result;
}
private boolean readAndExpect(CharacterReader reader, BooleanSupplier check) throws IOException {
reader.read();
return check.getAsBoolean();
}
/**
* Reads characters from the source resource, taking care of skipping comments,
* handling multi-line values and tracking {@code '\'} escapes.
@ -173,7 +200,9 @@ class OriginTrackedPropertiesLoader {
if (this.columnNumber == 0) {
skipLeadingWhitespace();
if (!wrappedLine) {
skipComment();
if (this.character == '!') {
skipComment();
}
}
}
if (this.character == '\\') {
@ -194,13 +223,10 @@ class OriginTrackedPropertiesLoader {
}
private void skipComment() throws IOException {
if (this.character == '#' || this.character == '!') {
while (this.character != '\n' && this.character != -1) {
this.character = this.reader.read();
}
this.columnNumber = -1;
read();
while (this.character != '\n' && this.character != -1) {
this.character = this.reader.read();
}
this.columnNumber = -1;
}
private void readEscaped() throws IOException {
@ -265,6 +291,37 @@ class OriginTrackedPropertiesLoader {
return new Location(this.reader.getLineNumber(), this.columnNumber);
}
boolean isPoundCharacter() {
return this.character == '#';
}
boolean isHyphenCharacter() {
return this.character == '-';
}
}
/**
* A single document within the properties file.
*/
static class Document {
private final Map<String, OriginTrackedValue> values = new LinkedHashMap<>();
void put(String key, OriginTrackedValue value) {
if (!key.isEmpty()) {
this.values.put(key, value);
}
}
boolean isEmpty() {
return this.values.isEmpty();
}
Map<String, OriginTrackedValue> asMap() {
return this.values;
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,10 +17,12 @@
package org.springframework.boot.env;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.springframework.boot.env.OriginTrackedPropertiesLoader.Document;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
@ -44,21 +46,31 @@ public class PropertiesPropertySourceLoader implements PropertySourceLoader {
@Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
Map<String, ?> properties = loadProperties(resource);
List<Map<String, ?>> properties = loadProperties(resource);
if (properties.isEmpty()) {
return Collections.emptyList();
}
return Collections
.singletonList(new OriginTrackedMapPropertySource(name, Collections.unmodifiableMap(properties), true));
List<PropertySource<?>> propertySources = new ArrayList<>(properties.size());
for (int i = 0; i < properties.size(); i++) {
String documentNumber = (properties.size() != 1) ? " (document #" + i + ")" : "";
propertySources.add(new OriginTrackedMapPropertySource(name + documentNumber,
Collections.unmodifiableMap(properties.get(i)), true));
}
return propertySources;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private Map<String, ?> loadProperties(Resource resource) throws IOException {
private List<Map<String, ?>> loadProperties(Resource resource) throws IOException {
String filename = resource.getFilename();
List<Map<String, ?>> result = new ArrayList<>();
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
return (Map) PropertiesLoaderUtils.loadProperties(resource);
result.add((Map) PropertiesLoaderUtils.loadProperties(resource));
}
else {
List<Document> documents = new OriginTrackedPropertiesLoader(resource).load();
documents.forEach((document) -> result.add(document.asMap()));
}
return new OriginTrackedPropertiesLoader(resource).load();
return result;
}
}

@ -16,12 +16,13 @@
package org.springframework.boot.env;
import java.util.Map;
import java.util.List;
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.env.OriginTrackedPropertiesLoader.Document;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.boot.origin.TextResourceOrigin;
import org.springframework.core.io.ClassPathResource;
@ -40,47 +41,48 @@ class OriginTrackedPropertiesLoaderTests {
private ClassPathResource resource;
private Map<String, OriginTrackedValue> properties;
private List<Document> documentes;
@BeforeEach
void setUp() throws Exception {
String path = "test-properties.properties";
this.resource = new ClassPathResource(path, getClass());
this.properties = new OriginTrackedPropertiesLoader(this.resource).load();
this.documentes = new OriginTrackedPropertiesLoader(this.resource).load();
}
@Test
void compareToJavaProperties() throws Exception {
Properties java = PropertiesLoaderUtils.loadProperties(this.resource);
Properties ours = new Properties();
new OriginTrackedPropertiesLoader(this.resource).load(false).forEach((k, v) -> ours.put(k, v.getValue()));
new OriginTrackedPropertiesLoader(this.resource).load(false).get(0).asMap()
.forEach((k, v) -> ours.put(k, v.getValue()));
assertThat(ours).isEqualTo(java);
}
@Test
void getSimpleProperty() {
OriginTrackedValue value = this.properties.get("test");
OriginTrackedValue value = getFromFirst("test");
assertThat(getValue(value)).isEqualTo("properties");
assertThat(getLocation(value)).isEqualTo("11:6");
}
@Test
void getSimplePropertyWithColonSeparator() {
OriginTrackedValue value = this.properties.get("test-colon-separator");
OriginTrackedValue value = getFromFirst("test-colon-separator");
assertThat(getValue(value)).isEqualTo("my-property");
assertThat(getLocation(value)).isEqualTo("15:23");
}
@Test
void getPropertyWithSeparatorSurroundedBySpaces() {
OriginTrackedValue value = this.properties.get("blah");
OriginTrackedValue value = getFromFirst("blah");
assertThat(getValue(value)).isEqualTo("hello world");
assertThat(getLocation(value)).isEqualTo("2:12");
}
@Test
void getUnicodeProperty() {
OriginTrackedValue value = this.properties.get("test-unicode");
OriginTrackedValue value = getFromFirst("test-unicode");
assertThat(getValue(value)).isEqualTo("properties&test");
assertThat(getLocation(value)).isEqualTo("12:14");
}
@ -95,165 +97,169 @@ class OriginTrackedPropertiesLoaderTests {
@Test
void getEscapedProperty() {
OriginTrackedValue value = this.properties.get("test=property");
OriginTrackedValue value = getFromFirst("test=property");
assertThat(getValue(value)).isEqualTo("helloworld");
assertThat(getLocation(value)).isEqualTo("14:15");
}
@Test
void getPropertyWithTab() {
OriginTrackedValue value = this.properties.get("test-tab-property");
OriginTrackedValue value = getFromFirst("test-tab-property");
assertThat(getValue(value)).isEqualTo("foo\tbar");
assertThat(getLocation(value)).isEqualTo("16:19");
}
@Test
void getPropertyWithBang() {
OriginTrackedValue value = this.properties.get("test-bang-property");
OriginTrackedValue value = getFromFirst("test-bang-property");
assertThat(getValue(value)).isEqualTo("foo!");
assertThat(getLocation(value)).isEqualTo("34:20");
}
@Test
void getPropertyWithValueComment() {
OriginTrackedValue value = this.properties.get("test-property-value-comment");
OriginTrackedValue value = getFromFirst("test-property-value-comment");
assertThat(getValue(value)).isEqualTo("foo !bar #foo");
assertThat(getLocation(value)).isEqualTo("36:29");
}
@Test
void getPropertyWithMultilineImmediateBang() {
OriginTrackedValue value = this.properties.get("test-multiline-immediate-bang");
OriginTrackedValue value = getFromFirst("test-multiline-immediate-bang");
assertThat(getValue(value)).isEqualTo("!foo");
assertThat(getLocation(value)).isEqualTo("39:1");
}
@Test
void getPropertyWithCarriageReturn() {
OriginTrackedValue value = this.properties.get("test-return-property");
OriginTrackedValue value = getFromFirst("test-return-property");
assertThat(getValue(value)).isEqualTo("foo\rbar");
assertThat(getLocation(value)).isEqualTo("17:22");
}
@Test
void getPropertyWithNewLine() {
OriginTrackedValue value = this.properties.get("test-newline-property");
OriginTrackedValue value = getFromFirst("test-newline-property");
assertThat(getValue(value)).isEqualTo("foo\nbar");
assertThat(getLocation(value)).isEqualTo("18:23");
}
@Test
void getPropertyWithFormFeed() {
OriginTrackedValue value = this.properties.get("test-form-feed-property");
OriginTrackedValue value = getFromFirst("test-form-feed-property");
assertThat(getValue(value)).isEqualTo("foo\fbar");
assertThat(getLocation(value)).isEqualTo("19:25");
}
@Test
void getPropertyWithWhiteSpace() {
OriginTrackedValue value = this.properties.get("test-whitespace-property");
OriginTrackedValue value = getFromFirst("test-whitespace-property");
assertThat(getValue(value)).isEqualTo("foo bar");
assertThat(getLocation(value)).isEqualTo("20:32");
}
@Test
void getCommentedOutPropertyShouldBeNull() {
assertThat(this.properties.get("commented-property")).isNull();
assertThat(this.properties.get("#commented-property")).isNull();
assertThat(this.properties.get("commented-two")).isNull();
assertThat(this.properties.get("!commented-two")).isNull();
assertThat(getFromFirst("commented-property")).isNull();
assertThat(getFromFirst("#commented-property")).isNull();
assertThat(getFromFirst("commented-two")).isNull();
assertThat(getFromFirst("!commented-two")).isNull();
}
@Test
void getMultiline() {
OriginTrackedValue value = this.properties.get("test-multiline");
OriginTrackedValue value = getFromFirst("test-multiline");
assertThat(getValue(value)).isEqualTo("ab\\c");
assertThat(getLocation(value)).isEqualTo("21:17");
}
@Test
void getImmediateMultiline() {
OriginTrackedValue value = this.properties.get("test-multiline-immediate");
OriginTrackedValue value = getFromFirst("test-multiline-immediate");
assertThat(getValue(value)).isEqualTo("foo");
assertThat(getLocation(value)).isEqualTo("32:1");
}
@Test
void getPropertyWithWhitespaceAfterKey() {
OriginTrackedValue value = this.properties.get("bar");
OriginTrackedValue value = getFromFirst("bar");
assertThat(getValue(value)).isEqualTo("foo=baz");
assertThat(getLocation(value)).isEqualTo("3:7");
}
@Test
void getPropertyWithSpaceSeparator() {
OriginTrackedValue value = this.properties.get("hello");
OriginTrackedValue value = getFromFirst("hello");
assertThat(getValue(value)).isEqualTo("world");
assertThat(getLocation(value)).isEqualTo("4:9");
}
@Test
void getPropertyWithBackslashEscaped() {
OriginTrackedValue value = this.properties.get("proper\\ty");
OriginTrackedValue value = getFromFirst("proper\\ty");
assertThat(getValue(value)).isEqualTo("test");
assertThat(getLocation(value)).isEqualTo("5:11");
}
@Test
void getPropertyWithEmptyValue() {
OriginTrackedValue value = this.properties.get("foo");
OriginTrackedValue value = getFromFirst("foo");
assertThat(getValue(value)).isEqualTo("");
assertThat(getLocation(value)).isEqualTo("7:0");
}
@Test
void getPropertyWithBackslashEscapedInValue() {
OriginTrackedValue value = this.properties.get("bat");
OriginTrackedValue value = getFromFirst("bat");
assertThat(getValue(value)).isEqualTo("a\\");
assertThat(getLocation(value)).isEqualTo("7:7");
}
@Test
void getPropertyWithSeparatorInValue() {
OriginTrackedValue value = this.properties.get("bling");
OriginTrackedValue value = getFromFirst("bling");
assertThat(getValue(value)).isEqualTo("a=b");
assertThat(getLocation(value)).isEqualTo("8:9");
}
@Test
void getListProperty() {
OriginTrackedValue apple = this.properties.get("foods[0]");
OriginTrackedValue apple = getFromFirst("foods[0]");
assertThat(getValue(apple)).isEqualTo("Apple");
assertThat(getLocation(apple)).isEqualTo("24:9");
OriginTrackedValue orange = this.properties.get("foods[1]");
OriginTrackedValue orange = getFromFirst("foods[1]");
assertThat(getValue(orange)).isEqualTo("Orange");
assertThat(getLocation(orange)).isEqualTo("25:1");
OriginTrackedValue strawberry = this.properties.get("foods[2]");
OriginTrackedValue strawberry = getFromFirst("foods[2]");
assertThat(getValue(strawberry)).isEqualTo("Strawberry");
assertThat(getLocation(strawberry)).isEqualTo("26:1");
OriginTrackedValue mango = this.properties.get("foods[3]");
OriginTrackedValue mango = getFromFirst("foods[3]");
assertThat(getValue(mango)).isEqualTo("Mango");
assertThat(getLocation(mango)).isEqualTo("27:1");
}
@Test
void getPropertyWithISO88591Character() {
OriginTrackedValue value = this.properties.get("test-iso8859-1-chars");
OriginTrackedValue value = getFromFirst("test-iso8859-1-chars");
assertThat(getValue(value)).isEqualTo("æ×ÈÅÞßáñÀÿ");
}
@Test
void getPropertyWithTrailingSpace() {
OriginTrackedValue value = this.properties.get("test-with-trailing-space");
OriginTrackedValue value = getFromFirst("test-with-trailing-space");
assertThat(getValue(value)).isEqualTo("trailing ");
}
@Test
void getPropertyWithEscapedTrailingSpace() {
OriginTrackedValue value = this.properties.get("test-with-escaped-trailing-space");
OriginTrackedValue value = getFromFirst("test-with-escaped-trailing-space");
assertThat(getValue(value)).isEqualTo("trailing ");
}
private OriginTrackedValue getFromFirst(String key) {
return this.documentes.get(0).asMap().get(key);
}
private Object getValue(OriginTrackedValue value) {
return (value != null) ? value.getValue() : null;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -48,6 +48,39 @@ class PropertiesPropertySourceLoaderTests {
assertThat(source.getProperty("test")).isEqualTo("properties");
}
@Test
void loadMultiDocumentPropertiesWithSeparatorAtTheBeginningofFile() throws Exception {
List<PropertySource<?>> loaded = this.loader.load("test.properties",
new ClassPathResource("multi-document-properties-2.properties", getClass()));
assertThat(loaded.size()).isEqualTo(2);
PropertySource<?> source1 = loaded.get(0);
PropertySource<?> source2 = loaded.get(1);
assertThat(source1.getProperty("blah")).isEqualTo("hello world");
assertThat(source2.getProperty("foo")).isEqualTo("bar");
}
@Test
void loadMultiDocumentProperties() throws Exception {
List<PropertySource<?>> loaded = this.loader.load("test.properties",
new ClassPathResource("multi-document-properties.properties", getClass()));
assertThat(loaded.size()).isEqualTo(2);
PropertySource<?> source1 = loaded.get(0);
PropertySource<?> source2 = loaded.get(1);
assertThat(source1.getProperty("blah")).isEqualTo("hello world");
assertThat(source2.getProperty("foo")).isEqualTo("bar");
}
@Test
void loadMultiDocumentPropertiesWithEmptyDocument() throws Exception {
List<PropertySource<?>> loaded = this.loader.load("test.properties",
new ClassPathResource("multi-document-properties-empty.properties", getClass()));
assertThat(loaded.size()).isEqualTo(2);
PropertySource<?> source1 = loaded.get(0);
PropertySource<?> source2 = loaded.get(1);
assertThat(source1.getProperty("blah")).isEqualTo("hello world");
assertThat(source2.getProperty("foo")).isEqualTo("bar");
}
@Test
void loadXml() throws Exception {
List<PropertySource<?>> loaded = this.loader.load("test.xml",

@ -0,0 +1,10 @@
#---
#test
blah=hello world
bar=baz
hello=world
#---
foo=bar
bling=biz
#comment1
#comment2

@ -0,0 +1,12 @@
#---
#test
blah=hello world
bar=baz
hello=world
#---
#---
foo=bar
bling=biz
#comment1
#comment2

@ -0,0 +1,10 @@
#test
blah=hello world
bar=baz
hello=world
#---
foo=bar
bling=biz
#comment1
#comment2
#---
Loading…
Cancel
Save