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

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.validation.springmodules.CommonsValidatorExTest.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.fail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.Form;
import org.apache.commons.validator.ValidatorException;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.ValidatorResult;
import org.apache.commons.validator.ValidatorResults;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

/**
 * {@link jp.terasoluna.fw.validation.springmodules.CommonsValidatorEx}
 * ?
 * 
 * <p>
 * <h4>???</h4>
 * Jakaruta-Commons?Validator
 * <p>
 * 
 * @see jp.terasoluna.fw.validation.springmodules.CommonsValidatorEx
 */
public class CommonsValidatorExTest {

    /**
     * testGetValidatorException01() <br>
     * <br>
     * 
     * () <br>
     * A,C <br>
     * <br>
     * (????) this.validatorException:ValidatorException<br>
     * 
     * <br>
     * ()
     * ValidatorException:this.validatorException???ValidatorException<br>
     * 
     * <br>
     * ????validatorException????? <br>
     * 
     * @throws Exception
     *             ?????
     */
    @Test
    public void testGetValidatorException01() throws Exception {
        // ??
        ValidatorResources resources = new ValidatorResources();
        CommonsValidatorEx commonsValidatorEx = new CommonsValidatorEx(resources, null);
        ValidatorException validatorException = new ValidatorException();
        ReflectionTestUtils.setField(commonsValidatorEx, "validatorException", validatorException);

        // 
        ValidatorException resultValidatorException = commonsValidatorEx.getValidatorException();

        // 
        assertSame(validatorException, resultValidatorException);
    }

    /**
     * testValidate01() <br>
     * <br>
     * 
     * () <br>
     * A <br>
     * <br>
     * (????) super.validate():???<br>
     * 
     * <br>
     * () ValidatorResults:super.validate()??<br>
     * () this.validatorException:null<br>
     * 
     * <br>
     * super.validate()??????super.validate()???????
     * <br>
     * 
     * @throws Exception
     *             ?????
     */
    @Test
    public void testValidate01() throws Exception {
        // ??
        CommonsValidatorEx_ValidatorResourcesStub01 resources = new CommonsValidatorEx_ValidatorResourcesStub01();
        Form form = new Form();
        resources.setForm(form);

        // super.validate()???????????
        // Field?????
        // super.validate()????field.validate()?
        // ???
        // super.validate()??field.validate()????????
        CommonsValidatorEx_FieldStub01 field = new CommonsValidatorEx_FieldStub01();
        List<Field> lFields = new ArrayList<Field>();
        lFields.add(field);
        ReflectionTestUtils.setField(form, "lFields", lFields);

        ValidatorResults validatorResults = new ValidatorResults();
        Map<String, ValidatorResult> hResults = new HashMap<String, ValidatorResult>();
        ValidatorResult validatorResult = new ValidatorResult(field);
        hResults.put("test", validatorResult);
        ReflectionTestUtils.setField(validatorResults, "hResults", hResults);

        field.validateReturn = validatorResults;

        CommonsValidatorEx commonsValidatorEx = new CommonsValidatorEx(resources, "formName");

        // 
        ValidatorResults result = commonsValidatorEx.validate();

        // 
        // result??field.validate()????????
        Map<?, ?> resultHResults = (Map<?, ?>) ReflectionTestUtils.getField(result, "hResults");
        assertEquals(1, resultHResults.size());
        assertSame(validatorResult, resultHResults.get("test"));
    }

    /**
     * testValidate02() <br>
     * <br>
     * 
     * () <br>
     * A,G <br>
     * <br>
     * (????) super.validate():ValidatorException?<br>
     * 
     * <br>
     * () :super.validate()???ValidatorException<br>
     * () this.validatorException:super.validate()???ValidatorException<br>
     * 
     * <br>
     * super.validate()?ValidatorException???????????????????
     * <br>
     * 
     * @throws Exception
     *             ?????
     */
    @Test
    public void testValidate02() throws Exception {
        // ??
        CommonsValidatorEx_ValidatorResourcesStub01 resources = new CommonsValidatorEx_ValidatorResourcesStub01();
        Form form = new Form();
        resources.setForm(form);

        // super.validate()???????????
        // Field?????
        // super.validate()????field.validate()?
        // ???
        // super.validate()??field.validate()???validatorException
        // ????????
        CommonsValidatorEx_FieldStub01 field = new CommonsValidatorEx_FieldStub01();
        List<Field> lFields = new ArrayList<Field>();
        lFields.add(field);
        ReflectionTestUtils.setField(form, "lFields", lFields);

        field.validatorException = new ValidatorException();

        CommonsValidatorEx commonsValidatorEx = new CommonsValidatorEx(resources, "formName");

        // 
        try {
            commonsValidatorEx.validate();
            fail();
        } catch (ValidatorException e) {
            // 
            // field.validate()???ValidatorException?????
            assertSame(field.validatorException, e);
            assertSame(e, commonsValidatorEx.getValidatorException());
        }
    }

    /**
     * testClear01()
     * <br><br>
     * 
     * ()
     * <br>
     * A,C
     * <br><br>
     * (????) this.validatorException:ValidatorException<br>
     *         
     * <br>
     * () this.validatorException:null<br>
     *         
     * <br>
     * ????validatorException????
     * <br>
     * 
     * @throws Exception ?????
     */
    @Test
    public void testClear01() throws Exception {
        // ??
        ValidatorResources resources = new ValidatorResources();
        CommonsValidatorEx commonsValidatorEx = new CommonsValidatorEx(resources, null);
        ValidatorException validatorException = new ValidatorException();
        ReflectionTestUtils.setField(commonsValidatorEx, "validatorException", validatorException);

        // 
        commonsValidatorEx.clear();
        ValidatorException result = (ValidatorException) ReflectionTestUtils.getField(commonsValidatorEx,
                "validatorException");

        // 
        assertNull(result);
    }
}