001 /* 002 * Copyright 2002-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.binding.form.support; 017 018 import java.util.Locale; 019 020 import junit.framework.TestCase; 021 022 import org.springframework.beans.TypeMismatchException; 023 import org.springframework.binding.form.FieldFace; 024 import org.springframework.binding.format.InvalidFormatException; 025 import org.springframework.binding.validation.ValidationMessage; 026 import org.springframework.binding.value.support.ValueHolder; 027 import org.springframework.context.support.MessageSourceAccessor; 028 import org.springframework.context.support.StaticMessageSource; 029 import org.springframework.richclient.core.LabelInfo; 030 031 /** 032 * Testcase for <code>DefaultBindingErrorMessageProvider</code> 033 * 034 * @author Peter De Bruycker 035 */ 036 public class DefaultBindingErrorMessageProviderTests extends TestCase { 037 038 public void testGetErrorMessage() { 039 DefaultBindingErrorMessageProvider provider = new DefaultBindingErrorMessageProvider(); 040 041 TestAbstractFormModel formModel = new TestAbstractFormModel(new Object()) { 042 public FieldFace getFieldFace(String field) { 043 return new DefaultFieldFace("Some Property", "", "", new LabelInfo("Some Property"), null); 044 } 045 }; 046 formModel.add("someProperty", new ValueHolder("value")); 047 048 StaticMessageSource messageSource = new StaticMessageSource(); 049 messageSource.addMessage("typeMismatch", Locale.getDefault(), "{0} has an invalid format \"{1}\""); 050 MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(messageSource); 051 provider.setMessageSourceAccessor(messageSourceAccessor); 052 053 ValidationMessage message = provider.getErrorMessage(formModel, "someProperty", "new value", 054 new IllegalArgumentException()); 055 056 assertNotNull(message); 057 assertEquals("someProperty", message.getProperty()); 058 assertEquals("Some Property has an invalid format \"new value\"", message.getMessage()); 059 } 060 061 public void testGetMessageCodeForException() { 062 DefaultBindingErrorMessageProvider provider = new DefaultBindingErrorMessageProvider(); 063 064 assertEquals("typeMismatch", provider.getMessageCodeForException(new TypeMismatchException(new Object(), 065 String.class))); 066 assertEquals("required", provider.getMessageCodeForException(new NullPointerException())); 067 assertEquals("typeMismatch", provider.getMessageCodeForException(new InvalidFormatException("", ""))); 068 assertEquals("typeMismatch", provider.getMessageCodeForException(new IllegalArgumentException())); 069 assertEquals("required", provider.getMessageCodeForException(new RuntimeException(new NullPointerException()))); 070 assertEquals("unknown", provider.getMessageCodeForException(new UnsupportedOperationException())); 071 } 072 073 }