ConfigCommandValidator implements Validator { @Override public void validate(final Object obj, final Errors errors) { final ConfigCommand command = (ConfigCommand) obj; final String repositoryUrl = command.getRepositoryUrl(); if (command.getName() != null && repositoryUrl != null) { if (!RepositoryName.isValid(command.getName())) { errors.rejectValue("name", "config.error.illegal-name"); } else if (application.getRepositoryNames().contains(new RepositoryName(command.getName()))) { errors.rejectValue("name", "config.error.duplicate-name"); } else { SVNURL url = null; try { url = SVNURL.parse(repositoryUrl); } catch (SventonException e) { logger.info(e); errors.rejectValue("repositoryUrl", "config.error.illegal-url"); } if (url != null && testConnections) { Credentials credentials; final RepositoryName repositoryName = new RepositoryName(command.getName()); try { credentials = new Credentials(command.getUserName(), command.getUserPassword()); testConnection(repositoryName, repositoryUrl, credentials); } catch (AuthenticationException e) { errors.rejectValue("accessMethod", "config.error.authentication-error"); } catch (SventonException e) { errors.rejectValue("repositoryUrl", "config.error.connection-error", new String[]{repositoryUrl}, "Unable to connect to repository [" + repositoryUrl + "]. Check URL."); } if (command.isCacheUsed()) { try { credentials = new Credentials(command.getCacheUserName(), command.getCacheUserPassword()); testConnection(repositoryName, repositoryUrl, credentials); } catch (SventonException e) { errors.rejectValue("cacheUsed", "config.error.authentication-error"); } } } } } } ConfigCommandValidator(); protected  ConfigCommandValidator(final boolean testConnections); @Autowired void setApplication(final Application application); void setRepositoryService(final RepositoryService repositoryService); @Override boolean supports(final Class clazz); @Autowired void setConnectionFactory(final SVNConnectionFactory connectionFactory); @Override void validate(final Object obj, final Errors errors);  }
@Test public void testValidate() throws Exception { final ConfigDirectory configDirectory = TestUtils.getTestConfigDirectory(); configDirectory.setCreateDirectories(false); final MockServletContext servletContext = new MockServletContext(); servletContext.setContextPath("sventon-test"); configDirectory.setServletContext(servletContext); final Application application = new Application(configDirectory); final ConfigCommandValidator validator = new ConfigCommandValidator(false); validator.setApplication(application); final ConfigCommand command = new ConfigCommand(); BindException exception = new BindException(command, "test"); validator.validate(command, exception); assertEquals(0, exception.getAllErrors().size()); command.setName("Illegal whitespace in name"); command.setRepositoryUrl("svn: command.setUserPassword(""); command.setUserName(""); validator.validate(command, exception); assertEquals(1, exception.getAllErrors().size()); assertEquals("config.error.illegal-name", exception.getFieldError("name").getCode()); exception = new BindException(command, "test"); command.setName(""); command.setRepositoryUrl("svn: command.setUserPassword(""); command.setUserName(""); validator.validate(command, exception); assertEquals(1, exception.getAllErrors().size()); assertEquals("config.error.illegal-name", exception.getFieldError("name").getCode()); exception = new BindException(command, "test"); command.setRepositoryUrl("svn: command.setName("default"); command.setUserPassword(""); command.setUserName(""); command.setCacheUserName(""); command.setCacheUserPassword(""); validator.validate(command, exception); assertEquals(0, exception.getAllErrors().size()); command.setRepositoryUrl(" svn: command.setUserPassword(""); command.setUserName(""); validator.validate(command, exception); assertEquals(0, exception.getAllErrors().size()); command.setUserPassword(null); command.setUserName(null); validator.validate(command, exception); assertEquals(0, exception.getAllErrors().size()); command.setRepositoryUrl(""); validator.validate(command, exception); assertEquals(1, exception.getAllErrors().size()); assertEquals("config.error.illegal-url", exception.getFieldError("repositoryUrl").getCode()); exception = new BindException(command, "test"); command.setRepositoryUrl("notavalidurl"); validator.validate(command, exception); assertEquals(1, exception.getAllErrors().size()); assertEquals("config.error.illegal-url", exception.getFieldError("repositoryUrl").getCode()); exception = new BindException(command, "test"); command.setRepositoryUrl("svn: command.setUserPassword(""); command.setUserName(""); validator.validate(command, exception); assertEquals(0, exception.getAllErrors().size()); command.setRepositoryUrl("svn: command.setName("default"); command.setUserPassword("admin"); command.setUserName("super-secret-pwd123"); command.setAccessMethod(USER); command.setCacheUserName(""); command.setCacheUserPassword(""); validator.validate(command, exception); assertEquals(0, exception.getAllErrors().size()); }