1   /*
2    * Copyright 2002-2004 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.richclient.application;
17  
18  import java.beans.PropertyEditor;
19  
20  import junit.framework.TestCase;
21  
22  import org.springframework.beans.propertyeditors.ClassEditor;
23  import org.springframework.richclient.application.support.DefaultPropertyEditorRegistry;
24  
25  /**
26   * Test cases for {@link DefaultPropertyEditorRegistry}
27   */
28  public class DefaultPropertyEditorRegistryTests extends TestCase {
29  
30      public void testRegisteringClass() throws Exception {
31          PropertyEditor pe;
32  
33          DefaultPropertyEditorRegistry registry = new DefaultPropertyEditorRegistry();
34  
35          registry.setPropertyEditor(D.class, ClassEditor.class);
36          pe = registry.getPropertyEditor(E.class);
37          assertNotNull(pe);
38          assertEquals(ClassEditor.class, pe.getClass());
39  
40          registry.setPropertyEditor(A.class, ClassEditor.class);
41          pe = registry.getPropertyEditor(B.class);
42          assertNull(pe);
43  
44          pe = registry.getPropertyEditor(E.class);
45          assertNotNull(pe);
46          assertEquals(ClassEditor.class, pe.getClass());
47  
48          pe = registry.getPropertyEditor(C.class);
49          assertNotNull(pe);
50          assertEquals(ClassEditor.class, pe.getClass());
51      }
52  
53      public void testRegisteringProperty() throws Exception {
54          PropertyEditor pe;
55  
56          DefaultPropertyEditorRegistry registry = new DefaultPropertyEditorRegistry();
57  
58          registry.setPropertyEditor(A.class, "something", ClassEditor.class);
59  
60          try {
61              registry.getPropertyEditor(B.class, "something");
62              fail("Should have thrown an IllegalArgumentException");
63          }
64          catch (IllegalArgumentException e) {
65              // should have been thrown
66          }
67  
68          pe = registry.getPropertyEditor(E.class, "something");
69          assertNotNull(pe);
70          assertEquals(ClassEditor.class, pe.getClass());
71  
72          pe = registry.getPropertyEditor(C.class, "something");
73          assertNotNull(pe);
74          assertEquals(ClassEditor.class, pe.getClass());
75  
76          pe = registry.getPropertyEditor(B.class, "bar");
77          assertNotNull(pe);
78  
79          registry.setPropertyEditor(Long.class, ClassEditor.class);
80          pe = registry.getPropertyEditor(B.class, "bar");
81          assertNotNull(pe);
82          assertEquals(ClassEditor.class, pe.getClass());
83      }
84  
85      interface A {
86          public String getSomething();
87  
88          public void setSomething(String newSomething);
89      }
90  
91      interface B {
92          public Long getBar();
93  
94          public void setBar(Long newBar);
95      }
96  
97      interface C extends A {
98      }
99  
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 }