RegexValueValidation implements Validation { @Override public ValidationResult validate(Config config) { if (config.hasPath(path)) { if (pattern.matcher(config.getString(path)).find()) { return new ValidationResult(this, Validity.VALID, "Configuration '" + path + "' matches the supplied pattern: " + pattern.pattern()); } else { return new ValidationResult(this, Validity.INVALID, "Configuration '" + path + "' does not match the supplied pattern: " + pattern.pattern()); } } else { return new ValidationResult(this, Validity.VALID, "Configuration '" + path + "' does not exist, so will not check it for allowed values"); } } RegexValueValidation(String path, String regex); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testValid() { RegexValueValidation v = new RegexValueValidation("query", "(?i)^refresh"); ValidationResult vr = v.validate(ConfigFactory.parseString("query = refresh foobar")); assertEquals(vr.getValidity(), Validity.VALID); vr = v.validate(ConfigFactory.parseString("query = REFRESH db.foobar")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testInvalid() { RegexValueValidation v = new RegexValueValidation("query", "(?i)^refresh "); ValidationResult vr = v.validate(ConfigFactory.parseString("query = refreshh foobar")); assertEquals(vr.getValidity(), Validity.INVALID); vr = v.validate(ConfigFactory.parseString("query = REFROOSH db.foobar")); assertEquals(vr.getValidity(), Validity.INVALID); }