1   /*
2    * Copyright 2002-2007 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.binding.form.support;
17  
18  import java.util.Locale;
19  
20  import junit.framework.TestCase;
21  
22  import org.springframework.beans.TypeMismatchException;
23  import org.springframework.binding.form.FieldFace;
24  import org.springframework.binding.format.InvalidFormatException;
25  import org.springframework.binding.validation.ValidationMessage;
26  import org.springframework.binding.value.support.ValueHolder;
27  import org.springframework.context.support.MessageSourceAccessor;
28  import org.springframework.context.support.StaticMessageSource;
29  import org.springframework.richclient.core.LabelInfo;
30  
31  /**
32   * Testcase for <code>DefaultBindingErrorMessageProvider</code>
33   * 
34   * @author Peter De Bruycker
35   */
36  public class DefaultBindingErrorMessageProviderTests extends TestCase {
37  
38  	public void testGetErrorMessage() {
39  		DefaultBindingErrorMessageProvider provider = new DefaultBindingErrorMessageProvider();
40  
41  		TestAbstractFormModel formModel = new TestAbstractFormModel(new Object()) {
42  			public FieldFace getFieldFace(String field) {
43  				return new DefaultFieldFace("Some Property", "", "", new LabelInfo("Some Property"), null);
44  			}
45  		};
46  		formModel.add("someProperty", new ValueHolder("value"));
47  
48  		StaticMessageSource messageSource = new StaticMessageSource();
49  		messageSource.addMessage("typeMismatch", Locale.getDefault(), "{0} has an invalid format \"{1}\"");
50  		MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(messageSource);
51  		provider.setMessageSourceAccessor(messageSourceAccessor);
52  
53  		ValidationMessage message = provider.getErrorMessage(formModel, "someProperty", "new value",
54  				new IllegalArgumentException());
55  
56  		assertNotNull(message);
57  		assertEquals("someProperty", message.getProperty());
58  		assertEquals("Some Property has an invalid format \"new value\"", message.getMessage());
59  	}
60  
61  	public void testGetMessageCodeForException() {
62  		DefaultBindingErrorMessageProvider provider = new DefaultBindingErrorMessageProvider();
63  
64  		assertEquals("typeMismatch", provider.getMessageCodeForException(new TypeMismatchException(new Object(),
65  				String.class)));
66  		assertEquals("required", provider.getMessageCodeForException(new NullPointerException()));
67  		assertEquals("typeMismatch", provider.getMessageCodeForException(new InvalidFormatException("", "")));
68  		assertEquals("typeMismatch", provider.getMessageCodeForException(new IllegalArgumentException()));
69  		assertEquals("required", provider.getMessageCodeForException(new RuntimeException(new NullPointerException())));
70  		assertEquals("unknown", provider.getMessageCodeForException(new UnsupportedOperationException()));
71  	}
72  
73  }