### Question:
DomainValidator implements ConstraintValidator<Domain, CharSequence> { @Override public boolean isValid(CharSequence domain, ConstraintValidatorContext context) { Matcher matcher = DOMAIN_NAME_REGEX.matcher(domain); if (matcher.matches()) { domain = matcher.group(1); return isValidTld(domain.toString()); } return allowLocal && DOMAIN_LABEL.matcher(domain).matches(); }  @Override boolean isValid(CharSequence domain, ConstraintValidatorContext context); @Override void initialize(Domain domain);  }### Answer:
@Test public void testValidDomains() { assertTrue("apache.org should validate", validator.isValid("apache.org", null)); assertTrue("www.google.com should validate", validator.isValid("www.google.com", null)); assertTrue("test-domain.com should validate", validator.isValid("test-domain.com", null)); assertTrue("test---domain.com should validate", validator.isValid("test---domain.com", null)); assertTrue("test-d-o-m-ain.com should validate", validator.isValid("test-d-o-m-ain.com", null)); assertTrue("two-letter domain label should validate", validator.isValid("as.uk", null)); assertTrue("case-insensitive ApAchE.Org should validate", validator.isValid("ApAchE.Org", null)); assertTrue("single-character domain label should validate", validator.isValid("z.com", null)); assertTrue("i.have.an-example.domain.name should validate", validator.isValid("i.have.an-example.domain.name", null)); }
@Test public void testInvalidDomains() { assertFalse("bare TLD .org shouldn't validate", validator.isValid(".org", null)); assertFalse("domain name with spaces shouldn't validate", validator.isValid(" apache.org ", null)); assertFalse("domain name containing spaces shouldn't validate", validator.isValid("apa che.org", null)); assertFalse("domain name starting with dash shouldn't validate", validator.isValid("-testdomain.name", null)); assertFalse("domain name ending with dash shouldn't validate", validator.isValid("testdomain-.name", null)); assertFalse("domain name starting with multiple dashes shouldn't validate", validator.isValid("---c.com", null)); assertFalse("domain name ending with multiple dashes shouldn't validate", validator.isValid("c--.com", null)); assertFalse("domain name with invalid TLD shouldn't validate", validator.isValid("apache.rog", null)); assertFalse("URL shouldn't validate", validator.isValid("http: assertFalse("Empty string shouldn't validate as domain name", validator.isValid(" ", null)); }
@Test public void testIDN() { assertTrue("b\u00fccher.ch in IDN should validate", validator.isValid("www.xn--bcher-kva.ch", null)); }