jp.terasoluna.fw.validation.springmodules.SpringValidationErrorsTest.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.validation.springmodules.SpringValidationErrorsTest.java

Source

/*
 * Copyright (c) 2007 NTT DATA Corporation
 *
 * 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.
 */

package jp.terasoluna.fw.validation.springmodules;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.FastHashMap;
import org.apache.commons.validator.Arg;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.Msg;
import org.apache.commons.validator.ValidatorAction;
import org.junit.Test;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.validation.Errors;

/**
 * {@link jp.terasoluna.fw.validation.springmodules.SpringValidationErrors} ?
 * <p>
 * <h4>???</h4> Spring?org.springframework.validation.Errors? ????<br>
 * addError?Field?ValidatorAction?null???
 * <p>
 * @see jp.terasoluna.fw.validation.springmodules.SpringValidationErrors
 */
public class SpringValidationErrorsTest {

    /**
     * testSetErrors01() <br>
     * <br>
     * () <br>
     * C <br>
     * <br>
     * () errors:Errors<br>
     * () errors:null<br>
     * <br>
     * () errors:???Errors<br>
     * <br>
     * ??????? <br>
     * @throws Exception ?????
     */
    @Test
    public void testSetErrors01() throws Exception {
        // ??
        SpringValidationErrors validation = new SpringValidationErrors();
        ReflectionTestUtils.setField(validation, "errors", null);

        Errors errors = new ErrorsImpl01();

        // 
        validation.setErrors(errors);

        // 
        assertSame(errors, ReflectionTestUtils.getField(validation, "errors"));
    }

    /**
     * testGetErrors01() <br>
     * <br>
     * () <br>
     * C <br>
     * <br>
     * () ??:?<br>
     * () errors:Errors<br>
     * <br>
     * () Errors:Errors<br>
     * <br>
     * ????????? <br>
     * @throws Exception ?????
     */
    @Test
    public void testGetErrors01() throws Exception {
        // ??
        SpringValidationErrors validation = new SpringValidationErrors();
        Errors errors = new ErrorsImpl01();
        ReflectionTestUtils.setField(validation, "errors", errors);

        // 
        Errors result = validation.getErrors();

        // 
        assertSame(errors, result);
    }

    /**
     * testAddErrors01() <br>
     * <br>
     * () <br>
     * C <br>
     * <br>
     * () bean:Object<br>
     * () field:Field<br>
     * field.getKey()="key"<br>
     * field.getMsg("name")="messageKey"<br>
     * field.getArg("name", 0)="arg0"<br>
     * field.getArg("name", 1)="arg1"<br>
     * field.getArg("name", 2)="arg2"<br>
     * field.getArg("name", 3)="arg3"<br>
     * () va:ValidationAction<br>
     * va.getName()="name"<br>
     * <br>
     * () rejectValue():??????<br>
     * fieldCode="key"<br>
     * errorCode="messageKey"<br>
     * args={<br>
     * MessageSourceResolvable{<br>
     * codes[0]={"arg0"}, arguments=null, defaultMessage="arg0"}, <br>
     * MessageSourceResolvable{<br>
     * codes[1]={"arg1"}, arguments=null, defaultMessage="arg1"}, <br>
     * MessageSourceResolvable{<br>
     * codes[2]={"arg2"}, arguments=null, defaultMessage="arg2"}, <br>
     * MessageSourceResolvable{<br>
     * codes[3]={"arg3"}, arguments=null, defaultMessage="arg3"}, <br>
     * }<br>
     * <br>
     * ?not null?? <br>
     * @throws Exception ?????
     */
    @Test
    public void testAddErrors01() throws Exception {
        // ??
        // bean
        Object bean = new Object();
        // field
        Field field = new Field();
        // field.getKey() : "key"
        field.setKey("key");

        // field.getMsg("name")?errorCode????
        FastHashMap hMsgs = new FastHashMap();
        Msg msg = new Msg();
        msg.setKey("messageKey");
        hMsgs.put("name", msg);
        ReflectionTestUtils.setField(field, "hMsgs", hMsgs);

        // Object[] args????
        @SuppressWarnings("rawtypes")
        Map[] args = new HashMap[4];

        // args[0]
        Arg arg = new Arg();
        arg.setKey("arg0");
        Map<String, Arg> hMap01 = new HashMap<String, Arg>();
        hMap01.put("name", arg);
        args[0] = hMap01;

        // args[1]
        arg = new Arg();
        arg.setKey("arg1");
        Map<String, Arg> hMap02 = new HashMap<String, Arg>();
        hMap02.put("name", arg);
        args[1] = hMap02;

        // args[2]
        arg = new Arg();
        arg.setKey("arg2");
        Map<String, Arg> hMap03 = new HashMap<String, Arg>();
        hMap03.put("name", arg);
        args[2] = hMap03;

        // args[3]
        arg = new Arg();
        arg.setKey("arg3");
        Map<String, Arg> hMap04 = new HashMap<String, Arg>();
        hMap04.put("name", arg);
        args[3] = hMap04;

        ReflectionTestUtils.setField(field, "args", args);

        // va
        ValidatorAction va = new ValidatorAction();

        // va.getName : "name"
        va.setName("name");

        // SpringValidationErrors?
        SpringValidationErrors validation = new SpringValidationErrors();

        // Errors? : ErrorsImpl01 - rejectValue???
        ErrorsImpl01 errors = new ErrorsImpl01();
        ReflectionTestUtils.setField(validation, "errors", errors);

        // 
        validation.addError(bean, field, va);

        // 
        ErrorsImpl01 assertErrors = (ErrorsImpl01) ReflectionTestUtils.getField(validation, "errors");
        // rejectValue?
        assertTrue(assertErrors.isRejectValue);

        // field?
        assertEquals("key", assertErrors.field);

        // errorCode?
        assertEquals("messageKey", assertErrors.errorCode);

        // assertSame(args, assertErrors.errorArgs);
        // errorArgs?
        Object[] objs = assertErrors.errorArgs;
        MessageSourceResolvable msr = null;
        for (int i = 0; i < objs.length; i++) {
            msr = (MessageSourceResolvable) objs[i];

            String[] strs = msr.getCodes();
            // codes[0] : "arg" + i
            assertEquals("arg" + i, strs[0]);
            // arguments : null
            assertNull(msr.getArguments());
            // defaultMessage : "arg" + i
            assertEquals("arg" + i, msr.getDefaultMessage());
        }

        // defaultMessage?
        assertEquals("messageKey", assertErrors.defaultMessage);
    }
}