IfPathExistsValidation implements Validation { @Override public ValidationResult validate(Config config) { if (config.hasPath(ifPath)) { return thenValidation.validate(config); } else { return new ValidationResult(this, Validity.VALID, "Conditional configuration '" + ifPath + "' does not exist, so it was not required that the validation (" + thenValidation + ") be checked"); } } IfPathExistsValidation(String ifPath, Validation thenValidation); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testValidWhenPathExists() { Validation v = new IfPathExistsValidation("hello", Validations.single().allowedValues("hello", 2)); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testInvalidWhenPathExists() { Validation v = new IfPathExistsValidation("hello", Validations.single().allowedValues("hello", 3)); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.INVALID); }
@Test public void testPathDoesntExist() { Validation v = new IfPathExistsValidation("world", Validations.single().allowedValues("world", 2)); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }