Skip to content

Instantly share code, notes, and snippets.

Created August 29, 2014 12:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chaudum/ac1e91f17899d7e4f160 to your computer and use it in GitHub Desktop.
package io.crate.metadata.settings;
import org.junit.Test;
import static org.junit.Assert.*;
public class CrateSettingsTest {
public void testStringSettingsValidation() throws Exception {
StringSetting stringSetting = new StringSetting(
Sets.newHashSet("foo", "bar", "foobar")
) {
public String name() { return "foo_bar_setting"; }
public String defaultValue() { return "foo"; }
String validation = stringSetting.validate("foo");
assertEquals(validation, null);
validation = stringSetting.validate("unknown");
assertEquals(validation, "'unknown' is not an allowed value. Allowed values are: bar, foo, foobar");
// ... more tests here ...
package io.crate.metadata.settings;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.settings.Settings;
import java.util.Set;
public abstract class StringSetting extends Setting<String> {
protected Set<String> allowedValues;
protected StringSetting(Set<String> allowedValues) {
this.allowedValues = allowedValues;
public String defaultValue() {
return "";
public String extract(Settings settings) {
return settings.get(settingName(), defaultValue());
* @return Error message if not valid, else null.
public String validate(String value) {
if (allowedValues != null && !allowedValues.contains(value)) {
return String.format("'%s' is not an allowed value. Allowed values are: %s",
value, Joiner.on(", ").join(allowedValues)
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment