DirectoryNameValidator { public boolean isValid(String dirName) { if (dirName == null || dirName.trim().isEmpty()) { return Boolean.FALSE; } if (!dirName.matches(VALID_DIR_REGEX)) { return Boolean.FALSE; } if (currentDirectory.alreadyHasChild(dirName)) { return Boolean.FALSE; } return Boolean.TRUE; } DirectoryNameValidator(Directory currentDirectory); String getValidationError(); boolean isValid(String dirName); static final String VALID_DIR_REGEX; }
@Test public void isValidTest() throws Exception { assertTrue(directoryNameValidator.isValid("app")); assertTrue(directoryNameValidator.isValid("my app")); assertTrue(directoryNameValidator.isValid("日本国")); assertFalse(directoryNameValidator.isValid("")); assertFalse(directoryNameValidator.isValid(" ")); assertFalse(directoryNameValidator.isValid("app\\")); assertFalse(directoryNameValidator.isValid("app/")); assertFalse(directoryNameValidator.isValid("app:")); assertFalse(directoryNameValidator.isValid("*app")); assertFalse(directoryNameValidator.isValid("a<pp")); assertFalse(directoryNameValidator.isValid("app>")); assertFalse(directoryNameValidator.isValid("ap|p")); assertFalse(directoryNameValidator.isValid("ap*p")); assertFalse(directoryNameValidator.isValid("ap?p")); }
@Test public void isValidTestDuplicatedDir() throws Exception { final Directory currentDirectory = new Directory("parent", "", "", new HashMap<String, List<String>>()); directoryNameValidator = new DirectoryNameValidator(currentDirectory); assertTrue(directoryNameValidator.isValid("app")); currentDirectory.addChildDirectory(new Directory("app", "", "", currentDirectory)); assertFalse(directoryNameValidator.isValid("app")); }