001 /*
002 * Copyright 2002-2004 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.richclient.application;
017
018 import java.beans.PropertyEditor;
019
020 import junit.framework.TestCase;
021
022 import org.springframework.beans.propertyeditors.ClassEditor;
023 import org.springframework.richclient.application.support.DefaultPropertyEditorRegistry;
024
025 /**
026 * Test cases for {@link DefaultPropertyEditorRegistry}
027 */
028 public class DefaultPropertyEditorRegistryTests extends TestCase {
029
030 public void testRegisteringClass() throws Exception {
031 PropertyEditor pe;
032
033 DefaultPropertyEditorRegistry registry = new DefaultPropertyEditorRegistry();
034
035 registry.setPropertyEditor(D.class, ClassEditor.class);
036 pe = registry.getPropertyEditor(E.class);
037 assertNotNull(pe);
038 assertEquals(ClassEditor.class, pe.getClass());
039
040 registry.setPropertyEditor(A.class, ClassEditor.class);
041 pe = registry.getPropertyEditor(B.class);
042 assertNull(pe);
043
044 pe = registry.getPropertyEditor(E.class);
045 assertNotNull(pe);
046 assertEquals(ClassEditor.class, pe.getClass());
047
048 pe = registry.getPropertyEditor(C.class);
049 assertNotNull(pe);
050 assertEquals(ClassEditor.class, pe.getClass());
051 }
052
053 public void testRegisteringProperty() throws Exception {
054 PropertyEditor pe;
055
056 DefaultPropertyEditorRegistry registry = new DefaultPropertyEditorRegistry();
057
058 registry.setPropertyEditor(A.class, "something", ClassEditor.class);
059
060 try {
061 registry.getPropertyEditor(B.class, "something");
062 fail("Should have thrown an IllegalArgumentException");
063 }
064 catch (IllegalArgumentException e) {
065 // should have been thrown
066 }
067
068 pe = registry.getPropertyEditor(E.class, "something");
069 assertNotNull(pe);
070 assertEquals(ClassEditor.class, pe.getClass());
071
072 pe = registry.getPropertyEditor(C.class, "something");
073 assertNotNull(pe);
074 assertEquals(ClassEditor.class, pe.getClass());
075
076 pe = registry.getPropertyEditor(B.class, "bar");
077 assertNotNull(pe);
078
079 registry.setPropertyEditor(Long.class, ClassEditor.class);
080 pe = registry.getPropertyEditor(B.class, "bar");
081 assertNotNull(pe);
082 assertEquals(ClassEditor.class, pe.getClass());
083 }
084
085 interface A {
086 public String getSomething();
087
088 public void setSomething(String newSomething);
089 }
090
091 interface B {
092 public Long getBar();
093
094 public void setBar(Long newBar);
095 }
096
097 interface C extends A {
098 }
099
100 static class D implements B, C {
101 private String something;
102
103 private Long bar;
104
105 public String getSomething() {
106 return something;
107 }
108
109 public void setSomething(String something) {
110 this.something = something;
111 }
112
113 public Long getBar() {
114 return bar;
115 }
116
117 public void setBar(Long bar) {
118 this.bar = bar;
119 }
120 }
121
122 static class E extends D {
123
124 }
125 }