<commit_before>/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.ui.core.controller;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.mifos.platform.validation.MifosBeanValidator;
import org.mifos.ui.validation.StubValidationContext;
import org.springframework.binding.message.Message;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

public class CreateSavingsAccountFormBeanTest {

    private CreateSavingsAccountFormBean formBean;

    private ValidationContext validationContext;

    private MifosBeanValidator validator;

    private LocalValidatorFactoryBean targetValidator;

    @Before
    public void setUp() {
        validator = new MifosBeanValidator();
        targetValidator = new LocalValidatorFactoryBean();
        targetValidator.afterPropertiesSet();
        validator.setTargetValidator(targetValidator);

        formBean = new CreateSavingsAccountFormBean();
        formBean.setValidator(validator);

        validationContext = new StubValidationContext();
    }

    @Test
    public void validateCustomerSearchStep_EmptySearchString() {
        formBean.validateCustomerSearchStep(validationContext);

        MessageContext messageContext = validationContext.getMessageContext();
        Message[] messages = messageContext.getAllMessages();

        Assert.assertEquals(1, messages.length);
        Message message = messages[0];
        Assert.assertEquals("searchString", message.getSource());
        checkErrorType(ErrorTypes.NotEmpty, message);
    }

    private void checkErrorType(ErrorTypes errorType, Message message) {
        String expected = errorType.name() + "."
                + formBean.getClass().getSimpleName() + "."
                + message.getSource();
        Assert.assertEquals(expected, message.getText());
    }
}

enum ErrorTypes {
    NotEmpty
}<commit_msg>MIFOS-4622: replaced error type enum with actual validation constraint<commit_after>/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.ui.core.controller;

import junit.framework.Assert;

import org.hibernate.validator.constraints.NotEmpty;
import org.junit.Before;
import org.junit.Test;
import org.mifos.platform.validation.MifosBeanValidator;
import org.mifos.ui.validation.StubValidationContext;
import org.springframework.binding.message.Message;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

public class CreateSavingsAccountFormBeanTest {

    private CreateSavingsAccountFormBean formBean;

    private ValidationContext validationContext;

    private MifosBeanValidator validator;

    private LocalValidatorFactoryBean targetValidator;

    @Before
    public void setUp() {
        validator = new MifosBeanValidator();
        targetValidator = new LocalValidatorFactoryBean();
        targetValidator.afterPropertiesSet();
        validator.setTargetValidator(targetValidator);

        formBean = new CreateSavingsAccountFormBean();
        formBean.setValidator(validator);

        validationContext = new StubValidationContext();
    }

    @Test
    public void validateCustomerSearchStep_EmptySearchString() {
        formBean.validateCustomerSearchStep(validationContext);

        MessageContext messageContext = validationContext.getMessageContext();
        Message[] messages = messageContext.getAllMessages();

        Assert.assertEquals(1, messages.length);
        Message message = messages[0];
        Assert.assertEquals("searchString", message.getSource());
        verifyErrorMessage(NotEmpty.class, message);
    }

    private void verifyErrorMessage(Class constraint, Message message) {
        String expected = constraint.getSimpleName() + "."
                + formBean.getClass().getSimpleName() + "."
                + message.getSource();
        Assert.assertEquals(expected, message.getText());
    }
}
{'fraction_non_alphanumeric': 0.08358689815643458, 'fraction_numerical': 0.006443529622337569, 'mean_word_length': 3.945132743362832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3025979', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1350, 'n_words': 391}