1   /*
2    * Copyright 2002-2006 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  
17  package org.springframework.binding.support;
18  
19  import java.util.Map;
20  
21  import org.springframework.binding.PropertyMetadataAccessStrategy;
22  import org.springframework.binding.form.FormModel;
23  import org.springframework.binding.form.FieldMetadata;
24  import org.springframework.binding.form.support.DefaultFormModel;
25  import org.springframework.richclient.test.SpringRichTestCase;
26  
27  /**
28   * @author andy
29   * @since May 8, 2006 5:20:39 PM
30   */
31  public class AnnotationAwareBeanPropertyAccessStrategyTests extends SpringRichTestCase {
32      public void testBeanWithNoAnnotations() {
33          final AnnotationAwareBeanPropertyAccessStrategy pas = new AnnotationAwareBeanPropertyAccessStrategy(new NoAnnotationTestBean());
34          final PropertyMetadataAccessStrategy mas = pas.getMetadataAccessStrategy();
35          
36          Map<String,Object> um = mas.getAllUserMetadata("name");
37          assertTrue(um == null || um.size() == 0);
38          um = mas.getAllUserMetadata("age");
39          assertTrue(um == null || um.size() == 0);
40          um = mas.getAllUserMetadata("rank");
41          assertTrue(um == null || um.size() == 0);
42      }
43      
44      public void testBeanWithAnnotations() throws Exception {
45          final AnnotationAwareBeanPropertyAccessStrategy pas = new AnnotationAwareBeanPropertyAccessStrategy(new TestBean());
46          final PropertyMetadataAccessStrategy mas = pas.getMetadataAccessStrategy();
47          
48          Map<String,Object> um = mas.getAllUserMetadata("name");
49          assertNotNull(um);
50          assertEquals(2, um.size());
51          assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.NoValueAnnotation"));
52          NoValueAnnotation nva = (NoValueAnnotation)um.get("@" + NoValueAnnotation.class.getName());
53          assertNotNull(nva);
54          
55          um = mas.getAllUserMetadata("age");
56          assertNotNull(um);
57          assertEquals(4, um.size());
58          assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.NoValueAnnotation"));
59          assertEquals("The Age Method", um.get("org.springframework.binding.support.SingleValueAnnotation"));
60          nva = (NoValueAnnotation)um.get("@" + NoValueAnnotation.class.getName());
61          assertNotNull(nva);
62          SingleValueAnnotation sva = (SingleValueAnnotation)um.get("@" + SingleValueAnnotation.class.getName());
63          assertNotNull(sva);
64          assertEquals("The Age Method", sva.value());
65          
66          um = mas.getAllUserMetadata("rank");
67          assertNotNull(um);
68          assertEquals(9, um.size());
69          assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.NoValueAnnotation"));
70          assertEquals("The Rank Method", um.get("org.springframework.binding.support.SingleValueAnnotation"));
71          assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.MultiValueAnnotation"));
72          assertEquals("First Test Name", um.get("org.springframework.binding.support.MultiValueAnnotation.name"));
73          assertEquals(24.5, um.get("org.springframework.binding.support.MultiValueAnnotation.age"));
74          assertEquals(10, um.get("org.springframework.binding.support.MultiValueAnnotation.rank"));
75          nva = (NoValueAnnotation)um.get("@" + NoValueAnnotation.class.getName());
76          assertNotNull(nva);
77          sva = (SingleValueAnnotation)um.get("@" + SingleValueAnnotation.class.getName());
78          assertNotNull(sva);
79          assertEquals("The Rank Method", sva.value());
80          MultiValueAnnotation mva = (MultiValueAnnotation)um.get("@" + MultiValueAnnotation.class.getName());
81          assertNotNull(mva);
82          assertEquals("First Test Name", mva.name());
83          assertEquals(24.5, mva.age());
84          assertEquals(10, mva.rank());
85          
86          um = mas.getAllUserMetadata("description");
87          assertTrue(um == null || um.size() == 0);
88      }
89      
90      public void testInFormModel() {
91          final AnnotationAwareBeanPropertyAccessStrategy pas = new AnnotationAwareBeanPropertyAccessStrategy(new TestBean());
92          final FormModel fm = new DefaultFormModel(pas);
93          
94          FieldMetadata pm = fm.getFieldMetadata("name"); 
95          assertNotNull(pm);
96          assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.NoValueAnnotation"));
97          NoValueAnnotation nva = (NoValueAnnotation)pm.getUserMetadata("@" + NoValueAnnotation.class.getName());
98          assertNotNull(nva);
99          assertEquals(2, pm.getAllUserMetadata().size());
100         
101         pm = fm.getFieldMetadata("age");
102         assertNotNull(pm);
103         assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.NoValueAnnotation"));
104         assertEquals("The Age Method", pm.getUserMetadata("org.springframework.binding.support.SingleValueAnnotation"));
105         nva = (NoValueAnnotation)pm.getUserMetadata("@" + NoValueAnnotation.class.getName());
106         assertNotNull(nva);
107         SingleValueAnnotation sva = (SingleValueAnnotation)pm.getUserMetadata("@" + SingleValueAnnotation.class.getName());
108         assertNotNull(sva);
109         assertEquals("The Age Method", sva.value());
110         assertEquals(4, pm.getAllUserMetadata().size());
111         
112         pm = fm.getFieldMetadata("rank");
113         assertNotNull(pm);
114         assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.NoValueAnnotation"));
115         assertEquals("The Rank Method", pm.getUserMetadata("org.springframework.binding.support.SingleValueAnnotation"));
116         assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation"));
117         assertEquals("First Test Name", pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation.name"));
118         assertEquals(24.5, pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation.age"));
119         assertEquals(10, pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation.rank"));
120         nva = (NoValueAnnotation)pm.getUserMetadata("@" + NoValueAnnotation.class.getName());
121         assertNotNull(nva);
122         sva = (SingleValueAnnotation)pm.getUserMetadata("@" + SingleValueAnnotation.class.getName());
123         assertNotNull(sva);
124         assertEquals("The Rank Method", sva.value());
125         MultiValueAnnotation mva = (MultiValueAnnotation)pm.getUserMetadata("@" + MultiValueAnnotation.class.getName());
126         assertNotNull(mva);
127         assertEquals("First Test Name", mva.name());
128         assertEquals(24.5, mva.age());
129         assertEquals(10, mva.rank());
130         assertEquals(9, pm.getAllUserMetadata().size());
131         
132         pm = fm.getFieldMetadata("description");
133         assertNotNull(pm);
134         assertTrue(pm.getAllUserMetadata() == null || pm.getAllUserMetadata().size() == 0);
135     }
136     
137     
138     
139     
140     
141     
142     
143     public static class NoAnnotationTestBean {
144         private String name;
145         private double age;
146         private int rank;
147 
148         public String getName() {
149             return name;
150         }
151 
152         public void setName(final String name) {
153             this.name = name;
154         }
155 
156         public double getAge() {
157             return age;
158         }
159 
160         public void setAge(final double age) {
161             this.age = age;
162         }
163 
164         public int getRank() {
165             return rank;
166         }
167 
168         public void setRank(final int rank) {
169             this.rank = rank;
170         }
171     }
172     
173     
174     
175     
176     
177     
178     public static class TestBean {
179         private String name;
180         private double age;
181         private int rank;
182         private String description;
183         
184         @NoValueAnnotation
185         public String getName() {
186             return name;
187         }
188         
189         public void setName(final String name) {
190             this.name = name;
191         }
192         
193         @SingleValueAnnotation("The Age Method")
194         public double getAge() {
195             return age;
196         }
197 
198         @NoValueAnnotation
199         public void setAge(final double age) {
200             this.age = age;
201         }
202 
203         @MultiValueAnnotation(name="Second Test Name", age=30, rank=5)
204         @SingleValueAnnotation("The Rank Method")
205         public void setRank(final int rank) {
206             this.rank = rank;
207         }
208 
209         @MultiValueAnnotation(name="First Test Name", age=24.5, rank=10)
210         @NoValueAnnotation
211         public int getRank() {
212             return rank;
213         }
214 
215         public String getDescription() {
216             return description;
217         }
218 
219         public void setDescription(final String description) {
220             this.description = description;
221         }
222     }
223 }