org.springmodules.validation.bean.converter.ModelAwareMessageCodesResolverTests.java Source code

Java tutorial

Introduction

Here is the source code for org.springmodules.validation.bean.converter.ModelAwareMessageCodesResolverTests.java

Source

/*
 * Copyright 2004-2005 the original author or authors.
 *
 * 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 org.springmodules.validation.bean.converter;

import junit.framework.TestCase;
import org.springframework.validation.MessageCodesResolver;

/**
 * Tests for {@link org.springmodules.validation.bean.converter.ModelAwareMessageCodesResolver}.
 *
 * @author Uri Boness
 */
public class ModelAwareMessageCodesResolverTests extends TestCase {

    private final static String CODE = "Person.name[not.null]";

    private ModelAwareMessageCodesResolver resolver;

    protected void setUp() throws Exception {
        resolver = new ModelAwareMessageCodesResolver(new MessageCodesResolverStub());
    }

    public void testResolveMessageCodes() throws Exception {
        String[] codes = resolver.resolveMessageCodes(CODE, "person");
        assertEquals(2, codes.length);
        assertEquals(CODE, codes[0]);
        assertEquals("dummy_code_1", codes[1]);
    }

    public void testResolveMessageCodes_WithFieldSpecification() throws Exception {
        String[] codes = resolver.resolveMessageCodes(CODE, "person", "name", String.class);
        assertEquals(2, codes.length);
        assertEquals(CODE, codes[0]);
        assertEquals("dummy_code_2", codes[1]);
    }

    private static class MessageCodesResolverStub implements MessageCodesResolver {

        public String[] resolveMessageCodes(String errorCode, String objectName) {
            return new String[] { "dummy_code_1" };
        }

        public String[] resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType) {
            return new String[] { "dummy_code_2" };
        }
    }
}