### Question:
BucketNameValidatorRepo { public static Validator<String> getBucketNameValidator(final String restrictionValue) { if (restrictionValue != null && "extended".equalsIgnoreCase(restrictionValue)) { return extendedValidator; } else if (restrictionValue != null && "dns-compliant".equalsIgnoreCase(restrictionValue)) { return dnsCompliantValidator; } else { return new Validator<String>() { @Override public boolean check(String value) { LOG.error("the value " + restrictionValue + " is not valid, must be either 'extended' or 'dns-compliant'. No validation " + "will be done on the specified bucket name (may result in errors)."); if (value != null && value.length() > 0) { return true; } return false; } }; } }  static Validator<String> getBucketNameValidator(final String restrictionValue);  }### Answer:
@Test public void extendedBucketNameValidatorTest() throws Exception { String restriction = "extended"; String largeName = String.format("%0" + 10 + "d", 0).replace("0", "abcdefghij"); assertTrue("expected that a " + largeName.length() + "-character name would be okay", BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(largeName)); largeName = String.format("%0" + 30 + "d", 0).replace("0", "abcdefghij"); assertTrue("expected that a " + largeName.length() + "-character name would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction).check(largeName)); String badName = "foo#bar"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = "foo bar"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = "foo@bar"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = "foo,bar"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); }
@Test public void dnsCompliantBucketNameValidatorTest() throws Exception { String restriction = "dns-compliant"; String largeName = String.format("%0" + 6 + "d", 0).replace("0", "abcdefghij"); assertTrue("expected that a " + largeName.length() + "-character name would be okay", BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(largeName)); largeName = String.format("%0" + 8 + "d", 0).replace("0", "abcdefgh"); assertTrue("expected that a " + largeName.length() + "-character name would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction).check(largeName)); String badName = "foo..bar"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = ".foobar"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = "-foobar"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = "foobar-"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = "foobar."; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); badName = "173.205.188.50"; assertTrue("expected that the name " + badName + " would not be okay", !BucketNameValidatorRepo.getBucketNameValidator(restriction) .check(badName)); }