Settings: Cleanup placeholder replacement
This change moves placeholder replacement to a pkg private class for settings. It also adds a null check when calling replacement, as settings objects can still contain null values, because we only prohibit nulls on file loading. Finally, this cleans up file and stream loading a bit to not have unnecessary exception wrapping.
This commit is contained in:
parent
08903f1ed8
commit
3adaf09675
|
@ -167,7 +167,7 @@ public class LogConfigurator {
|
|||
static void loadConfig(Path file, Settings.Builder settingsBuilder) {
|
||||
try {
|
||||
settingsBuilder.loadFromPath(file);
|
||||
} catch (SettingsException | NoClassDefFoundError e) {
|
||||
} catch (IOException | SettingsException | NoClassDefFoundError e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.common.property;
|
||||
package org.elasticsearch.common.settings;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
|
||||
|
@ -34,23 +34,12 @@ import java.util.Set;
|
|||
* Values for substitution can be supplied using a {@link Properties} instance or using a
|
||||
* {@link PlaceholderResolver}.
|
||||
*/
|
||||
public class PropertyPlaceholder {
|
||||
class PropertyPlaceholder {
|
||||
|
||||
private final String placeholderPrefix;
|
||||
private final String placeholderSuffix;
|
||||
private final boolean ignoreUnresolvablePlaceholders;
|
||||
|
||||
/**
|
||||
* Creates a new <code>PropertyPlaceholderHelper</code> that uses the supplied prefix and suffix. Unresolvable
|
||||
* placeholders are ignored.
|
||||
*
|
||||
* @param placeholderPrefix the prefix that denotes the start of a placeholder.
|
||||
* @param placeholderSuffix the suffix that denotes the end of a placeholder.
|
||||
*/
|
||||
public PropertyPlaceholder(String placeholderPrefix, String placeholderSuffix) {
|
||||
this(placeholderPrefix, placeholderSuffix, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>PropertyPlaceholderHelper</code> that uses the supplied prefix and suffix.
|
||||
*
|
||||
|
@ -59,12 +48,10 @@ public class PropertyPlaceholder {
|
|||
* @param ignoreUnresolvablePlaceholders indicates whether unresolvable placeholders should be ignored
|
||||
* (<code>true</code>) or cause an exception (<code>false</code>).
|
||||
*/
|
||||
public PropertyPlaceholder(String placeholderPrefix, String placeholderSuffix,
|
||||
PropertyPlaceholder(String placeholderPrefix, String placeholderSuffix,
|
||||
boolean ignoreUnresolvablePlaceholders) {
|
||||
Objects.requireNonNull(placeholderPrefix, "Argument 'placeholderPrefix' must not be null.");
|
||||
Objects.requireNonNull(placeholderSuffix, "Argument 'placeholderSuffix' must not be null.");
|
||||
this.placeholderPrefix = placeholderPrefix;
|
||||
this.placeholderSuffix = placeholderSuffix;
|
||||
this.placeholderPrefix = Objects.requireNonNull(placeholderPrefix);
|
||||
this.placeholderSuffix = Objects.requireNonNull(placeholderSuffix);
|
||||
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
||||
}
|
||||
|
||||
|
@ -75,15 +62,15 @@ public class PropertyPlaceholder {
|
|||
* @param value the value containing the placeholders to be replaced.
|
||||
* @param placeholderResolver the <code>PlaceholderResolver</code> to use for replacement.
|
||||
* @return the supplied value with placeholders replaced inline.
|
||||
* @throws NullPointerException if value is null
|
||||
*/
|
||||
public String replacePlaceholders(String key, String value, PlaceholderResolver placeholderResolver) {
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(value, "value can not be null for [" + key + "]");
|
||||
return parseStringValue(value, placeholderResolver, new HashSet<String>());
|
||||
String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {
|
||||
Objects.requireNonNull(value);
|
||||
return parseStringValue(value, placeholderResolver, new HashSet<>());
|
||||
}
|
||||
|
||||
protected String parseStringValue(String strVal, PlaceholderResolver placeholderResolver,
|
||||
Set<String> visitedPlaceholders) {
|
||||
private String parseStringValue(String strVal, PlaceholderResolver placeholderResolver,
|
||||
Set<String> visitedPlaceholders) {
|
||||
StringBuilder buf = new StringBuilder(strVal);
|
||||
|
||||
int startIndex = strVal.indexOf(this.placeholderPrefix);
|
||||
|
@ -164,7 +151,7 @@ public class PropertyPlaceholder {
|
|||
*
|
||||
* @see PropertyPlaceholder
|
||||
*/
|
||||
public interface PlaceholderResolver {
|
||||
interface PlaceholderResolver {
|
||||
|
||||
/**
|
||||
* Resolves the supplied placeholder name into the replacement value.
|
|
@ -25,7 +25,6 @@ import org.elasticsearch.common.Strings;
|
|||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.property.PropertyPlaceholder;
|
||||
import org.elasticsearch.common.settings.loader.SettingsLoader;
|
||||
import org.elasticsearch.common.settings.loader.SettingsLoaderFactory;
|
||||
import org.elasticsearch.common.unit.ByteSizeUnit;
|
||||
|
@ -1114,26 +1113,20 @@ public final class Settings implements ToXContent {
|
|||
* Loads settings from a url that represents them using the
|
||||
* {@link SettingsLoaderFactory#loaderFromSource(String)}.
|
||||
*/
|
||||
public Builder loadFromPath(Path path) throws SettingsException {
|
||||
try {
|
||||
return loadFromStream(path.getFileName().toString(), Files.newInputStream(path));
|
||||
} catch (IOException e) {
|
||||
throw new SettingsException("Failed to open stream for url [" + path + "]", e);
|
||||
}
|
||||
public Builder loadFromPath(Path path) throws IOException {
|
||||
// NOTE: loadFromStream will close the input stream
|
||||
return loadFromStream(path.getFileName().toString(), Files.newInputStream(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from a stream that represents them using the
|
||||
* {@link SettingsLoaderFactory#loaderFromSource(String)}.
|
||||
*/
|
||||
public Builder loadFromStream(String resourceName, InputStream is) throws SettingsException {
|
||||
public Builder loadFromStream(String resourceName, InputStream is) throws IOException {
|
||||
SettingsLoader settingsLoader = SettingsLoaderFactory.loaderFromResource(resourceName);
|
||||
try {
|
||||
Map<String, String> loadedSettings = settingsLoader.load(Streams.copyToString(new InputStreamReader(is, StandardCharsets.UTF_8)));
|
||||
put(loadedSettings);
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException("Failed to load settings from [" + resourceName + "]", e);
|
||||
}
|
||||
// NOTE: copyToString will close the input stream
|
||||
Map<String, String> loadedSettings = settingsLoader.load(Streams.copyToString(new InputStreamReader(is, StandardCharsets.UTF_8)));
|
||||
put(loadedSettings);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -1220,14 +1213,20 @@ public final class Settings implements ToXContent {
|
|||
return true;
|
||||
}
|
||||
};
|
||||
for (Map.Entry<String, String> entry : new HashMap<>(map).entrySet()) {
|
||||
String value = propertyPlaceholder.replacePlaceholders(entry.getKey(), entry.getValue(), placeholderResolver);
|
||||
Iterator<Map.Entry<String, String>> entryItr = map.entrySet().iterator();
|
||||
while (entryItr.hasNext()) {
|
||||
Map.Entry<String, String> entry = entryItr.next();
|
||||
if (entry.getValue() == null) {
|
||||
// a null value obviously can't be replaced
|
||||
continue;
|
||||
}
|
||||
String value = propertyPlaceholder.replacePlaceholders(entry.getValue(), placeholderResolver);
|
||||
// if the values exists and has length, we should maintain it in the map
|
||||
// otherwise, the replace process resolved into removing it
|
||||
if (Strings.hasLength(value)) {
|
||||
map.put(entry.getKey(), value);
|
||||
entry.setValue(value);
|
||||
} else {
|
||||
map.remove(entry.getKey());
|
||||
entryItr.remove();
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
|
|
@ -210,7 +210,7 @@ public class HunspellService extends AbstractComponent {
|
|||
* @param defaults The default settings for this dictionary
|
||||
* @return The resolved settings.
|
||||
*/
|
||||
private static Settings loadDictionarySettings(Path dir, Settings defaults) {
|
||||
private static Settings loadDictionarySettings(Path dir, Settings defaults) throws IOException {
|
||||
Path file = dir.resolve("settings.yml");
|
||||
if (Files.exists(file)) {
|
||||
return Settings.settingsBuilder().loadFromPath(file).put(defaults).build();
|
||||
|
|
|
@ -92,7 +92,11 @@ public class InternalSettingsPreparer {
|
|||
Path path = environment.configFile().resolve("elasticsearch" + allowedSuffix);
|
||||
if (Files.exists(path)) {
|
||||
if (!settingsFileFound) {
|
||||
output.loadFromPath(path);
|
||||
try {
|
||||
output.loadFromPath(path);
|
||||
} catch (IOException e) {
|
||||
throw new SettingsException("Failed to settings from " + path.toString(), e);
|
||||
}
|
||||
}
|
||||
settingsFileFound = true;
|
||||
foundSuffixes.add(allowedSuffix);
|
||||
|
|
|
@ -17,14 +17,13 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.common.property;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
package org.elasticsearch.common.settings;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.hasToString;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class PropertyPlaceholderTests extends ESTestCase {
|
||||
|
@ -34,10 +33,10 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
map.put("foo1", "bar1");
|
||||
map.put("foo2", "bar2");
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("bar1", propertyPlaceholder.replacePlaceholders("key", "{foo1}", placeholderResolver));
|
||||
assertEquals("a bar1b", propertyPlaceholder.replacePlaceholders("key", "a {foo1}b", placeholderResolver));
|
||||
assertEquals("bar1bar2", propertyPlaceholder.replacePlaceholders("key", "{foo1}{foo2}", placeholderResolver));
|
||||
assertEquals("a bar1 b bar2 c", propertyPlaceholder.replacePlaceholders("key", "a {foo1} b {foo2} c", placeholderResolver));
|
||||
assertEquals("bar1", propertyPlaceholder.replacePlaceholders("{foo1}", placeholderResolver));
|
||||
assertEquals("a bar1b", propertyPlaceholder.replacePlaceholders("a {foo1}b", placeholderResolver));
|
||||
assertEquals("bar1bar2", propertyPlaceholder.replacePlaceholders("{foo1}{foo2}", placeholderResolver));
|
||||
assertEquals("a bar1 b bar2 c", propertyPlaceholder.replacePlaceholders("a {foo1} b {foo2} c", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testVariousPrefixSuffix() {
|
||||
|
@ -48,24 +47,24 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
Map<String, String> map = new LinkedHashMap<>();
|
||||
map.put("foo", "bar");
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("bar", ppEqualsPrefix.replacePlaceholders("key", "{foo}", placeholderResolver));
|
||||
assertEquals("bar", ppLongerPrefix.replacePlaceholders("key", "${foo}", placeholderResolver));
|
||||
assertEquals("bar", ppShorterPrefix.replacePlaceholders("key", "{foo}}", placeholderResolver));
|
||||
assertEquals("bar", ppEqualsPrefix.replacePlaceholders("{foo}", placeholderResolver));
|
||||
assertEquals("bar", ppLongerPrefix.replacePlaceholders("${foo}", placeholderResolver));
|
||||
assertEquals("bar", ppShorterPrefix.replacePlaceholders("{foo}}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testDefaultValue() {
|
||||
PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("bar", propertyPlaceholder.replacePlaceholders("key", "${foo:bar}", placeholderResolver));
|
||||
assertEquals("", propertyPlaceholder.replacePlaceholders("key", "${foo:}", placeholderResolver));
|
||||
assertEquals("bar", propertyPlaceholder.replacePlaceholders("${foo:bar}", placeholderResolver));
|
||||
assertEquals("", propertyPlaceholder.replacePlaceholders("${foo:}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testIgnoredUnresolvedPlaceholder() {
|
||||
PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", true);
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("${foo}", propertyPlaceholder.replacePlaceholders("key", "${foo}", placeholderResolver));
|
||||
assertEquals("${foo}", propertyPlaceholder.replacePlaceholders("${foo}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testNotIgnoredUnresolvedPlaceholder() {
|
||||
|
@ -73,7 +72,7 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
Map<String, String> map = new LinkedHashMap<>();
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
try {
|
||||
propertyPlaceholder.replacePlaceholders("key", "${foo}", placeholderResolver);
|
||||
propertyPlaceholder.replacePlaceholders("${foo}", placeholderResolver);
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), is("Could not resolve placeholder 'foo'"));
|
||||
|
@ -84,7 +83,7 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, true, true);
|
||||
assertEquals("bar", propertyPlaceholder.replacePlaceholders("key", "bar${foo}", placeholderResolver));
|
||||
assertEquals("bar", propertyPlaceholder.replacePlaceholders("bar${foo}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testRecursive() {
|
||||
|
@ -94,8 +93,8 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
map.put("foo1", "${foo2}");
|
||||
map.put("foo2", "bar");
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("bar", propertyPlaceholder.replacePlaceholders("key", "${foo}", placeholderResolver));
|
||||
assertEquals("abarb", propertyPlaceholder.replacePlaceholders("key", "a${foo}b", placeholderResolver));
|
||||
assertEquals("bar", propertyPlaceholder.replacePlaceholders("${foo}", placeholderResolver));
|
||||
assertEquals("abarb", propertyPlaceholder.replacePlaceholders("a${foo}b", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testNestedLongerPrefix() {
|
||||
|
@ -106,7 +105,7 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
map.put("foo2", "bar");
|
||||
map.put("barbar", "baz");
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("baz", propertyPlaceholder.replacePlaceholders("key", "${bar${foo}}", placeholderResolver));
|
||||
assertEquals("baz", propertyPlaceholder.replacePlaceholders("${bar${foo}}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testNestedSameLengthPrefixSuffix() {
|
||||
|
@ -117,7 +116,7 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
map.put("foo2", "bar");
|
||||
map.put("barbar", "baz");
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("baz", propertyPlaceholder.replacePlaceholders("key", "{bar{foo}}", placeholderResolver));
|
||||
assertEquals("baz", propertyPlaceholder.replacePlaceholders("{bar{foo}}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testNestedShorterPrefix() {
|
||||
|
@ -128,7 +127,7 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
map.put("foo2", "bar");
|
||||
map.put("barbar", "baz");
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
assertEquals("baz", propertyPlaceholder.replacePlaceholders("key", "{bar{foo}}}}", placeholderResolver));
|
||||
assertEquals("baz", propertyPlaceholder.replacePlaceholders("{bar{foo}}}}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testCircularReference() {
|
||||
|
@ -138,7 +137,7 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
map.put("bar", "${foo}");
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, false, true);
|
||||
try {
|
||||
propertyPlaceholder.replacePlaceholders("key", "${foo}", placeholderResolver);
|
||||
propertyPlaceholder.replacePlaceholders("${foo}", placeholderResolver);
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), is("Circular placeholder reference 'foo' in property definitions"));
|
||||
|
@ -149,24 +148,7 @@ public class PropertyPlaceholderTests extends ESTestCase {
|
|||
PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, true, false);
|
||||
assertEquals("bar${foo}", propertyPlaceholder.replacePlaceholders("key", "bar${foo}", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testNullKey() {
|
||||
final PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
|
||||
final Map<String, String> map = new LinkedHashMap<>();
|
||||
final PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, true, false);
|
||||
expectThrows(NullPointerException.class, () -> propertyPlaceholder.replacePlaceholders(null, "value", placeholderResolver));
|
||||
}
|
||||
|
||||
public void testNullValue() {
|
||||
final PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
|
||||
final Map<String, String> map = new LinkedHashMap<>();
|
||||
final PropertyPlaceholder.PlaceholderResolver placeholderResolver = new SimplePlaceholderResolver(map, true, false);
|
||||
final String key = randomAsciiOfLength(10);
|
||||
NullPointerException e =
|
||||
expectThrows(NullPointerException.class, () -> propertyPlaceholder.replacePlaceholders(key, null, placeholderResolver));
|
||||
assertThat(e, hasToString("java.lang.NullPointerException: value can not be null for [" + key + "]"));
|
||||
assertEquals("bar${foo}", propertyPlaceholder.replacePlaceholders("bar${foo}", placeholderResolver));
|
||||
}
|
||||
|
||||
private class SimplePlaceholderResolver implements PropertyPlaceholder.PlaceholderResolver {
|
|
@ -19,6 +19,11 @@
|
|||
|
||||
package org.elasticsearch.common.settings.loader;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.elasticsearch.ElasticsearchParseException;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
|
@ -48,42 +53,39 @@ public class YamlSettingsLoaderTests extends ESTestCase {
|
|||
assertThat(settings.getAsArray("test1.test3")[1], equalTo("test3-2"));
|
||||
}
|
||||
|
||||
public void testIndentation() {
|
||||
final String yaml = "/org/elasticsearch/common/settings/loader/indentation-settings.yml";
|
||||
final SettingsException e =
|
||||
expectThrows(
|
||||
SettingsException.class,
|
||||
() -> settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)).build());
|
||||
assertThat(e.getMessage(), containsString("Failed to load settings"));
|
||||
public void testIndentation() throws Exception {
|
||||
String yaml = "/org/elasticsearch/common/settings/loader/indentation-settings.yml";
|
||||
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> {
|
||||
settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml));
|
||||
});
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("malformed"));
|
||||
}
|
||||
|
||||
public void testIndentationWithExplicitDocumentStart() {
|
||||
final String yaml = "/org/elasticsearch/common/settings/loader/indentation-with-explicit-document-start-settings.yml";
|
||||
final SettingsException e =
|
||||
expectThrows(
|
||||
SettingsException.class,
|
||||
() -> settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)).build());
|
||||
assertThat(e.getMessage(), containsString("Failed to load settings"));
|
||||
public void testIndentationWithExplicitDocumentStart() throws Exception {
|
||||
String yaml = "/org/elasticsearch/common/settings/loader/indentation-with-explicit-document-start-settings.yml";
|
||||
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> {
|
||||
settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml));
|
||||
});
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("malformed"));
|
||||
}
|
||||
|
||||
public void testDuplicateKeysThrowsException() {
|
||||
final String yaml = "foo: bar\nfoo: baz";
|
||||
final SettingsException e = expectThrows(SettingsException.class, () -> settingsBuilder().loadFromSource(yaml).build());
|
||||
String yaml = "foo: bar\nfoo: baz";
|
||||
SettingsException e = expectThrows(SettingsException.class, () -> {
|
||||
settingsBuilder().loadFromSource(yaml);
|
||||
});
|
||||
assertEquals(e.getCause().getClass(), ElasticsearchParseException.class);
|
||||
assertThat(
|
||||
e.toString(),
|
||||
containsString("duplicate settings key [foo] " +
|
||||
"found at line number [2], " +
|
||||
"column number [6], " +
|
||||
"previous value [bar], " +
|
||||
"current value [baz]"));
|
||||
String msg = e.getCause().getMessage();
|
||||
assertTrue(msg, msg.contains("duplicate settings key [foo] found"));
|
||||
assertTrue(msg, msg.contains("previous value [bar], current value [baz]"));
|
||||
}
|
||||
|
||||
public void testNullValuedSettingThrowsException() {
|
||||
final String yaml = "foo:";
|
||||
final ElasticsearchParseException e =
|
||||
expectThrows(ElasticsearchParseException.class, () -> new YamlSettingsLoader(false).load(yaml));
|
||||
assertThat(e.toString(), containsString("null-valued setting found for key [foo] found at line number [1], column number [5]"));
|
||||
public void testMissingValue() throws Exception {
|
||||
Path tmp = createTempFile("test", ".yaml");
|
||||
Files.write(tmp, Collections.singletonList("foo: # missing value\n"), StandardCharsets.UTF_8);
|
||||
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> {
|
||||
settingsBuilder().loadFromPath(tmp);
|
||||
});
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("null-valued setting found for key [foo]"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ public class AnalysisModuleTests extends ModuleTestCase {
|
|||
Collections.emptyMap(), Collections.singletonMap("myfilter", MyFilterTokenFilterFactory::new), Collections.emptyMap(), Collections.emptyMap());
|
||||
}
|
||||
|
||||
private Settings loadFromClasspath(String path) {
|
||||
private Settings loadFromClasspath(String path) throws IOException {
|
||||
return settingsBuilder().loadFromStream(path, getClass().getResourceAsStream(path))
|
||||
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
|
||||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
||||
|
|
|
@ -91,7 +91,7 @@ public class CompoundAnalysisTests extends ESTestCase {
|
|||
return terms;
|
||||
}
|
||||
|
||||
private Settings getJsonSettings() {
|
||||
private Settings getJsonSettings() throws IOException {
|
||||
String json = "/org/elasticsearch/index/analysis/test1.json";
|
||||
return settingsBuilder()
|
||||
.loadFromStream(json, getClass().getResourceAsStream(json))
|
||||
|
@ -100,7 +100,7 @@ public class CompoundAnalysisTests extends ESTestCase {
|
|||
.build();
|
||||
}
|
||||
|
||||
private Settings getYamlSettings() {
|
||||
private Settings getYamlSettings() throws IOException {
|
||||
String yaml = "/org/elasticsearch/index/analysis/test1.yml";
|
||||
return settingsBuilder()
|
||||
.loadFromStream(yaml, getClass().getResourceAsStream(yaml))
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.cloud.azure;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.PathUtils;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.plugin.discovery.azure.AzureDiscoveryPlugin;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ThirdParty;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Base class for Azure tests that require credentials.
|
||||
* <p>
|
||||
* You must specify {@code -Dtests.thirdparty=true -Dtests.config=/path/to/config}
|
||||
* in order to run these tests.
|
||||
*/
|
||||
@ThirdParty
|
||||
public abstract class AbstractAzureTestCase extends ESIntegTestCase {
|
||||
|
||||
@Override
|
||||
protected Settings nodeSettings(int nodeOrdinal) {
|
||||
return Settings.builder()
|
||||
.put(super.nodeSettings(nodeOrdinal))
|
||||
.put(readSettingsFromFile())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return pluginList(AzureDiscoveryPlugin.class);
|
||||
}
|
||||
|
||||
protected Settings readSettingsFromFile() {
|
||||
Settings.Builder settings = Settings.builder();
|
||||
settings.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir());
|
||||
|
||||
// if explicit, just load it and don't load from env
|
||||
try {
|
||||
if (Strings.hasText(System.getProperty("tests.config"))) {
|
||||
settings.loadFromPath(PathUtils.get((System.getProperty("tests.config"))));
|
||||
} else {
|
||||
throw new IllegalStateException("to run integration tests, you need to set -Dtests.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml");
|
||||
}
|
||||
} catch (SettingsException exception) {
|
||||
throw new IllegalStateException("your test configuration file is incorrect: " + System.getProperty("tests.config"), exception);
|
||||
}
|
||||
return settings.build();
|
||||
}
|
||||
}
|
|
@ -29,6 +29,7 @@ import org.elasticsearch.plugins.Plugin;
|
|||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ThirdParty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
|
@ -52,7 +53,11 @@ public abstract class AbstractAwsTestCase extends ESIntegTestCase {
|
|||
// if explicit, just load it and don't load from env
|
||||
try {
|
||||
if (Strings.hasText(System.getProperty("tests.config"))) {
|
||||
settings.loadFromPath(PathUtils.get(System.getProperty("tests.config")));
|
||||
try {
|
||||
settings.loadFromPath(PathUtils.get(System.getProperty("tests.config")));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("could not load aws tests config", e);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("to run integration tests, you need to set -Dtests.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml");
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import org.elasticsearch.plugin.repository.azure.AzureRepositoryPlugin;
|
|||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ThirdParty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
|
@ -58,7 +59,11 @@ public abstract class AbstractAzureWithThirdPartyTestCase extends AbstractAzureT
|
|||
// if explicit, just load it and don't load from env
|
||||
try {
|
||||
if (Strings.hasText(System.getProperty("tests.config"))) {
|
||||
settings.loadFromPath(PathUtils.get((System.getProperty("tests.config"))));
|
||||
try {
|
||||
settings.loadFromPath(PathUtils.get((System.getProperty("tests.config"))));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("could not load azure tests config", e);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("to run integration tests, you need to set -Dtests.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml");
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.elasticsearch.plugins.Plugin;
|
|||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ThirdParty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
|
@ -52,7 +53,11 @@ public abstract class AbstractAwsTestCase extends ESIntegTestCase {
|
|||
// if explicit, just load it and don't load from env
|
||||
try {
|
||||
if (Strings.hasText(System.getProperty("tests.config"))) {
|
||||
settings.loadFromPath(PathUtils.get(System.getProperty("tests.config")));
|
||||
try {
|
||||
settings.loadFromPath(PathUtils.get(System.getProperty("tests.config")));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("could not load aws tests config", e);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("to run integration tests, you need to set -Dtests.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue