<commit_before>package com.randomcoder.user;

import junit.framework.TestCase;

import org.springframework.validation.*;

public class ChangePasswordValidatorTest extends TestCase
{
	private ChangePasswordValidator validator;

	@Override
	public void setUp() throws Exception
	{
		validator = new ChangePasswordValidator();
		validator.setMinimumPasswordLength(6);
	}

	public void testSupports()
	{
		assertTrue("Validator doesn't support command class", validator.supports(ChangePasswordCommand.class));
	}

	public void testValidate()
	{
		FieldError error;
		BindException errors;
		
		// setup
		ChangePasswordCommand command = new ChangePasswordCommand();
		User user = new User();
		user.setUserName("validate-user");
		user.setPassword(User.hashPassword("<PASSWORD>"));
		command.setUser(user);
		
		// no data supplied
		errors = new BindException(command, "command");
		validator.validate(command, errors);
		assertEquals("Wrong number of errors occurred", 3, errors.getErrorCount());
		assertEquals("Wrong error count for oldPassword", 1, errors.getFieldErrorCount("oldPassword"));
		assertEquals("Wrong error count for password", 1, errors.getFieldErrorCount("password"));
		assertEquals("Wrong error count for password2", 1, errors.getFieldErrorCount("password2"));		
		
		// incorrect old password
		command.setOldPassword("<PASSWORD>");		
		errors = new BindException(command, "command");
		validator.validate(command, errors);
		assertEquals("Wrong error count for oldPassword", 1, errors.getFieldErrorCount("oldPassword"));
		error = (FieldError) errors.getFieldErrors("oldPassword").get(0);
		assertEquals("Wrong error code", "error.changepassword.oldpassword.nomatch", error.getCode());<commit_msg>Updated ChangePasswordValidator test to account for null User passwords<commit_after>package com.randomcoder.user;

import junit.framework.TestCase;

import org.springframework.validation.*;

public class ChangePasswordValidatorTest extends TestCase
{
	private ChangePasswordValidator validator;

	@Override
	public void setUp() throws Exception
	{
		validator = new ChangePasswordValidator();
		validator.setMinimumPasswordLength(6);
	}

	public void testSupports()
	{
		assertTrue("Validator doesn't support command class", validator.supports(ChangePasswordCommand.class));
	}

	public void testValidate()
	{
		FieldError error;
		BindException errors;
		
		// setup
		ChangePasswordCommand command = new ChangePasswordCommand();
		User user = new User();
		user.setUserName("validate-user");
		command.setUser(user);
		
		// old password supplied when user doesn't have one
		user.setPassword(null);
		command.setOldPassword("<PASSWORD>");
		errors = new BindException(command, "command");
		validator.validate(command, errors);
		assertEquals("Wrong error count for oldPassword", 1, errors.getFieldErrorCount("oldPassword"));
				
		// no data supplied
		user.setPassword(User.hashPassword("<PASSWORD>"));
		command.setOldPassword(null);
		errors = new BindException(command, "command");
		validator.validate(command, errors);
		assertEquals("Wrong number of errors occurred", 3, errors.getErrorCount());
		assertEquals("Wrong error count for oldPassword", 1, errors.getFieldErrorCount("oldPassword"));
		assertEquals("Wrong error count for password", 1, errors.getFieldErrorCount("password"));
		assertEquals("Wrong error count for password2", 1, errors.getFieldErrorCount("password2"));		
		
		// incorrect old password
		command.setOldPassword("<PASSWORD>");		
		errors = new BindException(command, "command");
		validator.validate(command, errors);
		assertEquals("Wrong error count for oldPassword", 1, errors.getFieldErrorCount("oldPassword"));
		error = (FieldError) errors.getFieldErrors("oldPassword").get(0);
		assertEquals("Wrong error code", "error.changepassword.oldpassword.nomatch", error.getCode());
{'fraction_non_alphanumeric': 0.11652763295099061, 'fraction_numerical': 0.004953076120959333, 'mean_word_length': 6.040366972477064, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7590457', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1017, 'n_words': 259}