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>
This commit is contained in:
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.InputStreamReader;
|
||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
import org.springframework.boot.origin.Origin;
|
import org.springframework.boot.origin.Origin;
|
||||||
import org.springframework.boot.origin.OriginTrackedValue;
|
import org.springframework.boot.origin.OriginTrackedValue;
|
||||||
|
|
@ -59,7 +62,7 @@ class OriginTrackedPropertiesLoader {
|
||||||
* @return the loaded properties
|
* @return the loaded properties
|
||||||
* @throws IOException on read error
|
* @throws IOException on read error
|
||||||
*/
|
*/
|
||||||
Map<String, OriginTrackedValue> load() throws IOException {
|
List<Document> load() throws IOException {
|
||||||
return load(true);
|
return load(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,18 +73,30 @@ class OriginTrackedPropertiesLoader {
|
||||||
* @return the loaded properties
|
* @return the loaded properties
|
||||||
* @throws IOException on read error
|
* @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)) {
|
try (CharacterReader reader = new CharacterReader(this.resource)) {
|
||||||
Map<String, OriginTrackedValue> result = new LinkedHashMap<>();
|
|
||||||
StringBuilder buffer = new StringBuilder();
|
StringBuilder buffer = new StringBuilder();
|
||||||
while (reader.read()) {
|
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();
|
String key = loadKey(buffer, reader).trim();
|
||||||
if (expandLists && key.endsWith("[]")) {
|
if (expandLists && key.endsWith("[]")) {
|
||||||
key = key.substring(0, key.length() - 2);
|
key = key.substring(0, key.length() - 2);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
do {
|
do {
|
||||||
OriginTrackedValue value = loadValue(buffer, reader, true);
|
OriginTrackedValue value = loadValue(buffer, reader, true);
|
||||||
put(result, key + "[" + (index++) + "]", value);
|
document.put(key + "[" + (index++) + "]", value);
|
||||||
if (!reader.isEndOfLine()) {
|
if (!reader.isEndOfLine()) {
|
||||||
reader.read();
|
reader.read();
|
||||||
}
|
}
|
||||||
|
|
@ -90,17 +105,15 @@ class OriginTrackedPropertiesLoader {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
OriginTrackedValue value = loadValue(buffer, reader, false);
|
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 {
|
private String loadKey(StringBuilder buffer, CharacterReader reader) throws IOException {
|
||||||
|
|
@ -136,6 +149,20 @@ class OriginTrackedPropertiesLoader {
|
||||||
return OriginTrackedValue.of(buffer.toString(), origin);
|
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,
|
* Reads characters from the source resource, taking care of skipping comments,
|
||||||
* handling multi-line values and tracking {@code '\'} escapes.
|
* handling multi-line values and tracking {@code '\'} escapes.
|
||||||
|
|
@ -173,7 +200,9 @@ class OriginTrackedPropertiesLoader {
|
||||||
if (this.columnNumber == 0) {
|
if (this.columnNumber == 0) {
|
||||||
skipLeadingWhitespace();
|
skipLeadingWhitespace();
|
||||||
if (!wrappedLine) {
|
if (!wrappedLine) {
|
||||||
skipComment();
|
if (this.character == '!') {
|
||||||
|
skipComment();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.character == '\\') {
|
if (this.character == '\\') {
|
||||||
|
|
@ -194,13 +223,10 @@ class OriginTrackedPropertiesLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void skipComment() throws IOException {
|
private void skipComment() throws IOException {
|
||||||
if (this.character == '#' || this.character == '!') {
|
while (this.character != '\n' && this.character != -1) {
|
||||||
while (this.character != '\n' && this.character != -1) {
|
this.character = this.reader.read();
|
||||||
this.character = this.reader.read();
|
|
||||||
}
|
|
||||||
this.columnNumber = -1;
|
|
||||||
read();
|
|
||||||
}
|
}
|
||||||
|
this.columnNumber = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readEscaped() throws IOException {
|
private void readEscaped() throws IOException {
|
||||||
|
|
@ -265,6 +291,37 @@ class OriginTrackedPropertiesLoader {
|
||||||
return new Location(this.reader.getLineNumber(), this.columnNumber);
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -17,10 +17,12 @@
|
||||||
package org.springframework.boot.env;
|
package org.springframework.boot.env;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.boot.env.OriginTrackedPropertiesLoader.Document;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||||
|
|
@ -44,21 +46,31 @@ public class PropertiesPropertySourceLoader implements PropertySourceLoader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
|
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
|
||||||
Map<String, ?> properties = loadProperties(resource);
|
List<Map<String, ?>> properties = loadProperties(resource);
|
||||||
if (properties.isEmpty()) {
|
if (properties.isEmpty()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
return Collections
|
List<PropertySource<?>> propertySources = new ArrayList<>(properties.size());
|
||||||
.singletonList(new OriginTrackedMapPropertySource(name, Collections.unmodifiableMap(properties), true));
|
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" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
private Map<String, ?> loadProperties(Resource resource) throws IOException {
|
private List<Map<String, ?>> loadProperties(Resource resource) throws IOException {
|
||||||
String filename = resource.getFilename();
|
String filename = resource.getFilename();
|
||||||
|
List<Map<String, ?>> result = new ArrayList<>();
|
||||||
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
|
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
|
||||||
return (Map) PropertiesLoaderUtils.loadProperties(resource);
|
result.add((Map) PropertiesLoaderUtils.loadProperties(resource));
|
||||||
}
|
}
|
||||||
return new OriginTrackedPropertiesLoader(resource).load();
|
else {
|
||||||
|
List<Document> documents = new OriginTrackedPropertiesLoader(resource).load();
|
||||||
|
documents.forEach((document) -> result.add(document.asMap()));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,13 @@
|
||||||
|
|
||||||
package org.springframework.boot.env;
|
package org.springframework.boot.env;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.env.OriginTrackedPropertiesLoader.Document;
|
||||||
import org.springframework.boot.origin.OriginTrackedValue;
|
import org.springframework.boot.origin.OriginTrackedValue;
|
||||||
import org.springframework.boot.origin.TextResourceOrigin;
|
import org.springframework.boot.origin.TextResourceOrigin;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
|
@ -40,47 +41,48 @@ class OriginTrackedPropertiesLoaderTests {
|
||||||
|
|
||||||
private ClassPathResource resource;
|
private ClassPathResource resource;
|
||||||
|
|
||||||
private Map<String, OriginTrackedValue> properties;
|
private List<Document> documentes;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() throws Exception {
|
void setUp() throws Exception {
|
||||||
String path = "test-properties.properties";
|
String path = "test-properties.properties";
|
||||||
this.resource = new ClassPathResource(path, getClass());
|
this.resource = new ClassPathResource(path, getClass());
|
||||||
this.properties = new OriginTrackedPropertiesLoader(this.resource).load();
|
this.documentes = new OriginTrackedPropertiesLoader(this.resource).load();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void compareToJavaProperties() throws Exception {
|
void compareToJavaProperties() throws Exception {
|
||||||
Properties java = PropertiesLoaderUtils.loadProperties(this.resource);
|
Properties java = PropertiesLoaderUtils.loadProperties(this.resource);
|
||||||
Properties ours = new Properties();
|
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);
|
assertThat(ours).isEqualTo(java);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getSimpleProperty() {
|
void getSimpleProperty() {
|
||||||
OriginTrackedValue value = this.properties.get("test");
|
OriginTrackedValue value = getFromFirst("test");
|
||||||
assertThat(getValue(value)).isEqualTo("properties");
|
assertThat(getValue(value)).isEqualTo("properties");
|
||||||
assertThat(getLocation(value)).isEqualTo("11:6");
|
assertThat(getLocation(value)).isEqualTo("11:6");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getSimplePropertyWithColonSeparator() {
|
void getSimplePropertyWithColonSeparator() {
|
||||||
OriginTrackedValue value = this.properties.get("test-colon-separator");
|
OriginTrackedValue value = getFromFirst("test-colon-separator");
|
||||||
assertThat(getValue(value)).isEqualTo("my-property");
|
assertThat(getValue(value)).isEqualTo("my-property");
|
||||||
assertThat(getLocation(value)).isEqualTo("15:23");
|
assertThat(getLocation(value)).isEqualTo("15:23");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithSeparatorSurroundedBySpaces() {
|
void getPropertyWithSeparatorSurroundedBySpaces() {
|
||||||
OriginTrackedValue value = this.properties.get("blah");
|
OriginTrackedValue value = getFromFirst("blah");
|
||||||
assertThat(getValue(value)).isEqualTo("hello world");
|
assertThat(getValue(value)).isEqualTo("hello world");
|
||||||
assertThat(getLocation(value)).isEqualTo("2:12");
|
assertThat(getLocation(value)).isEqualTo("2:12");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUnicodeProperty() {
|
void getUnicodeProperty() {
|
||||||
OriginTrackedValue value = this.properties.get("test-unicode");
|
OriginTrackedValue value = getFromFirst("test-unicode");
|
||||||
assertThat(getValue(value)).isEqualTo("properties&test");
|
assertThat(getValue(value)).isEqualTo("properties&test");
|
||||||
assertThat(getLocation(value)).isEqualTo("12:14");
|
assertThat(getLocation(value)).isEqualTo("12:14");
|
||||||
}
|
}
|
||||||
|
|
@ -95,165 +97,169 @@ class OriginTrackedPropertiesLoaderTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getEscapedProperty() {
|
void getEscapedProperty() {
|
||||||
OriginTrackedValue value = this.properties.get("test=property");
|
OriginTrackedValue value = getFromFirst("test=property");
|
||||||
assertThat(getValue(value)).isEqualTo("helloworld");
|
assertThat(getValue(value)).isEqualTo("helloworld");
|
||||||
assertThat(getLocation(value)).isEqualTo("14:15");
|
assertThat(getLocation(value)).isEqualTo("14:15");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithTab() {
|
void getPropertyWithTab() {
|
||||||
OriginTrackedValue value = this.properties.get("test-tab-property");
|
OriginTrackedValue value = getFromFirst("test-tab-property");
|
||||||
assertThat(getValue(value)).isEqualTo("foo\tbar");
|
assertThat(getValue(value)).isEqualTo("foo\tbar");
|
||||||
assertThat(getLocation(value)).isEqualTo("16:19");
|
assertThat(getLocation(value)).isEqualTo("16:19");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithBang() {
|
void getPropertyWithBang() {
|
||||||
OriginTrackedValue value = this.properties.get("test-bang-property");
|
OriginTrackedValue value = getFromFirst("test-bang-property");
|
||||||
assertThat(getValue(value)).isEqualTo("foo!");
|
assertThat(getValue(value)).isEqualTo("foo!");
|
||||||
assertThat(getLocation(value)).isEqualTo("34:20");
|
assertThat(getLocation(value)).isEqualTo("34:20");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithValueComment() {
|
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(getValue(value)).isEqualTo("foo !bar #foo");
|
||||||
assertThat(getLocation(value)).isEqualTo("36:29");
|
assertThat(getLocation(value)).isEqualTo("36:29");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithMultilineImmediateBang() {
|
void getPropertyWithMultilineImmediateBang() {
|
||||||
OriginTrackedValue value = this.properties.get("test-multiline-immediate-bang");
|
OriginTrackedValue value = getFromFirst("test-multiline-immediate-bang");
|
||||||
assertThat(getValue(value)).isEqualTo("!foo");
|
assertThat(getValue(value)).isEqualTo("!foo");
|
||||||
assertThat(getLocation(value)).isEqualTo("39:1");
|
assertThat(getLocation(value)).isEqualTo("39:1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithCarriageReturn() {
|
void getPropertyWithCarriageReturn() {
|
||||||
OriginTrackedValue value = this.properties.get("test-return-property");
|
OriginTrackedValue value = getFromFirst("test-return-property");
|
||||||
assertThat(getValue(value)).isEqualTo("foo\rbar");
|
assertThat(getValue(value)).isEqualTo("foo\rbar");
|
||||||
assertThat(getLocation(value)).isEqualTo("17:22");
|
assertThat(getLocation(value)).isEqualTo("17:22");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithNewLine() {
|
void getPropertyWithNewLine() {
|
||||||
OriginTrackedValue value = this.properties.get("test-newline-property");
|
OriginTrackedValue value = getFromFirst("test-newline-property");
|
||||||
assertThat(getValue(value)).isEqualTo("foo\nbar");
|
assertThat(getValue(value)).isEqualTo("foo\nbar");
|
||||||
assertThat(getLocation(value)).isEqualTo("18:23");
|
assertThat(getLocation(value)).isEqualTo("18:23");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithFormFeed() {
|
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(getValue(value)).isEqualTo("foo\fbar");
|
||||||
assertThat(getLocation(value)).isEqualTo("19:25");
|
assertThat(getLocation(value)).isEqualTo("19:25");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithWhiteSpace() {
|
void getPropertyWithWhiteSpace() {
|
||||||
OriginTrackedValue value = this.properties.get("test-whitespace-property");
|
OriginTrackedValue value = getFromFirst("test-whitespace-property");
|
||||||
assertThat(getValue(value)).isEqualTo("foo bar");
|
assertThat(getValue(value)).isEqualTo("foo bar");
|
||||||
assertThat(getLocation(value)).isEqualTo("20:32");
|
assertThat(getLocation(value)).isEqualTo("20:32");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getCommentedOutPropertyShouldBeNull() {
|
void getCommentedOutPropertyShouldBeNull() {
|
||||||
assertThat(this.properties.get("commented-property")).isNull();
|
assertThat(getFromFirst("commented-property")).isNull();
|
||||||
assertThat(this.properties.get("#commented-property")).isNull();
|
assertThat(getFromFirst("#commented-property")).isNull();
|
||||||
assertThat(this.properties.get("commented-two")).isNull();
|
assertThat(getFromFirst("commented-two")).isNull();
|
||||||
assertThat(this.properties.get("!commented-two")).isNull();
|
assertThat(getFromFirst("!commented-two")).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getMultiline() {
|
void getMultiline() {
|
||||||
OriginTrackedValue value = this.properties.get("test-multiline");
|
OriginTrackedValue value = getFromFirst("test-multiline");
|
||||||
assertThat(getValue(value)).isEqualTo("ab\\c");
|
assertThat(getValue(value)).isEqualTo("ab\\c");
|
||||||
assertThat(getLocation(value)).isEqualTo("21:17");
|
assertThat(getLocation(value)).isEqualTo("21:17");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getImmediateMultiline() {
|
void getImmediateMultiline() {
|
||||||
OriginTrackedValue value = this.properties.get("test-multiline-immediate");
|
OriginTrackedValue value = getFromFirst("test-multiline-immediate");
|
||||||
assertThat(getValue(value)).isEqualTo("foo");
|
assertThat(getValue(value)).isEqualTo("foo");
|
||||||
assertThat(getLocation(value)).isEqualTo("32:1");
|
assertThat(getLocation(value)).isEqualTo("32:1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithWhitespaceAfterKey() {
|
void getPropertyWithWhitespaceAfterKey() {
|
||||||
OriginTrackedValue value = this.properties.get("bar");
|
OriginTrackedValue value = getFromFirst("bar");
|
||||||
assertThat(getValue(value)).isEqualTo("foo=baz");
|
assertThat(getValue(value)).isEqualTo("foo=baz");
|
||||||
assertThat(getLocation(value)).isEqualTo("3:7");
|
assertThat(getLocation(value)).isEqualTo("3:7");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithSpaceSeparator() {
|
void getPropertyWithSpaceSeparator() {
|
||||||
OriginTrackedValue value = this.properties.get("hello");
|
OriginTrackedValue value = getFromFirst("hello");
|
||||||
assertThat(getValue(value)).isEqualTo("world");
|
assertThat(getValue(value)).isEqualTo("world");
|
||||||
assertThat(getLocation(value)).isEqualTo("4:9");
|
assertThat(getLocation(value)).isEqualTo("4:9");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithBackslashEscaped() {
|
void getPropertyWithBackslashEscaped() {
|
||||||
OriginTrackedValue value = this.properties.get("proper\\ty");
|
OriginTrackedValue value = getFromFirst("proper\\ty");
|
||||||
assertThat(getValue(value)).isEqualTo("test");
|
assertThat(getValue(value)).isEqualTo("test");
|
||||||
assertThat(getLocation(value)).isEqualTo("5:11");
|
assertThat(getLocation(value)).isEqualTo("5:11");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithEmptyValue() {
|
void getPropertyWithEmptyValue() {
|
||||||
OriginTrackedValue value = this.properties.get("foo");
|
OriginTrackedValue value = getFromFirst("foo");
|
||||||
assertThat(getValue(value)).isEqualTo("");
|
assertThat(getValue(value)).isEqualTo("");
|
||||||
assertThat(getLocation(value)).isEqualTo("7:0");
|
assertThat(getLocation(value)).isEqualTo("7:0");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithBackslashEscapedInValue() {
|
void getPropertyWithBackslashEscapedInValue() {
|
||||||
OriginTrackedValue value = this.properties.get("bat");
|
OriginTrackedValue value = getFromFirst("bat");
|
||||||
assertThat(getValue(value)).isEqualTo("a\\");
|
assertThat(getValue(value)).isEqualTo("a\\");
|
||||||
assertThat(getLocation(value)).isEqualTo("7:7");
|
assertThat(getLocation(value)).isEqualTo("7:7");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithSeparatorInValue() {
|
void getPropertyWithSeparatorInValue() {
|
||||||
OriginTrackedValue value = this.properties.get("bling");
|
OriginTrackedValue value = getFromFirst("bling");
|
||||||
assertThat(getValue(value)).isEqualTo("a=b");
|
assertThat(getValue(value)).isEqualTo("a=b");
|
||||||
assertThat(getLocation(value)).isEqualTo("8:9");
|
assertThat(getLocation(value)).isEqualTo("8:9");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getListProperty() {
|
void getListProperty() {
|
||||||
OriginTrackedValue apple = this.properties.get("foods[0]");
|
OriginTrackedValue apple = getFromFirst("foods[0]");
|
||||||
assertThat(getValue(apple)).isEqualTo("Apple");
|
assertThat(getValue(apple)).isEqualTo("Apple");
|
||||||
assertThat(getLocation(apple)).isEqualTo("24:9");
|
assertThat(getLocation(apple)).isEqualTo("24:9");
|
||||||
OriginTrackedValue orange = this.properties.get("foods[1]");
|
OriginTrackedValue orange = getFromFirst("foods[1]");
|
||||||
assertThat(getValue(orange)).isEqualTo("Orange");
|
assertThat(getValue(orange)).isEqualTo("Orange");
|
||||||
assertThat(getLocation(orange)).isEqualTo("25:1");
|
assertThat(getLocation(orange)).isEqualTo("25:1");
|
||||||
OriginTrackedValue strawberry = this.properties.get("foods[2]");
|
OriginTrackedValue strawberry = getFromFirst("foods[2]");
|
||||||
assertThat(getValue(strawberry)).isEqualTo("Strawberry");
|
assertThat(getValue(strawberry)).isEqualTo("Strawberry");
|
||||||
assertThat(getLocation(strawberry)).isEqualTo("26:1");
|
assertThat(getLocation(strawberry)).isEqualTo("26:1");
|
||||||
OriginTrackedValue mango = this.properties.get("foods[3]");
|
OriginTrackedValue mango = getFromFirst("foods[3]");
|
||||||
assertThat(getValue(mango)).isEqualTo("Mango");
|
assertThat(getValue(mango)).isEqualTo("Mango");
|
||||||
assertThat(getLocation(mango)).isEqualTo("27:1");
|
assertThat(getLocation(mango)).isEqualTo("27:1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithISO88591Character() {
|
void getPropertyWithISO88591Character() {
|
||||||
OriginTrackedValue value = this.properties.get("test-iso8859-1-chars");
|
OriginTrackedValue value = getFromFirst("test-iso8859-1-chars");
|
||||||
assertThat(getValue(value)).isEqualTo("æ×ÈÅÞßáñÀÿ");
|
assertThat(getValue(value)).isEqualTo("æ×ÈÅÞßáñÀÿ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithTrailingSpace() {
|
void getPropertyWithTrailingSpace() {
|
||||||
OriginTrackedValue value = this.properties.get("test-with-trailing-space");
|
OriginTrackedValue value = getFromFirst("test-with-trailing-space");
|
||||||
assertThat(getValue(value)).isEqualTo("trailing ");
|
assertThat(getValue(value)).isEqualTo("trailing ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWithEscapedTrailingSpace() {
|
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 ");
|
assertThat(getValue(value)).isEqualTo("trailing ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private OriginTrackedValue getFromFirst(String key) {
|
||||||
|
return this.documentes.get(0).asMap().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
private Object getValue(OriginTrackedValue value) {
|
private Object getValue(OriginTrackedValue value) {
|
||||||
return (value != null) ? value.getValue() : null;
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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");
|
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
|
@Test
|
||||||
void loadXml() throws Exception {
|
void loadXml() throws Exception {
|
||||||
List<PropertySource<?>> loaded = this.loader.load("test.xml",
|
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…
Reference in New Issue