AtMostOnePathExistsValidation implements Validation { @Override public ValidationResult validate(Config config) { int found = 0; for (String path : paths) { if (config.hasPath(path)) { if (found == 1) { return new ValidationResult(this, Validity.INVALID, "More than one of the following configurations was found, " + "when at most one must exist: " + paths); } else { if (type != null && !config.getValue(path).valueType().equals(type) && !ConfigUtils.canBeCoerced(config, path, type)) { return new ValidationResult(this, Validity.INVALID, "The configuration '" + path + "' was found but not of required type: " + type); } found++; } } } if (found == 1) { return new ValidationResult(this, Validity.VALID, "One of the following configurations was found, when at most one must exist: " + paths); } else { return new ValidationResult(this, Validity.VALID, "None of the following configurations was found, when at most one must exist: " + paths); } } AtMostOnePathExistsValidation(String... paths);  AtMostOnePathExistsValidation(ConfigValueType type, String... paths); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testZeroExists() { Validation v = new AtMostOnePathExistsValidation("hello", "world"); ValidationResult vr = v.validate(ConfigFactory.parseString("test = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testOneExists() { Validation v = new AtMostOnePathExistsValidation("hello", "world"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testOneExistsWithType() { Validation v = new AtMostOnePathExistsValidation(ConfigValueType.NUMBER, "hello", "world"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testMoreThanOneExists() { Validation v = new AtMostOnePathExistsValidation("hello", "world"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2\nworld = 5")); assertEquals(vr.getValidity(), Validity.INVALID); }
@Test public void testWrongType() { Validation v = new AtMostOnePathExistsValidation(ConfigValueType.STRING, "hello", "world"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.INVALID); }